Upload agent.py
Browse files
agent.py
CHANGED
|
@@ -119,6 +119,45 @@ class AgentRouter:
|
|
| 119 |
|
| 120 |
return tool_output
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
def respond(self, user_id: str, message: str) -> str:
|
| 123 |
memory_context = get_relevant_context(user_id, message)
|
| 124 |
route = self._classify(message)
|
|
@@ -135,6 +174,24 @@ class AgentRouter:
|
|
| 135 |
else:
|
| 136 |
tool_context = f"Tool used: {tool_name}\n{tool_output}"
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
response = self.model.generate(
|
| 139 |
message=message,
|
| 140 |
memory_context=memory_context,
|
|
|
|
| 119 |
|
| 120 |
return tool_output
|
| 121 |
|
| 122 |
+
@staticmethod
|
| 123 |
+
def _is_unhelpful_web_response(text: str) -> bool:
|
| 124 |
+
lower = text.lower()
|
| 125 |
+
bad_patterns = [
|
| 126 |
+
"i don't have access to real-time",
|
| 127 |
+
"i do not have access to real-time",
|
| 128 |
+
"i can't access real-time",
|
| 129 |
+
"cannot access real-time",
|
| 130 |
+
"as an ai language model",
|
| 131 |
+
"you can use any reliable news",
|
| 132 |
+
]
|
| 133 |
+
return any(pattern in lower for pattern in bad_patterns)
|
| 134 |
+
|
| 135 |
+
@staticmethod
|
| 136 |
+
def _summarize_web_tool_output(tool_output: str, message: str) -> str:
|
| 137 |
+
if tool_output.startswith("Web search unavailable"):
|
| 138 |
+
return "Web search is currently unavailable. Please try again in a moment."
|
| 139 |
+
|
| 140 |
+
if tool_output.startswith("No web results found"):
|
| 141 |
+
return f"I could not find recent web results for: {message}."
|
| 142 |
+
|
| 143 |
+
lines = [line.strip() for line in tool_output.splitlines() if line.strip()]
|
| 144 |
+
bullets = []
|
| 145 |
+
|
| 146 |
+
for line in lines[:5]:
|
| 147 |
+
# Expected line format:
|
| 148 |
+
# 1. Title | snippet text | Source: https://...
|
| 149 |
+
match = re.match(r"^\d+\.\s+(.*?)\s+\|\s+(.*?)\s+\|\s+Source:\s+(.*)$", line)
|
| 150 |
+
if match:
|
| 151 |
+
title, snippet, source = match.groups()
|
| 152 |
+
bullets.append(f"- {title}: {snippet} (Source: {source})")
|
| 153 |
+
else:
|
| 154 |
+
bullets.append(f"- {line}")
|
| 155 |
+
|
| 156 |
+
if not bullets:
|
| 157 |
+
return "I found web results, but could not format them cleanly. Please retry."
|
| 158 |
+
|
| 159 |
+
return "Here are the latest web results:\n" + "\n".join(bullets)
|
| 160 |
+
|
| 161 |
def respond(self, user_id: str, message: str) -> str:
|
| 162 |
memory_context = get_relevant_context(user_id, message)
|
| 163 |
route = self._classify(message)
|
|
|
|
| 174 |
else:
|
| 175 |
tool_context = f"Tool used: {tool_name}\n{tool_output}"
|
| 176 |
|
| 177 |
+
if tool_name == "web_search":
|
| 178 |
+
web_instruction = (
|
| 179 |
+
"Answer using only the provided web results. "
|
| 180 |
+
"Do not say you lack real-time access. "
|
| 181 |
+
"Provide a concise summary with sources."
|
| 182 |
+
)
|
| 183 |
+
response = self.model.generate(
|
| 184 |
+
message=f"{web_instruction}\n\nUser request: {message}",
|
| 185 |
+
memory_context=memory_context,
|
| 186 |
+
tool_context=tool_context,
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
if self._is_unhelpful_web_response(response):
|
| 190 |
+
response = self._summarize_web_tool_output(tool_output, message)
|
| 191 |
+
|
| 192 |
+
save_interaction(user_id, message, response)
|
| 193 |
+
return response
|
| 194 |
+
|
| 195 |
response = self.model.generate(
|
| 196 |
message=message,
|
| 197 |
memory_context=memory_context,
|