File size: 1,178 Bytes
bfa48a0 | 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 33 34 35 | from typing import Literal
from smolagents import Tool
from linkup import LinkupClient
class LinkupSearchTool(Tool):
name = "linkup_web_search"
description = "Performs a search for your text query using Linkup sdk then returns a string of the top search results."
inputs = {
"query": {"type": "string", "description": "The search query to perform."},
}
output_type = "string"
def __init__(self, answer_output_type: str = "sourcedAnswer",
**kwargs):
super().__init__(self)
import os
self.api_key = os.getenv("LINKUP_API_KEY")
if self.api_key is None:
raise ValueError("Missing Linkup API key. Make sure you have 'LINKUP_API_KEY' in your env variables.")
self.client = LinkupClient(api_key=self.api_key)
self.answer_output_type = answer_output_type
def forward(self, query: str) -> str:
response = self.client.search(
query=query,
depth="deep",
output_type=self.answer_output_type
)
answer_text = getattr(response, "answer", "No answer provided.")
return f'## Search Results\n:{answer_text}' |