Spaces:
Running
Running
nacho commited on
Commit ·
8651ef6
1
Parent(s): d14a146
fix: retry different accounts on browser failure (instead of 503)
Browse files
main.py
CHANGED
|
@@ -102,6 +102,22 @@ def _get_message_text(msg: Message) -> str:
|
|
| 102 |
return "".join(b.text for b in c if b.text and b.type == "text")
|
| 103 |
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def verify_api_key(authorization: Optional[str] = Header(None)) -> str:
|
| 106 |
if not authorization:
|
| 107 |
raise HTTPException(status_code=401, detail="Missing API key")
|
|
@@ -255,11 +271,8 @@ async def chat_completions(
|
|
| 255 |
|
| 256 |
model = request.model
|
| 257 |
|
| 258 |
-
account = await
|
| 259 |
-
|
| 260 |
try:
|
| 261 |
-
browser = await manager.get_or_create_browser_with_retry(account, headless=config.browser.headless)
|
| 262 |
-
|
| 263 |
if request.stream:
|
| 264 |
return StreamingResponse(
|
| 265 |
_stream_chat_response(
|
|
@@ -534,11 +547,8 @@ async def admin_chat(request: Request, admin_key: str = Header(...)):
|
|
| 534 |
prompt += f"\n\n[SYSTEM INSTRUCTION: You have access to the following tools:\n{tool_desc}\nIf you must use a tool to fulfill the request, output ONLY a JSON block wrapped in <tool_call>...</tool_call> tags, like:\n<tool_call>{{\"name\": \"tool_name\", \"arguments\": {{\"arg1\": \"value\"}} }}</tool_call>\nDo NOT output any other text if you are calling a tool.]"
|
| 535 |
|
| 536 |
model = req.model
|
| 537 |
-
account = await
|
| 538 |
-
|
| 539 |
try:
|
| 540 |
-
browser = await manager.get_or_create_browser_with_retry(account, headless=config.browser.headless)
|
| 541 |
-
|
| 542 |
if req.stream:
|
| 543 |
return StreamingResponse(
|
| 544 |
_stream_chat_response(
|
|
|
|
| 102 |
return "".join(b.text for b in c if b.text and b.type == "text")
|
| 103 |
|
| 104 |
|
| 105 |
+
async def _acquire_browser():
|
| 106 |
+
"""Acquire an account and get its browser, retrying different accounts on failure."""
|
| 107 |
+
last_error = ""
|
| 108 |
+
for _ in range(5):
|
| 109 |
+
account = await manager.acquire()
|
| 110 |
+
try:
|
| 111 |
+
browser = await manager.get_or_create_browser_with_retry(
|
| 112 |
+
account, headless=config.browser.headless
|
| 113 |
+
)
|
| 114 |
+
return account, browser
|
| 115 |
+
except Exception as e:
|
| 116 |
+
last_error = str(e)
|
| 117 |
+
await manager.release(account)
|
| 118 |
+
raise RuntimeError(f"All accounts failed: {last_error}")
|
| 119 |
+
|
| 120 |
+
|
| 121 |
def verify_api_key(authorization: Optional[str] = Header(None)) -> str:
|
| 122 |
if not authorization:
|
| 123 |
raise HTTPException(status_code=401, detail="Missing API key")
|
|
|
|
| 271 |
|
| 272 |
model = request.model
|
| 273 |
|
| 274 |
+
account, browser = await _acquire_browser()
|
|
|
|
| 275 |
try:
|
|
|
|
|
|
|
| 276 |
if request.stream:
|
| 277 |
return StreamingResponse(
|
| 278 |
_stream_chat_response(
|
|
|
|
| 547 |
prompt += f"\n\n[SYSTEM INSTRUCTION: You have access to the following tools:\n{tool_desc}\nIf you must use a tool to fulfill the request, output ONLY a JSON block wrapped in <tool_call>...</tool_call> tags, like:\n<tool_call>{{\"name\": \"tool_name\", \"arguments\": {{\"arg1\": \"value\"}} }}</tool_call>\nDo NOT output any other text if you are calling a tool.]"
|
| 548 |
|
| 549 |
model = req.model
|
| 550 |
+
account, browser = await _acquire_browser()
|
|
|
|
| 551 |
try:
|
|
|
|
|
|
|
| 552 |
if req.stream:
|
| 553 |
return StreamingResponse(
|
| 554 |
_stream_chat_response(
|