From 4e616cd2ed9450e17c76955026ffe23cb1b6e9c9 Mon Sep 17 00:00:00 2001 From: Daniel Avila Date: Fri, 10 Mar 2023 07:43:43 -0500 Subject: [PATCH] fix: error handling titles, remove openAI req --- .gitignore | 2 +- api/app/bingai.js | 23 ++++++++++++++--------- api/app/sydney.js | 29 +++++++++++++++++++---------- api/app/titleConvo.js | 42 +++++++++++++++++++++++++----------------- 4 files changed, 59 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index fa70c3c4c4..00840df40b 100644 --- a/.gitignore +++ b/.gitignore @@ -48,7 +48,7 @@ bower_components/ cache.json api/data/ .eslintrc.js -owner.yml +owner*.yml archive .vscode/settings.json diff --git a/api/app/bingai.js b/api/app/bingai.js index 4dfa8a71d5..d1fc07e28b 100644 --- a/api/app/bingai.js +++ b/api/app/bingai.js @@ -2,27 +2,32 @@ require('dotenv').config(); const { KeyvFile } = require('keyv-file'); const askBing = async ({ text, progressCallback, convo }) => { - const { BingAIClient } = (await import('@waylaidwanderer/chatgpt-api')); + const { BingAIClient } = await import('@waylaidwanderer/chatgpt-api'); - const bingAIClient = new BingAIClient({ - // "_U" cookie from bing.com + const clientOptions = { userToken: process.env.BING_TOKEN, - // If the above doesn't work, provide all your cookies as a string instead - // cookies: '', debug: false, cache: { store: new KeyvFile({ filename: './data/cache.json' }) } - }); + }; + + const cookies = process.env.BING_COOKIES; + + if (cookies?.length > 0) { + clientOptions.cookies = cookies; + delete clientOptions.userToken; + } + + const bingAIClient = new BingAIClient(clientOptions); let options = { - onProgress: async (partialRes) => await progressCallback(partialRes), + onProgress: async (partialRes) => await progressCallback(partialRes) }; if (convo) { options = { ...options, ...convo }; } - const res = await bingAIClient.sendMessage(text, options - ); + const res = await bingAIClient.sendMessage(text, options); return res; diff --git a/api/app/sydney.js b/api/app/sydney.js index fe47c74f57..cb2f83fc7e 100644 --- a/api/app/sydney.js +++ b/api/app/sydney.js @@ -2,30 +2,39 @@ require('dotenv').config(); const { KeyvFile } = require('keyv-file'); const askSydney = async ({ text, progressCallback, convo }) => { - const { BingAIClient } = (await import('@waylaidwanderer/chatgpt-api')); + const { BingAIClient } = await import('@waylaidwanderer/chatgpt-api'); - const sydneyClient = new BingAIClient({ - // "_U" cookie from bing.com + const clientOptions = { userToken: process.env.BING_TOKEN, - // If the above doesn't work, provide all your cookies as a string instead - // cookies: '', debug: false, cache: { store: new KeyvFile({ filename: './data/cache.json' }) } - }); + }; + + const cookies = process.env.BING_COOKIES; + + if (cookies?.length > 0) { + clientOptions.cookies = cookies; + delete clientOptions.userToken; + } + + const sydneyClient = new BingAIClient(clientOptions); let options = { jailbreakConversationId: true, - onProgress: async (partialRes) => await progressCallback(partialRes), + onProgress: async (partialRes) => await progressCallback(partialRes) }; if (convo.parentMessageId) { - options = { ...options, jailbreakConversationId: convo.jailbreakConversationId, parentMessageId: convo.parentMessageId }; + options = { + ...options, + jailbreakConversationId: convo.jailbreakConversationId, + parentMessageId: convo.parentMessageId + }; } console.log('sydney options', options); - const res = await sydneyClient.sendMessage(text, options - ); + const res = await sydneyClient.sendMessage(text, options); return res; diff --git a/api/app/titleConvo.js b/api/app/titleConvo.js index f793075d98..6b064b4b08 100644 --- a/api/app/titleConvo.js +++ b/api/app/titleConvo.js @@ -1,24 +1,32 @@ const { Configuration, OpenAIApi } = require('openai'); const titleConvo = async ({ message, response, model }) => { - 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: titling the conversation provided by a user in title case.' - }, - { role: 'user', content: `In 5 words or less, summarize the conversation below with a title in title case. Don't refer to the participants of the conversation by name. Do not include punctuation or quotation marks. Your response should be in title case, exclusively containing the title. Conversation:\n\nUser: "${message}"\n\n${model}: "${response}"\n\nTitle: ` }, - ] - }); + 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: titling the conversation provided by a user in title case.' + }, + { + role: 'user', + content: `In 5 words or less, summarize the conversation below with a title in title case. Don't refer to the participants of the conversation by name. Do not include punctuation or quotation marks. Your response should be in title case, exclusively containing the title. Conversation:\n\nUser: "${message}"\n\n${model}: "${response}"\n\nTitle: ` + } + ] + }); - //eslint-disable-next-line - return completion.data.choices[0].message.content.replace(/["\.]/g, ''); + //eslint-disable-next-line + return completion.data.choices[0].message.content.replace(/["\.]/g, ''); + } catch (error) { + console.log(error); + return 'New Chat'; + } }; module.exports = titleConvo;