Update agent.py
Browse files
agent.py
CHANGED
|
@@ -13,7 +13,7 @@ from langchain_google_genai import ChatGoogleGenerativeAI
|
|
| 13 |
from pathlib import Path
|
| 14 |
import tempfile
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
-
|
| 17 |
|
| 18 |
# constants
|
| 19 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -203,27 +203,27 @@ def download_and_read_file(task_id: str) -> str:
|
|
| 203 |
if os.path.exists(temp_path):
|
| 204 |
os.remove(temp_path)
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
|
| 228 |
|
| 229 |
model = build_gemini_llm()
|
|
@@ -235,7 +235,8 @@ tools = [
|
|
| 235 |
divide_numbers,
|
| 236 |
extract_text_from_image,
|
| 237 |
download_and_read_file,
|
| 238 |
-
|
|
|
|
| 239 |
]
|
| 240 |
|
| 241 |
model_with_tools = model.bind_tools(tools)
|
|
|
|
| 13 |
from pathlib import Path
|
| 14 |
import tempfile
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
+
import wikipedia
|
| 17 |
|
| 18 |
# constants
|
| 19 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 203 |
if os.path.exists(temp_path):
|
| 204 |
os.remove(temp_path)
|
| 205 |
|
| 206 |
+
@tool
|
| 207 |
+
def wikipedia_search(query: str):
|
| 208 |
+
"""
|
| 209 |
+
Search wikipedia for a query and return a max of three results.
|
| 210 |
+
Takes a string query as the search query, DO NOT include advanced search operators like "site:...", URLs, or file types, only accepts clean text keywords.
|
| 211 |
+
Args:
|
| 212 |
+
query (str): The clean search keywords without any search operators.
|
| 213 |
+
"""
|
| 214 |
+
try:
|
| 215 |
+
search_results = WikipediaLoader(query=query, load_max_docs=3).load()
|
| 216 |
|
| 217 |
+
if not search_results:
|
| 218 |
+
return f"No Wikipedia results found for {query}. Consider another query or try a web search."
|
| 219 |
+
return "\n\n---\n\n".join(
|
| 220 |
+
f"Title: {doc.metadata.get('title', 'Unknown')}\n"
|
| 221 |
+
f"Content: {doc.page_content}"
|
| 222 |
+
for doc in search_results
|
| 223 |
+
)
|
| 224 |
+
except Exception as e:
|
| 225 |
+
print(f"Wikipedia search failed for {query}: {e}")
|
| 226 |
+
return f"Wikipedia search failed for {query} due to {str(e)}."
|
| 227 |
|
| 228 |
|
| 229 |
model = build_gemini_llm()
|
|
|
|
| 235 |
divide_numbers,
|
| 236 |
extract_text_from_image,
|
| 237 |
download_and_read_file,
|
| 238 |
+
search_web,
|
| 239 |
+
wikipedia_search
|
| 240 |
]
|
| 241 |
|
| 242 |
model_with_tools = model.bind_tools(tools)
|