qyle commited on
Commit
773e082
·
verified ·
1 Parent(s): 07a1b2a

stress test without triage

Browse files
Files changed (1) hide show
  1. service.py +66 -0
service.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/champ/service.py
2
+
3
+ import asyncio
4
+ from dataclasses import dataclass
5
+ from pathlib import Path
6
+ from typing import Optional, Sequence
7
+
8
+ from langchain_community.vectorstores import FAISS as LCFAISS
9
+ from langchain_core.messages import HumanMessage
10
+
11
+
12
+ from .rag import load_vector_store
13
+ from .agent import build_champ_agent
14
+ from .triage import safety_triage
15
+
16
+
17
+ @dataclass
18
+ class ChampService:
19
+ base_dir: Path
20
+ hf_token: str
21
+ vector_store: Optional[LCFAISS] = None
22
+ agent = None
23
+
24
+ async def init(self):
25
+ rag_relpath = "rag_data/FAISS_ALLEN_20260129"
26
+ rag_dir = self.base_dir / rag_relpath
27
+
28
+ if not rag_dir.exists():
29
+ raise RuntimeError(
30
+ f"FAISS index not found at {rag_dir}. "
31
+ "Build it locally and upload it (index.faiss + index.pkl)."
32
+ )
33
+
34
+ loop = asyncio.get_running_loop()
35
+ self.vector_store = await loop.run_in_executor(
36
+ None,
37
+ load_vector_store,
38
+ self.base_dir,
39
+ self.hf_token,
40
+ rag_relpath,
41
+ )
42
+ self.agent = build_champ_agent(self.vector_store)
43
+
44
+ def invoke(self, lc_messages: Sequence) -> str:
45
+ if self.agent is None:
46
+ raise RuntimeError("CHAMP is not initialized yet.")
47
+ # --- Safety triage micro-layer (before LLM) ---
48
+ last_user_text = None
49
+ for m in reversed(lc_messages):
50
+ if isinstance(m, HumanMessage):
51
+ last_user_text = m.content
52
+ break
53
+
54
+ # if last_user_text:
55
+ # triggered, override_reply, reason = safety_triage(last_user_text)
56
+ # if triggered:
57
+ # return override_reply, {
58
+ # "triage_triggered": True,
59
+ # "triage_reason": reason,
60
+ # }
61
+
62
+ # result = self.agent.invoke({"messages": list(lc_messages)})
63
+ result = "If your infant is vomiting after every feeding, stop feeding for about 4 hours and give small sips of oral rehydration solution (ORS) or breast milk—about 5–15 ml every 5–15 minutes. Gradually re‑introduce her usual food once the vomiting lessens. Avoid giving juice, soft drinks, or rice water, and keep her hydrated with ORS. If vomiting continues, she becomes lethargic, or shows signs of dehydration, seek urgent medical care right away."
64
+ return result["messages"][-1].text.strip(), {
65
+ "triage_triggered": False,
66
+ }