From 5164cf46acf82fe53632a7d36d921985ea737deb Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 22 Mar 2023 09:38:38 -0400 Subject: [PATCH] chore: error handling for complete omission of env var --- api/app/titleConvo.js | 50 ++++++------ api/lib/db/indexSync.js | 4 + api/models/schema/convoSchema.js | 14 ++-- api/models/schema/messageSchema.js | 123 +++++++++++++++-------------- 4 files changed, 103 insertions(+), 88 deletions(-) diff --git a/api/app/titleConvo.js b/api/app/titleConvo.js index a089d6ca76..9575377b44 100644 --- a/api/app/titleConvo.js +++ b/api/app/titleConvo.js @@ -1,7 +1,7 @@ const { Configuration, OpenAIApi } = require('openai'); const _ = require('lodash'); -const proxyEnvToAxiosProxy = (proxyString) => { +const proxyEnvToAxiosProxy = proxyString => { if (!proxyString) return null; const regex = /^([^:]+):\/\/(?:([^:@]*):?([^:@]*)@)?([^:]+)(?::(\d+))?/; @@ -18,33 +18,37 @@ const proxyEnvToAxiosProxy = (proxyString) => { const titleConvo = async ({ model, text, response }) => { let title = 'New Chat'; + + const request = { + model: 'gpt-3.5-turbo', + messages: [ + { + role: 'system', + content: + 'You are a title-generator with one job: giving a conversation, detect the language and titling the conversation provided by a user in title case, using the same language.' + }, + { + role: 'user', + content: `In 5 words or less, summarize the conversation below with a title in title case using the language the user writes in. Don't refer to the participants of the conversation nor the language. Do not include punctuation or quotation marks. Your response should be in title case, exclusively containing the title. Conversation:\n\nUser: "${text}"\n\n${model}: "${JSON.stringify( + response?.text + )}"\n\nTitle: ` + } + ], + temperature: 0, + presence_penalty: 0, + frequency_penalty: 0 + }; + + // console.log('REQUEST', request); + try { const configuration = new Configuration({ apiKey: process.env.OPENAI_KEY }); const openai = new OpenAIApi(configuration); - const completion = await openai.createChatCompletion( - { - model: 'gpt-3.5-turbo', - messages: [ - { - role: 'system', - content: - 'You are a title-generator with one job: giving a conversation, detect the language and titling the conversation provided by a user in title case, using the same language.' - }, - { - role: 'user', - content: `In 5 words or less, summarize the conversation below with a title in title case using the language the user writes in. Don't refer to the participants of the conversation nor the language. Do not include punctuation or quotation marks. Your response should be in title case, exclusively containing the title. Conversation:\n\nUser: "${text}"\n\n${model}: "${JSON.stringify( - response?.text - )}"\n\nTitle: ` - } - ], - temperature: 0, - presence_penalty: 0, - frequency_penalty: 0, - }, - { proxy: proxyEnvToAxiosProxy(process.env.PROXY || null) } - ); + const completion = await openai.createChatCompletion(request, { + proxy: proxyEnvToAxiosProxy(process.env.PROXY || null) + }); //eslint-disable-next-line title = completion.data.choices[0].message.content.replace(/["\.]/g, ''); diff --git a/api/lib/db/indexSync.js b/api/lib/db/indexSync.js index cfa8650514..600b4fa6f0 100644 --- a/api/lib/db/indexSync.js +++ b/api/lib/db/indexSync.js @@ -6,6 +6,10 @@ const { MeiliSearch } = require('meilisearch'); // eslint-disable-next-line no-unused-vars async function indexSync(req, res, next) { 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({ host: process.env.MEILI_HOST, apiKey: process.env.MEILI_KEY diff --git a/api/models/schema/convoSchema.js b/api/models/schema/convoSchema.js index fe25aa024e..f9d8127b01 100644 --- a/api/models/schema/convoSchema.js +++ b/api/models/schema/convoSchema.js @@ -53,12 +53,14 @@ const convoSchema = mongoose.Schema( { timestamps: true } ); -convoSchema.plugin(mongoMeili, { - host: process.env.MEILI_HOST, - apiKey: process.env.MEILI_KEY, - indexName: 'convos', // Will get created automatically if it doesn't exist already - primaryKey: 'conversationId' -}); +if (process.env.MEILI_HOST && process.env.MEILI_KEY) { + convoSchema.plugin(mongoMeili, { + host: process.env.MEILI_HOST, + apiKey: process.env.MEILI_KEY, + indexName: 'convos', // Will get created automatically if it doesn't exist already + primaryKey: 'conversationId' + }); +} const Conversation = mongoose.models.Conversation || mongoose.model('Conversation', convoSchema); diff --git a/api/models/schema/messageSchema.js b/api/models/schema/messageSchema.js index 3ecb68a55a..01aa4feb2d 100644 --- a/api/models/schema/messageSchema.js +++ b/api/models/schema/messageSchema.js @@ -1,66 +1,71 @@ const mongoose = require('mongoose'); const mongoMeili = require('../plugins/mongoMeili'); -const messageSchema = mongoose.Schema({ - messageId: { - type: String, - unique: true, - required: true, - index: true, - meiliIndex: true +const messageSchema = mongoose.Schema( + { + messageId: { + type: String, + unique: true, + required: true, + index: true, + meiliIndex: true + }, + conversationId: { + type: String, + required: true, + meiliIndex: true + }, + conversationSignature: { + type: String + // required: true + }, + clientId: { + type: String + }, + invocationId: { + type: String + }, + parentMessageId: { + type: String + // required: true + }, + sender: { + type: String, + required: true, + meiliIndex: true + }, + text: { + type: String, + required: true, + meiliIndex: true + }, + isCreatedByUser: { + type: Boolean, + required: true, + default: false + }, + error: { + type: Boolean, + default: false + }, + _meiliIndex: { + type: Boolean, + required: false, + select: false, + default: false + } }, - conversationId: { - type: String, - required: true, - meiliIndex: true - }, - conversationSignature: { - type: String, - // required: true - }, - clientId: { - type: String, - }, - invocationId: { - type: String, - }, - parentMessageId: { - type: String, - // required: true - }, - sender: { - type: String, - required: true, - meiliIndex: true - }, - text: { - type: String, - required: true, - meiliIndex: true - }, - isCreatedByUser: { - type: Boolean, - required: true, - default: false - }, - error: { - type: Boolean, - default: false - }, - _meiliIndex: { - type: Boolean, - required: false, - select: false, - default: false - } -}, { timestamps: true }); + { timestamps: true } +); -messageSchema.plugin(mongoMeili, { - host: process.env.MEILI_HOST, - apiKey: process.env.MEILI_KEY, - indexName: 'messages', - primaryKey: 'messageId' -}); +if (process.env.MEILI_HOST && process.env.MEILI_KEY) { + messageSchema.plugin(mongoMeili, { + host: process.env.MEILI_HOST, + apiKey: process.env.MEILI_KEY, + indexName: 'messages', + primaryKey: 'messageId' + }); +} const Message = mongoose.models.Message || mongoose.model('Message', messageSchema); -module.exports = Message; \ No newline at end of file +module.exports = Message;