nflexxn/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import random3import 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 random_math_operation(num1: float, num2: float) -> str:13 """A tool that applies a random mathematical operation (+, -, *, /) to two numbers.14 Args:15 num1: The first number.16 num2: The second number.17 """18 operations = ['+', '-', '*', '/']19 op = random.choice(operations)20 21 try:22 if op == '+':23 result = num1 + num224 elif op == '-':25 result = num1 - num226 elif op == '*':27 result = num1 * num228 elif op == '/':29 if num2 == 0:30 return "Error: Division by zero is not allowed."31 result = num1 / num232 33 return f"The randomly chosen operation was '{op}'. The result of {num1} {op} {num2} is {result}"34 except Exception as e:35 return f"An error occurred during the operation: {str(e)}"36 37@tool38def get_current_time_in_timezone(timezone: str) -> str:39 """A tool that fetches the current local time in a specified timezone.40 Args:41 timezone: A string representing a valid timezone (e.g., 'America/New_York').42 """43 try:44 # Create timezone object45 tz = pytz.timezone(timezone)46 # Get current time in that timezone47 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")48 return f"The current local time in {timezone} is: {local_time}"49 except Exception as e:50 return f"Error fetching time for timezone '{timezone}': {str(e)}"51 52 53final_answer = FinalAnswerTool()54 55# 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:56# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 57 58model = HfApiModel(59max_tokens=2096,60temperature=0.5,61model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded62custom_role_conversions=None,63)64 65 66# Import tool from Hub67image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)68duck_search_tool = DuckDuckGoSearchTool() # Direct instantiation of the class69 70with open("prompts.yaml", 'r') as stream:71 prompt_templates = yaml.safe_load(stream)72 73agent = CodeAgent(74 model=model,75 tools=[final_answer, duck_search_tool, image_generation_tool, random_math_operation, get_current_time_in_timezone ], ## add your tools here (don't remove final answer)76 max_steps=6,77 verbosity_level=1,78 grammar=None,79 planning_interval=None,80 name=None,81 description=None,82 prompt_templates=prompt_templates83)84 85 86GradioUI(agent).launch()