openai/openai-python

use openai.proxies to set proxy doesn't work

shadowlinyf opened this issue · 7 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

before v1 I can use openai.proxy to set proxy, but after v1 I can't use it any more. At first I thought it would be good again after change openai.proxy to openai.proxies, but not working either.

To Reproduce

openai.proxies = {...proxy setting}

Code snippets

No response

OS

ubuntu20.04

Python version

python 3.9.17

Library version

1.2.4

from dotenv import load_dotenv, dotenv_values
load_dotenv('.env')
env_configs = dotenv_values()

import httpx as _httpx
from openai import OpenAI

proxies = {'http://': env_configs.get('http_proxy'), 'https://': env_configs.get('https_proxy')}
# proxies example:
# proxies = {'http://': 'http://127.0.0.1:1080', 'https://': 'http://127.0.0.1:1080'}

api_key = env_configs.get('OPENAI_API_KEY')
organization = env_configs.get('OPENAI_ORGANIZATION')

_http_client = _httpx.Client(proxies=proxies)
# https://www.python-httpx.org/advanced/#routing

chatgpt_client = OpenAI(api_key=api_key,
                organization=organization,
                http_client=_http_client  # Configure a custom httpx client
                )

This is supported and documented: https://github.com/openai/openai-python#configuring-the-http-client

Thank you @jussker for the example illustrating this!

proxyHost = "127.0.0.1"
proxyPort = 10809

client = OpenAI(http_client=httpx.Client(proxies=f"http://{proxyHost}:{proxyPort}"))

This is supported and documented: https://github.com/openai/openai-python#configuring-the-http-client

Thank you @jussker for the example illustrating this!

before v1 you can use openai.proxy to set a global proxy setting for all clients of openai package. So this feature will be removed from v1?

The feature has moved.

The feature has moved.

Is there sitll a way to set it globally for all openai client in v1?

What I would suggest is making a function like get_openai_client() which instantiates and memoizes an OpenAI client with your desired proxy settings, and importing and using that function everywhere you need an OpenAI client.