Skip to content

Commit

Permalink
feat: more tool like eps, current ratio (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamMandowara authored Jan 14, 2025
1 parent dd34b3c commit c0bb02c
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 59 deletions.
74 changes: 24 additions & 50 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
from ollama import chat
from ollama import ChatResponse
import yfinance as yf
import pandas as pd
import streamlit as st


def get_stock_prices(symbol:str, duration:str) -> pd.DataFrame:
"""Get stock prices from yahoo finance api
Args:
symbol (str): stock symbol
duration (str): time duration
Returns:
pd.DataFrame: stock prices
"""
suffix='.NS'
durations = {'1 year': '1y', '1month': '1mo', '1 month': '1mo', '1M': '1mo', '1year': '1y'}
if duration not in durations:
durations[duration] = duration
try:
stock = yf.Ticker(symbol+suffix)
data = stock.history(period=durations[duration])
return data
except Exception as e:
return None


def current_pe_ratio(symbol:str,) -> int:
"""Get current pe ratio for a stock
Args:
symbol (str): stock symbol
Returns:
int: current pe ratio
"""
suffix = '.NS'
stock = yf.Ticker(symbol+suffix)
return stock.info.get('trailingPE', None)

from tools import (get_address1, get_beta, get_marketcap, get_current_price, get_stock_prices, current_pe_ratio, get_52_week_high,
get_52_week_low,get_current_ratio, get_debt_to_equity, get_free_cash_flow, get_eps, get_price_to_book)


st.title('AI Stock Data Retriever')
Expand All @@ -52,12 +19,25 @@ def current_pe_ratio(symbol:str,) -> int:
available_functions = {
'get_stock_prices': get_stock_prices,
'current_pe_ratio': current_pe_ratio,
'get_current_price': get_current_price,
'get_marketcap': get_marketcap,
'get_beta': get_beta,
'get_address': get_address1,
'get_52_week_high':get_52_week_high,
'get_52_week_low':get_52_week_low,
'get_current_ratio':get_current_ratio,
'get_debt_to_equity':get_debt_to_equity,
'get_free_cash_flow':get_free_cash_flow,
'get_eps':get_eps,
'get_price_to_book':get_price_to_book

}

response: ChatResponse = chat(
'llama3.2:1b',
messages=messages,
tools=[get_stock_prices, current_pe_ratio],
tools=[get_stock_prices, current_pe_ratio, get_current_price, get_marketcap, get_beta, get_address1, get_52_week_high, get_52_week_low,
get_current_ratio, get_debt_to_equity, get_free_cash_flow, get_eps, get_price_to_book]
)

if response.message.tool_calls:
Expand All @@ -73,18 +53,12 @@ def current_pe_ratio(symbol:str,) -> int:
else:
st.write(output)
print('Function output:', output)
else:
print('Function', tool.function.name, 'not found')

# Only needed to chat with the model using the tool call results
if response.message.tool_calls:
# Add the function response to messages for the model to use
messages.append(response.message)
messages.append({'role': 'tool', 'content': str(output), 'name': tool.function.name})
messages.append(response.message)
messages.append({'role': 'tool', 'function output:': str(output), 'name': tool.function.name})

# Get final response from model with function outputs
final_response = chat('llama3.2:1b', messages=messages)
print('Final response:', final_response.message.content)

else:
print('No tool calls returned from model')
# Get final response from model with function outputs
final_response = chat('llama3.2:1b', messages=messages)
st.write(final_response.message.content)
print('Final response:', final_response.message.content)
else:
print('Function', tool.function.name, 'not found')
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[tool.poetry]
name = "2025"
name = "agent-from-scratch"
version = "0.1.0"
description = ""
authors = ["shubhammandowara <shubham77mandowara@gmail.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"
ollama = "^0.4.5"
ollama = "^0.4.6"
streamlit = "^1.41.1"
yfinance = "^0.2.51"
pandas = "^2.2.3"
Expand Down
149 changes: 149 additions & 0 deletions tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
from typing import Any
import yfinance as yf
import pandas as pd
def get_parameter(symbol, parameter: str) -> Any:
"""Retrieve a value for a specific parameter from the data dictionary.
Args:
symbol (str): stock symbol
parameter (str): The key representing the parameter.
Returns:
Any: The value of the parameter, or a message if not found.
"""
suffix = '.NS'
stock = yf.Ticker(symbol+suffix)
return stock.info.get(parameter, None)

def get_address1(symbol:str) -> str:
"""Retrieve address1.
Args:
symbol (str): Stock symbol
Return:
str: address
"""
return get_parameter(symbol, 'address1')


def get_current_price(symbol:str) -> float:
"""Retrieve the current stock price.
Args:
symbol (str): Stock symbol
Return:
float: price
"""
return get_parameter(symbol, 'currentPrice')


def get_marketcap(symbol:str) -> float:
"""Retrieve the marketcap
Args:
symbol (str): Stock symbol
Return:
float: price
"""
return get_parameter(symbol, 'marketCap')

def get_beta(symbol:str) -> float:
"""Retrieve the beta
Args:
symbol (str): Stock symbol
Return:
float: price
"""
return get_parameter(symbol, 'beta')

def get_stock_prices(symbol:str, duration:str) -> pd.DataFrame:
"""Get stock prices from yahoo finance api
Args:
symbol (str): stock symbol
duration (str): time duration
Returns:
pd.DataFrame: stock prices
"""
suffix='.NS'
durations = {'1 year': '1y', '1month': '1mo', '1 month': '1mo', '1M': '1mo', '1year': '1y'}
if duration not in durations:
durations[duration] = duration
try:
stock = yf.Ticker(symbol+suffix)
data = stock.history(period=durations[duration])
return data
except Exception as e:
return None


def current_pe_ratio(symbol:str) -> int:
"""Get current pe ratio for a stock
Args:
symbol (str): stock symbol
Returns:
int: current pe ratio
"""
return get_parameter(symbol, 'trailingPE')

import yfinance as yf

def get_52_week_low(symbol: str) -> float:
"""Retrieve the 52 week low price
Args:
symbol (str): Stock symbol
Return:
float: 52week low price
"""
return get_parameter(symbol, 'fiftyTwoWeekLow')

def get_52_week_high(symbol: str):
"""Retrieve the debt-to-equity
Args:
symbol (str): Stock symbol
Return:
float: 52 week high prices
"""
return get_parameter(symbol, 'fiftyTwoWeekHigh')

def get_price_to_book(symbol: str):
"""Retrieve the price to book value
Args:
symbol (str): Stock symbol
Return:
float: price to book value
"""
return get_parameter(symbol, 'priceToBook')

def get_eps(symbol: str) -> float:
"""Retrieve the get EPS
Args:
symbol (str): Stock symbol
Return:
float: EPS
"""
return get_parameter(symbol, 'trailingEps')

def get_current_ratio(symbol: str)-> float:
"""Retrieve the current ratio
Args:
symbol (str): Stock symbol
Return:
float: current ratio
"""
return get_parameter(symbol, 'currentRatio')

def get_debt_to_equity(symbol: str)-> float:
"""Retrieve the debt-to-equity
Args:
symbol (str): Stock symbol
Return:
float: debt to equity
"""
return get_parameter(symbol, 'debtToEquity')

def get_free_cash_flow(symbol: str):
"""Retrieve the free cash flow
Args:
symbol (str): Stock symbol
Return:
float: cashflow
"""
return get_parameter(symbol, 'freeCashflow')

0 comments on commit c0bb02c

Please sign in to comment.