fix: add proxy to titleConvo

This commit is contained in:
Wentao Lyu 2023-03-13 15:55:18 +08:00
parent 90dc171b34
commit 4a39965b22

View file

@ -1,5 +1,21 @@
const { Configuration, OpenAIApi } = require('openai');
const proxyEnvToAxiosProxy = (proxyString) => {
if (!proxyString) return null;
const regex = /^([^:]+):\/\/(?:([^:@]*):?([^:@]*)@)?([^:]+)(?::(\d+))?/;
const [, protocol, username, password, host, port] = proxyString.match(regex);
const proxyConfig = {
protocol,
host,
port: port ? parseInt(port) : undefined,
auth: username && password ? { username, password } : undefined
};
return proxyConfig
}
console.log(proxyEnvToAxiosProxy(process.env.PROXY || null))
const titleConvo = async ({ message, response, model }) => {
const configuration = new Configuration({
apiKey: process.env.OPENAI_KEY
@ -15,7 +31,7 @@ const titleConvo = async ({ message, response, model }) => {
},
{ role: 'user', content: `In 5 words or less, summarize the conversation below with a title in title case using the language the user writes in. 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: ` },
]
});
}, { proxy: proxyEnvToAxiosProxy(process.env.PROXY || null) });
//eslint-disable-next-line
return completion.data.choices[0].message.content.replace(/["\.]/g, '');