First_agent_template / homework.py
mathidot's picture
BUG FIX
884eda5
Raw
History Blame Contribute Delete
1.98 kB
# Create a CodeAgent with DuckDuckGo search capability
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
search_tool = DuckDuckGoSearchTool()
model = InferenceClientModel()
agent = CodeAgent(
tools=[search_tool], # Add search tool here
model=model # Add model here
)
# ============================================
from smolagents import (
CodeAgent,
ToolCallingAgent,
InferenceClientModel,
WebSearchTool,
)
import re
import requests
from markdownify import markdownify
from requests.exceptions import RequestException
from smolagents import tool
def visit_webpage(url: str) -> str:
"""Visits a webpage at the given URL and returns its content as a markdown string.
Args:
url: The URL of the webpage to visit.
Returns:
The content of the webpage converted to Markdown, or an error message if the request fails.
"""
try:
# Send a GET request to the URL
response = requests.get(url)
response.raise_for_status() # Raise an exception for bad status codes
# Convert the HTML content to Markdown
markdown_content = markdownify(response.text).strip()
# Remove multiple line breaks
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
return markdown_content
except RequestException as e:
return f"Error fetching the webpage: {str(e)}"
except Exception as e:
return f"An unexpected error occurred: {str(e)}"
web_agent = ToolCallingAgent(
tools=[DuckDuckGoSearchTool(), visit_webpage],
model=model,
max_steps=10,
name="search",
description="Runs web searches for you."
)
manager_agent = CodeAgent(
tools=[],
model=model,
managed_agents=[web_agent],
additional_authorized_imports=["time", "numpy", "pandas"],
)
agent = CodeAgent(
tools=[],
model=model,
sandbox=EX2Sandbox(),
additional_authorized_imports=["numpy"]
)