Skip to content
Snippets Groups Projects
Unverified Commit 326401cc authored by Dwight foster's avatar Dwight foster Committed by GitHub
Browse files

Dwight/finance tool (#10674)

parent 2be184b4
No related branches found
No related tags found
No related merge requests found
Showing
with 5346 additions and 1 deletion
......@@ -288,7 +288,7 @@ cd ./llama-index-packs
llamaindex-cli new-package --kind "packs" --name "my new pack"
cd ./llama-index-integrations/readers
llamaindex-cli new-pacakge --kind "readers" --name "new reader"
llamaindex-cli new-package --kind "readers" --name "new reader"
```
Executing the first set of shell commands will create a new folder called `llama-index-packs-my-new-pack`
......
llama_index/_static
.DS_Store
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
bin/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
etc/
include/
lib/
lib64/
parts/
sdist/
share/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
.ruff_cache
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
notebooks/
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pyvenv.cfg
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# Jetbrains
.idea
modules/
*.swp
# VsCode
.vscode
# pipenv
Pipfile
Pipfile.lock
# pyright
pyrightconfig.json
poetry_requirements(
name="poetry",
)
GIT_ROOT ?= $(shell git rev-parse --show-toplevel)
help: ## Show all Makefile targets.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'
format: ## Run code autoformatters (black).
pre-commit install
git ls-files | xargs pre-commit run black --files
lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files
test: ## Run tests via pytest.
pytest tests
watch-docs: ## Build and watch documentation.
sphinx-autobuild docs/ docs/_build/html --open-browser --watch $(GIT_ROOT)/llama_index/
# Yahoo Finance Tool
This tool connects to Yahoo Finance and allows an Agent to access stock, news, and financial data of a company.
## Usage
Here's an example of how to use this tool:
```python
from llama_index.tools.yahoo_finance import YahooFinanceToolSpec
from llama_index.agent import OpenAIAgent
tool_spec = YahooFinanceToolSpec()
agent = OpenAIAgent.from_tools(tool_spec.to_tool_list())
agent.chat("What is the price of Apple stock?")
agent.chat("What is the latest news about Apple?")
```
The tools available are:
`balance_sheet`: A tool that returns the balance sheet of a company.
`income_statement`: A tool that returns the income statement of a company.
`cash_flow`: A tool that returns the cash flow of a company.
`stock_news`: A tool that returns the latest news about a company.
`stock_basic_info`: A tool that returns basic information about a company including price.
`stock_analyst_recommendations`: A tool that returns analyst recommendations for a company.
This loader is designed to be used as a way to load data as a Tool in a Agent. See [here](https://github.com/emptycrown/llama-hub/tree/main) for examples.
%% Cell type:markdown id:ba49c2998ed32020 tags:
## Building a Yahoo Finance Agent
This tutorial walks you through the process of building a Yahoo Finance Agent using the `yahoo_finance` tool. The agent will be able to retrieve stock data, financial statements, and other financial information from Yahoo Finance.
%% Cell type:code id:initial_id tags:
``` python
!pip install llama-index yfinance
```
%% Cell type:code id:b095b103c429e4cb tags:
``` python
import openai
from llama_index.agent import OpenAIAgent
```
%% Cell type:code id:1e91dc5fcc2f4cf4 tags:
``` python
openai.api_key = "sk-..."
```
%% Cell type:code id:5b0877f51a351135 tags:
``` python
from llama_index.tools.yahoo_finance.base import YahooFinanceToolSpec
finance_tool = YahooFinanceToolSpec()
finance_tool_list = finance_tool.to_tool_list()
for tool in finance_tool_list:
print(tool.metadata.name)
```
%% Output
balance_sheet
income_statement
cash_flow
stock_basic_info
stock_analyst_recommendations
stock_news
%% Cell type:code id:c2d1e7865b0301f9 tags:
``` python
print(finance_tool.balance_sheet("AAPL"))
```
%% Output
Balance Sheet:
2023-09-30 2022-09-30 2021-09-30 2020-09-30
Treasury Shares Number 0.0 NaN NaN NaN
Ordinary Shares Number 15550061000.0 15943425000.0 16426786000.0 16976763000.0
Share Issued 15550061000.0 15943425000.0 16426786000.0 16976763000.0
Net Debt 81123000000.0 96423000000.0 89779000000.0 74420000000.0
Total Debt 111088000000.0 120069000000.0 124719000000.0 112436000000.0
Tangible Book Value 62146000000.0 50672000000.0 63090000000.0 65339000000.0
Invested Capital 173234000000.0 170741000000.0 187809000000.0 177775000000.0
Working Capital -1742000000.0 -18577000000.0 9355000000.0 38321000000.0
Net Tangible Assets 62146000000.0 50672000000.0 63090000000.0 65339000000.0
Common Stock Equity 62146000000.0 50672000000.0 63090000000.0 65339000000.0
Total Capitalization 157427000000.0 149631000000.0 172196000000.0 164006000000.0
Total Equity Gross Minority Interest 62146000000.0 50672000000.0 63090000000.0 65339000000.0
Stockholders Equity 62146000000.0 50672000000.0 63090000000.0 65339000000.0
Gains Losses Not Affecting Retained Earnings -11452000000.0 -11109000000.0 163000000.0 -406000000.0
Retained Earnings -214000000.0 -3068000000.0 5562000000.0 14966000000.0
Capital Stock 73812000000.0 64849000000.0 57365000000.0 50779000000.0
Common Stock 73812000000.0 64849000000.0 57365000000.0 50779000000.0
Total Liabilities Net Minority Interest 290437000000.0 302083000000.0 287912000000.0 258549000000.0
Total Non Current Liabilities Net Minority Interest 145129000000.0 148101000000.0 162431000000.0 153157000000.0
Other Non Current Liabilities 34391000000.0 32485000000.0 28636000000.0 26320000000.0
Tradeand Other Payables Non Current 15457000000.0 16657000000.0 24689000000.0 28170000000.0
Long Term Debt And Capital Lease Obligation 95281000000.0 98959000000.0 109106000000.0 98667000000.0
Long Term Debt 95281000000.0 98959000000.0 109106000000.0 98667000000.0
Current Liabilities 145308000000.0 153982000000.0 125481000000.0 105392000000.0
Other Current Liabilities 58829000000.0 60845000000.0 47493000000.0 42684000000.0
Current Deferred Liabilities 8061000000.0 7912000000.0 7612000000.0 6643000000.0
Current Deferred Revenue 8061000000.0 7912000000.0 7612000000.0 6643000000.0
Current Debt And Capital Lease Obligation 15807000000.0 21110000000.0 15613000000.0 13769000000.0
Current Debt 15807000000.0 21110000000.0 15613000000.0 13769000000.0
Other Current Borrowings 9822000000.0 11128000000.0 9613000000.0 8773000000.0
Commercial Paper 5985000000.0 9982000000.0 6000000000.0 4996000000.0
Payables And Accrued Expenses 62611000000.0 64115000000.0 54763000000.0 42296000000.0
Payables 62611000000.0 64115000000.0 54763000000.0 42296000000.0
Accounts Payable 62611000000.0 64115000000.0 54763000000.0 42296000000.0
Total Assets 352583000000.0 352755000000.0 351002000000.0 323888000000.0
Total Non Current Assets 209017000000.0 217350000000.0 216166000000.0 180175000000.0
Other Non Current Assets 46906000000.0 39053000000.0 48849000000.0 42522000000.0
Non Current Deferred Assets 17852000000.0 15375000000.0 NaN NaN
Non Current Deferred Taxes Assets 17852000000.0 15375000000.0 NaN NaN
Investments And Advances 100544000000.0 120805000000.0 127877000000.0 100887000000.0
Other Investments NaN 120805000000.0 127877000000.0 100887000000.0
Investmentin Financial Assets 100544000000.0 120805000000.0 127877000000.0 100887000000.0
Available For Sale Securities 100544000000.0 120805000000.0 127877000000.0 100887000000.0
Net PPE 43715000000.0 42117000000.0 39440000000.0 36766000000.0
Accumulated Depreciation -70884000000.0 -72340000000.0 -70283000000.0 -66760000000.0
Gross PPE 114599000000.0 114457000000.0 109723000000.0 103526000000.0
Leases 12839000000.0 11271000000.0 11023000000.0 10283000000.0
Machinery Furniture Equipment 78314000000.0 81060000000.0 78659000000.0 75291000000.0
Land And Improvements 23446000000.0 22126000000.0 20041000000.0 17952000000.0
Properties 0.0 0.0 0.0 0.0
Current Assets 143566000000.0 135405000000.0 134836000000.0 143713000000.0
Other Current Assets 14695000000.0 21223000000.0 14111000000.0 11264000000.0
Inventory 6331000000.0 4946000000.0 6580000000.0 4061000000.0
Receivables 60985000000.0 60932000000.0 51506000000.0 37445000000.0
Other Receivables 31477000000.0 32748000000.0 25228000000.0 21325000000.0
Accounts Receivable 29508000000.0 28184000000.0 26278000000.0 16120000000.0
Cash Cash Equivalents And Short Term Investments 61555000000.0 48304000000.0 62639000000.0 90943000000.0
Other Short Term Investments 31590000000.0 24658000000.0 27699000000.0 52927000000.0
Cash And Cash Equivalents 29965000000.0 23646000000.0 34940000000.0 38016000000.0
Cash Equivalents 1606000000.0 5100000000.0 17635000000.0 20243000000.0
Cash Financial 28359000000.0 18546000000.0 17305000000.0 17773000000.0
%% Cell type:code id:5bcaa33f006b4fb9 tags:
``` python
agent = OpenAIAgent.from_tools(finance_tool_list)
```
%% Cell type:code id:ba290fe8f1621839 tags:
``` python
print(agent.chat("What are the analyst recommendations for AAPL?"))
```
%% Output
The analyst recommendations for AAPL (Apple Inc.) are as follows:
- Current period:
- Strong Buy: 11
- Buy: 21
- Hold: 6
- Sell: 0
- Strong Sell: 0
- 1 month ago:
- Strong Buy: 10
- Buy: 20
- Hold: 12
- Sell: 1
- Strong Sell: 0
- 2 months ago:
- Strong Buy: 10
- Buy: 21
- Hold: 12
- Sell: 1
- Strong Sell: 0
- 3 months ago:
- Strong Buy: 10
- Buy: 24
- Hold: 7
- Sell: 1
- Strong Sell: 0
## init
from llama_index.tools.yahoo_finance.base import (
YahooFinanceToolSpec,
)
__all__ = ["YahooFinanceToolSpec"]
from llama_index.core.tools.tool_spec.base import BaseToolSpec
import yfinance as yf
import pandas as pd
class YahooFinanceToolSpec(BaseToolSpec):
"""Yahoo Finance tool spec."""
spec_functions = [
"balance_sheet",
"income_statement",
"cash_flow",
"stock_basic_info",
"stock_analyst_recommendations",
"stock_news",
]
def __init__(self) -> None:
"""Initialize the Yahoo Finance tool spec."""
def balance_sheet(self, ticker: str) -> str:
"""
Return the balance sheet of the stock.
Args:
ticker (str): the stock ticker to be given to yfinance
"""
stock = yf.Ticker(ticker)
balance_sheet = pd.DataFrame(stock.balance_sheet)
return "Balance Sheet: \n" + balance_sheet.to_string()
def income_statement(self, ticker: str) -> str:
"""
Return the income statement of the stock.
Args:
ticker (str): the stock ticker to be given to yfinance
"""
stock = yf.Ticker(ticker)
income_statement = pd.DataFrame(stock.income_stmt)
return "Income Statement: \n" + income_statement.to_string()
def cash_flow(self, ticker: str) -> str:
"""
Return the cash flow of the stock.
Args:
ticker (str): the stock ticker to be given to yfinance
"""
stock = yf.Ticker(ticker)
cash_flow = pd.DataFrame(stock.cashflow)
return "Cash Flow: \n" + cash_flow.to_string()
def stock_basic_info(self, ticker: str) -> str:
"""
Return the basic info of the stock. Ex: price, description, name.
Args:
ticker (str): the stock ticker to be given to yfinance
"""
stock = yf.Ticker(ticker)
return "Info: \n" + str(stock.info)
def stock_analyst_recommendations(self, ticker: str) -> str:
"""
Get the analyst recommendations for a stock.
Args:
ticker (str): the stock ticker to be given to yfinance
"""
stock = yf.Ticker(ticker)
return "Recommendations: \n" + str(stock.recommendations)
def stock_news(self, ticker: str) -> str:
"""
Get the most recent news titles of a stock.
Args:
ticker (str): the stock ticker to be given to yfinance
"""
stock = yf.Ticker(ticker)
news = stock.news
out = "News: \n"
for i in news:
out += i["title"] + "\n"
return out
Source diff could not be displayed: it is too large. Options to address this: view the blob.
[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core"]
[tool.codespell]
check-filenames = true
check-hidden = true
skip = "*.csv,*.html,*.json,*.jsonl,*.pdf,*.txt,*.ipynb"
[tool.mypy]
disallow_untyped_defs = true
# Remove venv skip when integrated with pre-commit
exclude = ["_static", "build", "examples", "notebooks", "venv"]
ignore_missing_imports = true
python_version = "3.8"
[tool.poetry]
authors = ["Your Name <you@example.com>"]
description = "llama-index tools yahoo_finance integration"
license = "MIT"
name = "llama-index-tools-yahoo-finance"
packages = [{include = "llama_index/"}]
readme = "README.md"
version = "0.1.0"
[tool.poetry.dependencies]
python = ">=3.8.1,<3.12"
llama-index-core = "^0.10.0"
yfinance = "^0.2.36"
[tool.poetry.group.dev.dependencies]
black = {extras = ["jupyter"], version = "<=23.9.1,>=23.7.0"}
codespell = {extras = ["toml"], version = ">=v2.2.6"}
ipython = "8.10.0"
jupyter = "^1.0.0"
mypy = "0.991"
pre-commit = "3.2.0"
pylint = "2.15.10"
pytest = "7.2.1"
pytest-mock = "3.11.1"
ruff = "0.0.292"
tree-sitter-languages = "^1.8.0"
types-Deprecated = ">=0.1.0"
types-PyYAML = "^6.0.12.12"
types-protobuf = "^4.24.0.4"
types-redis = "4.5.5.0"
types-requests = "2.28.11.8"
types-setuptools = "67.1.0.0"
python_tests()
from llama_index.core.tools.tool_spec.base import BaseToolSpec
from llama_index.tools.yahoo_finance import YahooFinanceToolSpec
def test_class():
names_of_base_classes = [b.__name__ for b in YahooFinanceToolSpec.__mro__]
assert BaseToolSpec.__name__ in names_of_base_classes
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment