latest_from_arxiv / tools /search_arxiv.py
ryanrwatkins's picture
Upload agent
aa31d0d verified
from smolagents import Tool
from typing import Any, Optional
class SimpleTool(Tool):
name = "search_arxiv"
description = "Searches for the most recent article on arXiv related to the given search term."
inputs = {"search_term":{"type":"string","description":"A keyword or phrase used to find relevant articles on arXiv."}}
output_type = "string"
def forward(self, search_term:str)-> str: #it's import to specify the return type
"""
Searches for the most recent article on arXiv related to the given search term.
Args:
search_term: A keyword or phrase used to find relevant articles on arXiv.
"""
import requests
formatted_search_term = search_term.replace(" ", "+")
url = f"http://export.arxiv.org/api/query?search_query=all:\'{formatted_search_term}\'&sortBy=lastUpdatedDate&sortOrder=descending"
#data = urllib.request.urlopen(url)
data = requests.get(url)
arxiv_result = data.read().decode('utf-8')
return f"The most recent article on {search_term:} is: {arxiv_result}"