This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
223 lines (177 loc) · 7.24 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
from lamini.generation.generation_node import GenerationNode
from lamini.generation.generation_pipeline import GenerationPipeline
from lamini.generation.base_prompt_object import PromptObject
import jsonlines
import collections
import asyncio
from tqdm import tqdm
from typing import Union, Iterator, AsyncIterator
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.WARNING,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
async def main():
earnings_calls = load_earnings_calls()
answers = QuestionAnswerPipeline().call(earnings_calls)
await save_answers(answers)
async def load_earnings_calls():
path = "/app/lamini-earnings-calls/data/earnings-transcripts.jsonl"
with jsonlines.open(path) as reader:
for line in reader:
logger.info(f"Loaded earnings call for {line['ticker']}")
yield PromptObject(prompt="", data=line)
class QuestionAnswerPipeline(GenerationPipeline):
def __init__(self):
super(QuestionAnswerPipeline, self).__init__()
self.question_generator = QuestionGenerator()
self.answer_generator = AnswerGenerator()
def forward(self, x):
x = self.question_generator(x)
x = self.answer_generator(x)
return x
class QuestionGenerator(GenerationNode):
def __init__(self):
super(QuestionGenerator, self).__init__(
model_name="mistralai/Mistral-7B-Instruct-v0.1", max_tokens=150
)
def generate(
self,
prompt: Union[Iterator[PromptObject], AsyncIterator[PromptObject]],
*args,
**kwargs,
):
prompts = self.transform_prompt(prompt)
results = super(QuestionGenerator, self).generate(
prompts,
output_type={
"question_1": "string",
"question_2": "string",
"question_3": "string",
},
*args,
**kwargs,
)
processed_results = self.process_results(results)
return processed_results
async def process_results(self, results):
async for result in results:
logger.info(f"Generated question for {result}")
if result is None:
continue
if "question_1" not in result.response:
continue
if "question_2" not in result.response:
continue
if "question_3" not in result.response:
continue
questions = result.response["question_1"], result.response["question_2"], result.response["question_3"]
for question in questions:
result = PromptObject(prompt=question, data=result.data.copy())
yield result
async def transform_prompt(self, prompts):
async for prompt in prompts:
chunks = chunk_prompt(prompt)
for chunk in chunks:
chunk.prompt = self.make_prompt(chunk)
logger.info(f"Generating question for {chunk.data['ticker']}, {chunk.data['q']}")
yield chunk
def make_prompt(self, chunk):
prompt = (
"<s>[INSTR]You are a financial analyst with extensive experience at Goldman Sachs."
)
prompt += "You are reading the earnings call transcript for the following company:\n\n"
prompt += "====================\n\n"
prompt += get_company_info(chunk) + "\n"
prompt += "====================\n\n"
prompt += (
"You are reading the following section of the earnings call transcript:\n\n"
)
prompt += "====================\n\n"
prompt += chunk.data["transcript"]
prompt += "====================\n\n"
prompt += "Consider the numbers in the transscript. "
prompt += "Ask three questions about the numbers in the transcript that require precise answers. "
prompt += "Only ask questions that can be answered using the transcript."
prompt +="[/INSTR]"
return prompt
def chunk_prompt(prompt):
transcript = prompt.data["transcript"]
chunk_size = 4096
chunk_step = 2048
for i in range(0, len(transcript), chunk_step):
chunk = transcript[i : i + chunk_size]
chunked_data = prompt.data.copy()
chunked_data["transcript"] = chunk
prompt_object = PromptObject(prompt=prompt.prompt, data=chunked_data)
yield prompt_object
def get_company_info(chunk):
info = f"Company: {chunk.data['exchange']}\n"
info += f"Ticker: {chunk.data['ticker']}\n"
info += f"Date: {chunk.data['date']}\n"
info += f"Quarter: {chunk.data['q']}\n"
return info
class AnswerGenerator(GenerationNode):
def __init__(self):
super(AnswerGenerator, self).__init__(
model_name="mistralai/Mistral-7B-Instruct-v0.1", max_tokens=150
)
def generate(
self,
prompt: Union[Iterator[PromptObject], AsyncIterator[PromptObject]],
*args,
**kwargs,
):
prompts = self.transform_prompt(prompt)
results = super(AnswerGenerator, self).generate(prompts, *args, **kwargs)
processed_results = self.process_results(results)
return processed_results
async def process_results(self, results):
async for result in results:
logger.info(f"Generated answer for {result}")
if result is None:
continue
yield result
async def transform_prompt(self, prompts):
async for prompt in prompts:
prompt.data["question"] = prompt.prompt
prompt.prompt = self.make_prompt(prompt)
yield prompt
def make_prompt(self, chunk):
prompt = (
"<s>[INSTR] You are a financial analyst with extensive experience at Goldman Sachs."
)
prompt += "You are reading the earnings call transcript for the following company:\n\n"
prompt += "====================\n\n"
prompt += get_company_info(chunk)
prompt += "====================\n\n"
prompt += (
"You are reading the following section of the earnings call transcript:\n\n"
)
prompt += "====================\n\n"
prompt += chunk.data["transcript"] + "\n"
prompt += "====================\n\n"
prompt += "Consider the numbers in the transscript. "
prompt += "If the answer to the question cannot be found in the transcript, reply that you do not know. "
prompt += "Answer the following questions about the numbers in the transcript. "
prompt += chunk.prompt
prompt += "[/INSTR]"
return prompt
async def save_answers(answers):
path = "/app/lamini-earnings-calls/data/answers.jsonl"
with jsonlines.open(path, "w") as writer:
pbar = tqdm(desc="Saving answers", unit=" answers")
async for answer in answers:
answer = {
"ticker": answer.data["ticker"],
"q": answer.data["q"],
"date": answer.data["date"],
"transcript": answer.data["transcript"],
"prompt": answer.prompt,
"question": answer.data["question"],
"answer": answer.response["output"],
}
writer.write(answer)
pbar.update()
asyncio.run(main())