-
Notifications
You must be signed in to change notification settings - Fork 0
/
finance_agent.py
309 lines (250 loc) · 9.67 KB
/
finance_agent.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
303
304
305
306
307
308
309
import os
import pandas as pd
from io import StringIO
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langgraph.graph import StateGraph, END
from tavily import TavilyClient
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.pydantic_v1 import BaseModel
# from langgraph.checkpoint.sqlite import SqliteSaver
from langgraph.checkpoint.memory import MemorySaver
from typing import List
# memory = SqliteSaver.from_conn_string(":memory:")
memory = MemorySaver()
load_dotenv()
openai_key = os.getenv("OPENAI_API_KEY2")
tavily_key = os.getenv("TAVILY_API_KEY")
llm_name = "gpt-4o-mini"
llm_model = ChatOpenAI(
openai_api_key=openai_key,
model_name=llm_name,
temperature=0,
)
tavily = TavilyClient(api_key=tavily_key)
class AgentState(BaseModel):
task: str
competitors: List[str]
csv_file: str
financial_data: str = ""
analysis: str = ""
competitor_data: str = ""
comparison: str = ""
feedback: str = ""
report: str = ""
content: List[str]
revision_number: int
max_revisions: int
class Queries(BaseModel):
queries: List[str]
GATHER_FINANCIALS_PROMPT = """
You are a financial analyst. You are given a csv file with financial data for a company.
Your task is to extract the financial data and provide a report.
"""
ANALYZE_DATA_PROMPT = """
You are a financial analyst. You are given a csv file with financial data for a company.
Your task is to analyze the financial data and provide a report.
"""
RESEARCH_COMPETITORS_PROMPT = """
You are a financial analyst. You are given a csv file with financial data for a company.
Your task is to research the competitors of the company and provide a report.
"""
COMPETE_PERFORMANCE_PROMPT = """
You are a financial analyst. You are given a csv file with financial data for a company.
Your task is to complete the performance report.
"""
FEEDBACK_PROMPT = """
You are a financial analyst. You are given a csv file with financial data for a company.
Your task is to provide feedback on the performance report.
"""
WRITE_REPORT_PROMPT = """
You are a financial analyst. You are given a csv file with financial data for a company.
Your task is to write the financial report.
"""
RESEARCH_CRITIQUE_PROMPT = """
You are a financial analyst. You are given a csv file with financial data for a company.
Your task is to research and critique the financial report. Generate a max of 3 queries to search for more information.
"""
def gather_financials_node(state: AgentState):
csv_file = state.csv_file
df = pd.read_csv(StringIO(csv_file))
financial_data_to_string = df.to_string(index=False)
combined_content = (
f"{state.task}\nHere is the financial data:\n{financial_data_to_string}"
)
messages = [
SystemMessage(content=GATHER_FINANCIALS_PROMPT),
HumanMessage(content=combined_content),
]
response = llm_model.invoke(messages)
financial_data = response.content
return AgentState(**{**state.dict(), "financial_data": financial_data})
def analyze_data_node(state: AgentState):
financial_data = state.financial_data
messages = [
SystemMessage(content=ANALYZE_DATA_PROMPT),
HumanMessage(content=financial_data),
]
response = llm_model.invoke(messages)
analysis = response.content
return AgentState(**{**state.dict(), "analysis": analysis})
def research_competitors_node(state: AgentState):
content = state.content or []
for competitor in state.competitors:
# Create a prompt for the LLM to generate queries
prompt = f"Generate search queries to research financial information about {competitor}. The queries should help gather data for comparison with our company (Awesome Software Inc.)."
response = llm_model.invoke(
[
SystemMessage(content=RESEARCH_COMPETITORS_PROMPT),
HumanMessage(content=prompt),
]
)
# Parse the response into a list of queries
queries = [q.strip() for q in response.content.split("\n") if q.strip()]
for query in queries:
search_results = tavily.search(query=query, max_results=1)
for r in search_results["results"]:
content.append(r["content"])
return AgentState(**{**state.dict(), "content": content})
def compare_performance_node(state: AgentState):
content = "\n".join(state.content or [])
user_message = HumanMessage(
content=f"{state.task}\nHere is the financial analysis:\n{state.analysis}"
)
messages = [
SystemMessage(content=COMPETE_PERFORMANCE_PROMPT.format(content=content)),
user_message,
]
response = llm_model.invoke(messages)
return AgentState(
**{
**state.dict(),
"comparison": response.content,
"revision_number": state.revision_number + 1,
}
)
def collect_feedback_node(state: AgentState):
messages = [
SystemMessage(content=FEEDBACK_PROMPT),
HumanMessage(content=state.comparison),
]
response = llm_model.invoke(messages)
return AgentState(**{**state.dict(), "feedback": response.content})
def research_critique_node(state: AgentState):
queries = llm_model.with_structured_output(Queries).invoke(
[
SystemMessage(content=RESEARCH_CRITIQUE_PROMPT),
HumanMessage(content=state.feedback),
]
)
content = state.content or []
for q in queries.queries:
search_results = tavily.search(query=q, max_results=2)
for r in search_results["results"]:
content.append(r["content"])
return AgentState(**{**state.dict(), "content": content})
def write_report_node(state: AgentState):
messages = [
SystemMessage(content=WRITE_REPORT_PROMPT),
HumanMessage(content=state.comparison),
]
response = llm_model.invoke(messages)
return AgentState(**{**state.dict(), "report": response.content})
def should_continue(state: AgentState):
if state.revision_number > state.max_revisions:
return END
return "collect_feedback"
flow = StateGraph(AgentState)
flow.add_node("gather_financials", gather_financials_node)
flow.add_node("analyze_data", analyze_data_node)
flow.add_node("research_competitors", research_competitors_node)
flow.add_node("compare_performance", compare_performance_node)
flow.add_node("collect_feedback", collect_feedback_node)
flow.add_node("research_critique", research_critique_node)
flow.add_node("write_report", write_report_node)
flow.set_entry_point("gather_financials")
flow.add_conditional_edges(
"compare_performance",
should_continue,
{END: END, "collect_feedback": "collect_feedback"},
)
flow.add_edge("gather_financials", "analyze_data")
flow.add_edge("analyze_data", "research_competitors")
flow.add_edge("research_competitors", "compare_performance")
flow.add_edge("collect_feedback", "research_critique")
flow.add_edge("research_critique", "compare_performance")
flow.add_edge("compare_performance", "write_report")
graph = flow.compile(checkpointer=memory)
graph.get_graph().draw_mermaid_png(output_file_path="graph.png")
# COMMENT OUT AFTER TESTING TO USE STREAMLIT
# def read_csv_file(csv_file_path: str) -> str:
# with open(csv_file_path, "r") as file:
# print("Reading CSV file...")
# return file.read()
# if __name__ == "__main__":
# task = "Analyze the financial data for our company (Awesome Software Inc.) comparing to our competitors."
# competitors = ["invidia"]
# csv_file_path = "./data/financial_data.csv"
# if not os.path.exists(csv_file_path):
# print(f"File {csv_file_path} does not exist")
# else:
# print("starting...")
# csv_data = read_csv_file(csv_file_path)
# initial_state = AgentState(
# task=task,
# competitors=competitors,
# csv_file=csv_data,
# revision_number=1,
# max_revision_number=2,
# financial_data="",
# analysis="",
# competitor_data="",
# comparison="",
# feedback="",
# report="",
# content=[],
# )
# thread = {"configurable": {"thread_id": "1"}}
# for s in graph.stream(initial_state, thread):
# print(s)
# STREAMLIT CODE
import streamlit as st
def main():
st.title("Financial Report Generator")
task = st.text_input(
"Enter the task:",
"Analyze the financial data for our company (Awesome Software Inc.) comparing to our competitors.",
)
competitors = st.text_area(
"Enter the competitors (comma-separated):", "Microsoft, Apple, Google, Amazon"
)
max_revisions = st.number_input(
"Enter the maximum revisions:", min_value=1, value=2
)
uploaded_file = st.file_uploader("Choose a company CSV file", type=["csv"])
if st.button("Generate Report") and uploaded_file is not None:
csv_data = uploaded_file.getvalue().decode("utf-8")
initial_state = AgentState(
task=task,
competitors=[comp.strip() for comp in competitors.split(",")],
csv_file=csv_data,
revision_number=1,
max_revisions=max_revisions,
financial_data="",
analysis="",
competitor_data="",
comparison="",
feedback="",
report="",
content=[],
)
thread = {"configurable": {"thread_id": "1"}}
final_state = None
for s in graph.stream(initial_state, thread):
st.write(s)
final_state = s
if final_state and "report" in final_state:
st.subheader("Financial Report")
st.write(final_state["report"])
if __name__ == "__main__":
main()