openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

Plz add 'proxy' option for configuration

Open zhongkai opened this issue 1 year ago • 2 comments

Describe the feature or improvement you're requesting

I want to break through some firewalls to access the openai api, but I don't want to affect the access speed of other services. In addition to customizing proxy rules for openai domain names, the best way is to support the configuration of request proxies, which will facilitate the overall configuration.

Additional context

No response

zhongkai avatar Mar 03 '23 19:03 zhongkai

Since the axios is used for requesting, you can try the following, the second param is the axios configuration (I have changed the solution from proxy => httpsAgent as proxy seems not working well in axios):

  import HttpsProxyAgent from 'https-proxy-agent'

  const rst = await openai.createChatCompletion(
    {
      model: 'gpt-3.5-turbo',
      messages: [{ role: 'user', content: 'Hello!' }]
    },
    {
      proxy: false,
      httpAgent: HttpsProxyAgent('http://proxy-host:proxy-port'),
      httpsAgent: HttpsProxyAgent('http://proxy-host:proxy-port')
    }
  )

brooklynb7 avatar Mar 04 '23 03:03 brooklynb7

There is no reliable mainland agent recommended

xmenbe avatar Mar 04 '23 04:03 xmenbe

Maybe you not need proxy configuration option (。・∀・)ノ゙

I also had a need to use proxy, found a basePath configuration option by looking up the source code.

import { Configuration, OpenAIApi } from 'openai';

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
  basePath: `https://${process.env.OPENAI_PROXY_URL}/v1`
});
const openai = new OpenAIApi(configuration);

xueelf avatar Mar 21 '23 08:03 xueelf

Since the axios is used for requesting, you can try the following, the second param is the axios configuration (I have changed the solution from proxy => httpsAgent as proxy seems not working well in axios):

  import HttpsProxyAgent from 'https-proxy-agent'

  const rst = await openai.createChatCompletion(
    {
      model: 'gpt-3.5-turbo',
      messages: [{ role: 'user', content: 'Hello!' }]
    },
    {
      proxy: false,
      httpAgent: HttpsProxyAgent('http://proxy-host:proxy-port'),
      httpsAgent: HttpsProxyAgent('http://proxy-host:proxy-port')
    }
  )

I love you sooooooooooooooooooooo much! 解决了我一个多小时的问题。

little-spider-cyber avatar Mar 26 '23 06:03 little-spider-cyber

Our upcoming v4 also has support for proxies, as documented here: https://github.com/openai/openai-node/tree/v4#configuring-an-https-agent-eg-for-proxies

rattrayalex avatar Jul 10 '23 01:07 rattrayalex