Fix the bug that prevents automatic polling.
Browse files- README.md +1 -0
- main.py +5 -3
- response.py +25 -23
README.md
CHANGED
@@ -72,6 +72,7 @@ api_keys:
|
|
72 |
- anthropic/claude-3-5-sonnet # 可以使用的模型名称,仅可以使用名为 anthropic 提供商提供的 claude-3-5-sonnet 模型。其他提供商的 claude-3-5-sonnet 模型不可以使用。
|
73 |
preferences:
|
74 |
USE_ROUND_ROBIN: true # 是否使用轮询负载均衡,true 为使用,false 为不使用,默认为 true
|
|
|
75 |
```
|
76 |
|
77 |
## Docker Local Deployment
|
|
|
72 |
- anthropic/claude-3-5-sonnet # 可以使用的模型名称,仅可以使用名为 anthropic 提供商提供的 claude-3-5-sonnet 模型。其他提供商的 claude-3-5-sonnet 模型不可以使用。
|
73 |
preferences:
|
74 |
USE_ROUND_ROBIN: true # 是否使用轮询负载均衡,true 为使用,false 为不使用,默认为 true
|
75 |
+
AUTO_RETRY: true # 是否自动重试,自动重试下一个提供商,true 为自动重试,false 为不自动重试,默认为 true
|
76 |
```
|
77 |
|
78 |
## Docker Local Deployment
|
main.py
CHANGED
@@ -133,12 +133,14 @@ class ModelRequestHandler:
|
|
133 |
# 检查是否启用轮询
|
134 |
api_index = api_list.index(token)
|
135 |
use_round_robin = False
|
|
|
136 |
if config['api_keys'][api_index].get("preferences"):
|
137 |
use_round_robin = config['api_keys'][api_index]["preferences"].get("USE_ROUND_ROBIN")
|
|
|
138 |
|
139 |
-
return await self.try_all_providers(request, matching_providers, use_round_robin)
|
140 |
|
141 |
-
async def try_all_providers(self, request: RequestModel, providers: List[Dict], use_round_robin: bool):
|
142 |
num_providers = len(providers)
|
143 |
start_index = self.last_provider_index + 1 if use_round_robin else 0
|
144 |
|
@@ -153,7 +155,7 @@ class ModelRequestHandler:
|
|
153 |
print(f"Error with provider {provider['provider']}: {str(e)}")
|
154 |
# traceback.print_exc()
|
155 |
print('\033[0m')
|
156 |
-
if
|
157 |
continue
|
158 |
else:
|
159 |
raise HTTPException(status_code=500, detail="Error: Current provider response failed!")
|
|
|
133 |
# 检查是否启用轮询
|
134 |
api_index = api_list.index(token)
|
135 |
use_round_robin = False
|
136 |
+
auto_retry = False
|
137 |
if config['api_keys'][api_index].get("preferences"):
|
138 |
use_round_robin = config['api_keys'][api_index]["preferences"].get("USE_ROUND_ROBIN")
|
139 |
+
auto_retry = config['api_keys'][api_index]["preferences"].get("AUTO_RETRY")
|
140 |
|
141 |
+
return await self.try_all_providers(request, matching_providers, use_round_robin, auto_retry)
|
142 |
|
143 |
+
async def try_all_providers(self, request: RequestModel, providers: List[Dict], use_round_robin: bool, auto_retry: bool):
|
144 |
num_providers = len(providers)
|
145 |
start_index = self.last_provider_index + 1 if use_round_robin else 0
|
146 |
|
|
|
155 |
print(f"Error with provider {provider['provider']}: {str(e)}")
|
156 |
# traceback.print_exc()
|
157 |
print('\033[0m')
|
158 |
+
if auto_retry:
|
159 |
continue
|
160 |
else:
|
161 |
raise HTTPException(status_code=500, detail="Error: Current provider response failed!")
|
response.py
CHANGED
@@ -155,26 +155,28 @@ async def fetch_response(client, url, headers, payload):
|
|
155 |
continue
|
156 |
|
157 |
async def fetch_response_stream(client, url, headers, payload, engine, model):
|
158 |
-
for _ in range(2):
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
155 |
continue
|
156 |
|
157 |
async def fetch_response_stream(client, url, headers, payload, engine, model):
|
158 |
+
# for _ in range(2):
|
159 |
+
try:
|
160 |
+
if engine == "gemini":
|
161 |
+
async for chunk in fetch_gemini_response_stream(client, url, headers, payload, model):
|
162 |
+
yield chunk
|
163 |
+
elif engine == "claude":
|
164 |
+
async for chunk in fetch_claude_response_stream(client, url, headers, payload, model):
|
165 |
+
yield chunk
|
166 |
+
elif engine == "gpt":
|
167 |
+
async for chunk in fetch_gpt_response_stream(client, url, headers, payload):
|
168 |
+
yield chunk
|
169 |
+
elif engine == "openrouter":
|
170 |
+
async for chunk in fetch_gpt_response_stream(client, url, headers, payload):
|
171 |
+
yield chunk
|
172 |
+
else:
|
173 |
+
raise ValueError("Unknown response")
|
174 |
+
# break
|
175 |
+
except httpx.ConnectError as e:
|
176 |
+
# print(f"fetch_response_stream 连接错误: {e}")
|
177 |
+
yield {"error": f"500", "details": "fetch_response_stream Connect Error"}
|
178 |
+
# continue
|
179 |
+
except httpx.ReadTimeout as e:
|
180 |
+
# print(f"fetch_response_stream 读取响应超时 {e}")
|
181 |
+
yield {"error": f"500", "details": "fetch_response_stream Read Response Timeout"}
|
182 |
+
# continue
|