From 20d61356588cbcd77a7d511aadbc1ede7441d050 Mon Sep 17 00:00:00 2001 From: InterestingDarkness <109194923+InterestingDarkness@users.noreply.github.com> Date: Fri, 3 Mar 2023 10:43:27 +0800 Subject: [PATCH] Add support for SOCKS proxy in ChatGPTAPI fetch (#214) --- service/src/chatgpt/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/service/src/chatgpt/index.ts b/service/src/chatgpt/index.ts index 8f18600..80f8ecc 100644 --- a/service/src/chatgpt/index.ts +++ b/service/src/chatgpt/index.ts @@ -27,8 +27,17 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI apiKey: process.env.OPENAI_API_KEY, debug: false, } - - api = new ChatGPTAPI({ ...options }) + let fetchFn + 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, + }) + fetchFn = (url, options) => { + return fetch(url, { agent, ...options }) + } + } + api = new ChatGPTAPI({ ...options, fetch: fetchFn }) apiModel = 'ChatGPTAPI' } else {