misba-code/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6from tools.final_answer import FinalAnswerTool 7 8from Gradio_UI import GradioUI9 10# Below is an example of a tool that does nothing. Amaze us with your creativity !11@tool12def search_tool(query:str, num_results:int = 3)-> 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 """searches the web using DuckDuckGo and return relevant results15 Args:16 query: The search query string17 num_results: Number of results to return18 """19 20 try:21 #initialize22 search = DuckDuckGoSearchTool()23 24 #perform the search25 results = search.search(query,num_results = num_results)26 27 #format the results 28 formatted_results = [] 29 30 output = f"🔍 DuckDuckGo Search Results for query: '{query}'\n"31 output += f"Number of results requested: {num_results}\n"32 output += "----------------------------------------\n\n"33 34 for i, result in enumerate(results,1):35 formatted_results.append(36 f"{i}. Title: {result['title']}\n"37 f" snippet: {result['snippet']}\n"38 f" URL:{result['link']}\n"39 )40 41 #combine results into a single string42 if formatted_results:43 return "search Results:\n\n" + "\n".join(formatted_results)44 else:45 return "no results found for the given query."46 47 except Exception as e:48 return f"error performing search: {str(e)}"49 50@tool51def get_current_time_in_timezone(timezone: str) -> str:52 """A tool that fetches the current local time in a specified timezone.53 Args:54 timezone: A string representing a valid timezone (e.g., 'America/New_York').55 """56 try:57 # Create timezone object58 tz = pytz.timezone(timezone)59 # Get current time in that timezone60 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")61 return f"The current local time in {timezone} is: {local_time}"62 except Exception as e:63 return f"Error fetching time for timezone '{timezone}': {str(e)}"64 65 66final_answer = FinalAnswerTool()67 68# 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:69# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 70 71model = HfApiModel(72max_tokens=2096,73temperature=0.5,74model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded75custom_role_conversions=None,76)77 78 79# Import tool from Hub80image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)81 82with open("prompts.yaml", 'r') as stream:83 prompt_templates = yaml.safe_load(stream)84 85agent = CodeAgent(86 model=model,87 tools=[88 final_answer,89 search_tool,90 get_current_time_in_timezone], 91 max_steps=6,92 verbosity_level=1,93 grammar=None,94 planning_interval=None,95 name=None,96 description=None,97 prompt_templates=prompt_templates98)99 100 101GradioUI(agent).launch()