keyword_solve
Browse files
app.py
CHANGED
@@ -188,13 +188,31 @@ class BasicAgent:
|
|
188 |
return state
|
189 |
|
190 |
def _perform_search(self, state: AgentState) -> AgentState:
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
state["current_step"] = "recheck"
|
199 |
return state
|
200 |
|
|
|
188 |
return state
|
189 |
|
190 |
def _perform_search(self, state: AgentState) -> AgentState:
|
191 |
+
try:
|
192 |
+
results = simple_search(state["search_query"], max_results=5)
|
193 |
+
print("\nSearch Results:")
|
194 |
+
for i, s in enumerate(results, 1):
|
195 |
+
print(f"[{i}] {s[:120]}…")
|
196 |
+
|
197 |
+
if not results:
|
198 |
+
print("Warning: No search results found")
|
199 |
+
state["needs_search"] = True # Try reformulating query
|
200 |
+
else:
|
201 |
+
state["needs_search"] = False
|
202 |
+
|
203 |
+
state["history"].append({"step": "search", "results": results})
|
204 |
+
state["logs"]["search"] = {
|
205 |
+
"query": state["search_query"],
|
206 |
+
"results_count": len(results),
|
207 |
+
"results": results
|
208 |
+
}
|
209 |
+
|
210 |
+
except Exception as e:
|
211 |
+
print(f"Search error: {str(e)}")
|
212 |
+
state["needs_search"] = True
|
213 |
+
state["history"].append({"step": "search", "error": str(e)})
|
214 |
+
state["logs"]["search_error"] = str(e)
|
215 |
+
|
216 |
state["current_step"] = "recheck"
|
217 |
return state
|
218 |
|
tools.py
CHANGED
@@ -79,7 +79,40 @@ def retry_ddg(
|
|
79 |
# -------- the only search function your agent will call
|
80 |
def simple_search(query: str, max_results: int = 5) -> List[str]:
|
81 |
"""
|
82 |
-
Perform a
|
|
|
83 |
"""
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# -------- the only search function your agent will call
|
80 |
def simple_search(query: str, max_results: int = 5) -> List[str]:
|
81 |
"""
|
82 |
+
Perform a web search using DuckDuckGo and return formatted results.
|
83 |
+
Includes retry logic and better error handling.
|
84 |
"""
|
85 |
+
def _raw_search(q: str, max_results: int) -> List[str]:
|
86 |
+
try:
|
87 |
+
with DDGS() as ddgs:
|
88 |
+
results = []
|
89 |
+
for r in ddgs.text(q, max_results=max_results):
|
90 |
+
# Format each result with title, link and snippet
|
91 |
+
result = f"{r['title']} – {r['link']}\n{r['body']}"
|
92 |
+
results.append(result)
|
93 |
+
return results
|
94 |
+
except Exception as e:
|
95 |
+
print(f"Search error: {str(e)}")
|
96 |
+
return []
|
97 |
+
|
98 |
+
# Retry logic with exponential backoff
|
99 |
+
max_attempts = 4
|
100 |
+
base_delay = 10 # seconds
|
101 |
+
|
102 |
+
for attempt in range(max_attempts):
|
103 |
+
try:
|
104 |
+
results = _raw_search(query, max_results)
|
105 |
+
if results:
|
106 |
+
return results
|
107 |
+
print(f"Attempt {attempt + 1}/{max_attempts}: No results found")
|
108 |
+
except Exception as e:
|
109 |
+
print(f"Attempt {attempt + 1}/{max_attempts} failed: {str(e)}")
|
110 |
+
if attempt < max_attempts - 1:
|
111 |
+
delay = base_delay * (2 ** attempt) # exponential backoff
|
112 |
+
print(f"Retrying in {delay}s...")
|
113 |
+
time.sleep(delay)
|
114 |
+
else:
|
115 |
+
print(f"All {max_attempts} attempts failed. Last exception: {str(e)}")
|
116 |
+
return []
|
117 |
+
|
118 |
+
return []
|