Dalageo/First_AI_Agent
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@tool12def calculator(expression: str)-> str: # Returns a string result13 """14 A tool able to solve mathematical expressions using the mathjs.org API.15 16 Args:17 expression: A mathematical expression to solve (e.g., "1+2", "3*5").18 19 Returns:20 str: The result of the calculation if successful, or an error message if the request fails.21 """22 # URL-encode to ensure correct API interpretation23 encoded_expression = urllib.parse.quote(expression, safe="") 24 25 try:26 api_url = f"http://api.mathjs.org/v4/?expr={encoded_expression}"27 28 response = requests.get(api_url)29 30 if response.status_code == 200:31 return f"Result: {response.text}"32 else:33 return f"Error: {response.text}"34 except Exception as e:35 return f"Error: {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()54model = HfApiModel(55max_tokens=2096,56temperature=0.5,57# model_id='replace_with_your_inference_endpoint', # Replace with your inference endpoint58custom_role_conversions=None,59)60 61 62# Import tool from Hub63image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)64 65with open("prompts.yaml", 'r') as stream:66 prompt_templates = yaml.safe_load(stream)67 68agent = CodeAgent(69 model=model,70 tools=[final_answer, calculator, get_current_time_in_timezone, image_generation_tool], ## add your tools here (don't remove final answer)71 max_steps=6,72 verbosity_level=1,73 grammar=None,74 planning_interval=None,75 name=None,76 description=None,77 prompt_templates=prompt_templates78)79 80 81GradioUI(agent).launch()