Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,31 @@ import base64
|
|
| 13 |
from smolagents import load_tool
|
| 14 |
from typing import Any
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
model = InferenceClientModel("qwen/Qwen2.5-0.5B-Instruct",
|
| 17 |
max_tokens=512,
|
| 18 |
system_message="""
|
|
|
|
| 13 |
from smolagents import load_tool
|
| 14 |
from typing import Any
|
| 15 |
|
| 16 |
+
class DuckDuckGoSearchTool(Tool):
|
| 17 |
+
name = "web_search"
|
| 18 |
+
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
| 19 |
+
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
| 20 |
+
output_type = "string"
|
| 21 |
+
|
| 22 |
+
def __init__(self, max_results=10, **kwargs):
|
| 23 |
+
super().__init__()
|
| 24 |
+
self.max_results = max_results
|
| 25 |
+
try:
|
| 26 |
+
from duckduckgo_search import DDGS
|
| 27 |
+
except ImportError as e:
|
| 28 |
+
raise ImportError(
|
| 29 |
+
"You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
| 30 |
+
) from e
|
| 31 |
+
self.ddgs = DDGS(**kwargs)
|
| 32 |
+
|
| 33 |
+
def forward(self, query: str) -> str:
|
| 34 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
| 35 |
+
if len(results) == 0:
|
| 36 |
+
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 37 |
+
postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|
| 38 |
+
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
model = InferenceClientModel("qwen/Qwen2.5-0.5B-Instruct",
|
| 42 |
max_tokens=512,
|
| 43 |
system_message="""
|