mathu1998/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6import calendar7from tools.final_answer import FinalAnswerTool8 9from Gradio_UI import GradioUI10 11# Below is an example of a tool that does nothing. Amaze us with your creativity !12@tool13def find_day(arg1:str)-> str: #it's import to specify the return type14 #Keep this format for the description / args / args description but feel free to modify the tool15 """A tool that tells you what day of the week any given date will be16 Args:17 arg1: A date in the format 'dd/mm/yyyy' e.g. ('11/02/1998')18 """19 try:20 day, month, year = (int(i) for i in arg1.split('/'))21 dayNumber = calendar.weekday(year,month,day)22 days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday","Sunday"]23 dayprint = days[dayNumber]24 return f"The day on {arg1} is: {dayprint}"25 except Exception as e:26 return f"Error fetching the day for: '{arg1}': {str(e)}"27 28 29@tool30def get_current_time_in_timezone(timezone: str) -> str:31 """A tool that fetches the current local time in a specified timezone.32 Args:33 timezone: A string representing a valid timezone (e.g., 'America/New_York').34 """35 try:36 # Create timezone object37 tz = pytz.timezone(timezone)38 # Get current time in that timezone39 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")40 return f"The current local time in {timezone} is: {local_time}"41 except Exception as e:42 return f"Error fetching time for timezone '{timezone}': {str(e)}"43 44 45final_answer = FinalAnswerTool()46 47# 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:48# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 49 50model = HfApiModel(51max_tokens=2096,52temperature=0.5,53model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded54custom_role_conversions=None,55)56 57 58# Import tool from Hub59image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)60 61with open("prompts.yaml", 'r') as stream:62 prompt_templates = yaml.safe_load(stream)63 64agent = CodeAgent(65 model=model,66 tools=[final_answer,image_generation_tool,get_current_time_in_timezone,find_day], ## add your tools here (don't remove final answer)67 max_steps=6,68 verbosity_level=1,69 grammar=None,70 planning_interval=None,71 name=None,72 description=None,73 prompt_templates=prompt_templates74)75 76 77GradioUI(agent).launch()