mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
chore: error handling for complete omission of env var
This commit is contained in:
parent
277685c218
commit
5164cf46ac
4 changed files with 103 additions and 88 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
const { Configuration, OpenAIApi } = require('openai');
|
const { Configuration, OpenAIApi } = require('openai');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
const proxyEnvToAxiosProxy = (proxyString) => {
|
const proxyEnvToAxiosProxy = proxyString => {
|
||||||
if (!proxyString) return null;
|
if (!proxyString) return null;
|
||||||
|
|
||||||
const regex = /^([^:]+):\/\/(?:([^:@]*):?([^:@]*)@)?([^:]+)(?::(\d+))?/;
|
const regex = /^([^:]+):\/\/(?:([^:@]*):?([^:@]*)@)?([^:]+)(?::(\d+))?/;
|
||||||
|
|
@ -18,13 +18,8 @@ const proxyEnvToAxiosProxy = (proxyString) => {
|
||||||
|
|
||||||
const titleConvo = async ({ model, text, response }) => {
|
const titleConvo = async ({ model, text, response }) => {
|
||||||
let title = 'New Chat';
|
let title = 'New Chat';
|
||||||
try {
|
|
||||||
const configuration = new Configuration({
|
const request = {
|
||||||
apiKey: process.env.OPENAI_KEY
|
|
||||||
});
|
|
||||||
const openai = new OpenAIApi(configuration);
|
|
||||||
const completion = await openai.createChatCompletion(
|
|
||||||
{
|
|
||||||
model: 'gpt-3.5-turbo',
|
model: 'gpt-3.5-turbo',
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
|
|
@ -41,10 +36,19 @@ const titleConvo = async ({ model, text, response }) => {
|
||||||
],
|
],
|
||||||
temperature: 0,
|
temperature: 0,
|
||||||
presence_penalty: 0,
|
presence_penalty: 0,
|
||||||
frequency_penalty: 0,
|
frequency_penalty: 0
|
||||||
},
|
};
|
||||||
{ proxy: proxyEnvToAxiosProxy(process.env.PROXY || null) }
|
|
||||||
);
|
// console.log('REQUEST', request);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const configuration = new Configuration({
|
||||||
|
apiKey: process.env.OPENAI_KEY
|
||||||
|
});
|
||||||
|
const openai = new OpenAIApi(configuration);
|
||||||
|
const completion = await openai.createChatCompletion(request, {
|
||||||
|
proxy: proxyEnvToAxiosProxy(process.env.PROXY || null)
|
||||||
|
});
|
||||||
|
|
||||||
//eslint-disable-next-line
|
//eslint-disable-next-line
|
||||||
title = completion.data.choices[0].message.content.replace(/["\.]/g, '');
|
title = completion.data.choices[0].message.content.replace(/["\.]/g, '');
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@ const { MeiliSearch } = require('meilisearch');
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
async function indexSync(req, res, next) {
|
async function indexSync(req, res, next) {
|
||||||
try {
|
try {
|
||||||
|
if (!process.env.MEILI_HOST || !process.env.MEILI_KEY || !process.env.SEARCH) {
|
||||||
|
throw new Error('Meilisearch not configured, search will be disabled.');
|
||||||
|
}
|
||||||
|
|
||||||
const client = new MeiliSearch({
|
const client = new MeiliSearch({
|
||||||
host: process.env.MEILI_HOST,
|
host: process.env.MEILI_HOST,
|
||||||
apiKey: process.env.MEILI_KEY
|
apiKey: process.env.MEILI_KEY
|
||||||
|
|
|
||||||
|
|
@ -53,12 +53,14 @@ const convoSchema = mongoose.Schema(
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (process.env.MEILI_HOST && process.env.MEILI_KEY) {
|
||||||
convoSchema.plugin(mongoMeili, {
|
convoSchema.plugin(mongoMeili, {
|
||||||
host: process.env.MEILI_HOST,
|
host: process.env.MEILI_HOST,
|
||||||
apiKey: process.env.MEILI_KEY,
|
apiKey: process.env.MEILI_KEY,
|
||||||
indexName: 'convos', // Will get created automatically if it doesn't exist already
|
indexName: 'convos', // Will get created automatically if it doesn't exist already
|
||||||
primaryKey: 'conversationId'
|
primaryKey: 'conversationId'
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const Conversation = mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
|
const Conversation = mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const mongoMeili = require('../plugins/mongoMeili');
|
const mongoMeili = require('../plugins/mongoMeili');
|
||||||
const messageSchema = mongoose.Schema({
|
const messageSchema = mongoose.Schema(
|
||||||
|
{
|
||||||
messageId: {
|
messageId: {
|
||||||
type: String,
|
type: String,
|
||||||
unique: true,
|
unique: true,
|
||||||
|
|
@ -14,17 +15,17 @@ const messageSchema = mongoose.Schema({
|
||||||
meiliIndex: true
|
meiliIndex: true
|
||||||
},
|
},
|
||||||
conversationSignature: {
|
conversationSignature: {
|
||||||
type: String,
|
type: String
|
||||||
// required: true
|
// required: true
|
||||||
},
|
},
|
||||||
clientId: {
|
clientId: {
|
||||||
type: String,
|
type: String
|
||||||
},
|
},
|
||||||
invocationId: {
|
invocationId: {
|
||||||
type: String,
|
type: String
|
||||||
},
|
},
|
||||||
parentMessageId: {
|
parentMessageId: {
|
||||||
type: String,
|
type: String
|
||||||
// required: true
|
// required: true
|
||||||
},
|
},
|
||||||
sender: {
|
sender: {
|
||||||
|
|
@ -52,14 +53,18 @@ const messageSchema = mongoose.Schema({
|
||||||
select: false,
|
select: false,
|
||||||
default: false
|
default: false
|
||||||
}
|
}
|
||||||
}, { timestamps: true });
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (process.env.MEILI_HOST && process.env.MEILI_KEY) {
|
||||||
messageSchema.plugin(mongoMeili, {
|
messageSchema.plugin(mongoMeili, {
|
||||||
host: process.env.MEILI_HOST,
|
host: process.env.MEILI_HOST,
|
||||||
apiKey: process.env.MEILI_KEY,
|
apiKey: process.env.MEILI_KEY,
|
||||||
indexName: 'messages',
|
indexName: 'messages',
|
||||||
primaryKey: 'messageId'
|
primaryKey: 'messageId'
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const Message = mongoose.models.Message || mongoose.model('Message', messageSchema);
|
const Message = mongoose.models.Message || mongoose.model('Message', messageSchema);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue