Spaces:
Running
Running
Updated again
Browse files
app.py
CHANGED
|
@@ -376,11 +376,14 @@ def collect_full_response(payload, model, session_key):
|
|
| 376 |
print(f"Trying endpoint: {url}")
|
| 377 |
print(f"Payload: {json.dumps(payload, indent=2)}")
|
| 378 |
|
|
|
|
|
|
|
|
|
|
| 379 |
with requests.post(
|
| 380 |
url,
|
| 381 |
json=payload,
|
| 382 |
headers=get_headers(),
|
| 383 |
-
stream=True
|
| 384 |
timeout=120
|
| 385 |
) as response:
|
| 386 |
|
|
@@ -400,52 +403,69 @@ def collect_full_response(payload, model, session_key):
|
|
| 400 |
}
|
| 401 |
}, response.status_code
|
| 402 |
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
if len(debug_captured_chunks) < 3:
|
| 410 |
-
debug_captured_chunks.append(repr(chunk))
|
| 411 |
-
|
| 412 |
-
buffer += chunk
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
if not line:
|
| 419 |
-
continue
|
| 420 |
-
|
| 421 |
-
if line.startswith('data: '):
|
| 422 |
-
line = line[6:]
|
| 423 |
-
|
| 424 |
-
if line == '[DONE]':
|
| 425 |
-
continue
|
| 426 |
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
if buffer.strip():
|
| 436 |
-
if buffer.strip().startswith('data: '):
|
| 437 |
-
buffer = buffer.strip()[6:]
|
| 438 |
-
content, msg_id, packet_type = parse_onyx_stream_chunk(buffer.strip())
|
| 439 |
-
if msg_id:
|
| 440 |
-
last_message_id = msg_id
|
| 441 |
-
if content and packet_type in ['content', 'legacy', 'raw', 'error']:
|
| 442 |
-
full_content += content
|
| 443 |
-
|
| 444 |
-
# Update session (only if we got a valid message ID)
|
| 445 |
-
if session_key in chat_sessions_cache and last_message_id:
|
| 446 |
-
chat_sessions_cache[session_key]['parent_message_id'] = last_message_id
|
| 447 |
|
| 448 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
|
| 450 |
except requests.exceptions.RequestException as e:
|
| 451 |
print(f"Request error: {e}")
|
|
|
|
| 376 |
print(f"Trying endpoint: {url}")
|
| 377 |
print(f"Payload: {json.dumps(payload, indent=2)}")
|
| 378 |
|
| 379 |
+
# Check if we are requesting a stream or simple JSON
|
| 380 |
+
is_streaming_request = payload.get('stream', False)
|
| 381 |
+
|
| 382 |
with requests.post(
|
| 383 |
url,
|
| 384 |
json=payload,
|
| 385 |
headers=get_headers(),
|
| 386 |
+
stream=is_streaming_request, # Use stream=True only if requested
|
| 387 |
timeout=120
|
| 388 |
) as response:
|
| 389 |
|
|
|
|
| 403 |
}
|
| 404 |
}, response.status_code
|
| 405 |
|
| 406 |
+
# CASE 1: Non-Streaming Response (JSON)
|
| 407 |
+
if not is_streaming_request:
|
| 408 |
+
try:
|
| 409 |
+
data = response.json()
|
| 410 |
+
# Extract content - logs show 'answer' field is used
|
| 411 |
+
full_content = data.get('answer') or data.get('message') or data.get('content') or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
|
| 413 |
+
# Extract Message ID if present to update session
|
| 414 |
+
msg_id = data.get('message_id')
|
| 415 |
+
if session_key in chat_sessions_cache and msg_id:
|
| 416 |
+
chat_sessions_cache[session_key]['parent_message_id'] = msg_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
+
break # Success
|
| 419 |
+
|
| 420 |
+
except json.JSONDecodeError:
|
| 421 |
+
print("Failed to decode JSON response")
|
| 422 |
+
# Fallback to text if JSON fails
|
| 423 |
+
full_content = response.text
|
| 424 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
+
# CASE 2: Streaming Response (Original Logic)
|
| 427 |
+
else:
|
| 428 |
+
buffer = ""
|
| 429 |
+
for chunk in response.iter_content(chunk_size=None, decode_unicode=True):
|
| 430 |
+
if chunk:
|
| 431 |
+
buffer += chunk
|
| 432 |
+
|
| 433 |
+
while '\n' in buffer:
|
| 434 |
+
line, buffer = buffer.split('\n', 1)
|
| 435 |
+
line = line.strip()
|
| 436 |
+
|
| 437 |
+
if not line:
|
| 438 |
+
continue
|
| 439 |
+
|
| 440 |
+
if line.startswith('data: '):
|
| 441 |
+
line = line[6:]
|
| 442 |
+
|
| 443 |
+
if line == '[DONE]':
|
| 444 |
+
continue
|
| 445 |
+
|
| 446 |
+
content, msg_id, packet_type = parse_onyx_stream_chunk(line)
|
| 447 |
+
if msg_id:
|
| 448 |
+
last_message_id = msg_id
|
| 449 |
+
if packet_type == 'stop':
|
| 450 |
+
break
|
| 451 |
+
if content and packet_type in ['content', 'legacy', 'raw', 'error']:
|
| 452 |
+
full_content += content
|
| 453 |
+
|
| 454 |
+
if buffer.strip():
|
| 455 |
+
if buffer.strip().startswith('data: '):
|
| 456 |
+
buffer = buffer.strip()[6:]
|
| 457 |
+
content, msg_id, packet_type = parse_onyx_stream_chunk(buffer.strip())
|
| 458 |
+
if msg_id:
|
| 459 |
+
last_message_id = msg_id
|
| 460 |
+
if content and packet_type in ['content', 'legacy', 'raw', 'error']:
|
| 461 |
+
full_content += content
|
| 462 |
+
|
| 463 |
+
# Update session (only if we got a valid message ID)
|
| 464 |
+
if session_key in chat_sessions_cache and last_message_id:
|
| 465 |
+
chat_sessions_cache[session_key]['parent_message_id'] = last_message_id
|
| 466 |
+
|
| 467 |
+
break
|
| 468 |
+
|
| 469 |
|
| 470 |
except requests.exceptions.RequestException as e:
|
| 471 |
print(f"Request error: {e}")
|