Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -36,38 +36,66 @@ def web_search(query: str) -> str:
|
|
| 36 |
"""Search DuckDuckGo for a query and return maximum 3 results.
|
| 37 |
Args:
|
| 38 |
query: The search query."""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
### =============== DOCUMENT PROCESSING TOOLS =============== ###
|
| 73 |
# File handling still requires external tools
|
|
@@ -103,6 +131,7 @@ sys_msg = SystemMessage(content=system_prompt)
|
|
| 103 |
|
| 104 |
tools = [
|
| 105 |
web_search,
|
|
|
|
| 106 |
save_and_read_file,
|
| 107 |
]
|
| 108 |
|
|
|
|
| 36 |
"""Search DuckDuckGo for a query and return maximum 3 results.
|
| 37 |
Args:
|
| 38 |
query: The search query."""
|
| 39 |
+
try:
|
| 40 |
+
search_results = DuckDuckGoSearchResults(max_results=3).invoke(query=query)
|
| 41 |
+
# DuckDuckGo returns results as a string, so we need to parse it
|
| 42 |
+
# The format is typically [snippet: text, title: title, link: url]
|
| 43 |
+
formatted_results = []
|
| 44 |
+
|
| 45 |
+
# Remove outer quotes if present and format properly
|
| 46 |
+
if isinstance(search_results, str):
|
| 47 |
+
# Simple parsing of the DuckDuckGo result format
|
| 48 |
+
result_entries = search_results.split('], [')
|
| 49 |
+
for entry in result_entries:
|
| 50 |
+
# Clean up the entry
|
| 51 |
+
entry = entry.replace('[', '').replace(']', '').strip()
|
| 52 |
+
if entry:
|
| 53 |
+
# Extract components
|
| 54 |
+
parts = {}
|
| 55 |
+
for part in entry.split(', '):
|
| 56 |
+
if ': ' in part:
|
| 57 |
+
key, value = part.split(': ', 1)
|
| 58 |
+
parts[key] = value
|
| 59 |
+
|
| 60 |
+
# Format as document
|
| 61 |
+
if 'snippet' in parts and 'link' in parts:
|
| 62 |
+
doc_content = parts.get('snippet', '')
|
| 63 |
+
doc_source = parts.get('link', '')
|
| 64 |
+
formatted_results.append(
|
| 65 |
+
f'<Document source="{doc_source}" page=""/>\n{doc_content}\n</Document>'
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if not formatted_results:
|
| 69 |
+
return {"web_results": "No results found for the query. Try using wikipedia_search instead."}
|
| 70 |
+
|
| 71 |
+
return {"web_results": "\n\n---\n\n".join(formatted_results)}
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return {"web_results": f"Error performing web search: {str(e)}. Try using wikipedia_search instead."}
|
| 74 |
+
|
| 75 |
+
@tool
|
| 76 |
+
def wikipedia_search(query: str) -> str:
|
| 77 |
+
"""Search Wikipedia for a query.
|
| 78 |
+
Args:
|
| 79 |
+
query: The search query."""
|
| 80 |
+
try:
|
| 81 |
+
# Use WikipediaLoader which works better in restricted environments
|
| 82 |
+
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 83 |
+
|
| 84 |
+
if not docs:
|
| 85 |
+
return {"wikipedia_results": "No Wikipedia results found for the query."}
|
| 86 |
+
|
| 87 |
+
formatted_results = []
|
| 88 |
+
for doc in docs:
|
| 89 |
+
title = doc.metadata.get('title', 'Unknown Title')
|
| 90 |
+
source = doc.metadata.get('source', 'Wikipedia')
|
| 91 |
+
content = doc.page_content[:1000] + "..." if len(doc.page_content) > 1000 else doc.page_content
|
| 92 |
+
formatted_results.append(
|
| 93 |
+
f'<Document source="{source}" title="{title}">\n{content}\n</Document>'
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return {"wikipedia_results": "\n\n---\n\n".join(formatted_results)}
|
| 97 |
+
except Exception as e:
|
| 98 |
+
return {"wikipedia_results": f"Error searching Wikipedia: {str(e)}."}
|
| 99 |
|
| 100 |
### =============== DOCUMENT PROCESSING TOOLS =============== ###
|
| 101 |
# File handling still requires external tools
|
|
|
|
| 131 |
|
| 132 |
tools = [
|
| 133 |
web_search,
|
| 134 |
+
wikipedia_search,
|
| 135 |
save_and_read_file,
|
| 136 |
]
|
| 137 |
|