Skip to content

Commit 4a9f516

Browse files
committed
Add httpx_trust_env arg for openai_compatible model
1 parent 0aaff1d commit 4a9f516

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lmms_eval/models/simple/openai_compatible.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from dotenv import find_dotenv, load_dotenv
2323
from loguru import logger as eval_logger
24-
from openai import AzureOpenAI, OpenAI
24+
from openai import AzureOpenAI, DefaultHttpxClient, OpenAI
2525
from PIL import Image
2626

2727
load_dotenv(verbose=True)
@@ -39,8 +39,16 @@ def __init__(
3939
response_persistent_folder: str = None,
4040
azure_openai: bool = False,
4141
max_frames_num: int = 10,
42+
httpx_trust_env: bool = True,
4243
**kwargs,
4344
) -> None:
45+
"""
46+
:param httpx_trust_env: bool
47+
httpx.Client used by openai-python has trust_env set to True by default. A
48+
False value of this param constructs a httpx.Client with trust_env set to
49+
False. Such a httpx.Client ignores environment variables (HTTP_PROXY,
50+
HTTPS_PROXY, ALL_PROXY) and macOS proxy server settings.
51+
"""
4452
super().__init__()
4553
self.model_version = model_version
4654
self.timeout = timeout
@@ -64,10 +72,16 @@ def __init__(
6472
self.response_cache = {}
6573
self.cache_mode = "start"
6674

75+
# In China mainland, people usually use a VPN client to access international web
76+
# sites such as Google. Such a client usually configures macOS proxy server
77+
# settings. openai-python uses a httpx.Client with trust_env set to True. Such a
78+
# httpx.Client uses macOS proxy server settings. Adding httpx_trust_env option
79+
# allows httpx to ignore proxy server settings set by VPN clients.
80+
http_client = DefaultHttpxClient(trust_env=httpx_trust_env) if not httpx_trust_env else None
6781
self.client = (
68-
OpenAI(api_key=os.getenv("OPENAI_API_KEY"), base_url=os.getenv("OPENAI_API_BASE"))
82+
OpenAI(api_key=os.getenv("OPENAI_API_KEY"), base_url=os.getenv("OPENAI_API_BASE"), http_client=http_client)
6983
if not azure_openai
70-
else AzureOpenAI(api_key=os.getenv("AZURE_OPENAI_API_KEY"), azure_endpoint=os.getenv("AZURE_OPENAI_API_BASE"), api_version=os.getenv("AZURE_OPENAI_API_VERSION"))
84+
else AzureOpenAI(api_key=os.getenv("AZURE_OPENAI_API_KEY"), azure_endpoint=os.getenv("AZURE_OPENAI_API_BASE"), api_version=os.getenv("AZURE_OPENAI_API_VERSION"), http_client=http_client)
7185
)
7286

7387
accelerator = Accelerator()

0 commit comments

Comments
 (0)