Zenith Wang commited on
Commit
59cc222
·
1 Parent(s): 42ceccb

Fix proxies error with robust client initialization and httpx fallback

Browse files
Files changed (2) hide show
  1. app.py +28 -2
  2. requirements.txt +2 -1
app.py CHANGED
@@ -101,8 +101,34 @@ def process_message(message, history, system_prompt, temperature, max_tokens, to
101
 
102
  # 创建客户端并调用API
103
  try:
104
- client = OpenAI(api_key=STEP_API_KEY, base_url=BASE_URL)
105
- print("[DEBUG] Client created successfully")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
  print("[DEBUG] Calling API...")
108
  response = client.chat.completions.create(
 
101
 
102
  # 创建客户端并调用API
103
  try:
104
+ # 清除所有可能的代理环境变量
105
+ import os
106
+ proxy_vars = ['HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy',
107
+ 'ALL_PROXY', 'all_proxy', 'NO_PROXY', 'no_proxy']
108
+ for var in proxy_vars:
109
+ if var in os.environ:
110
+ del os.environ[var]
111
+ print(f"[DEBUG] Removed {var} from environment")
112
+
113
+ # 尝试创建客户端
114
+ try:
115
+ # 方法1:直接创建
116
+ client = OpenAI(api_key=STEP_API_KEY, base_url=BASE_URL)
117
+ print("[DEBUG] Client created successfully (method 1)")
118
+ except TypeError as e:
119
+ if 'proxies' in str(e):
120
+ print(f"[DEBUG] Method 1 failed with proxy error, trying method 2")
121
+ # 方法2:使用httpx客户端
122
+ import httpx
123
+ http_client = httpx.Client(trust_env=False)
124
+ client = OpenAI(
125
+ api_key=STEP_API_KEY,
126
+ base_url=BASE_URL,
127
+ http_client=http_client
128
+ )
129
+ print("[DEBUG] Client created successfully (method 2)")
130
+ else:
131
+ raise e
132
 
133
  print("[DEBUG] Calling API...")
134
  response = client.chat.completions.create(
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  gradio==4.19.2
2
  openai==1.12.0
3
- Pillow==10.2.0
 
 
1
  gradio==4.19.2
2
  openai==1.12.0
3
+ Pillow==10.2.0
4
+ httpx>=0.24.0