-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
302 lines (261 loc) · 11.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import time
import openai
from dotenv import load_dotenv, find_dotenv
import requests
import json
import os
from openai.types.beta import Assistant
from openai.types.beta.thread import Thread
from openai.types.beta.threads.thread_message import ThreadMessage
from openai.types.beta.threads.run_submit_tool_outputs_params import ToolOutput
from openai.types.beta.threads.run import Run
_: bool = load_dotenv(find_dotenv()) # read local .env file
FMP_API_KEY: str | None = os.getenv("FMP_API_KEY")
client: openai.OpenAI = openai.OpenAI()
# Define financial statement functions
def get_income_statement(ticker: str, period: str, limit: int) -> str:
"""
Retrieves income statement data for a given stock ticker.
Args:
ticker (str): Stock ticker symbol.
period (str): The period (e.g., 'annual' or 'quarterly').
limit (int): The maximum number of records to retrieve.
Returns:
str: JSON string containing income statement data.
"""
url: str = f"https://financialmodelingprep.com/api/v3/income-statement/{ticker}?period={period}&limit={limit}&apikey={FMP_API_KEY}"
response: requests.Response = requests.get(url)
return json.dumps(response.json())
def get_balance_sheet(ticker: str, period: str, limit: int) -> str:
"""
Retrieves the balance sheet statement for a given ticker.
Args:
ticker (str): The ticker symbol of the company.
period (str): The period of the balance sheet statement (e.g., 'annual', 'quarter').
limit (int): The number of periods to retrieve.
Returns:
str: The balance sheet statement in JSON format.
"""
url: str = f"https://financialmodelingprep.com/api/v3/balance-sheet-statement/{ticker}?period={period}&limit={limit}&apikey={FMP_API_KEY}"
response: requests.Response = requests.get(url)
return json.dumps(response.json())
def get_cash_flow_statement(ticker: str, period: str, limit: int) -> str:
"""
Retrieves the cash flow statement for a given ticker.
Args:
ticker (str): The ticker symbol of the company.
period (str): The period for which the cash flow statement is requested (e.g., annual, quarterly).
limit (int): The number of periods to retrieve.
Returns:
str: The cash flow statement in JSON format.
"""
url: str = f"https://financialmodelingprep.com/api/v3/cash-flow-statement/{ticker}?period={period}&limit={limit}&apikey={FMP_API_KEY}"
response: requests.Response = requests.get(url)
return json.dumps(response.json())
def get_key_metrics(ticker: str, period: str, limit: int) -> str:
"""
Retrieves key metrics for a given ticker.
Args:
ticker (str): The ticker symbol of the company.
period (str): The period for which key metrics are retrieved.
limit (int): The maximum number of key metrics to retrieve.
Returns:
str: A JSON string containing the key metrics data.
"""
url: str = f"https://financialmodelingprep.com/api/v3/key-metrics/{ticker}?period={period}&limit={limit}&apikey={FMP_API_KEY}"
response: requests.Response = requests.get(url)
return json.dumps(response.json())
def get_financial_ratios(ticker: str, period: str, limit: int) -> str:
"""
Retrieves financial ratios for a given ticker.
Args:
ticker (str): The ticker symbol of the company.
period (str): The period for which the ratios are requested (e.g., annual, quarterly).
limit (int): The maximum number of ratios to retrieve.
Returns:
str: A JSON string containing the financial ratios.
"""
url: str = f"https://financialmodelingprep.com/api/v3/ratios/{ticker}?period={period}&limit={limit}&apikey={FMP_API_KEY}"
response: requests.Response = requests.get(url)
return json.dumps(response.json())
def get_financial_growth(ticker: str, period: str, limit: int) -> str:
"""
Retrieves the cash flow statement growth data for a given ticker.
Args:
ticker (str): The ticker symbol of the company.
period (str): The time period for the data (e.g., 'annual', 'quarter').
limit (int): The number of records to retrieve.
Returns:
str: The JSON string representation of the response data.
"""
url: str = f"https://financialmodelingprep.com/api/v3/cash-flow-statement-growth/{ticker}?period={period}&limit={limit}&apikey={FMP_API_KEY}"
response: requests.Response = requests.get(url)
return json.dumps(response.json())
# Map available functions
available_functions: dict = {
"get_income_statement": get_income_statement,
"get_balance_sheet": get_balance_sheet,
"get_cash_flow_statement": get_cash_flow_statement,
"get_key_metrics": get_key_metrics,
"get_financial_ratios": get_financial_ratios,
"get_financial_growth": get_financial_growth,
}
# Define the assistant function
def run_assistant(user_message: str):
# Creating an assistant with specific instructions and tools
assistant: Assistant = client.beta.assistants.create(
instructions="Act as a financial analyst by accessing detailed financial data through the Financial Modeling Prep API. Your capabilities include analyzing key metrics, comprehensive financial statements, vital financial ratios, and tracking financial growth trends. ",
model="gpt-3.5-turbo-1106",
tools=[
# The first tool is a function that retrieves income statement data
{
"type": "function",
"function": {
"name": "get_income_statement",
"description": "Retrieves income statement data for a given stock ticker.",
"parameters": {
"type": "object",
"properties": {
"ticker": {"type": "string"},
"period": {"type": "string"},
"limit": {"type": "integer"},
},
"required": ["ticker"],
},
},
},
# The second tool is a function that retrieves balance sheet data
{
"type": "function",
"function": {
"name": "get_balance_sheet",
"parameters": {
"type": "object",
"properties": {
"ticker": {"type": "string"},
"period": {"type": "string"},
"limit": {"type": "integer"},
},
},
},
},
# The third tool is a function that retrieves cash flow statement data
{
"type": "function",
"function": {
"name": "get_cash_flow_statement",
"parameters": {
"type": "object",
"properties": {
"ticker": {"type": "string"},
"period": {"type": "string"},
"limit": {"type": "integer"},
},
},
},
},
# The fourth tool is a function that retrieves key metrics
{
"type": "function",
"function": {
"name": "get_key_metrics",
"parameters": {
"type": "object",
"properties": {
"ticker": {"type": "string"},
"period": {"type": "string"},
"limit": {"type": "integer"},
},
},
},
},
# The fifth tool is a function that retrieves financial ratios
{
"type": "function",
"function": {
"name": "get_financial_ratios",
"parameters": {
"type": "object",
"properties": {
"ticker": {"type": "string"},
"period": {"type": "string"},
"limit": {"type": "integer"},
},
},
},
},
# The sixth tool is a function that retrieves financial growth data
{
"type": "function",
"function": {
"name": "get_financial_growth",
"parameters": {
"type": "object",
"properties": {
"ticker": {"type": "string"},
"period": {"type": "string"},
"limit": {"type": "integer"},
},
},
},
},
],
)
# Creating a new thread
thread: Thread = client.beta.threads.create()
# First Request Adding a user message to the thread
message = client.beta.threads.messages.create(
thread_id=thread.id, role="user", content=user_message
)
# Running the assistant on the created thread
run: Run = client.beta.threads.runs.create(
thread_id=thread.id, assistant_id=assistant.id
)
while True:
runStatus = client.beta.threads.runs.retrieve(
thread_id=thread.id, run_id=run.id
)
# This means run is making a function call
if (
runStatus.status == "requires_action"
and runStatus.required_action is not None
):
if (
runStatus.required_action.submit_tool_outputs
and runStatus.required_action.submit_tool_outputs.tool_calls
):
toolCalls = runStatus.required_action.submit_tool_outputs.tool_calls
tool_outputs: list[ToolOutput] = []
for toolcall in toolCalls:
function_name = toolcall.function.name
function_args = json.loads(toolcall.function.arguments)
if function_name in available_functions:
function_to_call = available_functions[function_name]
response = function_to_call(**function_args)
tool_outputs.append(
{
"tool_call_id": toolcall.id,
"output": response,
}
)
# Submit tool outputs and update the run
client.beta.threads.runs.submit_tool_outputs(
thread_id=thread.id, run_id=run.id, tool_outputs=tool_outputs
)
elif runStatus.status == "completed":
messages: list[ThreadMessage] = client.beta.threads.messages.list(
thread_id=thread.id
)
for message in messages.data:
message_content = message.content[0].text.value
return message_content
break # Exit the loop after processing the completed run
elif run.status == "failed":
print("Run failed.")
break
elif run.status in ["in_progress", "queued"]:
print(f"Run is {run.status}. Waiting...")
time.sleep(5) # Wait for 5 seconds before checking again
else:
print(f"Unexpected status: {run.status}")
break