Update src/ai_core.py
Browse files- src/ai_core.py +37 -0
src/ai_core.py
CHANGED
|
@@ -87,6 +87,43 @@ def _flatten_intake(intake: Dict[str, Any]) -> str:
|
|
| 87 |
f"Meds: {', '.join(meds)}",
|
| 88 |
]).strip()
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# ----------------- SOAP generation -----------------
|
| 92 |
def _build_corpus_context(chunks: List[Dict[str, Any]], max_chars_per_chunk: int = 400) -> str:
|
|
|
|
| 87 |
f"Meds: {', '.join(meds)}",
|
| 88 |
]).strip()
|
| 89 |
|
| 90 |
+
def aiTriageSuggest(intake: Dict[str, Any]) -> Dict[str, Any]:
|
| 91 |
+
"""Keyword + LLM-based specialty triage."""
|
| 92 |
+
rule_spec, rule_mode = _keyword_triage(intake)
|
| 93 |
+
sys = {"role": "system", "content": (
|
| 94 |
+
"You are an intake triage assistant for an e-consult service. "
|
| 95 |
+
"Return STRICT JSON with keys: specialty (lowercase), "
|
| 96 |
+
"care_mode in ['e_consult_ok','in_person_recommended','urgent_emergent'], "
|
| 97 |
+
"supported (bool), rationale (<=50 words)."
|
| 98 |
+
)}
|
| 99 |
+
user = {
|
| 100 |
+
"role": "user",
|
| 101 |
+
"content": _flatten_intake(intake)
|
| 102 |
+
+ f"\n\nRule engine suggestion: specialty={rule_spec}, care_mode={rule_mode}."
|
| 103 |
+
}
|
| 104 |
+
try:
|
| 105 |
+
out = generate_chat([sys, user], max_new_tokens=160, temperature=0.0, do_sample=False)
|
| 106 |
+
obj = _safe_json_extract(out)
|
| 107 |
+
except Exception:
|
| 108 |
+
obj = {}
|
| 109 |
+
|
| 110 |
+
specialty = (obj.get("specialty") or rule_spec or "general").lower()
|
| 111 |
+
care_mode = (obj.get("care_mode") or rule_mode or "e_consult_ok")
|
| 112 |
+
supported = bool(obj.get("supported")) if "supported" in obj else (care_mode == "e_consult_ok")
|
| 113 |
+
rationale = obj.get("rationale") or (
|
| 114 |
+
f"Keyword triage matched => specialty={specialty}, care_mode={care_mode}."
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
return {
|
| 118 |
+
"status": "ok" if obj else "rule_only",
|
| 119 |
+
"specialty": specialty,
|
| 120 |
+
"care_mode": care_mode,
|
| 121 |
+
"supported": supported,
|
| 122 |
+
"rationale": rationale[:400],
|
| 123 |
+
"model_notes": "LLM+rules" if obj else "rules_only",
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
|
| 127 |
|
| 128 |
# ----------------- SOAP generation -----------------
|
| 129 |
def _build_corpus_context(chunks: List[Dict[str, Any]], max_chars_per_chunk: int = 400) -> str:
|