nikeshn commited on
Commit
0676dbc
Β·
verified Β·
1 Parent(s): 3b9ab84

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py CHANGED
@@ -502,6 +502,69 @@ async def health():
502
  return {"status": "ok", "service": "ResearchBee", "version": "1.3.0"}
503
 
504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505
  # ── OpenAlex related works per journal ────────────────────────────────────
506
  @app.get("/api/openalex-works")
507
  async def openalex_works(request: Request, source_id: str, per_page: int = 5):
 
502
  return {"status": "ok", "service": "ResearchBee", "version": "1.3.0"}
503
 
504
 
505
+ @app.get("/api/chat-debug")
506
+ async def chat_debug():
507
+ """Diagnostic endpoint β€” tests each step of chat without streaming."""
508
+ results = {}
509
+ try:
510
+ from ku_knowledge import get_knowledge
511
+ kb = get_knowledge()
512
+ results["kb_length"] = len(kb)
513
+ results["step1"] = "KB loaded OK"
514
+ except Exception as e:
515
+ return {"failed_at": "kb", "error": str(e)}
516
+
517
+ try:
518
+ from prompts import CHAT_SYSTEM_PROMPT_BASE
519
+ system = CHAT_SYSTEM_PROMPT_BASE.replace("{KU_KNOWLEDGE}", kb).replace("{OPF_DATA}", "none")
520
+ results["system_length"] = len(system)
521
+ results["step2"] = "Prompt OK"
522
+ except Exception as e:
523
+ return {"failed_at": "prompt", "error": str(e)}
524
+
525
+ try:
526
+ client = get_client()
527
+ results["step3"] = "Client OK"
528
+ except Exception as e:
529
+ return {"failed_at": "client", "error": str(e)}
530
+
531
+ try:
532
+ resp = await client.chat.completions.create(
533
+ model=MODEL,
534
+ messages=[
535
+ {"role": "system", "content": "Reply with just: OK"},
536
+ {"role": "user", "content": "test"},
537
+ ],
538
+ max_tokens=5,
539
+ temperature=0,
540
+ stream=False,
541
+ )
542
+ results["step4"] = "LLM call OK: " + resp.choices[0].message.content
543
+ except Exception as e:
544
+ return {"failed_at": "llm_call", "error": str(e), "results_so_far": results}
545
+
546
+ try:
547
+ resp2 = await client.chat.completions.create(
548
+ model=MODEL,
549
+ messages=[
550
+ {"role": "system", "content": "Reply with just: OK"},
551
+ {"role": "user", "content": "test"},
552
+ ],
553
+ max_tokens=5,
554
+ temperature=0,
555
+ stream=True,
556
+ )
557
+ tokens = []
558
+ async for chunk in resp2:
559
+ t = chunk.choices[0].delta.content if chunk.choices else None
560
+ if t: tokens.append(t)
561
+ results["step5"] = "Streaming OK: " + "".join(tokens)
562
+ except Exception as e:
563
+ return {"failed_at": "streaming", "error": str(e), "results_so_far": results}
564
+
565
+ return {"all_ok": True, "results": results}
566
+
567
+
568
  # ── OpenAlex related works per journal ────────────────────────────────────
569
  @app.get("/api/openalex-works")
570
  async def openalex_works(request: Request, source_id: str, per_page: int = 5):