File size: 931 Bytes
a8837ad 24ab472 75437ce a8837ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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." |