Spaces:
Running
Running
updates
Browse files- app.py +52 -3
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -6,6 +6,9 @@ from PIL import Image
|
|
| 6 |
import re
|
| 7 |
import sys
|
| 8 |
import datetime
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
from pydantic import Field, BaseModel
|
| 11 |
from vectara_agent.agent import Agent, AgentType, AgentStatusType
|
|
@@ -28,6 +31,8 @@ tickers = {
|
|
| 28 |
years = [2020, 2021, 2022, 2023, 2024]
|
| 29 |
initial_prompt = "How can I help you today?"
|
| 30 |
|
|
|
|
|
|
|
| 31 |
def create_tools(cfg):
|
| 32 |
|
| 33 |
def get_company_info() -> list[str]:
|
|
@@ -44,6 +49,32 @@ def create_tools(cfg):
|
|
| 44 |
"""
|
| 45 |
return years
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
class QueryTranscriptsArgs(BaseModel):
|
| 48 |
query: str = Field(..., description="The user query.")
|
| 49 |
year: int = Field(..., description=f"The year. an integer between {min(years)} and {max(years)}.")
|
|
@@ -72,6 +103,7 @@ def create_tools(cfg):
|
|
| 72 |
[
|
| 73 |
get_company_info,
|
| 74 |
get_valid_years,
|
|
|
|
| 75 |
]
|
| 76 |
) +
|
| 77 |
tools_factory.standard_tools() +
|
|
@@ -98,6 +130,7 @@ def initialize_agent(agent_type: AgentType, _cfg):
|
|
| 98 |
def update_func(status_type: AgentStatusType, msg: str):
|
| 99 |
output = f"{status_type.value} - {msg}"
|
| 100 |
st.session_state.thinking_placeholder.text(output)
|
|
|
|
| 101 |
|
| 102 |
agent = Agent(
|
| 103 |
agent_type=agent_type,
|
|
@@ -114,6 +147,8 @@ def launch_bot(agent_type: AgentType):
|
|
| 114 |
st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "🦖"}]
|
| 115 |
st.session_state.thinking_message = "Agent at work..."
|
| 116 |
st.session_state.agent = initialize_agent(agent_type, cfg)
|
|
|
|
|
|
|
| 117 |
|
| 118 |
st.set_page_config(page_title="Financial Assistant", layout="wide")
|
| 119 |
if 'cfg' not in st.session_state:
|
|
@@ -137,14 +172,19 @@ def launch_bot(agent_type: AgentType):
|
|
| 137 |
)
|
| 138 |
|
| 139 |
st.markdown("\n\n")
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
st.markdown("---")
|
| 144 |
st.markdown(
|
| 145 |
"## How this works?\n"
|
| 146 |
"This app was built with [Vectara](https://vectara.com).\n\n"
|
| 147 |
-
"It demonstrates the use of Agentic
|
| 148 |
)
|
| 149 |
st.markdown("---")
|
| 150 |
|
|
@@ -176,6 +216,15 @@ def launch_bot(agent_type: AgentType):
|
|
| 176 |
st.session_state.thinking_placeholder.empty()
|
| 177 |
st.rerun()
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
sys.stdout.flush()
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
|
|
|
| 6 |
import re
|
| 7 |
import sys
|
| 8 |
import datetime
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import requests
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
from pydantic import Field, BaseModel
|
| 14 |
from vectara_agent.agent import Agent, AgentType, AgentStatusType
|
|
|
|
| 31 |
years = [2020, 2021, 2022, 2023, 2024]
|
| 32 |
initial_prompt = "How can I help you today?"
|
| 33 |
|
| 34 |
+
load_dotenv()
|
| 35 |
+
|
| 36 |
def create_tools(cfg):
|
| 37 |
|
| 38 |
def get_company_info() -> list[str]:
|
|
|
|
| 49 |
"""
|
| 50 |
return years
|
| 51 |
|
| 52 |
+
# Tool to get the income statement for a given company and year using the FMP API
|
| 53 |
+
def get_income_statement(
|
| 54 |
+
ticker=Field(description="the ticker symbol of the company."),
|
| 55 |
+
year=Field(description="the year for which to get the income statement."),
|
| 56 |
+
) -> str:
|
| 57 |
+
"""
|
| 58 |
+
Get the income statement for a given company and year using the FMP (https://financialmodelingprep.com) API.
|
| 59 |
+
Returns a dictionary with the income statement data.
|
| 60 |
+
"""
|
| 61 |
+
fmp_api_key = os.environ.get("FMP_API_KEY", None)
|
| 62 |
+
if fmp_api_key is None:
|
| 63 |
+
return "FMP_API_KEY environment variable not set. This tool does not work."
|
| 64 |
+
url = f"https://financialmodelingprep.com/api/v3/income-statement/{ticker}?apikey={fmp_api_key}"
|
| 65 |
+
response = requests.get(url)
|
| 66 |
+
if response.status_code == 200:
|
| 67 |
+
data = response.json()
|
| 68 |
+
income_statement = pd.DataFrame(data)
|
| 69 |
+
income_statement["date"] = pd.to_datetime(income_statement["date"])
|
| 70 |
+
income_statement_specific_year = income_statement[
|
| 71 |
+
income_statement["date"].dt.year == year
|
| 72 |
+
]
|
| 73 |
+
values_dict = income_statement_specific_year.to_dict(orient="records")[0]
|
| 74 |
+
return f"Financial results: {', '.join([f'{key}: {value}' for key, value in values_dict.items() if key not in ['date', 'cik', 'link', 'finalLink']])}"
|
| 75 |
+
else:
|
| 76 |
+
return "FMP API returned error. This tool does not work."
|
| 77 |
+
|
| 78 |
class QueryTranscriptsArgs(BaseModel):
|
| 79 |
query: str = Field(..., description="The user query.")
|
| 80 |
year: int = Field(..., description=f"The year. an integer between {min(years)} and {max(years)}.")
|
|
|
|
| 103 |
[
|
| 104 |
get_company_info,
|
| 105 |
get_valid_years,
|
| 106 |
+
get_income_statement,
|
| 107 |
]
|
| 108 |
) +
|
| 109 |
tools_factory.standard_tools() +
|
|
|
|
| 130 |
def update_func(status_type: AgentStatusType, msg: str):
|
| 131 |
output = f"{status_type.value} - {msg}"
|
| 132 |
st.session_state.thinking_placeholder.text(output)
|
| 133 |
+
st.session_state.log_messages.append(output)
|
| 134 |
|
| 135 |
agent = Agent(
|
| 136 |
agent_type=agent_type,
|
|
|
|
| 147 |
st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "🦖"}]
|
| 148 |
st.session_state.thinking_message = "Agent at work..."
|
| 149 |
st.session_state.agent = initialize_agent(agent_type, cfg)
|
| 150 |
+
st.session_state.log_messages = []
|
| 151 |
+
st.session_state.show_logs = False
|
| 152 |
|
| 153 |
st.set_page_config(page_title="Financial Assistant", layout="wide")
|
| 154 |
if 'cfg' not in st.session_state:
|
|
|
|
| 172 |
)
|
| 173 |
|
| 174 |
st.markdown("\n\n")
|
| 175 |
+
bc1, bc2 = st.columns([1, 1])
|
| 176 |
+
with bc1:
|
| 177 |
+
if st.button('Start Over'):
|
| 178 |
+
reset()
|
| 179 |
+
with bc2:
|
| 180 |
+
if st.button('Show Logs'):
|
| 181 |
+
st.session_state.show_logs = not st.session_state.show_logs
|
| 182 |
|
| 183 |
st.markdown("---")
|
| 184 |
st.markdown(
|
| 185 |
"## How this works?\n"
|
| 186 |
"This app was built with [Vectara](https://vectara.com).\n\n"
|
| 187 |
+
"It demonstrates the use of Agentic RAG functionality with Vectara"
|
| 188 |
)
|
| 189 |
st.markdown("---")
|
| 190 |
|
|
|
|
| 216 |
st.session_state.thinking_placeholder.empty()
|
| 217 |
st.rerun()
|
| 218 |
|
| 219 |
+
# Display log messages in an expander
|
| 220 |
+
if st.session_state.show_logs:
|
| 221 |
+
with st.expander("Agent Log Messages", expanded=True):
|
| 222 |
+
for msg in st.session_state.log_messages:
|
| 223 |
+
st.write(msg)
|
| 224 |
+
if st.button('Close Logs'):
|
| 225 |
+
st.session_state.show_logs = False
|
| 226 |
+
st.rerun()
|
| 227 |
+
|
| 228 |
sys.stdout.flush()
|
| 229 |
|
| 230 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -7,4 +7,5 @@ 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
|
|
|
|
| 7 |
llama-index-indices-managed-vectara==0.1.4
|
| 8 |
llama-index-agent-openai==0.1.5
|
| 9 |
pydantic==1.10.15
|
| 10 |
+
python-dotenv==1.0.1
|
| 11 |
git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git
|