From 4a39965b22d1ab0feab0baf74e274ee60dfcd460 Mon Sep 17 00:00:00 2001 From: Wentao Lyu <35-wentao.lyu@users.noreply.git.stereye.tech> Date: Mon, 13 Mar 2023 15:55:18 +0800 Subject: [PATCH] fix: add proxy to titleConvo --- api/app/titleConvo.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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, '');