Update agent.py
Browse files
agent.py
CHANGED
|
@@ -13,6 +13,9 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
| 13 |
from pathlib import Path
|
| 14 |
import tempfile
|
| 15 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# constants
|
| 18 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -148,31 +151,97 @@ def build_gemini_llm():
|
|
| 148 |
# return f"Failed to read file: {e}"
|
| 149 |
# except Exception as e:
|
| 150 |
# return f"error downloading or reading file: {str(e)}"
|
| 151 |
-
import traceback
|
| 152 |
-
|
| 153 |
@tool
|
| 154 |
def wikipedia_search(query: str) -> str:
|
| 155 |
"""
|
| 156 |
-
Search
|
| 157 |
-
Takes a string query as the keywords to search
|
| 158 |
Args:
|
| 159 |
-
query
|
| 160 |
"""
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
# fix wikipedia engine builds invalid URL for region="wt-wt"(default) issue from duckducksearchrun
|
| 178 |
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
|
|
@@ -186,7 +255,11 @@ search_ddgs = DuckDuckGoSearchRun(
|
|
| 186 |
|
| 187 |
@tool
|
| 188 |
def search_web(query: str) -> str:
|
| 189 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
try:
|
| 191 |
result = search_ddgs.invoke(query)
|
| 192 |
if not result:
|
|
|
|
| 13 |
from pathlib import Path
|
| 14 |
import tempfile
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
+
import traceback
|
| 17 |
+
import time
|
| 18 |
+
import random
|
| 19 |
|
| 20 |
# constants
|
| 21 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 151 |
# return f"Failed to read file: {e}"
|
| 152 |
# except Exception as e:
|
| 153 |
# return f"error downloading or reading file: {str(e)}"
|
|
|
|
|
|
|
| 154 |
@tool
|
| 155 |
def wikipedia_search(query: str) -> str:
|
| 156 |
"""
|
| 157 |
+
Search Wikipedia for a query and return relevant results.
|
|
|
|
| 158 |
Args:
|
| 159 |
+
query: Keywords to search on Wikipedia.
|
| 160 |
"""
|
| 161 |
+
|
| 162 |
+
url = "https://en.wikipedia.org/w/api.php"
|
| 163 |
+
params = {
|
| 164 |
+
"action": "query",
|
| 165 |
+
"list": "search",
|
| 166 |
+
"srsearch": query,
|
| 167 |
+
"format": "json",
|
| 168 |
+
"srlimit": 3,
|
| 169 |
+
"utf8": 1,
|
| 170 |
+
}
|
| 171 |
+
headers = {
|
| 172 |
+
"User-Agent": "MyLangGraphAgent/1.0"
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
max_retries = 3
|
| 176 |
+
for attempt in range(max_retries):
|
| 177 |
+
try:
|
| 178 |
+
response = requests.get(
|
| 179 |
+
url,
|
| 180 |
+
params=params,
|
| 181 |
+
headers=headers,
|
| 182 |
+
timeout=10,
|
| 183 |
+
)
|
| 184 |
+
print(
|
| 185 |
+
f"Wikipedia status={response.status_code}, "
|
| 186 |
+
f"content-type={response.headers.get('content-type')}"
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
response.raise_for_status()
|
| 190 |
+
|
| 191 |
+
data = response.json()
|
| 192 |
+
results = data.get("query", {}).get("search", [])
|
| 193 |
+
if not results:
|
| 194 |
+
return f"No Wikipedia results found for '{query}'."
|
| 195 |
+
return "\n\n---\n\n".join(
|
| 196 |
+
f"Title: {item['title']}\n"
|
| 197 |
+
f"Snippet: {item.get('snippet', '')}"
|
| 198 |
+
for item in results
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
except requests.exceptions.RequestException as e:
|
| 202 |
+
print(
|
| 203 |
+
f"Wikipedia request failed "
|
| 204 |
+
f"attempt {attempt + 1}/{max_retries}: {e}"
|
| 205 |
+
)
|
| 206 |
+
except requests.exceptions.JSONDecodeError:
|
| 207 |
+
print(
|
| 208 |
+
f"Wikipedia returned non-JSON response. "
|
| 209 |
+
f"Status={response.status_code}"
|
| 210 |
+
)
|
| 211 |
+
print("Response preview:")
|
| 212 |
+
print(response.text[:500])
|
| 213 |
+
|
| 214 |
+
if attempt < max_retries - 1:
|
| 215 |
+
delay = 2 ** attempt + random.random()
|
| 216 |
+
print(f"Retrying in {delay:.2f}s")
|
| 217 |
+
time.sleep(delay)
|
| 218 |
+
return (
|
| 219 |
+
f"Wikipedia search temporarily failed for '{query}'. "
|
| 220 |
+
"Please use another search source."
|
| 221 |
+
)
|
| 222 |
+
# @tool
|
| 223 |
+
# def wikipedia_search(query: str) -> str:
|
| 224 |
+
# """
|
| 225 |
+
# Search wikipedia for a query and return results.
|
| 226 |
+
# Takes a string query as the keywords to search
|
| 227 |
+
# Args:
|
| 228 |
+
# query (str): Keywords you want to search.
|
| 229 |
+
# """
|
| 230 |
+
# try:
|
| 231 |
+
# search_results = WikipediaLoader(query=query, load_max_docs=3).load()
|
| 232 |
+
|
| 233 |
+
# if not search_results:
|
| 234 |
+
# return f"No Wikipedia results found for {query}. Consider another query or try a web search."
|
| 235 |
+
# print("wiki result:")
|
| 236 |
+
# print(search_results)
|
| 237 |
+
# return "---\n".join(
|
| 238 |
+
# f"Title: {doc.metadata.get('title', 'Unknown')}\n"
|
| 239 |
+
# f"Content: {doc.page_content}"
|
| 240 |
+
# for doc in search_results
|
| 241 |
+
# )
|
| 242 |
+
# except Exception as e:
|
| 243 |
+
# traceback.print_exc()
|
| 244 |
+
# return f"Wikipedia search failed: {type(e).__name__}: {e}"
|
| 245 |
|
| 246 |
# fix wikipedia engine builds invalid URL for region="wt-wt"(default) issue from duckducksearchrun
|
| 247 |
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
|
|
|
|
| 255 |
|
| 256 |
@tool
|
| 257 |
def search_web(query: str) -> str:
|
| 258 |
+
"""
|
| 259 |
+
Search the web using DuckDuckGoSearchRun.
|
| 260 |
+
Args:
|
| 261 |
+
query: Keywords to search, only keywords and spaces.
|
| 262 |
+
"""
|
| 263 |
try:
|
| 264 |
result = search_ddgs.invoke(query)
|
| 265 |
if not result:
|