chore: error handling for complete omission of env var

This commit is contained in:
Danny Avila 2023-03-22 09:38:38 -04:00
parent 277685c218
commit 5164cf46ac
4 changed files with 103 additions and 88 deletions

View file

@ -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,13 +18,8 @@ const proxyEnvToAxiosProxy = (proxyString) => {
const titleConvo = async ({ model, text, response }) => {
let title = 'New Chat';
try {
const configuration = new Configuration({
apiKey: process.env.OPENAI_KEY
});
const openai = new OpenAIApi(configuration);
const completion = await openai.createChatCompletion(
{
const request = {
model: 'gpt-3.5-turbo',
messages: [
{
@ -41,10 +36,19 @@ const titleConvo = async ({ model, text, response }) => {
],
temperature: 0,
presence_penalty: 0,
frequency_penalty: 0,
},
{ proxy: proxyEnvToAxiosProxy(process.env.PROXY || null) }
);
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(request, {
proxy: proxyEnvToAxiosProxy(process.env.PROXY || null)
});
//eslint-disable-next-line
title = completion.data.choices[0].message.content.replace(/["\.]/g, '');

View file

@ -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

View file

@ -53,12 +53,14 @@ const convoSchema = mongoose.Schema(
{ timestamps: true }
);
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);

View file

@ -1,6 +1,7 @@
const mongoose = require('mongoose');
const mongoMeili = require('../plugins/mongoMeili');
const messageSchema = mongoose.Schema({
const messageSchema = mongoose.Schema(
{
messageId: {
type: String,
unique: true,
@ -14,17 +15,17 @@ const messageSchema = mongoose.Schema({
meiliIndex: true
},
conversationSignature: {
type: String,
type: String
// required: true
},
clientId: {
type: String,
type: String
},
invocationId: {
type: String,
type: String
},
parentMessageId: {
type: String,
type: String
// required: true
},
sender: {
@ -52,14 +53,18 @@ const messageSchema = mongoose.Schema({
select: false,
default: false
}
}, { timestamps: true });
},
{ timestamps: true }
);
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);