Spaces:
Runtime error
Runtime error
Upload agent
Browse files- agent.json +4 -2
- app.py +3 -1
- requirements.txt +1 -0
- tools/web_search.py +27 -0
agent.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
{
|
| 2 |
"class": "CodeAgent",
|
| 3 |
"tools": [
|
|
|
|
| 4 |
"final_answer"
|
| 5 |
],
|
| 6 |
"model": {
|
| 7 |
"class": "LiteLLMModel",
|
| 8 |
"data": {
|
| 9 |
"num_ctx": 8192,
|
| 10 |
-
"last_input_token_count":
|
| 11 |
-
"last_output_token_count":
|
| 12 |
"model_id": "ollama_chat/qwen3:4b",
|
| 13 |
"api_base": "http://127.0.0.1:11434"
|
| 14 |
}
|
|
@@ -37,6 +38,7 @@
|
|
| 37 |
"name": null,
|
| 38 |
"description": null,
|
| 39 |
"requirements": [
|
|
|
|
| 40 |
"smolagents"
|
| 41 |
],
|
| 42 |
"authorized_imports": [
|
|
|
|
| 1 |
{
|
| 2 |
"class": "CodeAgent",
|
| 3 |
"tools": [
|
| 4 |
+
"web_search",
|
| 5 |
"final_answer"
|
| 6 |
],
|
| 7 |
"model": {
|
| 8 |
"class": "LiteLLMModel",
|
| 9 |
"data": {
|
| 10 |
"num_ctx": 8192,
|
| 11 |
+
"last_input_token_count": 5566,
|
| 12 |
+
"last_output_token_count": 141,
|
| 13 |
"model_id": "ollama_chat/qwen3:4b",
|
| 14 |
"api_base": "http://127.0.0.1:11434"
|
| 15 |
}
|
|
|
|
| 38 |
"name": null,
|
| 39 |
"description": null,
|
| 40 |
"requirements": [
|
| 41 |
+
"duckduckgo_search",
|
| 42 |
"smolagents"
|
| 43 |
],
|
| 44 |
"authorized_imports": [
|
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from smolagents import GradioUI, CodeAgent, LiteLLMModel
|
|
| 5 |
# Get current directory path
|
| 6 |
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 7 |
|
|
|
|
| 8 |
from tools.final_answer import FinalAnswerTool as FinalAnswer
|
| 9 |
|
| 10 |
|
|
@@ -15,6 +16,7 @@ model_id='ollama_chat/qwen3:4b',
|
|
| 15 |
api_base='http://127.0.0.1:11434',
|
| 16 |
)
|
| 17 |
|
|
|
|
| 18 |
final_answer = FinalAnswer()
|
| 19 |
|
| 20 |
|
|
@@ -23,7 +25,7 @@ with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream:
|
|
| 23 |
|
| 24 |
agent = CodeAgent(
|
| 25 |
model=model,
|
| 26 |
-
tools=[],
|
| 27 |
managed_agents=[],
|
| 28 |
class='CodeAgent',
|
| 29 |
max_steps=20,
|
|
|
|
| 5 |
# Get current directory path
|
| 6 |
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 7 |
|
| 8 |
+
from tools.web_search import DuckDuckGoSearchTool as WebSearch
|
| 9 |
from tools.final_answer import FinalAnswerTool as FinalAnswer
|
| 10 |
|
| 11 |
|
|
|
|
| 16 |
api_base='http://127.0.0.1:11434',
|
| 17 |
)
|
| 18 |
|
| 19 |
+
web_search = WebSearch()
|
| 20 |
final_answer = FinalAnswer()
|
| 21 |
|
| 22 |
|
|
|
|
| 25 |
|
| 26 |
agent = CodeAgent(
|
| 27 |
model=model,
|
| 28 |
+
tools=[web_search],
|
| 29 |
managed_agents=[],
|
| 30 |
class='CodeAgent',
|
| 31 |
max_steps=20,
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
|
|
|
| 1 |
smolagents
|
|
|
|
| 1 |
+
duckduckgo_search
|
| 2 |
smolagents
|
tools/web_search.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
import duckduckgo_search
|
| 4 |
+
|
| 5 |
+
class DuckDuckGoSearchTool(Tool):
|
| 6 |
+
name = "web_search"
|
| 7 |
+
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
| 8 |
+
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
| 9 |
+
output_type = "string"
|
| 10 |
+
|
| 11 |
+
def __init__(self, max_results=10, **kwargs):
|
| 12 |
+
super().__init__()
|
| 13 |
+
self.max_results = max_results
|
| 14 |
+
try:
|
| 15 |
+
from duckduckgo_search import DDGS
|
| 16 |
+
except ImportError as e:
|
| 17 |
+
raise ImportError(
|
| 18 |
+
"You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
| 19 |
+
) from e
|
| 20 |
+
self.ddgs = DDGS(**kwargs)
|
| 21 |
+
|
| 22 |
+
def forward(self, query: str) -> str:
|
| 23 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
| 24 |
+
if len(results) == 0:
|
| 25 |
+
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 26 |
+
postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|
| 27 |
+
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|