Final_Assignment_Agents_Course / agents /web_search_tool.py
ignatiev's picture
fix: add multi modal inputs
7e9feed
Raw History Blame Contribute Delete
1.74 kB
import os
import requests
from smolagents import tool
import arxiv
@tool
def search_in_google(query: str) -> str:
"""
Perform a Google search using the provided query and return the first result.
Args:
query (str): The search query to perform a web search on Google.
"""
GOOGLE_CUSTOM_SEARCH_API_KEY = os.getenv("GOOGLE_CUSTOM_SEARCH_API_KEY")
GOOGLE_CUSTOM_SEARCH_SEARCH_ENGINE_ID = os.getenv("GOOGLE_CUSTOM_SEARCH_SEARCH_ENGINE_ID")
response = requests.get(f"https://www.googleapis.com/customsearch/v1?key={GOOGLE_CUSTOM_SEARCH_API_KEY}&cx={GOOGLE_CUSTOM_SEARCH_SEARCH_ENGINE_ID}&q={query}")
response_json = response.json()
search_results = response_json.get("items", [])
processed_results = []
for result in search_results:
title = result.get("title")
link = result.get("link")
snippet = result.get("snippet")
processed_results.append(f"Title: {title}\nLink: {link}\nSnippet: {snippet}\n")
return "\n".join(processed_results)
@tool
def search_in_arxiv(query: str) -> str:
"""
Perform a search in arXiv for research articles using the provided query and return the first result.
Args:
query (str): The search query to perform a web search on arXiv.
"""
search = arxiv.Search(
query=query,
max_results=10,
sort_by=arxiv.SortCriterion.SubmittedDate,
sort_order=arxiv.SortOrder.Descending,
)
results = list(search.results())
processed_results = []
# Display metadata and summaries
for doc in results:
processed_results.append(f"Title: {doc.title}\nSummary: {doc.summary}\nLink: {doc.entry_id}\n")
return "\n".join(processed_results)