Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -521,6 +521,67 @@ async def chat_debug3(req: ChatRequest):
|
|
| 521 |
"language": req.language,
|
| 522 |
}
|
| 523 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
@app.get("/api/chat-debug")
|
| 525 |
async def chat_debug():
|
| 526 |
"""Diagnostic endpoint — tests each step of chat without streaming."""
|
|
|
|
| 521 |
"language": req.language,
|
| 522 |
}
|
| 523 |
|
| 524 |
+
@app.post("/api/chat-debug4")
|
| 525 |
+
async def chat_debug4(req: ChatRequest, request: Request):
|
| 526 |
+
"""Step through the exact chat endpoint logic and report where it fails."""
|
| 527 |
+
import traceback
|
| 528 |
+
results = {}
|
| 529 |
+
try:
|
| 530 |
+
# Step 1: rate limit
|
| 531 |
+
ip = get_client_ip(request)
|
| 532 |
+
results["s1_ip"] = ip
|
| 533 |
+
|
| 534 |
+
# Step 2: last message
|
| 535 |
+
last_msg = req.messages[-1].content.strip()
|
| 536 |
+
results["s2_last_msg"] = last_msg
|
| 537 |
+
|
| 538 |
+
# Step 3: quick_route
|
| 539 |
+
fast_route = quick_route(last_msg)
|
| 540 |
+
results["s3_fast_route"] = fast_route
|
| 541 |
+
|
| 542 |
+
# Step 4: detect journal
|
| 543 |
+
journal_name = detect_journal_policy_question(last_msg)
|
| 544 |
+
results["s4_journal"] = journal_name
|
| 545 |
+
|
| 546 |
+
# Step 5: build system prompt
|
| 547 |
+
system = CHAT_SYSTEM_PROMPT_BASE.replace(
|
| 548 |
+
"{KU_KNOWLEDGE}", get_knowledge()
|
| 549 |
+
).replace(
|
| 550 |
+
"{OPF_DATA}", "No OPF data."
|
| 551 |
+
)
|
| 552 |
+
results["s5_system_len"] = len(system)
|
| 553 |
+
|
| 554 |
+
# Step 6: build messages
|
| 555 |
+
messages = [{"role": "system", "content": system}]
|
| 556 |
+
messages += [{"role": m.role, "content": m.content} for m in req.messages]
|
| 557 |
+
results["s6_msg_count"] = len(messages)
|
| 558 |
+
|
| 559 |
+
# Step 7: get client
|
| 560 |
+
client = get_client()
|
| 561 |
+
results["s7_client"] = "ok"
|
| 562 |
+
|
| 563 |
+
# Step 8: streaming create call
|
| 564 |
+
response = await client.chat.completions.create(
|
| 565 |
+
model=MODEL,
|
| 566 |
+
messages=messages,
|
| 567 |
+
max_tokens=20,
|
| 568 |
+
temperature=0.3,
|
| 569 |
+
stream=True,
|
| 570 |
+
)
|
| 571 |
+
tokens = []
|
| 572 |
+
async for chunk in response:
|
| 573 |
+
token = chunk.choices[0].delta.content if chunk.choices else None
|
| 574 |
+
if token:
|
| 575 |
+
tokens.append(token)
|
| 576 |
+
results["s8_stream_tokens"] = "".join(tokens)
|
| 577 |
+
|
| 578 |
+
return {"all_steps_ok": True, "results": results}
|
| 579 |
+
|
| 580 |
+
except Exception as e:
|
| 581 |
+
results["error"] = str(e)
|
| 582 |
+
results["traceback"] = traceback.format_exc()
|
| 583 |
+
return {"failed": True, "results": results}
|
| 584 |
+
|
| 585 |
@app.get("/api/chat-debug")
|
| 586 |
async def chat_debug():
|
| 587 |
"""Diagnostic endpoint — tests each step of chat without streaming."""
|