Smellingsus/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 my_custom_tool(query:str, num_results:int = 5)-> str:13 """Performs a DuckDuckGo web search.14 15 Args:16 query: The search term or question to use for the DuckDuckGo search.17 num_results: The number of results to fetch (though DuckDuckGoSearchTool18 might return results differently).19 """ # <--- ADDED DOCSTRING HERE20 try:21 search_tool = DuckDuckGoSearchTool()22 23 search_result = search_tool.run(query)24 25 return f"DuckDuckGo Search Result for '{query}':\n{search_result}"26 27 except Exception as e:28 return f"An error occurred during search: {e}"29 30@tool31def get_current_time_in_timezone(timezone: str) -> str:32 """A tool that fetches the current local time in a specified timezone.33 Args:34 timezone: A string representing a valid timezone (e.g., 'America/New_York').35 """36 try:37 # Create timezone object38 tz = pytz.timezone(timezone)39 # Get current time in that timezone40 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")41 return f"The current local time in {timezone} is: {local_time}"42 except Exception as e:43 return f"Error fetching time for timezone '{timezone}': {str(e)}"44 45 46final_answer = FinalAnswerTool()47 48# Important Note:49# You are instantiating tools directly with values here:50# my_tool = my_custom_tool("Find the best restaurants for me in New York", 5)51# timeny = get_current_time_in_timezone("Amerca/New_York")52#53# For the agent to decide WHEN and WITH WHAT ARGUMENTS to call the tool,54# you should pass the *function reference* itself, not the result of calling it.55# The agent will then generate code like `my_custom_tool(query="...", num_results=...)`.56#57# Change these lines to:58my_tool_function_ref = my_custom_tool # Reference to the function itself59timeny_function_ref = get_current_time_in_timezone # Reference to the function itself60 61 62model = HfApiModel(63 max_tokens=2096,64 temperature=0.5,65 model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded66 custom_role_conversions=None,67)68 69 70# Import tool from Hub71image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)72 73with open("prompts.yaml", 'r') as stream:74 prompt_templates = yaml.safe_load(stream)75 76agent = CodeAgent(77 model=model,78 # Pass the function references here79 tools=[final_answer, my_tool_function_ref, timeny_function_ref, image_generation_tool], # Add image_generation_tool too if you want the agent to use it80 max_steps=6,81 verbosity_level=1,82 grammar=None,83 planning_interval=None,84 name=None,85 description=None,86 prompt_templates=prompt_templates87)88 89 90GradioUI(agent).launch()