softstone123/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# Below is an example of a tool that does nothing. Amaze us with your creativity !11# @tool12# def my_custom_tool(company_name:str, metal_name:str)-> str: #it's import to specify the return type13# #Keep this format for the description / args / args description but feel free to modify the tool14# """A tool that does nothing yet 15# Args:16# company_name: the company/stock name to check the price17# metal_name: the name of the metal to check the price e.g. gold, silver etc.18# """19# return "What magic will you build ?"20 21 22@tool23def calculate_age(date_of_birth:str)->str:24 """A tool that calculates the age from date of birth 25 Args:26 date_of_birth: the date of birth in dd-mm-yyyy format27 """28 29 birth_date = datetime.datetime.strptime(date_of_birth, "%d-%m-%Y")30 current_date = datetime.datetime.now()31 32 years = current_date.year - birth_date.year33 months = current_date.month - birth_date.month34 days = current_date.day - birth_date.day35 36 if days < 0:37 months -= 138 days += 30 # approximation39 40 if months < 0:41 years -= 142 months += 1243 44 return f"Age is: {years} years, {months} months and {days} days"45 46@tool47def get_current_time_in_timezone(timezone: str) -> str:48 """A tool that fetches the current local time in a specified timezone.49 Args:50 timezone: A string representing a valid timezone (e.g., 'America/New_York').51 """52 try:53 # Create timezone object54 tz = pytz.timezone(timezone)55 # Get current time in that timezone56 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")57 return f"The current local time in {timezone} is: {local_time}"58 except Exception as e:59 return f"Error fetching time for timezone '{timezone}': {str(e)}"60 61 62final_answer = FinalAnswerTool()63 64# 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:65# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 66 67model = HfApiModel(68max_tokens=2096,69temperature=0.5,70model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded71custom_role_conversions=None,72)73 74 75# Import tool from Hub76image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)77 78with open("prompts.yaml", 'r') as stream:79 prompt_templates = yaml.safe_load(stream)80 81agent = CodeAgent(82 model=model,83 tools=[final_answer,calculate_age], ## add your tools here (don't remove final answer)84 max_steps=6,85 verbosity_level=1,86 grammar=None,87 planning_interval=None,88 name=None,89 description=None,90 prompt_templates=prompt_templates91)92 93 94GradioUI(agent).launch()