ofermend commited on
Commit
c9767eb
·
1 Parent(s): adf13ff
Files changed (2) hide show
  1. agent.py +45 -37
  2. requirements.txt +1 -1
agent.py CHANGED
@@ -33,6 +33,42 @@ tickers = {
33
  years = range(2015, 2025)
34
  initial_prompt = "How can I help you today?"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def create_assistant_tools(cfg):
37
 
38
  def get_company_info() -> list[str]:
@@ -50,35 +86,6 @@ def create_assistant_tools(cfg):
50
  """
51
  return years
52
 
53
- # Tool to get the income statement for a given company and year using the FMP API
54
- @lru_cache(maxsize=128)
55
- def fmp_income_statement(
56
- ticker: str = Field(description="the ticker symbol of the company.", examples=["AAPL", "GOOG", "AMZN"]),
57
- year: int = Field(description="the year for which to get the income statement.", examples=[2020, 2021, 2022]),
58
- ) -> str:
59
- """
60
- Get the income statement for a given company and year using the FMP (https://financialmodelingprep.com) API.
61
- Returns a dictionary with the income statement data. All data is in USD, but you can convert it to more compact form like K, M, B.
62
- """
63
- fmp_api_key = os.environ.get("FMP_API_KEY", None)
64
- if fmp_api_key is None:
65
- return "FMP_API_KEY environment variable not set. This tool does not work."
66
- url = f"https://financialmodelingprep.com/api/v3/income-statement/{ticker}?apikey={fmp_api_key}"
67
- response = requests.get(url)
68
- if response.status_code == 200:
69
- data = response.json()
70
- income_statement = pd.DataFrame(data)
71
- if len(income_statement) == 0 or "date" not in income_statement.columns:
72
- return "No data found for the given ticker symbol."
73
- income_statement["date"] = pd.to_datetime(income_statement["date"])
74
- income_statement_specific_year = income_statement[
75
- income_statement["date"].dt.year == int(year)
76
- ]
77
- values_dict = income_statement_specific_year.to_dict(orient="records")[0]
78
- return f"Financial results: {', '.join([f'{key}={value}' for key, value in values_dict.items() if key not in ['date', 'cik', 'link', 'finalLink']])}"
79
-
80
- return f"FMP API returned error {response.status_code}. This tool does not work."
81
-
82
  class QueryTranscriptsArgs(BaseModel):
83
  query: str = Field(..., description="The user query, always in the form of a question", examples=["what are the risks reported?", "who are the competitors?"])
84
  year: int | str = Field(
@@ -103,7 +110,7 @@ def create_assistant_tools(cfg):
103
  summary_num_results = 10,
104
  vectara_summarizer = summarizer,
105
  include_citations = True,
106
- verbose=True,
107
  )
108
 
109
  class SearchTranscriptsArgs(BaseModel):
@@ -120,10 +127,10 @@ def create_assistant_tools(cfg):
120
  tool_description = """
121
  Given a company name and year, and a user query, retrieves the most relevant text from analyst call transcripts about the company related to the user query.
122
  """,
123
- tool_args_schema = QueryTranscriptsArgs,
124
  reranker = "multilingual_reranker_v1", rerank_k = 100,
125
  lambda_val = 0.005,
126
- verbose=True
127
  )
128
 
129
  tools_factory = ToolsFactory()
@@ -142,14 +149,15 @@ def create_assistant_tools(cfg):
142
  def initialize_agent(_cfg, agent_progress_callback=None):
143
  financial_bot_instructions = """
144
  - You are a helpful financial assistant, with expertise in financial reporting, in conversation with a user.
 
 
 
 
 
 
145
  - Use the ask_transcripts tool to answer most questions about the company's financial performance, risks, opportunities, strategy, competitors, and more.
146
- - responses from ask_transcripts are summarized. You don't need to further summarize them.
147
  - Respond in a compact format by using appropriate units of measure (e.g., K for thousands, M for millions, B for billions).
148
  Do not report the same number twice (e.g. $100K and 100,000 USD).
149
- - Always use the 'income_statement' tool to obtain accurate financial data like revenues, expenses, net income, and other financial metrics
150
- for a specific company, for any the year 2020 or later.
151
- - Use the 'fmp_income_statement' tool (with the company ticker and year) to obtain financial data for any year before 2020.
152
- - Always check the 'get_company_info' and 'get_valid_years' tools to validate company and year are valid.
153
  - Do not include URLs unless they are provided in the output of a tool you use.
154
  - When querying a tool for a numeric value or KPI, use a concise and non-ambiguous description of what you are looking for.
155
  - If you calculate a metric, make sure you have all the necessary information to complete the calculation. Don't guess.
 
33
  years = range(2015, 2025)
34
  initial_prompt = "How can I help you today?"
35
 
36
+ # Tool to get the income statement for a given company and year using the FMP API
37
+ @lru_cache(maxsize=128)
38
+ def fmp_income_statement(
39
+ ticker: str = Field(description="the ticker symbol of the company.", examples=["AAPL", "GOOG", "AMZN"]),
40
+ year: int = Field(description="the year for which to get the income statement.", examples=[2020, 2021, 2022]),
41
+ ) -> str:
42
+ """
43
+ Get the income statement for a given company and year using the FMP (https://financialmodelingprep.com) API.
44
+ Args:
45
+ ticker (str): the ticker symbol of the company.
46
+ year (int): the year for which to get the income statement.
47
+
48
+ Returns:
49
+ A dictionary with the income statement data.
50
+ All data is in USD, but you can convert it to more compact form like K, M, B.
51
+ """
52
+ fmp_api_key = os.environ.get("FMP_API_KEY", None)
53
+ if fmp_api_key is None:
54
+ return "FMP_API_KEY environment variable not set. This tool does not work."
55
+ url = f"https://financialmodelingprep.com/api/v3/income-statement/{ticker}?apikey={fmp_api_key}"
56
+ response = requests.get(url)
57
+ if response.status_code == 200:
58
+ data = response.json()
59
+ income_statement = pd.DataFrame(data)
60
+ if len(income_statement) == 0 or "date" not in income_statement.columns:
61
+ return "No data found for the given ticker symbol."
62
+ income_statement["date"] = pd.to_datetime(income_statement["date"])
63
+ income_statement_specific_year = income_statement[
64
+ income_statement["date"].dt.year == int(year)
65
+ ]
66
+ values_dict = income_statement_specific_year.to_dict(orient="records")[0]
67
+ return f"Financial results: {', '.join([f'{key}={value}' for key, value in values_dict.items() if key not in ['date', 'cik', 'link', 'finalLink']])}"
68
+
69
+ return f"FMP API returned error {response.status_code}. This tool does not work."
70
+
71
+
72
  def create_assistant_tools(cfg):
73
 
74
  def get_company_info() -> list[str]:
 
86
  """
87
  return years
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  class QueryTranscriptsArgs(BaseModel):
90
  query: str = Field(..., description="The user query, always in the form of a question", examples=["what are the risks reported?", "who are the competitors?"])
91
  year: int | str = Field(
 
110
  summary_num_results = 10,
111
  vectara_summarizer = summarizer,
112
  include_citations = True,
113
+ verbose=False,
114
  )
115
 
116
  class SearchTranscriptsArgs(BaseModel):
 
127
  tool_description = """
128
  Given a company name and year, and a user query, retrieves the most relevant text from analyst call transcripts about the company related to the user query.
129
  """,
130
+ tool_args_schema = SearchTranscriptsArgs,
131
  reranker = "multilingual_reranker_v1", rerank_k = 100,
132
  lambda_val = 0.005,
133
+ verbose=False
134
  )
135
 
136
  tools_factory = ToolsFactory()
 
149
  def initialize_agent(_cfg, agent_progress_callback=None):
150
  financial_bot_instructions = """
151
  - You are a helpful financial assistant, with expertise in financial reporting, in conversation with a user.
152
+ - Always use the 'income_statement' tool to obtain accurate financial data like revenues, expenses, net income, and other financial metrics
153
+ for a specific company, for any the year 2020 or later.
154
+ - Use the 'fmp_income_statement' tool (with the company ticker and year) to obtain financial data for any year before 2020,
155
+ - Use the 'fmp_income_statement` tool (with the company ticker and year) to obtain financial data for any year on or after 2020, when the 'income_statement'
156
+ did not return any data useful to respond to the user query.
157
+ - Always check the 'get_company_info' and 'get_valid_years' tools to validate company and year are valid.
158
  - Use the ask_transcripts tool to answer most questions about the company's financial performance, risks, opportunities, strategy, competitors, and more.
 
159
  - Respond in a compact format by using appropriate units of measure (e.g., K for thousands, M for millions, B for billions).
160
  Do not report the same number twice (e.g. $100K and 100,000 USD).
 
 
 
 
161
  - Do not include URLs unless they are provided in the output of a tool you use.
162
  - When querying a tool for a numeric value or KPI, use a concise and non-ambiguous description of what you are looking for.
163
  - If you calculate a metric, make sure you have all the necessary information to complete the calculation. Don't guess.
requirements.txt CHANGED
@@ -6,4 +6,4 @@ streamlit_feedback==0.1.3
6
  uuid==1.30
7
  langdetect==1.0.9
8
  langcodes==3.4.0
9
- vectara-agentic==0.2.0
 
6
  uuid==1.30
7
  langdetect==1.0.9
8
  langcodes==3.4.0
9
+ vectara-agentic==0.2.1