Spaces:
Sleeping
Sleeping
File size: 2,183 Bytes
43a4b46 e90c3c4 43a4b46 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """Drive the five PRD use cases (plus a memory follow-up) through /chat and
print the routed intent, streamed answer, and citations for each."""
from __future__ import annotations
import json
import sys
import httpx
API = "http://127.0.0.1:8000"
THREAD = "test-session-1"
QUESTIONS = [
("UC-1 (quantitative)", "What's the engagement rate of each?"),
("UC-2 (quantitative)", "Who's the creator of Video B and what's their follower count?"),
("UC-3 (qualitative)", "Compare the hooks in the first 5 seconds."),
("UC-4 (qualitative)", "Suggest improvements for B based on what worked in A."),
("UC-5 (hybrid)", "Why did Video A get more engagement than Video B?"),
("Follow-up (memory)", "Based on that, which video's style should I copy next time?"),
]
def ask(label: str, message: str) -> None:
print(f"\n{'='*70}\n{label}\nQ: {message}\n{'-'*70}")
intent = None
citations = []
answer_parts: list[str] = []
with httpx.stream(
"POST", f"{API}/chat",
json={"message": message, "thread_id": THREAD}, timeout=120,
) as r:
r.raise_for_status()
for line in r.iter_lines():
if not line.startswith("data: "):
continue
evt = json.loads(line[6:])
t = evt.get("type")
if t == "intent":
intent = evt["intent"]
elif t == "citations":
citations = evt["citations"]
elif t == "token":
answer_parts.append(evt["content"])
sys.stdout.write(evt["content"])
sys.stdout.flush()
elif t == "error":
print(f"\n[SERVER ERROR] {evt['message']}")
elif t == "done":
break
print(f"\n{'-'*70}")
print(f"ROUTED INTENT: {intent}")
if citations:
cites = ", ".join(
f"[V{c['video_id']} chunk {c['chunk_id']} {c['modality']}]" for c in citations
)
print(f"CITATIONS: {cites}")
else:
print("CITATIONS: (none — quantitative path)")
def main() -> None:
for label, q in QUESTIONS:
ask(label, q)
if __name__ == "__main__":
main()
|