2023-03-01 21:39:57 -05:00
|
|
|
require('dotenv').config();
|
|
|
|
|
const { KeyvFile } = require('keyv-file');
|
2023-04-02 14:34:12 -04:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
const browserClient = async ({
|
|
|
|
|
text,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
model,
|
2023-04-10 00:41:34 +08:00
|
|
|
token,
|
2023-03-31 03:22:57 +08:00
|
|
|
onProgress,
|
|
|
|
|
abortController
|
|
|
|
|
}) => {
|
2023-03-01 21:39:57 -05:00
|
|
|
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-01 21:39:57 -05:00
|
|
|
};
|
|
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
const clientOptions = {
|
2023-04-05 21:21:02 +08:00
|
|
|
// Warning: This will expose your access token to a third party. Consider the risks before using this.
|
2023-04-03 17:30:59 -04:00
|
|
|
reverseProxyUrl: process.env.CHATGPT_REVERSE_PROXY || 'https://bypass.churchless.tech/api/conversation',
|
2023-03-31 03:22:57 +08:00
|
|
|
// Access token from https://chat.openai.com/api/auth/session
|
2023-04-10 00:41:34 +08:00
|
|
|
accessToken: process.env.CHATGPT_TOKEN == 'user_provide' ? token : process.env.CHATGPT_TOKEN ?? null,
|
2023-04-05 21:21:02 +08:00
|
|
|
model: model,
|
2023-03-31 03:22:57 +08:00
|
|
|
// debug: true
|
|
|
|
|
proxy: process.env.PROXY || null
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-01 21:39:57 -05:00
|
|
|
const client = new ChatGPTBrowserClient(clientOptions, store);
|
2023-03-17 03:13:42 +08:00
|
|
|
let options = { onProgress, abortController };
|
2023-03-01 21:39:57 -05:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
if (!!parentMessageId && !!conversationId) {
|
|
|
|
|
options = { ...options, parentMessageId, conversationId };
|
2023-03-01 21:39:57 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-02 14:34:12 -04:00
|
|
|
console.log('gptBrowser clientOptions', clientOptions);
|
2023-03-28 22:28:43 -04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 21:39:57 -05:00
|
|
|
const res = await client.sendMessage(text, options);
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = { browserClient };
|