Pytai/First_agent_template
0
1from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool2import datetime3import requests4import pytz5import yaml6import os7from 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 currency_calculator(currency: str, amount: int)-> str:14 """A tool that retrieves currency based on the dollar and calculates the dollar equivalent of the input currency.15 Args:16 currency: A string representing a valid currency (e.g., 'EUR')17 amount: An integer representing the amount of the currency (e.g., 10)18 """19 20 CURRENCY_API_KEY = os.getenv("CURRENCY_API_KEY")21 22 currency_url = f"https://api.freecurrencyapi.com/v1/latest?apikey={CURRENCY_API_KEY}"23 24 try:25 26 response = requests.get(currency_url)27 28 if response.status_code != 200:29 raise Exception30 31 data = response.json()32 33 currency_rate = data.get('data').get(currency)34 35 total_amount = amount / currency_rate36 37 return f"{amount} {currency} is equal to {total_amount:.2f} USD."38 39 except Exception as e:40 41 return f"An error occurred during execution: {str(e)}"42 43@tool44def get_current_time_in_timezone(timezone: str) -> str:45 """A tool that fetches the current local time in a specified timezone.46 Args:47 timezone: A string representing a valid timezone (e.g., 'America/New_York').48 """49 try:50 # Create timezone object51 tz = pytz.timezone(timezone)52 # Get current time in that timezone53 local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")54 return f"The current local time in {timezone} is: {local_time}"55 except Exception as e:56 return f"Error fetching time for timezone '{timezone}': {str(e)}"57 58 59final_answer = FinalAnswerTool()60 61# 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:62# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' 63 64model = HfApiModel(65max_tokens=2096,66temperature=0.5,67model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded68custom_role_conversions=None,69)70 71 72# Import tool from Hub73image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)74 75with open("prompts.yaml", 'r') as stream:76 prompt_templates = yaml.safe_load(stream)77 78agent = CodeAgent(79 model=model,80 tools=[final_answer, currency_calculator], ## add your tools here (don't remove final answer)81 max_steps=6,82 verbosity_level=1,83 grammar=None,84 planning_interval=None,85 name=None,86 description=None,87 prompt_templates=prompt_templates88)89 90 91GradioUI(agent).launch()