shuttleship/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@tool12def football_player_facts(arg1:str, arg2:int)-> str: #it's important to specify the return type13 #Keep this format for the description / args / args description but feel free to modify the tool14 """A tool that fetches facts about a particular football player in a particular year 15 Args:16 arg1: name of the football player to get the facts about (eg., Leonel Messi, Lamine Yamal)17 arg2: the year for which we need to get the facts (eg., 2022)18 19 Returns:20 the facts about that particular footballer (eg., scored 30 goals and given 19 assists in 2022)21 """22 23 24 25 try:26 query = f"facts about footballer {arg1} in {arg2}"27 28 search_tool = DuckDuckGoSearchTool()29 results = search_tool(query) # Returns a list of search results30 31 match = re.search(r"(\d{1,3}(?:,\d{3})*)", results) # Look for large numbers32 33 if search_results:34 return search_results35 else:36 return f"I couldn't find the facts for the {arg1}."37 except Exception as e:38 return f"Error fetching category for the term {arg1}: {str(e)}"39 40@tool41def get_current_time_in_timezone(timezone: str) -> str:42 """A tool that fetches the current local time in a specified timezone.43 Args:44 timezone: A string representing a valid timezone (e.g., 'America/New_York').45 """46 try:47 # Create timezone object48 tz = pytz.timezone(timezone)49 # Get current time in that timezone50 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")51 return f"The current local time in {timezone} is: {local_time}"52 except Exception as e:53 return f"Error fetching time for timezone '{timezone}': {str(e)}"54 55 56final_answer = FinalAnswerTool()57 58# 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:59# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 60 61model = HfApiModel(62max_tokens=2096,63temperature=0.5,64model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded65custom_role_conversions=None,66)67 68 69# Import tool from Hub70image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)71 72with open("prompts.yaml", 'r') as stream:73 prompt_templates = yaml.safe_load(stream)74 75agent = CodeAgent(76 model=model,77 tools=[final_answer], ## add your tools here (don't remove final answer)78 max_steps=6,79 verbosity_level=1,80 grammar=None,81 planning_interval=None,82 name=None,83 description=None,84 prompt_templates=prompt_templates85)86 87 88GradioUI(agent).launch()