Create linkup_utils.py
Browse files- linkup_utils.py +35 -0
linkup_utils.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Literal
|
| 2 |
+
|
| 3 |
+
from smolagents import Tool
|
| 4 |
+
from linkup import LinkupClient
|
| 5 |
+
|
| 6 |
+
class LinkupSearchTool(Tool):
|
| 7 |
+
name = "linkup_web_search"
|
| 8 |
+
description = "Performs a search for your text query using Linkup sdk then returns a string of the top search results."
|
| 9 |
+
inputs = {
|
| 10 |
+
"query": {"type": "string", "description": "The search query to perform."},
|
| 11 |
+
}
|
| 12 |
+
output_type = "string"
|
| 13 |
+
|
| 14 |
+
def __init__(self, answer_output_type: str = "sourcedAnswer",
|
| 15 |
+
**kwargs):
|
| 16 |
+
super().__init__(self)
|
| 17 |
+
import os
|
| 18 |
+
|
| 19 |
+
self.api_key = os.getenv("LINKUP_API_KEY")
|
| 20 |
+
if self.api_key is None:
|
| 21 |
+
raise ValueError("Missing Linkup API key. Make sure you have 'LINKUP_API_KEY' in your env variables.")
|
| 22 |
+
|
| 23 |
+
self.client = LinkupClient(api_key=self.api_key)
|
| 24 |
+
self.answer_output_type = answer_output_type
|
| 25 |
+
|
| 26 |
+
def forward(self, query: str) -> str:
|
| 27 |
+
response = self.client.search(
|
| 28 |
+
query=query,
|
| 29 |
+
depth="deep",
|
| 30 |
+
output_type=self.answer_output_type
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
answer_text = getattr(response, "answer", "No answer provided.")
|
| 34 |
+
|
| 35 |
+
return f'## Search Results\n:{answer_text}'
|