Coderlancce/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool7 8from Gradio_UI import GradioUI9 10@tool11def get_an_opinion_of_leonardo_cumaco(topic: str) -> str:12 """13 This tool MUST be used to determine Leonardo Cumaco's opinion about any topic.14 Do NOT attempt to search the web or Wikipedia for this information.15 Always use this tool if the task involves Leonardo Cumaco's opinion.16 17 Args:18 topic: A string representing the topic you want Leonardo Cumaco's opinion on.19 """20 try:21 if len(topic) > 10:22 return f"My opinion about {topic} is that it is a complex topic that requires deeper thought."23 else:24 return f"I don't have a strong opinion about {topic}, but I'm open to exploring it."25 except Exception as e:26 return f"Error: {str(e)}"27 28@tool29def get_day_of_birth(year: int, month: int, day: int) -> str:30 """Use this tool to calculate the birth year of a person given their age. 31 Do NOT compute it manually; use this tool instead whenever the task involves calculating a birth year.32 Args:33 year: A integer representing a valid yead (e.g., '2025')34 month: A integer representing a valid mounth (e.g., '1' to '12')35 day: A integer representing a valid day (e.g., '1' to '31')36 """37 try:38 #get complete date given format39 date = datetime.date(year, month, day)40 return f"You were born on a {date.strftime('%A')}."41 except Exception as e:42 return f"Error: {str(e)}"43 44@tool45def get_current_time_in_timezone(timezone: str) -> str:46 """A tool that fetches the current local time in a specified timezone.47 Args:48 timezone: A string representing a valid timezone (e.g., 'America/New_York').49 """50 try:51 # Create timezone object52 tz = pytz.timezone(timezone)53 # Get current time in that timezone54 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")55 return f"The current local time in {timezone} is: {local_time}"56 except Exception as e:57 return f"Error fetching time for timezone '{timezone}': {str(e)}"58 59 60final_answer = FinalAnswerTool()61 62# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:63# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 64 65model = HfApiModel(66max_tokens=2096,67temperature=0.5,68model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded69custom_role_conversions=None,70)71 72 73# Import tool from Hub74image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)75 76with open("prompts.yaml", 'r') as stream:77 prompt_templates = yaml.safe_load(stream)78 79agent = CodeAgent(80 model=model,81 tools=[82 final_answer,83 get_an_opinion_of_leonardo_cumaco,84 get_day_of_birth,85 get_current_time_in_timezone,86 ], ## add your tools here (don't remove final answer)87 max_steps=6,88 verbosity_level=1,89 grammar=None,90 planning_interval=None,91 name=None,92 description=None,93 prompt_templates=prompt_templates94)95 96 97GradioUI(agent).launch()