mathidot111/First_agent_template
0
1# Create a CodeAgent with DuckDuckGo search capability2from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel3 4search_tool = DuckDuckGoSearchTool()5 6model = InferenceClientModel()7 8agent = CodeAgent(9 tools=[search_tool], # Add search tool here10 model=model # Add model here11)12 13# ============================================14 15 16from smolagents import (17 CodeAgent,18 ToolCallingAgent,19 InferenceClientModel,20 WebSearchTool,21)22import re23import requests24from markdownify import markdownify25from requests.exceptions import RequestException26from smolagents import tool27 28def visit_webpage(url: str) -> str:29 """Visits a webpage at the given URL and returns its content as a markdown string.30 31 Args:32 url: The URL of the webpage to visit.33 34 Returns:35 The content of the webpage converted to Markdown, or an error message if the request fails.36 """37 try:38 # Send a GET request to the URL39 response = requests.get(url)40 response.raise_for_status() # Raise an exception for bad status codes41 42 # Convert the HTML content to Markdown43 markdown_content = markdownify(response.text).strip()44 45 # Remove multiple line breaks46 markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)47 48 return markdown_content49 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)}"54 55web_agent = ToolCallingAgent(56 tools=[DuckDuckGoSearchTool(), visit_webpage],57 model=model,58 max_steps=10,59 name="search",60 description="Runs web searches for you."61)62 63manager_agent = CodeAgent(64 tools=[],65 model=model,66 managed_agents=[web_agent],67 additional_authorized_imports=["time", "numpy", "pandas"],68)69 70agent = CodeAgent(71 tools=[],72 model=model,73 sandbox=EX2Sandbox(),74 additional_authorized_imports=["numpy"]75)76 