Mtchmann/First_agent_template
1
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(arg1:str, arg2:int)-> 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 arg1: the first argument17 arg2: the second argument18 """19 return "What magic will you build ?"20 21@tool22def get_current_time_in_timezone(timezone: str) -> str:23 """A tool that fetches the current local time in a specified timezone.24 Args:25 timezone: A string representing a valid timezone (e.g., 'America/New_York').26 """27 try:28 # Create timezone object29 tz = pytz.timezone(timezone)30 # Get current time in that timezone31 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")32 return f"The current local time in {timezone} is: {local_time}"33 except Exception as e:34 return f"Error fetching time for timezone '{timezone}': {str(e)}"35 36# New DeepResearch Tool37@tool38def deepresearch(query: str, depth: int = 2) -> str:39 """A tool that performs deep research on a given topic using iterative searches.40 Args:41 query: The research query topic.42 depth: The number of iterative search steps to perform (default is 2).43 """44 research_results = []45 # Create an instance of DuckDuckGoSearchTool46 search_tool = DuckDuckGoSearchTool()47 initial_result = search_tool(query)48 research_results.append(f"Initial search:\n{initial_result}")49 50 # Perform additional iterations for deeper research51 for i in range(1, depth):52 #Modify the query slightly for additional detail53 detailed_query = f"{query} analysis part {i}"54 detailed_result = search_tool(detailed_query)55 research_results.append(f"Additional research (iteration {i}:\n{detailed_result}")56 57 # Aggregate all the search results into one comprehensive answer58 aggregated_results = "\n\n".join(research_results)59 return f"Deep research results for '{query}':\n{aggregated_results}"60 61final_answer = FinalAnswerTool()62model = HfApiModel(63max_tokens=2096,64temperature=0.5,65model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded66custom_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 tools=[79 final_answer,80 DuckDuckGoSearchTool(),81 image_generation_tool,82 my_custom_tool,83 get_current_time_in_timezone,84 deepresearch85 ], ## add your tools here (don't remove final answer)86 max_steps=6,87 verbosity_level=1,88 grammar=None,89 planning_interval=None,90 name=None,91 description=None,92 prompt_templates=prompt_templates93)94 95 96GradioUI(agent).launch()