Spaces:
Sleeping
Sleeping
Upload agent
Browse files- agent.json +9 -2
- app.py +13 -3
- requirements.txt +2 -0
- tools/catering_service_tool.py +27 -0
- tools/suggest_menu.py +23 -0
- tools/superhero_party_theme_generator.py +22 -0
- tools/visit_webpage.py +53 -0
- tools/web_search.py +116 -0
agent.json
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
{
|
| 2 |
"class": "CodeAgent",
|
| 3 |
"tools": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"final_answer"
|
| 5 |
],
|
| 6 |
"model": {
|
|
@@ -26,12 +31,14 @@
|
|
| 26 |
"post_messages": "Based on the above, please provide an answer to the following user task:\n{{task}}"
|
| 27 |
}
|
| 28 |
},
|
| 29 |
-
"max_steps":
|
| 30 |
-
"verbosity_level":
|
| 31 |
"planning_interval": null,
|
| 32 |
"name": null,
|
| 33 |
"description": null,
|
| 34 |
"requirements": [
|
|
|
|
|
|
|
| 35 |
"smolagents"
|
| 36 |
],
|
| 37 |
"authorized_imports": [
|
|
|
|
| 1 |
{
|
| 2 |
"class": "CodeAgent",
|
| 3 |
"tools": [
|
| 4 |
+
"web_search",
|
| 5 |
+
"visit_webpage",
|
| 6 |
+
"suggest_menu",
|
| 7 |
+
"catering_service_tool",
|
| 8 |
+
"superhero_party_theme_generator",
|
| 9 |
"final_answer"
|
| 10 |
],
|
| 11 |
"model": {
|
|
|
|
| 31 |
"post_messages": "Based on the above, please provide an answer to the following user task:\n{{task}}"
|
| 32 |
}
|
| 33 |
},
|
| 34 |
+
"max_steps": 10,
|
| 35 |
+
"verbosity_level": 2,
|
| 36 |
"planning_interval": null,
|
| 37 |
"name": null,
|
| 38 |
"description": null,
|
| 39 |
"requirements": [
|
| 40 |
+
"markdownify",
|
| 41 |
+
"requests",
|
| 42 |
"smolagents"
|
| 43 |
],
|
| 44 |
"authorized_imports": [
|
app.py
CHANGED
|
@@ -5,6 +5,11 @@ from smolagents import GradioUI, CodeAgent, InferenceClientModel
|
|
| 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 |
|
|
@@ -13,6 +18,11 @@ model = InferenceClientModel(
|
|
| 13 |
model_id='Qwen/Qwen3-Next-80B-A3B-Thinking',
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
final_answer = FinalAnswer()
|
| 17 |
|
| 18 |
|
|
@@ -21,10 +31,10 @@ with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream:
|
|
| 21 |
|
| 22 |
agent = CodeAgent(
|
| 23 |
model=model,
|
| 24 |
-
tools=[],
|
| 25 |
managed_agents=[],
|
| 26 |
-
max_steps=
|
| 27 |
-
verbosity_level=
|
| 28 |
planning_interval=None,
|
| 29 |
name=None,
|
| 30 |
description=None,
|
|
|
|
| 5 |
# Get current directory path
|
| 6 |
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 7 |
|
| 8 |
+
from tools.web_search import WebSearchTool 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
|
| 12 |
+
from tools.superhero_party_theme_generator import SuperheroPartyThemeTool as SuperheroPartyThemeGenerator
|
| 13 |
from tools.final_answer import FinalAnswerTool as FinalAnswer
|
| 14 |
|
| 15 |
|
|
|
|
| 18 |
model_id='Qwen/Qwen3-Next-80B-A3B-Thinking',
|
| 19 |
)
|
| 20 |
|
| 21 |
+
web_search = WebSearch()
|
| 22 |
+
visit_webpage = VisitWebpage()
|
| 23 |
+
suggest_menu = SuggestMenu()
|
| 24 |
+
catering_service_tool = CateringServiceTool()
|
| 25 |
+
superhero_party_theme_generator = SuperheroPartyThemeGenerator()
|
| 26 |
final_answer = FinalAnswer()
|
| 27 |
|
| 28 |
|
|
|
|
| 31 |
|
| 32 |
agent = CodeAgent(
|
| 33 |
model=model,
|
| 34 |
+
tools=[web_search, visit_webpage, suggest_menu, catering_service_tool, superhero_party_theme_generator],
|
| 35 |
managed_agents=[],
|
| 36 |
+
max_steps=10,
|
| 37 |
+
verbosity_level=2,
|
| 38 |
planning_interval=None,
|
| 39 |
name=None,
|
| 40 |
description=None,
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
|
|
|
|
|
|
| 1 |
smolagents
|
|
|
|
| 1 |
+
markdownify
|
| 2 |
+
requests
|
| 3 |
smolagents
|
tools/catering_service_tool.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from typing import Any, Optional
|
| 3 |
+
|
| 4 |
+
class SimpleTool(Tool):
|
| 5 |
+
name = "catering_service_tool"
|
| 6 |
+
description = "This tool returns the highest-rated catering service 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 service in Gotham City.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
query: A search term for finding catering services.
|
| 16 |
+
"""
|
| 17 |
+
# Example list of catering services and their ratings
|
| 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)
|
| 25 |
+
best_service = max(services, key=services.get)
|
| 26 |
+
|
| 27 |
+
return best_service
|
tools/suggest_menu.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from typing import Any, Optional
|
| 3 |
+
|
| 4 |
+
class SimpleTool(Tool):
|
| 5 |
+
name = "suggest_menu"
|
| 6 |
+
description = "Suggests a menu based on the occasion."
|
| 7 |
+
inputs = {'occasion': {'type': 'string', 'description': 'The type of occasion for the party.'}}
|
| 8 |
+
output_type = "string"
|
| 9 |
+
|
| 10 |
+
def forward(self, occasion: str) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Suggests a menu based on the occasion.
|
| 13 |
+
Args:
|
| 14 |
+
occasion: The type of occasion for the party.
|
| 15 |
+
"""
|
| 16 |
+
if occasion == "casual":
|
| 17 |
+
return "Pizza, snacks, and drinks."
|
| 18 |
+
elif occasion == "formal":
|
| 19 |
+
return "3-course dinner with wine and dessert."
|
| 20 |
+
elif occasion == "superhero":
|
| 21 |
+
return "Buffet with high-energy and healthy food."
|
| 22 |
+
else:
|
| 23 |
+
return "Custom menu for the butler."
|
tools/superhero_party_theme_generator.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
|
| 4 |
+
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 theme 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 |
+
|
| 12 |
+
def forward(self, category: str):
|
| 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
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"
|
| 9 |
+
description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
| 10 |
+
inputs = {'url': {'type': 'string', 'description': 'The url of the webpage to visit.'}}
|
| 11 |
+
output_type = "string"
|
| 12 |
+
|
| 13 |
+
def __init__(self, max_output_length: int = 40000):
|
| 14 |
+
super().__init__()
|
| 15 |
+
self.max_output_length = max_output_length
|
| 16 |
+
|
| 17 |
+
def _truncate_content(self, content: str, max_length: int) -> str:
|
| 18 |
+
if len(content) <= max_length:
|
| 19 |
+
return content
|
| 20 |
+
return (
|
| 21 |
+
content[:max_length] + f"\n..._This content has been truncated to stay below {max_length} characters_...\n"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def forward(self, url: str) -> str:
|
| 25 |
+
try:
|
| 26 |
+
import re
|
| 27 |
+
|
| 28 |
+
import requests
|
| 29 |
+
from markdownify import markdownify
|
| 30 |
+
from requests.exceptions import RequestException
|
| 31 |
+
except ImportError as e:
|
| 32 |
+
raise ImportError(
|
| 33 |
+
"You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
|
| 34 |
+
) from e
|
| 35 |
+
try:
|
| 36 |
+
# Send a GET request to the URL with a 20-second timeout
|
| 37 |
+
response = requests.get(url, timeout=20)
|
| 38 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 39 |
+
|
| 40 |
+
# Convert the HTML content to Markdown
|
| 41 |
+
markdown_content = markdownify(response.text).strip()
|
| 42 |
+
|
| 43 |
+
# Remove multiple line breaks
|
| 44 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 45 |
+
|
| 46 |
+
return self._truncate_content(markdown_content, self.max_output_length)
|
| 47 |
+
|
| 48 |
+
except requests.exceptions.Timeout:
|
| 49 |
+
return "The request timed out. Please try again later or check the URL."
|
| 50 |
+
except RequestException as e:
|
| 51 |
+
return f"Error fetching the webpage: {str(e)}"
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"An unexpected error occurred: {str(e)}"
|
tools/web_search.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
import requests
|
| 4 |
+
import xml
|
| 5 |
+
import html
|
| 6 |
+
|
| 7 |
+
class WebSearchTool(Tool):
|
| 8 |
+
name = "web_search"
|
| 9 |
+
description = "Performs a web search for a query and returns a string of the top search results formatted as markdown with titles, links, and descriptions."
|
| 10 |
+
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
| 11 |
+
output_type = "string"
|
| 12 |
+
|
| 13 |
+
def __init__(self, max_results: int = 10, engine: str = "duckduckgo"):
|
| 14 |
+
super().__init__()
|
| 15 |
+
self.max_results = max_results
|
| 16 |
+
self.engine = engine
|
| 17 |
+
|
| 18 |
+
def forward(self, query: str) -> str:
|
| 19 |
+
results = self.search(query)
|
| 20 |
+
if len(results) == 0:
|
| 21 |
+
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 22 |
+
return self.parse_results(results)
|
| 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 search_bing(self, query: str) -> list:
|
| 97 |
+
import xml.etree.ElementTree as ET
|
| 98 |
+
|
| 99 |
+
import requests
|
| 100 |
+
|
| 101 |
+
response = requests.get(
|
| 102 |
+
"https://www.bing.com/search",
|
| 103 |
+
params={"q": query, "format": "rss"},
|
| 104 |
+
)
|
| 105 |
+
response.raise_for_status()
|
| 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
|