Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -64,34 +64,41 @@ def process_tool_output(tool_output):
|
|
| 64 |
return tool_output.get('title', str(tool_output))
|
| 65 |
return str(tool_output)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
return process_tool_output(results)
|
| 76 |
-
|
| 77 |
print(f"Error in DuckDuckGoSearchTool: {e}")
|
| 78 |
return f"Search error: {e}"
|
| 79 |
custome_duck_search = CustomDuckDuckGoSearchTool()
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
custome_web_search = CustomVisitWebpageTool()
|
| 96 |
|
| 97 |
#=======Agent========#
|
|
|
|
| 64 |
return tool_output.get('title', str(tool_output))
|
| 65 |
return str(tool_output)
|
| 66 |
|
| 67 |
+
@tool
|
| 68 |
+
def CustomDuckDuckGoSearchTool(query:str): -> str
|
| 69 |
+
"""
|
| 70 |
+
Perform a DuckDuckGo search and return a string result.
|
| 71 |
+
|
| 72 |
+
Args:
|
| 73 |
+
query: the question you input
|
| 74 |
+
"""
|
| 75 |
+
try:
|
| 76 |
+
with DDGS() as ddgs:
|
| 77 |
+
results = list(ddgs.text(query, max_results=3)) # Convert generator to list
|
| 78 |
return process_tool_output(results)
|
| 79 |
+
except Exception as e:
|
| 80 |
print(f"Error in DuckDuckGoSearchTool: {e}")
|
| 81 |
return f"Search error: {e}"
|
| 82 |
custome_duck_search = CustomDuckDuckGoSearchTool()
|
| 83 |
|
| 84 |
+
@tool
|
| 85 |
+
def CustomVisitWebpageTool(query:str):->str
|
| 86 |
+
"""
|
| 87 |
+
Visit a webpage and extract content as a string.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
query: the question you input
|
| 91 |
+
"""
|
| 92 |
+
try:
|
| 93 |
+
response = requests.get(url, timeout=10)
|
| 94 |
+
response.raise_for_status()
|
| 95 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 96 |
+
# Extract text from the webpage
|
| 97 |
+
text = soup.get_text(separator=' ', strip=True)
|
| 98 |
+
return text[:1000] # Limit output length to avoid overload
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"Error in VisitWebpageTool: {e}")
|
| 101 |
+
return f"Webpage access error: {e}"
|
| 102 |
custome_web_search = CustomVisitWebpageTool()
|
| 103 |
|
| 104 |
#=======Agent========#
|