from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, load_tool, tool import os import datetime # import requests import pytz import yaml from tools.final_answer import FinalAnswerTool from alpha_vantage.timeseries import TimeSeries from Gradio_UI import GradioUI alpha_api_key = os.getenv("alpha_api_key") @tool def get_stock_price(ticker: str) -> str: """A tool that gets stock price Args: ticker: A string representing a valid stock symbol (e.g. NVDA) """ ticker_symbol = ticker.upper() # Create a TimeSeries object ts = TimeSeries(key=alpha_api_key, output_format='pandas') # Get the latest stock data data, meta_data = ts.get_intraday(symbol=ticker_symbol, interval='1min', outputsize='compact') if not data.empty: # Print the latest closing price (intraday data) # print(f"The current price of {ticker_symbol} is ${data['4. close'][0]:.2f}") return (f"${data['4. close'][0]:.2f}") else: return ("Error: Failed to retrieve stock data.") @tool def hqtool(arg1: str, arg2: int) -> str: # it's import to specify the return type # Keep this format for the description / args / args description but feel free to modify the tool """A tool that does nothing yet Args: arg1: the first argument arg2: the second argument """ return "What magic will you build ?" @tool def get_current_time_in_timezone(timezone: str) -> str: """A tool that fetches the current local time in a specified timezone. Args: timezone: A string representing a valid timezone (e.g., 'America/New_York'). """ try: # Create timezone object tz = pytz.timezone(timezone) # Get current time in that timezone local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") return f"The current local time in {timezone} is: {local_time}" except Exception as e: return f"Error fetching time for timezone '{timezone}': {str(e)}" final_answer = FinalAnswerTool() together_api_key = os.getenv("together_api_key") model = OpenAIServerModel( model_id="Qwen/Qwen2.5-Coder-32B-Instruct", custom_role_conversions=None, api_key=together_api_key, api_base='https://api.together.xyz/v1' ) # Import tool from Hub image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[get_stock_price, image_generation_tool, get_current_time_in_timezone, final_answer, DuckDuckGoSearchTool()], # add or remove tools here max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) GradioUI(agent).launch()