TsukiMegumi/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(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 22@tool23def calculator(a:int, b:int)-> int:24 """A tool that Multiplies two integers. 25Args:26a: the first argument27b: the second argument28"""29 30 return a*b31 32 print(calculator.to_string())33 34 35@tool36def current_moon_phase(moonphase: str) -> str:37 """ A tool that fetches the current moon cycle in a specified area or city.38 Args:39 moonphase: The moon's current phase for today and tonight40 """41 try:42 ddg = DuckDuckGoSearchTool()43 query = f"What is the current {moonphase} in {city}"44 results = ddg(query)45 46 47 return f"The current moon phase in {city} is {moonphase}"48 except Excepton as e:49 return f"Error fetching current moon phase"50 51@tool52def get_current_time_in_timezone(timezone: str) -> str:53 """A tool that fetches the current local time in a specified timezone.54 Args:55 timezone: A string representing a valid timezone (e.g., 'America/New_York').56 """57 try:58 # Create timezone object59 tz = pytz.timezone(timezone)60 # Get current time in that timezone61 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")62 return f"The current local time in {timezone} is: {local_time}"63 except Exception as e:64 return f"Error fetching time for timezone '{timezone}': {str(e)}"65 66 67final_answer = FinalAnswerTool()68 69# 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:70# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 71 72model = HfApiModel(73max_tokens=2096,74temperature=0.5,75model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded76custom_role_conversions=None,77)78 79 80# Import tool from Hub81image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)82 83with open("prompts.yaml", 'r') as stream:84 prompt_templates = yaml.safe_load(stream)85 86agent = CodeAgent(87 model=model,88 tools=[final_answer], ## add your tools here (don't remove final answer)89 max_steps=6,90 verbosity_level=1,91 grammar=None,92 planning_interval=None,93 name=None,94 description=None,95 prompt_templates=prompt_templates96)97 98 99GradioUI(agent).launch()