2023-03-03 15:52:06 -05:00
|
|
|
require('dotenv').config();
|
|
|
|
|
const { KeyvFile } = require('keyv-file');
|
|
|
|
|
|
|
|
|
|
const clientOptions = {
|
|
|
|
|
modelOptions: {
|
|
|
|
|
model: 'gpt-3.5-turbo'
|
|
|
|
|
},
|
2023-03-11 14:12:51 +08:00
|
|
|
proxy: process.env.PROXY || null,
|
2023-03-03 15:52:06 -05:00
|
|
|
debug: false
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-17 03:13:42 +08:00
|
|
|
const customClient = async ({ text, onProgress, convo, promptPrefix, chatGptLabel, abortController }) => {
|
2023-03-03 15:52:06 -05:00
|
|
|
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
|
|
|
|
const store = {
|
2023-03-07 13:53:23 -05:00
|
|
|
store: new KeyvFile({ filename: './data/cache.json' })
|
2023-03-03 15:52:06 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
clientOptions.chatGptLabel = chatGptLabel;
|
|
|
|
|
|
2023-03-11 17:36:10 -05:00
|
|
|
if (promptPrefix?.length > 0) {
|
2023-03-03 15:52:06 -05:00
|
|
|
clientOptions.promptPrefix = promptPrefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
|
|
|
|
|
|
2023-03-17 03:13:42 +08:00
|
|
|
let options = { onProgress, abortController };
|
2023-03-03 15:52:06 -05:00
|
|
|
if (!!convo.parentMessageId && !!convo.conversationId) {
|
|
|
|
|
options = { ...options, ...convo };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const res = await client.sendMessage(text, options);
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = customClient;
|