Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -18,6 +18,7 @@ from langchain_community.document_loaders import ImageCaptionLoader
|
|
| 18 |
import requests, time
|
| 19 |
import pandas as pd
|
| 20 |
from pathlib import Path
|
|
|
|
| 21 |
from langchain_community.tools import WikipediaQueryRun
|
| 22 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 23 |
from langchain_community.document_loaders import YoutubeLoader
|
|
@@ -35,9 +36,33 @@ def duckduck_websearch(query: str) -> str:
|
|
| 35 |
Args:
|
| 36 |
query: what you want to search
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
@tool
|
| 43 |
def serper_websearch(query: str) -> str:
|
|
@@ -323,6 +348,7 @@ class BasicAgent:
|
|
| 323 |
except Exception as e:
|
| 324 |
sleep_time = base_sleep * (attempt + 1)
|
| 325 |
if attempt < max_retries - 1:
|
|
|
|
| 326 |
print(f"Attempt {attempt + 1} failed. Retrying in {sleep_time} seconds...")
|
| 327 |
time.sleep(sleep_time)
|
| 328 |
continue
|
|
@@ -372,8 +398,8 @@ class BasicAgent:
|
|
| 372 |
agent=agent,
|
| 373 |
tools=self.tools,
|
| 374 |
memory=self.memory,
|
| 375 |
-
max_iterations=
|
| 376 |
verbose=True,
|
| 377 |
handle_parsing_errors=True,
|
| 378 |
-
return_only_outputs=True # This ensures we only get the final output
|
| 379 |
)
|
|
|
|
| 18 |
import requests, time
|
| 19 |
import pandas as pd
|
| 20 |
from pathlib import Path
|
| 21 |
+
from bs4 import BeautifulSoup
|
| 22 |
from langchain_community.tools import WikipediaQueryRun
|
| 23 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 24 |
from langchain_community.document_loaders import YoutubeLoader
|
|
|
|
| 36 |
Args:
|
| 37 |
query: what you want to search
|
| 38 |
"""
|
| 39 |
+
try:
|
| 40 |
+
search = DuckDuckGoSearchResults()
|
| 41 |
+
results = search.invoke(query)
|
| 42 |
+
return "\n".join(results)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(str(e))
|
| 45 |
+
print('Try to use request method for duckcudckgo Search')
|
| 46 |
+
base_url = "https://html.duckduckgo.com/html"
|
| 47 |
+
params = {"q": query}
|
| 48 |
+
response = requests.get(base_url, params=params, timeout=10)
|
| 49 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 50 |
+
for result in soup.find_all('div', {'class': 'result'}):
|
| 51 |
+
title = result.find('a', {'class': 'result__a'})
|
| 52 |
+
snippet = result.find('a', {'class': 'result__snippet'})
|
| 53 |
+
if title and snippet:
|
| 54 |
+
results.append({
|
| 55 |
+
'title': title.get_text(),
|
| 56 |
+
'snippet': snippet.get_text(),
|
| 57 |
+
'url': title.get('href')
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
# Format results
|
| 61 |
+
formatted_results = []
|
| 62 |
+
for r in results[:10]: # Limit to top 5 results
|
| 63 |
+
formatted_results.append(f"[{r['title']}]({r['url']})\n{r['snippet']}\n")
|
| 64 |
+
|
| 65 |
+
return "## Search Results\n\n" + "\n".join(formatted_results)
|
| 66 |
|
| 67 |
@tool
|
| 68 |
def serper_websearch(query: str) -> str:
|
|
|
|
| 348 |
except Exception as e:
|
| 349 |
sleep_time = base_sleep * (attempt + 1)
|
| 350 |
if attempt < max_retries - 1:
|
| 351 |
+
print(str(e))
|
| 352 |
print(f"Attempt {attempt + 1} failed. Retrying in {sleep_time} seconds...")
|
| 353 |
time.sleep(sleep_time)
|
| 354 |
continue
|
|
|
|
| 398 |
agent=agent,
|
| 399 |
tools=self.tools,
|
| 400 |
memory=self.memory,
|
| 401 |
+
max_iterations=30,
|
| 402 |
verbose=True,
|
| 403 |
handle_parsing_errors=True,
|
| 404 |
+
# return_only_outputs=True # This ensures we only get the final output
|
| 405 |
)
|