Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import yaml
|
|
@@ -114,23 +115,30 @@ class BasicAgent:
|
|
| 114 |
description="Your friendly AI agent to help you with any task",
|
| 115 |
prompt_templates=prompt_templates,
|
| 116 |
)
|
| 117 |
-
# keep the fallback map on the instance
|
| 118 |
self.rag_fallbacks = rag_fallbacks
|
| 119 |
|
| 120 |
def __call__(self, question: str) -> str:
|
| 121 |
-
#
|
| 122 |
try:
|
| 123 |
-
|
| 124 |
except Exception:
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
# if
|
| 128 |
-
if not
|
| 129 |
q_low = question.lower()
|
| 130 |
for key, val in self.rag_fallbacks.items():
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
return val
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
# -----------------------------------------------------------------------------
|
| 136 |
# 3) EVALUATION LOGIC (unchanged)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
import yaml
|
|
|
|
| 115 |
description="Your friendly AI agent to help you with any task",
|
| 116 |
prompt_templates=prompt_templates,
|
| 117 |
)
|
|
|
|
| 118 |
self.rag_fallbacks = rag_fallbacks
|
| 119 |
|
| 120 |
def __call__(self, question: str) -> str:
|
| 121 |
+
# 1) run the agent
|
| 122 |
try:
|
| 123 |
+
raw_ans = self.agent.run(question)
|
| 124 |
except Exception:
|
| 125 |
+
raw_ans = ""
|
| 126 |
+
# 2) stringify for the empty/failure check
|
| 127 |
+
ans_str = str(raw_ans)
|
| 128 |
|
| 129 |
+
# 3) if it failed to produce anything meaningful, try RAG fallback
|
| 130 |
+
if not ans_str.strip() or ans_str.lower().startswith("i'm sorry"):
|
| 131 |
q_low = question.lower()
|
| 132 |
for key, val in self.rag_fallbacks.items():
|
| 133 |
+
# extract tokens > 2 chars from the key
|
| 134 |
+
tokens = re.findall(r"\w+", key.lower())
|
| 135 |
+
tokens = [t for t in tokens if len(t) > 2]
|
| 136 |
+
# if all appear in the question, use this fallback
|
| 137 |
+
if all(tok in q_low for tok in tokens):
|
| 138 |
return val
|
| 139 |
+
|
| 140 |
+
# otherwise return the real answer
|
| 141 |
+
return raw_ans
|
| 142 |
|
| 143 |
# -----------------------------------------------------------------------------
|
| 144 |
# 3) EVALUATION LOGIC (unchanged)
|