2023-02-12 22:24:36 -05:00
|
|
|
require('dotenv').config();
|
|
|
|
|
const { KeyvFile } = require('keyv-file');
|
2023-03-31 03:22:57 +08:00
|
|
|
// const set = new Set(['gpt-4', 'text-davinci-003', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301']);
|
2023-02-12 22:24:36 -05:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
const askClient = async ({
|
|
|
|
|
text,
|
|
|
|
|
parentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
model,
|
|
|
|
|
chatGptLabel,
|
|
|
|
|
promptPrefix,
|
|
|
|
|
temperature,
|
|
|
|
|
top_p,
|
|
|
|
|
presence_penalty,
|
2023-04-01 23:27:44 +08:00
|
|
|
frequency_penalty,
|
2023-03-31 03:22:57 +08:00
|
|
|
onProgress,
|
|
|
|
|
abortController
|
|
|
|
|
}) => {
|
2023-03-01 21:39:57 -05:00
|
|
|
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
2023-02-21 21:30:56 -05:00
|
|
|
const store = {
|
2023-03-07 13:53:23 -05:00
|
|
|
store: new KeyvFile({ filename: './data/cache.json' })
|
2023-02-21 21:30:56 -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.
|
|
|
|
|
reverseProxyUrl: process.env.OPENAI_REVERSE_PROXY || null,
|
|
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
modelOptions: {
|
|
|
|
|
model: model,
|
|
|
|
|
temperature,
|
|
|
|
|
top_p,
|
2023-04-01 23:27:44 +08:00
|
|
|
presence_penalty,
|
|
|
|
|
frequency_penalty
|
2023-03-31 03:22:57 +08:00
|
|
|
},
|
2023-04-05 21:21:02 +08:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
chatGptLabel,
|
|
|
|
|
promptPrefix,
|
|
|
|
|
proxy: process.env.PROXY || null,
|
|
|
|
|
debug: false
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-01 21:39:57 -05:00
|
|
|
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
|
2023-03-17 03:13:42 +08:00
|
|
|
let options = { onProgress, abortController };
|
2023-02-12 22:24:36 -05:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
if (!!parentMessageId && !!conversationId) {
|
|
|
|
|
options = { ...options, parentMessageId, conversationId };
|
2023-02-12 22:24:36 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-13 21:15:28 -05:00
|
|
|
const res = await client.sendMessage(text, options);
|
2023-02-12 22:24:36 -05:00
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = { askClient };
|