Upload tool
Browse files- app.py +5 -0
- requirements.txt +2 -0
- tool.py +78 -0
app.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import launch_gradio_demo
|
2 |
+
from tool import LinkupSearchTool
|
3 |
+
|
4 |
+
tool = LinkupSearchTool()
|
5 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
linkup
|
2 |
+
smolagents
|
tool.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
import linkup
|
4 |
+
|
5 |
+
class LinkupSearchTool(Tool):
|
6 |
+
"""LinkupSearchTool tool.
|
7 |
+
|
8 |
+
LinkupSearchTool performs web search queries using the Linkup API, which includes results from both
|
9 |
+
public and premium web sources. It supports two modes of search depth — standard and deep — allowing
|
10 |
+
for flexible information retrieval depending on the complexity of the query.
|
11 |
+
|
12 |
+
Setup:
|
13 |
+
Make sure your Linkup API key is exported to the environment:
|
14 |
+
|
15 |
+
.. code-block:: bash
|
16 |
+
|
17 |
+
pip install linkup-sdk
|
18 |
+
export LINKUP_API_KEY="your-api-key"
|
19 |
+
|
20 |
+
Example:
|
21 |
+
from smolagents import load_tool, CodeAgent, InferenceClientModel
|
22 |
+
|
23 |
+
agent = CodeAgent(
|
24 |
+
tools=[LinkupSearchTool()],
|
25 |
+
model=InferenceClientModel(),
|
26 |
+
)
|
27 |
+
agent.run(query="What was Microsoft's revenue last quarter and was it well perceived by the market?", depth="deep")
|
28 |
+
|
29 |
+
Args:
|
30 |
+
linkup_api_key (str | None): Optional. If not provided, LinkupClient will check the LINKUP_API_KEY env variable.
|
31 |
+
"""
|
32 |
+
name = "search_web"
|
33 |
+
description = "Performs an online search using Linkup search engine and retrieves the top results as a string. This function is useful for accessing real-time information, including news, articles, and other relevant web content."
|
34 |
+
output_type = "string"
|
35 |
+
|
36 |
+
def __init__(self, linkup_api_key: str | None = None) -> None:
|
37 |
+
super().__init__()
|
38 |
+
try:
|
39 |
+
from linkup import LinkupClient
|
40 |
+
except ImportError as e:
|
41 |
+
raise ImportError(
|
42 |
+
"You must install package `linkup-sdk` to run this tool: for instance run `pip install linkup-sdk`."
|
43 |
+
) from e
|
44 |
+
|
45 |
+
self.inputs = {
|
46 |
+
"query": {"type": "string", "description": "The search query to perform."},
|
47 |
+
"depth": {
|
48 |
+
"type": "string",
|
49 |
+
"enum": ["standard", "deep"],
|
50 |
+
"description": "The search depth to perform. Use 'standard' for straightforward queries with likely direct answers (e.g., facts, definitions, simple explanations). Use 'deep' for: 1) complex queries requiring comprehensive analysis or information synthesis, 2) queries containing uncommon terms, specialized jargon, or abbreviations that may need additional context, or 3) questions likely requiring up-to-date or specialized web search results to answer effectively.",
|
51 |
+
},
|
52 |
+
}
|
53 |
+
|
54 |
+
self.client = LinkupClient(api_key=linkup_api_key)
|
55 |
+
|
56 |
+
def forward(self, query: str, depth: str) -> str:
|
57 |
+
|
58 |
+
if depth not in ["standard", "deep"]:
|
59 |
+
raise ValueError("depth must be 'standard' or 'deep'")
|
60 |
+
|
61 |
+
try:
|
62 |
+
response = self.client.search(
|
63 |
+
query=query, depth=depth, output_type="searchResults"
|
64 |
+
)
|
65 |
+
|
66 |
+
results = getattr(response, "results", [{"content": "No answer provided."}])
|
67 |
+
|
68 |
+
except Exception as e:
|
69 |
+
return f"Error occurred during Linkup search: {str(e)}"
|
70 |
+
|
71 |
+
formatted_results = []
|
72 |
+
for i, doc in enumerate(results, start=1):
|
73 |
+
formatted_results.append(
|
74 |
+
f"{i}. **{doc.name or 'Untitled'}**\n URL: {doc.url or 'N/A'}\n {doc.content or ''}"
|
75 |
+
)
|
76 |
+
|
77 |
+
formatted = "\n".join(formatted_results)
|
78 |
+
return f"**Search Results:**\n\n{formatted}"
|