Spaces:
Sleeping
Sleeping
Update backend/agent.py
Browse files- backend/agent.py +7 -7
backend/agent.py
CHANGED
|
@@ -15,11 +15,10 @@ def normalize(s):
|
|
| 15 |
|
| 16 |
def clean(s):
|
| 17 |
s = unicodedata.normalize("NFKC", s)
|
| 18 |
-
# 移除所有不可見字元(zero-width space, BOM, control chars)
|
| 19 |
return "".join(ch for ch in s if ch.isprintable())
|
| 20 |
|
| 21 |
-
def run_agent(user_input: str):
|
| 22 |
|
|
|
|
| 23 |
logging.warning(f"[AGENT] user_input = {user_input}")
|
| 24 |
|
| 25 |
token = clean(user_input.split()[-1])
|
|
@@ -34,11 +33,9 @@ def run_agent(user_input: str):
|
|
| 34 |
for t in triggers:
|
| 35 |
t_clean = clean(t)
|
| 36 |
|
| 37 |
-
# token 開頭匹配(用 clean 後的版本)
|
| 38 |
if t_clean.startswith(token) and token != t_clean:
|
| 39 |
prefix_candidates.append(t_clean)
|
| 40 |
|
| 41 |
-
# 多選 prefix
|
| 42 |
if len(prefix_candidates) > 1:
|
| 43 |
return {
|
| 44 |
"type": "trigger-multi-prefix",
|
|
@@ -46,7 +43,6 @@ def run_agent(user_input: str):
|
|
| 46 |
"candidates": prefix_candidates
|
| 47 |
}
|
| 48 |
|
| 49 |
-
# 單一 prefix
|
| 50 |
if len(prefix_candidates) == 1:
|
| 51 |
return {
|
| 52 |
"type": "trigger-prefix",
|
|
@@ -64,7 +60,11 @@ def run_agent(user_input: str):
|
|
| 64 |
return run_fixed_output(skill_name)
|
| 65 |
|
| 66 |
# -----------------------------
|
| 67 |
-
# 3. fallback → AI
|
| 68 |
# -----------------------------
|
| 69 |
logging.warning("[AGENT] → fallback to AI")
|
| 70 |
-
return run_ai_output(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def clean(s):
|
| 17 |
s = unicodedata.normalize("NFKC", s)
|
|
|
|
| 18 |
return "".join(ch for ch in s if ch.isprintable())
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
+
def run_agent(user_input: str, patient_id: int, db):
|
| 22 |
logging.warning(f"[AGENT] user_input = {user_input}")
|
| 23 |
|
| 24 |
token = clean(user_input.split()[-1])
|
|
|
|
| 33 |
for t in triggers:
|
| 34 |
t_clean = clean(t)
|
| 35 |
|
|
|
|
| 36 |
if t_clean.startswith(token) and token != t_clean:
|
| 37 |
prefix_candidates.append(t_clean)
|
| 38 |
|
|
|
|
| 39 |
if len(prefix_candidates) > 1:
|
| 40 |
return {
|
| 41 |
"type": "trigger-multi-prefix",
|
|
|
|
| 43 |
"candidates": prefix_candidates
|
| 44 |
}
|
| 45 |
|
|
|
|
| 46 |
if len(prefix_candidates) == 1:
|
| 47 |
return {
|
| 48 |
"type": "trigger-prefix",
|
|
|
|
| 60 |
return run_fixed_output(skill_name)
|
| 61 |
|
| 62 |
# -----------------------------
|
| 63 |
+
# 3. fallback → AI(帶 patient_id + db)
|
| 64 |
# -----------------------------
|
| 65 |
logging.warning("[AGENT] → fallback to AI")
|
| 66 |
+
return run_ai_output(
|
| 67 |
+
input_text=user_input,
|
| 68 |
+
patient_id=patient_id,
|
| 69 |
+
db=db
|
| 70 |
+
)
|