Spaces:
Sleeping
Sleeping
initial
Browse files- Dockerfile +27 -0
- README.md +6 -5
- Vectara-logo.png +0 -0
- app.py +178 -0
- requirements.txt +10 -0
- vectara-agent-cache.sqlite +0 -0
Dockerfile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /app/requirements.txt
|
| 6 |
+
|
| 7 |
+
RUN if [ -z "$GITHUB_TOKEN" ]; then echo "GITHUB_TOKEN is not set"; exit 1; fi && \
|
| 8 |
+
sed -i "s/{GITHUB_TOKEN}/$GITHUB_TOKEN/g" /app/requirements.txt
|
| 9 |
+
RUN pip3 install --no-cache-dir -r /app/requirements.txt
|
| 10 |
+
|
| 11 |
+
# User
|
| 12 |
+
RUN useradd -m -u 1000 user
|
| 13 |
+
USER user
|
| 14 |
+
ENV HOME /home/user
|
| 15 |
+
ENV PATH $HOME/.local/bin:$PATH
|
| 16 |
+
|
| 17 |
+
WORKDIR $HOME
|
| 18 |
+
RUN mkdir app
|
| 19 |
+
WORKDIR $HOME/app
|
| 20 |
+
COPY . $HOME/app
|
| 21 |
+
|
| 22 |
+
EXPOSE 8501
|
| 23 |
+
CMD streamlit run app.py \
|
| 24 |
+
--server.headless true \
|
| 25 |
+
--server.enableCORS false \
|
| 26 |
+
--server.enableXsrfProtection false \
|
| 27 |
+
--server.fileWatcherType none
|
README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
---
|
| 2 |
-
title: Finance
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: streamlit
|
| 7 |
-
sdk_version: 1.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Finance Chatbot
|
| 3 |
+
emoji: π¨
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: streamlit
|
| 7 |
+
sdk_version: 1.32.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
+
short_description: An AI assistant with company financial reports
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
Vectara-logo.png
ADDED
|
app.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from omegaconf import OmegaConf
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import re
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
from pydantic import Field, BaseModel
|
| 10 |
+
from vectara_agent.agent import Agent, AgentType, AgentStatusType
|
| 11 |
+
from vectara_agent.tools import ToolsFactory
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
tickers = {
|
| 15 |
+
"AAPL": "Apple Computer",
|
| 16 |
+
"GOOG": "Google",
|
| 17 |
+
"AMZN": "Amazon",
|
| 18 |
+
"SNOW": "Snowflake",
|
| 19 |
+
"TEAM": "Atlassian",
|
| 20 |
+
"TSLA": "Tesla",
|
| 21 |
+
"NVDA": "Nvidia",
|
| 22 |
+
"MSFT": "Microsoft",
|
| 23 |
+
"AMD": "Advanced Micro Devices",
|
| 24 |
+
}
|
| 25 |
+
years = [2020, 2021, 2022, 2023, 2024]
|
| 26 |
+
initial_prompt = "How can I help you today?"
|
| 27 |
+
|
| 28 |
+
def create_tools(cfg):
|
| 29 |
+
|
| 30 |
+
def get_company_info() -> list[str]:
|
| 31 |
+
"""
|
| 32 |
+
Returns a dictionary of companies you can query about their financial reports.
|
| 33 |
+
The output is a dictionary of valid ticker symbols mapped to company names.
|
| 34 |
+
You can use this to identify the companies you can query about, and their ticker information.
|
| 35 |
+
"""
|
| 36 |
+
return tickers
|
| 37 |
+
|
| 38 |
+
def get_valid_years() -> list[str]:
|
| 39 |
+
"""
|
| 40 |
+
Returns a list of the years for which financial reports are available.
|
| 41 |
+
"""
|
| 42 |
+
return years
|
| 43 |
+
|
| 44 |
+
class QueryFinancialReportsArgs(BaseModel):
|
| 45 |
+
query: str = Field(..., description="The user query. Must be a question about the company's financials, and should not include the company name, ticker or year.")
|
| 46 |
+
year: int = Field(..., description=f"The year. an integer between {min(years)} and {max(years)}.")
|
| 47 |
+
ticker: str = Field(..., description=f"The company ticker. Must be a valid ticket symbol from the list {tickers.keys()}.")
|
| 48 |
+
|
| 49 |
+
tools_factory = ToolsFactory(vectara_api_key=cfg.api_key,
|
| 50 |
+
vectara_customer_id=cfg.customer_id,
|
| 51 |
+
vectara_corpus_id=cfg.corpus_id)
|
| 52 |
+
query_financial_reports = tools_factory.create_rag_tool(
|
| 53 |
+
tool_name = "query_financial_reports",
|
| 54 |
+
tool_description = """
|
| 55 |
+
Given a company name and year,
|
| 56 |
+
returns a response (str) to a user query about the company's financials for that year.
|
| 57 |
+
When using this tool, make sure to provide the a valid company ticker and a year.
|
| 58 |
+
Use this tool to get financial information one metric at a time.
|
| 59 |
+
""",
|
| 60 |
+
tool_args_schema = QueryFinancialReportsArgs,
|
| 61 |
+
tool_filter_template = "doc.year = {year} and doc.ticker = '{ticker}'",
|
| 62 |
+
reranker = "slingshot", rerank_k = 100,
|
| 63 |
+
n_sentences_before = 2, n_sentences_after = 2, lambda_val = 0.0,
|
| 64 |
+
summary_num_results = 15,
|
| 65 |
+
vectara_summarizer = 'vectara-summary-ext-24-05-med-omni',
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
return (tools_factory.get_tools(
|
| 69 |
+
[
|
| 70 |
+
get_company_info,
|
| 71 |
+
get_valid_years,
|
| 72 |
+
]
|
| 73 |
+
) +
|
| 74 |
+
tools_factory.standard_tools() +
|
| 75 |
+
tools_factory.financial_tools() +
|
| 76 |
+
[query_financial_reports]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
def launch_bot(agent_type: AgentType):
|
| 80 |
+
def reset():
|
| 81 |
+
cfg = st.session_state.cfg
|
| 82 |
+
st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "π¦"}]
|
| 83 |
+
st.session_state.thinking_message = "Agent at work..."
|
| 84 |
+
|
| 85 |
+
# Create the agent
|
| 86 |
+
print("Creating agent...")
|
| 87 |
+
|
| 88 |
+
def update_func(status_type: AgentStatusType, msg: str):
|
| 89 |
+
output = f"{status_type.value} - {msg}"
|
| 90 |
+
st.session_state.thinking_placeholder.text(output)
|
| 91 |
+
|
| 92 |
+
financial_bot_instructions = """
|
| 93 |
+
- You are a helpful financial assistant in conversation with a user. Use your financial expertise when crafting a query to the tool, to ensure you get the most accurate information.
|
| 94 |
+
- You can answer questions, provide insights, or summarize any information from financial reports.
|
| 95 |
+
- A user may refer to a company's ticker instead of its full name - consider those the same when a user is asking about a company.
|
| 96 |
+
- When calculating a financial metric, make sure you have all the information from tools to complete the calculation.
|
| 97 |
+
- In many cases you may need to query tools on each sub-metric separately before computing the final metric.
|
| 98 |
+
- When using a tool to obtain financial data, consider the fact that information for a certain year may be reported in the the following year's report.
|
| 99 |
+
- Report financial data in a consistent manner. For example if you report revenue in thousands, always report revenue in thousands.
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
st.session_state.agent = Agent(
|
| 103 |
+
agent_type = agent_type,
|
| 104 |
+
tools = create_tools(cfg),
|
| 105 |
+
topic = "10-K financial reports",
|
| 106 |
+
custom_instructions = financial_bot_instructions,
|
| 107 |
+
update_func = update_func
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
if 'cfg' not in st.session_state:
|
| 111 |
+
cfg = OmegaConf.create({
|
| 112 |
+
'customer_id': str(os.environ['VECTARA_CUSTOMER_ID']),
|
| 113 |
+
'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
|
| 114 |
+
'api_key': str(os.environ['VECTARA_API_KEY']),
|
| 115 |
+
})
|
| 116 |
+
st.session_state.cfg = cfg
|
| 117 |
+
reset()
|
| 118 |
+
|
| 119 |
+
cfg = st.session_state.cfg
|
| 120 |
+
st.set_page_config(page_title="Financial Assistant", layout="wide")
|
| 121 |
+
|
| 122 |
+
# left side content
|
| 123 |
+
with st.sidebar:
|
| 124 |
+
image = Image.open('Vectara-logo.png')
|
| 125 |
+
st.image(image, width=250)
|
| 126 |
+
st.markdown("## Welcome to the financial assistant demo.\n\n\n")
|
| 127 |
+
companies = ", ".join(tickers.values())
|
| 128 |
+
st.markdown(
|
| 129 |
+
f"This assistant can help you with any questions about the financials of the following companies:\n\n **{companies}**.\n\n"
|
| 130 |
+
"You can ask questions, analyze data, provide insights, or summarize any information from financial reports."
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
st.markdown("\n\n")
|
| 134 |
+
if st.button('Start Over'):
|
| 135 |
+
reset()
|
| 136 |
+
|
| 137 |
+
st.markdown("---")
|
| 138 |
+
st.markdown(
|
| 139 |
+
"## How this works?\n"
|
| 140 |
+
"This app was built with [Vectara](https://vectara.com).\n\n"
|
| 141 |
+
"It demonstrates the use of Agentic Chat functionality with Vectara"
|
| 142 |
+
)
|
| 143 |
+
st.markdown("---")
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
if "messages" not in st.session_state.keys():
|
| 147 |
+
reset()
|
| 148 |
+
|
| 149 |
+
# Display chat messages
|
| 150 |
+
for message in st.session_state.messages:
|
| 151 |
+
with st.chat_message(message["role"], avatar=message["avatar"]):
|
| 152 |
+
st.write(message["content"])
|
| 153 |
+
|
| 154 |
+
# User-provided prompt
|
| 155 |
+
if prompt := st.chat_input():
|
| 156 |
+
st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'π§βπ»'})
|
| 157 |
+
with st.chat_message("user", avatar='π§βπ»'):
|
| 158 |
+
print(f"Starting new question: {prompt}\n")
|
| 159 |
+
st.write(prompt)
|
| 160 |
+
|
| 161 |
+
# Generate a new response if last message is not from assistant
|
| 162 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 163 |
+
with st.chat_message("assistant", avatar='π€'):
|
| 164 |
+
with st.spinner(st.session_state.thinking_message):
|
| 165 |
+
st.session_state.thinking_placeholder = st.empty()
|
| 166 |
+
res = st.session_state.agent.chat(prompt)
|
| 167 |
+
cleaned = re.sub(r'\[\d+\]', '', res.response).replace('$', '\\$')
|
| 168 |
+
st.write(cleaned)
|
| 169 |
+
message = {"role": "assistant", "content": cleaned, "avatar": 'π€'}
|
| 170 |
+
st.session_state.messages.append(message)
|
| 171 |
+
st.session_state.thinking_placeholder.empty()
|
| 172 |
+
|
| 173 |
+
sys.stdout.flush()
|
| 174 |
+
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
print("Starting up...")
|
| 177 |
+
launch_bot(agent_type = AgentType.REACT)
|
| 178 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests_to_curl==1.1.0
|
| 2 |
+
toml==0.10.2
|
| 3 |
+
omegaconf==2.3.0
|
| 4 |
+
syrupy==4.0.8
|
| 5 |
+
streamlit==1.32.2
|
| 6 |
+
llama-index==0.10.42
|
| 7 |
+
llama-index-indices-managed-vectara==0.1.4
|
| 8 |
+
llama-index-agent-openai==0.1.5
|
| 9 |
+
pydantic==1.10.15
|
| 10 |
+
git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git
|
vectara-agent-cache.sqlite
ADDED
|
Binary file (24.6 kB). View file
|
|
|