Final_Assignment_Template / agent_tools.py
Refractored codes.
f0ae93a
raw
history blame
3.03 kB
import requests
from bs4 import BeautifulSoup
def duckduckgo_search(query: str, num_results: int = 3) -> list:
"""
Perform a search using DuckDuckGo and return the results.
Args:
query: The search query string
num_results: Maximum number of results to return (default: 3)
Returns:
List of dictionaries containing search results with title, url, and snippet
"""
print(f"Performing DuckDuckGo search for: {query}")
try:
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
# Format the query for the URL
formatted_query = query.replace(' ', '+')
url = f"https://html.duckduckgo.com/html/?q={formatted_query}"
# Send the request
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
# Parse the HTML response
soup = BeautifulSoup(response.text, 'html.parser')
# Extract search results
results = []
for result in soup.select('.result'):
title_elem = result.select_one('.result__title')
link_elem = result.select_one('.result__url')
snippet_elem = result.select_one('.result__snippet')
if title_elem and link_elem:
title = title_elem.get_text(strip=True)
url = link_elem.get('href') if link_elem.get('href') else link_elem.get_text(strip=True)
snippet = snippet_elem.get_text(strip=True) if snippet_elem else ""
results.append({
"title": title,
"url": url,
"snippet": snippet
})
if len(results) >= num_results:
break
print(f"Found {len(results)} results for query: {query}")
return results
except Exception as e:
print(f"Error during DuckDuckGo search: {e}")
return []
# Dictionary mapping tool names to their functions
TOOLS_MAPPING = {
"duckduckgo_search": duckduckgo_search
}
# Tool definitions for LLM API
TOOLS_DEFINITION = [
{
"type": "function",
"function": {
"name": "duckduckgo_search",
"description": "Search the web using DuckDuckGo and return relevant results",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query string"
},
"num_results": {
"type": "integer",
"description": "Maximum number of results to return",
"default": 5
}
},
"required": ["query"]
}
}
}
]