PooriaT/First_AI_agent
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_cutom_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 combines the arguments and performs a simple calculation.15 16 Args:17 arg1: A string.18 arg2: An integer.19 20 """21 try:22 result = int(arg1) * arg2 # Attempt conversion and multiplication23 return f"The result of {arg1} * {arg2} is: {result}"24 except ValueError:25 return "Error: arg1 must be convertible to an integer."26 except Exception as e:27 return f"An unexpected error occurred: {e}"28 29@tool30def get_current_time_in_timezone(timezone: str) -> str:31 """A tool that fetches the current local time in a specified timezone.32 Args:33 timezone: A string representing a valid timezone (e.g., 'America/New_York').34 """35 try:36 # Create timezone object37 tz = pytz.timezone(timezone)38 # Get current time in that timezone39 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")40 return f"The current local time in {timezone} is: {local_time}"41 except Exception as e:42 return f"Error fetching time for timezone '{timezone}': {str(e)}"43 44 45@tool46def fetch_webpage_content(url: str) -> str:47 """Fetches the content of a webpage.48 49 Args:50 url: The URL of the webpage to fetch.51 """52 try:53 response = requests.get(url, timeout=10) # Added timeout for safety54 response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)55 return response.text56 except requests.exceptions.RequestException as e:57 return f"Error fetching URL '{url}': {e}"58 59final_answer = FinalAnswerTool()60model = HfApiModel(61max_tokens=2096,62temperature=0.5,63model_id='meta-llama/Llama-3.2-1B',# it is possible that this model may be overloaded64custom_role_conversions=None,65)66 67 68# Import tool from Hub69image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)70 71with open("prompts.yaml", 'r') as stream:72 prompt_templates = yaml.safe_load(stream)73 74agent = CodeAgent(75 model=model,76 tools=[final_answer], ## add your tools here (don't remove final answer)77 max_steps=6,78 verbosity_level=1,79 grammar=None,80 planning_interval=None,81 name=None,82 description=None,83 prompt_templates=prompt_templates84)85 86 87GradioUI(agent).launch()