diff --git a/api/app/titleConvo.js b/api/app/titleConvo.js index e0461eabaf..52e35b841a 100644 --- a/api/app/titleConvo.js +++ b/api/app/titleConvo.js @@ -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, '');