fix: error handling titles, remove openAI req

This commit is contained in:
Daniel Avila 2023-03-10 07:43:43 -05:00
parent 12cf3405e4
commit 4e616cd2ed
4 changed files with 59 additions and 37 deletions

2
.gitignore vendored
View file

@ -48,7 +48,7 @@ bower_components/
cache.json cache.json
api/data/ api/data/
.eslintrc.js .eslintrc.js
owner.yml owner*.yml
archive archive
.vscode/settings.json .vscode/settings.json

View file

@ -2,27 +2,32 @@ require('dotenv').config();
const { KeyvFile } = require('keyv-file'); const { KeyvFile } = require('keyv-file');
const askBing = async ({ text, progressCallback, convo }) => { const askBing = async ({ text, progressCallback, convo }) => {
const { BingAIClient } = (await import('@waylaidwanderer/chatgpt-api')); const { BingAIClient } = await import('@waylaidwanderer/chatgpt-api');
const bingAIClient = new BingAIClient({ const clientOptions = {
// "_U" cookie from bing.com
userToken: process.env.BING_TOKEN, userToken: process.env.BING_TOKEN,
// If the above doesn't work, provide all your cookies as a string instead
// cookies: '',
debug: false, debug: false,
cache: { store: new KeyvFile({ filename: './data/cache.json' }) } 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 = { let options = {
onProgress: async (partialRes) => await progressCallback(partialRes), onProgress: async (partialRes) => await progressCallback(partialRes)
}; };
if (convo) { if (convo) {
options = { ...options, ...convo }; options = { ...options, ...convo };
} }
const res = await bingAIClient.sendMessage(text, options const res = await bingAIClient.sendMessage(text, options);
);
return res; return res;

View file

@ -2,30 +2,39 @@ require('dotenv').config();
const { KeyvFile } = require('keyv-file'); const { KeyvFile } = require('keyv-file');
const askSydney = async ({ text, progressCallback, convo }) => { const askSydney = async ({ text, progressCallback, convo }) => {
const { BingAIClient } = (await import('@waylaidwanderer/chatgpt-api')); const { BingAIClient } = await import('@waylaidwanderer/chatgpt-api');
const sydneyClient = new BingAIClient({ const clientOptions = {
// "_U" cookie from bing.com
userToken: process.env.BING_TOKEN, userToken: process.env.BING_TOKEN,
// If the above doesn't work, provide all your cookies as a string instead
// cookies: '',
debug: false, debug: false,
cache: { store: new KeyvFile({ filename: './data/cache.json' }) } 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 = { let options = {
jailbreakConversationId: true, jailbreakConversationId: true,
onProgress: async (partialRes) => await progressCallback(partialRes), onProgress: async (partialRes) => await progressCallback(partialRes)
}; };
if (convo.parentMessageId) { if (convo.parentMessageId) {
options = { ...options, jailbreakConversationId: convo.jailbreakConversationId, parentMessageId: convo.parentMessageId }; options = {
...options,
jailbreakConversationId: convo.jailbreakConversationId,
parentMessageId: convo.parentMessageId
};
} }
console.log('sydney options', options); console.log('sydney options', options);
const res = await sydneyClient.sendMessage(text, options const res = await sydneyClient.sendMessage(text, options);
);
return res; return res;

View file

@ -1,6 +1,7 @@
const { Configuration, OpenAIApi } = require('openai'); const { Configuration, OpenAIApi } = require('openai');
const titleConvo = async ({ message, response, model }) => { const titleConvo = async ({ message, response, model }) => {
try {
const configuration = new Configuration({ const configuration = new Configuration({
apiKey: process.env.OPENAI_KEY apiKey: process.env.OPENAI_KEY
}); });
@ -13,12 +14,19 @@ const titleConvo = async ({ message, response, model }) => {
content: content:
'You are a title-generator with one job: titling the conversation provided by a user in title case.' '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: ` }, {
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 //eslint-disable-next-line
return completion.data.choices[0].message.content.replace(/["\.]/g, ''); return completion.data.choices[0].message.content.replace(/["\.]/g, '');
} catch (error) {
console.log(error);
return 'New Chat';
}
}; };
module.exports = titleConvo; module.exports = titleConvo;