pedrogaprieto's picture
Update tools.py
24ab472 verified
raw
history blame contribute delete
931 Bytes
from smolagents import Tool
import requests
import os
class SerperSearchTool(Tool):
name = "serper_search"
description = "Searches the web for a given query and returns the top result's title and URL."
inputs = {
"query": {
"type": "string",
"description": "The search query to look up on the web."
}
}
output_type = "string"
def forward(self, query: str) -> str:
headers = {
"X-API-KEY": os.environ['SERPER_API_KEY'],
"Content-Type": "application/json"
}
payload = {
"q": query
}
response = requests.post("https://google.serper.dev/search", headers=headers, json=payload)
data = response.json()
if "organic" in data and data["organic"]:
top = data["organic"][0]
return f"{top['title']}: {top['link']}"
return "No relevant results found."