diff --git a/service/src/chatgpt/index.ts b/service/src/chatgpt/index.ts index 61432959b9..7a69c417e6 100644 --- a/service/src/chatgpt/index.ts +++ b/service/src/chatgpt/index.ts @@ -26,6 +26,25 @@ if (!process.env.OPENAI_API_KEY && !process.env.OPENAI_ACCESS_TOKEN) throw new Error('Missing OPENAI_API_KEY or OPENAI_ACCESS_TOKEN environment variable') let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI +function setupProxy(options) { + if (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) { + const agent = new SocksProxyAgent({ + hostname: process.env.SOCKS_PROXY_HOST, + port: process.env.SOCKS_PROXY_PORT, + }) + options.fetch = (url, options) => { + return fetch(url, { agent, ...options }) + } + } + + const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy + if (httpsProxy) { + const agent = new HttpsProxyAgent(httpsProxy) + options.fetch = (url, options) => { + return fetch(url, { agent, ...options }) + } + } +} (async () => { // More Info: https://github.com/transitive-bullshit/chatgpt-api @@ -45,23 +64,7 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI if (process.env.OPENAI_API_BASE_URL && process.env.OPENAI_API_BASE_URL.trim().length > 0) options.apiBaseUrl = process.env.OPENAI_API_BASE_URL - if (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) { - const agent = new SocksProxyAgent({ - hostname: process.env.SOCKS_PROXY_HOST, - port: process.env.SOCKS_PROXY_PORT, - }) - options.fetch = (url, options) => { - return fetch(url, { agent, ...options }) - } - } - - const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy - if (httpsProxy) { - const agent = new HttpsProxyAgent(httpsProxy) - options.fetch = (url, options) => { - return fetch(url, { agent, ...options }) - } - } + setupProxy(options) api = new ChatGPTAPI({ ...options }) apiModel = 'ChatGPTAPI' @@ -72,23 +75,7 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI debug: false, } - if (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) { - const agent = new SocksProxyAgent({ - hostname: process.env.SOCKS_PROXY_HOST, - port: process.env.SOCKS_PROXY_PORT, - }) - options.fetch = (url, options) => { - return fetch(url, { agent, ...options }) - } - } - - const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy - if (httpsProxy) { - const agent = new HttpsProxyAgent(httpsProxy) - options.fetch = (url, options) => { - return fetch(url, { agent, ...options }) - } - } + setupProxy(options) if (process.env.API_REVERSE_PROXY) options.apiReverseProxyUrl = process.env.API_REVERSE_PROXY