ruoya/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6import urllib.parse7from tools.final_answer import FinalAnswerTool8 9from Gradio_UI import GradioUI10 11# Below is an example of a tool that does nothing. Amaze us with your creativity !12@tool13def my_custom_tool(arg1:str)-> str: #it's import to specify the return type14 #Keep this format for the description / args / args description but feel free to modify the tool15 """A tool that fetches scientists data16 Args:17 arg1: Scientist to query18 Returns:19 String representing the scientists data20 """21 url = f'https://zonal-dorisa-louisgituhi-a992d867.koyeb.app/scientists/{arg1}'22 23 try:24 response = requests.get(url)25 response.raise_for_status()26 return response.json()27 except requests.exceptions.RequestException as e:28 return {"error": str(e)}29 30 31@tool32def get_current_time_in_timezone(timezone: str) -> str:33 """A tool that fetches the current local time in a specified timezone.34 Args:35 timezone: A string representing a valid timezone (e.g., 'America/New_York').36 """37 try:38 # Create timezone object39 tz = pytz.timezone(timezone)40 # Get current time in that timezone41 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")42 return f"The current local time in {timezone} is: {local_time}"43 except Exception as e:44 return f"Error fetching time for timezone '{timezone}': {str(e)}"45 46 47final_answer = FinalAnswerTool()48 49# 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:50# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 51 52def create_agent():53 """Create and configure the agent with all tools"""54 model = HfApiModel(55 max_tokens=2096,56 temperature=0.8,57 model_id='Qwen/Qwen2.5-Coder-32B-Instruct',58 custom_role_conversions=None,59 )60 61 with open("prompts.yaml", 'r') as stream:62 prompt_templates = yaml.safe_load(stream)63 64 agent = CodeAgent(65 model=model,66 tools=[67 FinalAnswerTool(),68 # query_temperatures,69 # continue_conversation,70 # clear_memory,71 my_custom_tool # Add the new tool72 ],73 max_steps=7,74 verbosity_level=50,75 prompt_templates=prompt_templates,76 additional_authorized_imports=['math'] # Allow math module77 )78 79 80 return agent81 82if __name__ == "__main__":83 agent = create_agent()84 GradioUI(agent).launch()