LibreChat/api/app/clients/chatgpt-browser.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

require('dotenv').config();
const { KeyvFile } = require('keyv-file');
const modelMap = new Map([
['Default (GPT-3.5)', 'text-davinci-002-render-sha'],
['Legacy (GPT-3.5)', 'text-davinci-002-render-paid'],
['GPT-4', 'gpt-4']
]);
2023-03-31 03:22:57 +08:00
const browserClient = async ({
text,
parentMessageId,
conversationId,
model,
onProgress,
abortController
}) => {
const { ChatGPTBrowserClient } = await import('@waylaidwanderer/chatgpt-api');
const store = {
2023-03-07 13:53:23 -05:00
store: new KeyvFile({ filename: './data/cache.json' })
};
2023-03-31 03:22:57 +08:00
const clientOptions = {
// Warning: This will expose your access token to a third party. Consider the risks before using this.
reverseProxyUrl: 'https://bypass.churchless.tech/api/conversation',
2023-03-31 03:22:57 +08:00
// Access token from https://chat.openai.com/api/auth/session
accessToken: process.env.CHATGPT_TOKEN,
model: modelMap.get(model),
2023-03-31 03:22:57 +08:00
// debug: true
proxy: process.env.PROXY || null
};
const client = new ChatGPTBrowserClient(clientOptions, store);
2023-03-17 03:13:42 +08:00
let options = { onProgress, abortController };
2023-03-31 03:22:57 +08:00
if (!!parentMessageId && !!conversationId) {
options = { ...options, parentMessageId, conversationId };
}
console.log('gptBrowser clientOptions', clientOptions);
2023-03-31 03:22:57 +08:00
if (parentMessageId === '00000000-0000-0000-0000-000000000000') {
2023-03-20 00:51:56 -04:00
delete options.conversationId;
}
const res = await client.sendMessage(text, options);
return res;
};
module.exports = { browserClient };