Spaces:
Build error
Build error
Download agents/web_search_tool.py from ignatiev/Final_Assignment_Agents_Course: direct link, hf CLI and curl.
- Browser
- Download file 1.74 kB
-
https://huggingface.co/spaces/ignatiev/Final_Assignment_Agents_Course/resolve/main/agents/web_search_tool.py
- Command line
-
hf download hf://spaces/ignatiev/Final_Assignment_Agents_Course/agents/web_search_tool.py
-
curl -L -o web_search_tool.py https://huggingface.co/spaces/ignatiev/Final_Assignment_Agents_Course/resolve/main/agents/web_search_tool.py
1.74 kB
| import os | |
| import requests | |
| from smolagents import tool | |
| import arxiv | |
| 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) | |
| 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) |