Spaces:
Configuration error
Configuration error
Upload agent
Browse files- agent.json +6 -2
- app.py +7 -4
- requirements.txt +1 -0
- tools/catering_service_tool.py +3 -3
- tools/suggest_menu.py +8 -6
- tools/superhero_party_theme_generator.py +3 -3
- tools/visit_webpage.py +1 -1
- tools/web_search.py +45 -101
agent.json
CHANGED
|
@@ -9,9 +9,12 @@
|
|
| 9 |
"final_answer"
|
| 10 |
],
|
| 11 |
"model": {
|
| 12 |
-
"class": "
|
| 13 |
"data": {
|
| 14 |
-
"
|
|
|
|
|
|
|
|
|
|
| 15 |
}
|
| 16 |
},
|
| 17 |
"managed_agents": {},
|
|
@@ -37,6 +40,7 @@
|
|
| 37 |
"name": null,
|
| 38 |
"description": null,
|
| 39 |
"requirements": [
|
|
|
|
| 40 |
"markdownify",
|
| 41 |
"requests",
|
| 42 |
"smolagents"
|
|
|
|
| 9 |
"final_answer"
|
| 10 |
],
|
| 11 |
"model": {
|
| 12 |
+
"class": "LiteLLMModel",
|
| 13 |
"data": {
|
| 14 |
+
"max_tokens": 2048,
|
| 15 |
+
"num_ctx": 8192,
|
| 16 |
+
"model_id": "ollama_chat/qwen2.5-coder:7b",
|
| 17 |
+
"api_base": "http://127.0.0.1:11434"
|
| 18 |
}
|
| 19 |
},
|
| 20 |
"managed_agents": {},
|
|
|
|
| 40 |
"name": null,
|
| 41 |
"description": null,
|
| 42 |
"requirements": [
|
| 43 |
+
"ddgs",
|
| 44 |
"markdownify",
|
| 45 |
"requests",
|
| 46 |
"smolagents"
|
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import yaml
|
| 2 |
import os
|
| 3 |
-
from smolagents import GradioUI, CodeAgent,
|
| 4 |
|
| 5 |
# Get current directory path
|
| 6 |
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 7 |
|
| 8 |
-
from tools.web_search import
|
| 9 |
from tools.visit_webpage import VisitWebpageTool as VisitWebpage
|
| 10 |
from tools.suggest_menu import SimpleTool as SuggestMenu
|
| 11 |
from tools.catering_service_tool import SimpleTool as CateringServiceTool
|
|
@@ -14,8 +14,11 @@ from tools.final_answer import FinalAnswerTool as FinalAnswer
|
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
-
model =
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
web_search = WebSearch()
|
|
|
|
| 1 |
import yaml
|
| 2 |
import os
|
| 3 |
+
from smolagents import GradioUI, CodeAgent, LiteLLMModel
|
| 4 |
|
| 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.visit_webpage import VisitWebpageTool as VisitWebpage
|
| 10 |
from tools.suggest_menu import SimpleTool as SuggestMenu
|
| 11 |
from tools.catering_service_tool import SimpleTool as CateringServiceTool
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
+
model = LiteLLMModel(
|
| 18 |
+
max_tokens=2048,
|
| 19 |
+
num_ctx=8192,
|
| 20 |
+
model_id='ollama_chat/qwen2.5-coder:7b',
|
| 21 |
+
api_base='http://127.0.0.1:11434',
|
| 22 |
)
|
| 23 |
|
| 24 |
web_search = WebSearch()
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
markdownify
|
| 2 |
requests
|
| 3 |
smolagents
|
|
|
|
| 1 |
+
ddgs
|
| 2 |
markdownify
|
| 3 |
requests
|
| 4 |
smolagents
|
tools/catering_service_tool.py
CHANGED
|
@@ -3,13 +3,13 @@ from typing import Any, Optional
|
|
| 3 |
|
| 4 |
class SimpleTool(Tool):
|
| 5 |
name = "catering_service_tool"
|
| 6 |
-
description = "This tool returns the highest-rated catering
|
| 7 |
inputs = {'query': {'type': 'string', 'description': 'A search term for finding catering services.'}}
|
| 8 |
output_type = "string"
|
| 9 |
|
| 10 |
def forward(self, query: str) -> str:
|
| 11 |
"""
|
| 12 |
-
This tool returns the highest-rated catering
|
| 13 |
|
| 14 |
Args:
|
| 15 |
query: A search term for finding catering services.
|
|
@@ -18,7 +18,7 @@ class SimpleTool(Tool):
|
|
| 18 |
services = {
|
| 19 |
"Gotham Catering Co.": 4.9,
|
| 20 |
"Wayne Manor Catering": 4.8,
|
| 21 |
-
"Gotham City Events": 4.7
|
| 22 |
}
|
| 23 |
|
| 24 |
# Find the highest rated catering service (simulating search query filtering)
|
|
|
|
| 3 |
|
| 4 |
class SimpleTool(Tool):
|
| 5 |
name = "catering_service_tool"
|
| 6 |
+
description = "This tool returns the highest-rated catering services in Gotham City."
|
| 7 |
inputs = {'query': {'type': 'string', 'description': 'A search term for finding catering services.'}}
|
| 8 |
output_type = "string"
|
| 9 |
|
| 10 |
def forward(self, query: str) -> str:
|
| 11 |
"""
|
| 12 |
+
This tool returns the highest-rated catering services in Gotham City.
|
| 13 |
|
| 14 |
Args:
|
| 15 |
query: A search term for finding catering services.
|
|
|
|
| 18 |
services = {
|
| 19 |
"Gotham Catering Co.": 4.9,
|
| 20 |
"Wayne Manor Catering": 4.8,
|
| 21 |
+
"Gotham City Events": 4.7
|
| 22 |
}
|
| 23 |
|
| 24 |
# Find the highest rated catering service (simulating search query filtering)
|
tools/suggest_menu.py
CHANGED
|
@@ -3,21 +3,23 @@ from typing import Any, Optional
|
|
| 3 |
|
| 4 |
class SimpleTool(Tool):
|
| 5 |
name = "suggest_menu"
|
| 6 |
-
description = "Suggests a menu based on
|
| 7 |
-
inputs = {'occasion': {'type': 'string', 'description': 'The
|
| 8 |
output_type = "string"
|
| 9 |
|
| 10 |
def forward(self, occasion: str) -> str:
|
| 11 |
"""
|
| 12 |
-
Suggests a menu based on
|
|
|
|
| 13 |
Args:
|
| 14 |
-
occasion: The
|
| 15 |
"""
|
|
|
|
| 16 |
if occasion == "casual":
|
| 17 |
return "Pizza, snacks, and drinks."
|
| 18 |
-
elif occasion ==
|
| 19 |
return "3-course dinner with wine and dessert."
|
| 20 |
-
elif occasion ==
|
| 21 |
return "Buffet with high-energy and healthy food."
|
| 22 |
else:
|
| 23 |
return "Custom menu for the butler."
|
|
|
|
| 3 |
|
| 4 |
class SimpleTool(Tool):
|
| 5 |
name = "suggest_menu"
|
| 6 |
+
description = "Suggests a menu based on occasion"
|
| 7 |
+
inputs = {'occasion': {'type': 'string', 'description': 'The of occasion for the party'}}
|
| 8 |
output_type = "string"
|
| 9 |
|
| 10 |
def forward(self, occasion: str) -> str:
|
| 11 |
"""
|
| 12 |
+
Suggests a menu based on occasion
|
| 13 |
+
|
| 14 |
Args:
|
| 15 |
+
occasion: The of occasion for the party
|
| 16 |
"""
|
| 17 |
+
|
| 18 |
if occasion == "casual":
|
| 19 |
return "Pizza, snacks, and drinks."
|
| 20 |
+
elif occasion == 'formal':
|
| 21 |
return "3-course dinner with wine and dessert."
|
| 22 |
+
elif occasion == 'superhero':
|
| 23 |
return "Buffet with high-energy and healthy food."
|
| 24 |
else:
|
| 25 |
return "Custom menu for the butler."
|
tools/superhero_party_theme_generator.py
CHANGED
|
@@ -5,7 +5,7 @@ class SuperheroPartyThemeTool(Tool):
|
|
| 5 |
name = "superhero_party_theme_generator"
|
| 6 |
description = """
|
| 7 |
This tool suggests creative superhero-themed party ideas based on a category.
|
| 8 |
-
It returns a unique party
|
| 9 |
inputs = {'category': {'type': 'string', 'description': "The type of superhero party (e.g., 'classic heroes', 'villain masquerade', 'futuristic gotham')."}}
|
| 10 |
output_type = "string"
|
| 11 |
|
|
@@ -13,10 +13,10 @@ class SuperheroPartyThemeTool(Tool):
|
|
| 13 |
themes = {
|
| 14 |
"classic heroes": "Justice League Gala: Guests come dressed as their favorite DC heroes with themed cocktails like 'The Kryptonite Punch'.",
|
| 15 |
"villain masquerade": "Gotham Rogues' Ball: A mysterious masquerade where guests dress as classic Batman villains.",
|
| 16 |
-
"futuristic
|
| 17 |
}
|
| 18 |
|
| 19 |
-
return themes.get(category.lower(), "Themed party idea not found.
|
| 20 |
|
| 21 |
def __init__(self, *args, **kwargs):
|
| 22 |
self.is_initialized = False
|
|
|
|
| 5 |
name = "superhero_party_theme_generator"
|
| 6 |
description = """
|
| 7 |
This tool suggests creative superhero-themed party ideas based on a category.
|
| 8 |
+
It returns a unique party them idea."""
|
| 9 |
inputs = {'category': {'type': 'string', 'description': "The type of superhero party (e.g., 'classic heroes', 'villain masquerade', 'futuristic gotham')."}}
|
| 10 |
output_type = "string"
|
| 11 |
|
|
|
|
| 13 |
themes = {
|
| 14 |
"classic heroes": "Justice League Gala: Guests come dressed as their favorite DC heroes with themed cocktails like 'The Kryptonite Punch'.",
|
| 15 |
"villain masquerade": "Gotham Rogues' Ball: A mysterious masquerade where guests dress as classic Batman villains.",
|
| 16 |
+
"futuristic Gotham": "Neo-Gotham Night: A cyberpunk-style party inspired by Batman Beyond, with neon decorations and futuristic gadgets."
|
| 17 |
}
|
| 18 |
|
| 19 |
+
return themes.get(category.lower(), "Themed party idea not found. try 'classic heroes', 'villain masquerade', or 'futuristic Gotham'.")
|
| 20 |
|
| 21 |
def __init__(self, *args, **kwargs):
|
| 22 |
self.is_initialized = False
|
tools/visit_webpage.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
import requests
|
| 4 |
import re
|
| 5 |
import markdownify
|
|
|
|
| 6 |
|
| 7 |
class VisitWebpageTool(Tool):
|
| 8 |
name = "visit_webpage"
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
|
|
|
| 3 |
import re
|
| 4 |
import markdownify
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
class VisitWebpageTool(Tool):
|
| 8 |
name = "visit_webpage"
|
tools/web_search.py
CHANGED
|
@@ -1,116 +1,60 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
name = "web_search"
|
| 9 |
-
description = "Performs a web search
|
| 10 |
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
| 11 |
output_type = "string"
|
| 12 |
|
| 13 |
-
def __init__(self, max_results: int = 10,
|
| 14 |
super().__init__()
|
| 15 |
self.max_results = max_results
|
| 16 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def forward(self, query: str) -> str:
|
| 19 |
-
|
|
|
|
| 20 |
if len(results) == 0:
|
| 21 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def search(self, query: str) -> list:
|
| 25 |
-
if self.engine == "duckduckgo":
|
| 26 |
-
return self.search_duckduckgo(query)
|
| 27 |
-
elif self.engine == "bing":
|
| 28 |
-
return self.search_bing(query)
|
| 29 |
-
else:
|
| 30 |
-
raise ValueError(f"Unsupported engine: {self.engine}")
|
| 31 |
-
|
| 32 |
-
def parse_results(self, results: list) -> str:
|
| 33 |
-
return "## Search Results\n\n" + "\n\n".join(
|
| 34 |
-
[f"[{result['title']}]({result['link']})\n{result['description']}" for result in results]
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
def search_duckduckgo(self, query: str) -> list:
|
| 38 |
-
import requests
|
| 39 |
-
|
| 40 |
-
response = requests.get(
|
| 41 |
-
"https://lite.duckduckgo.com/lite/",
|
| 42 |
-
params={"q": query},
|
| 43 |
-
headers={"User-Agent": "Mozilla/5.0"},
|
| 44 |
-
)
|
| 45 |
-
response.raise_for_status()
|
| 46 |
-
parser = self._create_duckduckgo_parser()
|
| 47 |
-
parser.feed(response.text)
|
| 48 |
-
return parser.results
|
| 49 |
-
|
| 50 |
-
def _create_duckduckgo_parser(self):
|
| 51 |
-
from html.parser import HTMLParser
|
| 52 |
-
|
| 53 |
-
class SimpleResultParser(HTMLParser):
|
| 54 |
-
def __init__(self):
|
| 55 |
-
super().__init__()
|
| 56 |
-
self.results = []
|
| 57 |
-
self.current = {}
|
| 58 |
-
self.capture_title = False
|
| 59 |
-
self.capture_description = False
|
| 60 |
-
self.capture_link = False
|
| 61 |
-
|
| 62 |
-
def handle_starttag(self, tag, attrs):
|
| 63 |
-
attrs = dict(attrs)
|
| 64 |
-
if tag == "a" and attrs.get("class") == "result-link":
|
| 65 |
-
self.capture_title = True
|
| 66 |
-
elif tag == "td" and attrs.get("class") == "result-snippet":
|
| 67 |
-
self.capture_description = True
|
| 68 |
-
elif tag == "span" and attrs.get("class") == "link-text":
|
| 69 |
-
self.capture_link = True
|
| 70 |
-
|
| 71 |
-
def handle_endtag(self, tag):
|
| 72 |
-
if tag == "a" and self.capture_title:
|
| 73 |
-
self.capture_title = False
|
| 74 |
-
elif tag == "td" and self.capture_description:
|
| 75 |
-
self.capture_description = False
|
| 76 |
-
elif tag == "span" and self.capture_link:
|
| 77 |
-
self.capture_link = False
|
| 78 |
-
elif tag == "tr":
|
| 79 |
-
# Store current result if all parts are present
|
| 80 |
-
if {"title", "description", "link"} <= self.current.keys():
|
| 81 |
-
self.current["description"] = " ".join(self.current["description"])
|
| 82 |
-
self.results.append(self.current)
|
| 83 |
-
self.current = {}
|
| 84 |
-
|
| 85 |
-
def handle_data(self, data):
|
| 86 |
-
if self.capture_title:
|
| 87 |
-
self.current["title"] = data.strip()
|
| 88 |
-
elif self.capture_description:
|
| 89 |
-
self.current.setdefault("description", [])
|
| 90 |
-
self.current["description"].append(data.strip())
|
| 91 |
-
elif self.capture_link:
|
| 92 |
-
self.current["link"] = "https://" + data.strip()
|
| 93 |
-
|
| 94 |
-
return SimpleResultParser()
|
| 95 |
|
| 96 |
-
def
|
| 97 |
-
import
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
root = ET.fromstring(response.text)
|
| 107 |
-
items = root.findall(".//item")
|
| 108 |
-
results = [
|
| 109 |
-
{
|
| 110 |
-
"title": item.findtext("title"),
|
| 111 |
-
"link": item.findtext("link"),
|
| 112 |
-
"description": item.findtext("description"),
|
| 113 |
-
}
|
| 114 |
-
for item in items[: self.max_results]
|
| 115 |
-
]
|
| 116 |
-
return results
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
+
import time
|
| 4 |
+
import ddgs
|
| 5 |
+
|
| 6 |
+
class DuckDuckGoSearchTool(Tool):
|
| 7 |
+
"""Web search tool that performs searches using the DuckDuckGo search engine.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
max_results (`int`, default `10`): Maximum number of search results to return.
|
| 11 |
+
rate_limit (`float`, default `1.0`): Maximum queries per second. Set to `None` to disable rate limiting.
|
| 12 |
+
**kwargs: Additional keyword arguments for the `DDGS` client.
|
| 13 |
+
|
| 14 |
+
Examples:
|
| 15 |
+
```python
|
| 16 |
+
>>> from smolagents import DuckDuckGoSearchTool
|
| 17 |
+
>>> web_search_tool = DuckDuckGoSearchTool(max_results=5, rate_limit=2.0)
|
| 18 |
+
>>> results = web_search_tool("Hugging Face")
|
| 19 |
+
>>> print(results)
|
| 20 |
+
```
|
| 21 |
+
"""
|
| 22 |
name = "web_search"
|
| 23 |
+
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
| 24 |
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
| 25 |
output_type = "string"
|
| 26 |
|
| 27 |
+
def __init__(self, max_results: int = 10, rate_limit: float | None = 1.0, **kwargs):
|
| 28 |
super().__init__()
|
| 29 |
self.max_results = max_results
|
| 30 |
+
self.rate_limit = rate_limit
|
| 31 |
+
self._min_interval = 1.0 / rate_limit if rate_limit else 0.0
|
| 32 |
+
self._last_request_time = 0.0
|
| 33 |
+
try:
|
| 34 |
+
from ddgs import DDGS
|
| 35 |
+
except ImportError as e:
|
| 36 |
+
raise ImportError(
|
| 37 |
+
"You must install package `ddgs` to run this tool: for instance run `pip install ddgs`."
|
| 38 |
+
) from e
|
| 39 |
+
self.ddgs = DDGS(**kwargs)
|
| 40 |
|
| 41 |
def forward(self, query: str) -> str:
|
| 42 |
+
self._enforce_rate_limit()
|
| 43 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
| 44 |
if len(results) == 0:
|
| 45 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 46 |
+
postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
|
| 47 |
+
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def _enforce_rate_limit(self) -> None:
|
| 50 |
+
import time
|
| 51 |
|
| 52 |
+
# No rate limit enforced
|
| 53 |
+
if not self.rate_limit:
|
| 54 |
+
return
|
| 55 |
|
| 56 |
+
now = time.time()
|
| 57 |
+
elapsed = now - self._last_request_time
|
| 58 |
+
if elapsed < self._min_interval:
|
| 59 |
+
time.sleep(self._min_interval - elapsed)
|
| 60 |
+
self._last_request_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|