Skip to content

Commit

Permalink
Hierarchical agent for all things Finance (#11387)
Browse files Browse the repository at this point in the history
  • Loading branch information
345ishaan authored Mar 6, 2024
1 parent 22dd782 commit 8ed753d
Show file tree
Hide file tree
Showing 10 changed files with 712 additions and 0 deletions.
153 changes: 153 additions & 0 deletions llama-index-packs/llama-index-packs-finchat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
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
3 changes: 3 additions & 0 deletions llama-index-packs/llama-index-packs-finchat/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
poetry_requirements(
name="poetry",
)
17 changes: 17 additions & 0 deletions llama-index-packs/llama-index-packs-finchat/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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/
61 changes: 61 additions & 0 deletions llama-index-packs/llama-index-packs-finchat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Finance Chat Llama Pack based on OpenAIAgent

This LlamaPack implements a hierarchical agent based on LLM for financial chat and information extraction purposed.

LLM agent is connected to various open financial apis as well as daily updated SP500 postgres SQL database storing
opening & closing price, volume as well as past earnings.

Based on the query, the agent reasons and routes to available tools or runs SQL query to retrieve information and
combine information to answer.

### Installation

```bash
pip install llama-index llama-index-tools-finchat
```

## CLI Usage

You can download llamapacks directly using `llamaindex-cli`, which comes installed with the `llama-index` python package:

```bash
llamaindex-cli download-llamapack FinanceChatPack --download-dir ./finchat_pack
```

You can then inspect the files at `./finchat_pack` and use them as a template for your own project.

## Code Usage

You can download the pack to a the `./finchat_pack` directory:

```python
from llama_index.core.llama_pack import download_llama_pack

FinanceChatPack = download_llama_pack("FinanceChatPack", "./finchat_pack")
```

To use this tool, you'll need a few API keys:

- POLYGON_API_KEY -- <https://polygon.io/>
- FINNHUB_API_KEY -- <https://finnhub.io/>
- ALPHA_VANTAGE_API_KEY -- <https://www.alphavantage.co/>
- NEWSAPI_API_KEY -- <https://newsapi.org/>
- POSTGRES_DB_URI -- 'postgresql://postgres.xhlcobfkbhtwmckmszqp:fingptpassword#[email protected]:5432/postgres' (You can also host your own postgres SQL DB with the same table signatures. To use different signatures, modification is required in giving query examples for SQL code generation.)

```python
fin_agent = FinanceChatPack(
POLYGON_API_KEY,
FINNHUB_API_KEY,
ALPHA_VANTAGE_API_KEY,
NEWSAPI_API_KEY,
OPENAI_API_KEY,
)
```

From here, you can use the pack, or inspect and modify the pack in `./finchat_pack`.

The `run()` function chats with the agent and sends the response of the input query.

```python
response = fin_agent.run("<query>")
```
1 change: 1 addition & 0 deletions llama-index-packs/llama-index-packs-finchat/examples/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources()
37 changes: 37 additions & 0 deletions llama-index-packs/llama-index-packs-finchat/examples/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
This example demonstrates how to set up and test chatting with a finance agent using the FinanceChatPack.
It involves collecting necessary API keys and initializing the FinanceChatPack with these keys and a PostgreSQL database URI.
"""

import getpass
from llama_index.packs.finchat import FinanceChatPack

# Prompting the user to enter all necessary API keys for finance data access and OpenAI
openai_api_key = getpass.getpass("Enter your OpenAI API key: ")
polygon_api_key = getpass.getpass("Enter your Polygon API key: ")
finnhub_api_key = getpass.getpass("Enter your Finnhub API key: ")
alpha_vantage_api_key = getpass.getpass("Enter your Alpha Vantage API key: ")
newsapi_api_key = getpass.getpass("Enter your NewsAPI API key: ")

# PostgreSQL database URI for storing and accessing financial data
postgres_db_uri = "postgresql://postgres.xhlcobfkbhtwmckmszqp:fingptpassword#[email protected]:5432/postgres"

# Initializing the FinanceChatPack with the collected API keys and database URI
finance_chat_pack = FinanceChatPack(
polygon_api_key=polygon_api_key,
finnhub_api_key=finnhub_api_key,
alpha_vantage_api_key=alpha_vantage_api_key,
newsapi_api_key=newsapi_api_key,
openai_api_key=openai_api_key,
postgres_db_uri=postgres_db_uri,
)

# Notifying the user that the FinanceChatPack has been initialized and is ready for testing
print(
"FinanceChatPack initialized successfully. Ready for testing chat interactions with the finance agent."
)


user_query = "Find similar companies to Rivian?"
response = finance_chat_pack.run(user_query)
print("Finance agent response:", response)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from llama_index.packs.finchat.base import FinanceChatPack

__all__ = ["FinanceChatPack"]
Loading

0 comments on commit 8ed753d

Please sign in to comment.