Update app.py
Browse files
app.py
CHANGED
|
@@ -78,66 +78,19 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error fetching time for timezone '{timezone}': {e}"
|
| 80 |
|
| 81 |
-
# -----------------------------------------------------------------------------
|
| 82 |
-
# 1b) FILE-UTILITIES
|
| 83 |
-
# -----------------------------------------------------------------------------
|
| 84 |
-
@tool
|
| 85 |
-
def list_uploaded_files() -> str:
|
| 86 |
-
"""
|
| 87 |
-
Lists all files and directories in /mnt/data (where uploads appear).
|
| 88 |
-
"""
|
| 89 |
-
try:
|
| 90 |
-
items = os.listdir("/mnt/data")
|
| 91 |
-
if not items:
|
| 92 |
-
return "No files uploaded."
|
| 93 |
-
return ", ".join(items)
|
| 94 |
-
except Exception as e:
|
| 95 |
-
return f"Error listing files: {e}"
|
| 96 |
-
|
| 97 |
-
@tool
|
| 98 |
-
def read_file(path: str) -> str:
|
| 99 |
-
"""
|
| 100 |
-
Reads a text file from /mnt/data and returns its contents.
|
| 101 |
-
|
| 102 |
-
Args:
|
| 103 |
-
path: Filename under /mnt/data to read.
|
| 104 |
-
"""
|
| 105 |
-
full = os.path.join("/mnt/data", path)
|
| 106 |
-
try:
|
| 107 |
-
with open(full, "r") as f:
|
| 108 |
-
return f.read()
|
| 109 |
-
except Exception as e:
|
| 110 |
-
return f"Error reading {path}: {e}"
|
| 111 |
-
|
| 112 |
-
@tool
|
| 113 |
-
def read_excel(path: str) -> str:
|
| 114 |
-
"""
|
| 115 |
-
Reads an Excel file from /mnt/data and returns its contents as a markdown table.
|
| 116 |
-
|
| 117 |
-
Args:
|
| 118 |
-
path: Filename under /mnt/data to read.
|
| 119 |
-
"""
|
| 120 |
-
full = os.path.join("/mnt/data", path)
|
| 121 |
-
try:
|
| 122 |
-
df = pd.read_excel(full)
|
| 123 |
-
# pandas.DataFrame.to_markdown requires tabulate
|
| 124 |
-
return df.to_markdown(index=False)
|
| 125 |
-
except Exception as e:
|
| 126 |
-
return f"Error reading Excel {path}: {e}"
|
| 127 |
-
|
| 128 |
# -----------------------------------------------------------------------------
|
| 129 |
# 2) LOAD PROMPTS & RAG MAP
|
| 130 |
# -----------------------------------------------------------------------------
|
| 131 |
with open("prompts.yaml", "r") as f:
|
| 132 |
prompt_templates = yaml.safe_load(f)
|
| 133 |
|
| 134 |
-
#
|
| 135 |
rag_fallbacks = prompt_templates.get("rag_fallbacks", {})
|
| 136 |
|
| 137 |
@tool
|
| 138 |
def rag_fallback(question: str) -> str:
|
| 139 |
"""
|
| 140 |
-
Returns a pre
|
| 141 |
|
| 142 |
Args:
|
| 143 |
question: The user’s question.
|
|
@@ -171,12 +124,9 @@ class BasicAgent:
|
|
| 171 |
get_weather,
|
| 172 |
generate_pun,
|
| 173 |
web_search,
|
| 174 |
-
|
| 175 |
-
read_file,
|
| 176 |
-
read_excel,
|
| 177 |
-
rag_fallback, # fallback now a tool
|
| 178 |
],
|
| 179 |
-
max_steps=
|
| 180 |
verbosity_level=1,
|
| 181 |
name="Son_of_Anton",
|
| 182 |
description="Your friendly AI agent to help you with any task",
|
|
@@ -185,23 +135,25 @@ class BasicAgent:
|
|
| 185 |
self.rag_fallbacks = rag_fallbacks
|
| 186 |
|
| 187 |
def __call__(self, question: str) -> str:
|
| 188 |
-
# 1) run the agent
|
| 189 |
try:
|
| 190 |
raw_ans = self.agent.run(question)
|
| 191 |
except Exception:
|
| 192 |
raw_ans = ""
|
| 193 |
-
ans_str = str(raw_ans)
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
| 201 |
for key, val in self.rag_fallbacks.items():
|
| 202 |
tokens = re.findall(r"\w+", key.lower())
|
| 203 |
tokens = [t for t in tokens if len(t) > 2]
|
| 204 |
-
if tokens and all(tok in
|
| 205 |
return val
|
| 206 |
|
| 207 |
return raw_ans
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"Error fetching time for timezone '{timezone}': {e}"
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# -----------------------------------------------------------------------------
|
| 82 |
# 2) LOAD PROMPTS & RAG MAP
|
| 83 |
# -----------------------------------------------------------------------------
|
| 84 |
with open("prompts.yaml", "r") as f:
|
| 85 |
prompt_templates = yaml.safe_load(f)
|
| 86 |
|
| 87 |
+
# **make sure your prompts.yaml has a top-level key `rag_fallbacks:`**
|
| 88 |
rag_fallbacks = prompt_templates.get("rag_fallbacks", {})
|
| 89 |
|
| 90 |
@tool
|
| 91 |
def rag_fallback(question: str) -> str:
|
| 92 |
"""
|
| 93 |
+
Returns a pre-cached answer if the question matches any RAG key.
|
| 94 |
|
| 95 |
Args:
|
| 96 |
question: The user’s question.
|
|
|
|
| 124 |
get_weather,
|
| 125 |
generate_pun,
|
| 126 |
web_search,
|
| 127 |
+
rag_fallback, # ← our new fallback tool
|
|
|
|
|
|
|
|
|
|
| 128 |
],
|
| 129 |
+
max_steps=7, # ← allow one extra step for fallback
|
| 130 |
verbosity_level=1,
|
| 131 |
name="Son_of_Anton",
|
| 132 |
description="Your friendly AI agent to help you with any task",
|
|
|
|
| 135 |
self.rag_fallbacks = rag_fallbacks
|
| 136 |
|
| 137 |
def __call__(self, question: str) -> str:
|
| 138 |
+
# 1) run the agent (it now has rag_fallback available as step 7)
|
| 139 |
try:
|
| 140 |
raw_ans = self.agent.run(question)
|
| 141 |
except Exception:
|
| 142 |
raw_ans = ""
|
|
|
|
| 143 |
|
| 144 |
+
ans_str = str(raw_ans).strip().lower()
|
| 145 |
+
# 2) manual fallback if still empty/apologetic/unhelpful
|
| 146 |
+
if (
|
| 147 |
+
not ans_str
|
| 148 |
+
or ans_str.startswith("i'm sorry")
|
| 149 |
+
or "unanswered" in ans_str
|
| 150 |
+
or "none" in ans_str
|
| 151 |
+
or "failed" in ans_str
|
| 152 |
+
):
|
| 153 |
for key, val in self.rag_fallbacks.items():
|
| 154 |
tokens = re.findall(r"\w+", key.lower())
|
| 155 |
tokens = [t for t in tokens if len(t) > 2]
|
| 156 |
+
if tokens and all(tok in ans_str or tok in question.lower() for tok in tokens):
|
| 157 |
return val
|
| 158 |
|
| 159 |
return raw_ans
|