21
21
22
22
from dotenv import find_dotenv , load_dotenv
23
23
from loguru import logger as eval_logger
24
- from openai import AzureOpenAI , OpenAI
24
+ from openai import AzureOpenAI , DefaultHttpxClient , OpenAI
25
25
from PIL import Image
26
26
27
27
load_dotenv (verbose = True )
@@ -39,8 +39,16 @@ def __init__(
39
39
response_persistent_folder : str = None ,
40
40
azure_openai : bool = False ,
41
41
max_frames_num : int = 10 ,
42
+ httpx_trust_env : bool = True ,
42
43
** kwargs ,
43
44
) -> 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
+ """
44
52
super ().__init__ ()
45
53
self .model_version = model_version
46
54
self .timeout = timeout
@@ -64,10 +72,16 @@ def __init__(
64
72
self .response_cache = {}
65
73
self .cache_mode = "start"
66
74
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
67
81
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 )
69
83
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 )
71
85
)
72
86
73
87
accelerator = Accelerator ()
0 commit comments