bricksandwich/First_agent_template
2
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7from Gradio_UI import GradioUI8 9 10 11@tool12def xkcd_tool(id:int = 0)-> str:13 """A tool that fetches XKCD comics. For the latest comic do not pass an id argument.14 This tool will return the requested comic title, alt text, and a url which points to the comic image.15 Create a Markdown version of the comic with the following requirements:16 - The title should be a level 1 header.17 - The alt text should be a blcokquote.18 - The image should be displayed. using the following syntax: 19 The Final Answer should be This markdown string20 Args:21 id: a number representing a valid XKCD comic ID, or none for the latest comic.22 """23 try:24 url = f"https://xkcd.com/{id}/info.0.json" if id else "https://xkcd.com/info.0.json"25 response = requests.get(url)26 if response.status_code == 200:27 data = response.json()28 return {29 "title": data["title"],30 "alt": data["alt"],31 "img_url": data["img"]32 }33 else:34 return "Failed to fetch XKCD comic"35 except Exception as e:36 return f"Error fetching XKCD comic: {str(e)}"37 38@tool39def get_current_time_in_timezone(timezone: str) -> str:40 """A tool that fetches the current local time in a specified timezone.41 Args:42 timezone: A string representing a valid timezone (e.g., 'America/New_York').43 """44 try:45 # Create timezone object46 tz = pytz.timezone(timezone)47 # Get current time in that timezone48 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")49 return f"The current local time in {timezone} is: {local_time}"50 except Exception as e:51 return f"Error fetching time for timezone '{timezone}': {str(e)}"52 53 54final_answer = FinalAnswerTool()55model = HfApiModel(56max_tokens=2096,57temperature=0.5,58model_id='https://jc26mwg228mkj8dw.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded59custom_role_conversions=None,60)61 62 63# Import tool from Hub64image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)65 66with open("prompts.yaml", 'r') as stream:67 prompt_templates = yaml.safe_load(stream)68 69agent = CodeAgent(70 model=model,71 tools=[final_answer,get_current_time_in_timezone,xkcd_tool], ## add your tools here (don't remove final answer)72 max_steps=6,73 verbosity_level=1,74 grammar=None,75 planning_interval=None,76 name=None,77 description=None,78 prompt_templates=prompt_templates79)80 81 82 83 84GradioUI(agent).launch()