Spaces:
Sleeping
Sleeping
Added get_stock_price_tool
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import datetime
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
|
@@ -18,6 +19,49 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -84,7 +128,8 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 84 |
|
| 85 |
agent = CodeAgent(
|
| 86 |
model=model,
|
| 87 |
-
tools=[final_answer, get_current_time_in_timezone, get_current_time_in_timezone_non_IANA],
|
|
|
|
| 88 |
max_steps=6,
|
| 89 |
verbosity_level=1,
|
| 90 |
grammar=None,
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import yfinance as yf
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
|
|
|
| 19 |
"""
|
| 20 |
return "What magic will you build ?"
|
| 21 |
|
| 22 |
+
@tool
|
| 23 |
+
def get_stock_price_tool(stock_symbol:str)-> str:
|
| 24 |
+
"""A tool that fetches the current price of a specified stock.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
stock_symbol: A string representing a valid stock on the NASDAQ stock exchange (E.g., AMZN, GOOGL).
|
| 28 |
+
"""
|
| 29 |
+
try:
|
| 30 |
+
#1. Create a ticker object (container) for the stock symbol (i.e., an instance of the yf.Ticker class,
|
| 31 |
+
#whereby the stock symbol initializes the object). It holds the logic and methods for fetching data.
|
| 32 |
+
ticker = yf.Ticker(stock_symbol)
|
| 33 |
+
|
| 34 |
+
#2. API call (executing a network request) to fetch (return) the stock data.
|
| 35 |
+
#Accessing the.info attribute via dot notation, triggers the API call.
|
| 36 |
+
info = ticker.info
|
| 37 |
+
#The info variable now holds a Python dictionary (data structure) of all available fields/keys
|
| 38 |
+
#(price, volume, name, sector, etc.) associated with that specific stock symbol.
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
#3. Extract the price, using the 'regularMarketPrice' field, and the .get(...) method.
|
| 42 |
+
# This price is typically the current trading price during market hours.
|
| 43 |
+
price = info.get('regularMarketPrice')
|
| 44 |
+
|
| 45 |
+
if price is None or price == 0:
|
| 46 |
+
# Fallback check, sometimes the price is in 'currentPrice' or not available
|
| 47 |
+
return f"Error: The stock price for '{stock_symbol}' could not be found. Please ensure the stock symbol is correct."
|
| 48 |
+
|
| 49 |
+
#4. Format the stock price into US dollars (e.g., $1,234.56).
|
| 50 |
+
formatted_US_dollars = "${:,.2f}".format(price)
|
| 51 |
+
|
| 52 |
+
#5. Use the canonical symbol from the Ticker object for the output.
|
| 53 |
+
#Canonical symbol refers to the single, officially recognized, and standardized identifier for an entity within a specific system.
|
| 54 |
+
#ticker.ticker is an attribute (a piece of data) of this ticker object itself.
|
| 55 |
+
#This attribute holds the canonical stock symbol string (e.g., 'AMZN') that the external API recognizes
|
| 56 |
+
#for the object that was initialized with or identified, ensuring our output is accurate.
|
| 57 |
+
symbol_used = ticker.ticker
|
| 58 |
+
|
| 59 |
+
return f"The current stock price for {symbol_used} is {formatted_US_dollars}."
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
#Catch general errors (network issues, invalid symbol format, etc.)
|
| 63 |
+
return f"Error fetching price for stock '{stock_symbol}'. Details: The request failed."
|
| 64 |
+
|
| 65 |
@tool
|
| 66 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 67 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 128 |
|
| 129 |
agent = CodeAgent(
|
| 130 |
model=model,
|
| 131 |
+
tools=[final_answer, get_current_time_in_timezone, get_current_time_in_timezone_non_IANA, get_stock_price_tool],
|
| 132 |
+
## add your tools here (don't remove final answer)
|
| 133 |
max_steps=6,
|
| 134 |
verbosity_level=1,
|
| 135 |
grammar=None,
|