-
Notifications
You must be signed in to change notification settings - Fork 657
/
Copy pathengine.py
341 lines (305 loc) · 11.9 KB
/
engine.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from typing import Dict, List, Optional
import logging
from pathlib import Path
from datetime import datetime
import s3fs
from fsspec.asyn import AsyncFileSystem
from llama_index import (
ServiceContext,
VectorStoreIndex,
StorageContext,
load_indices_from_storage,
)
from llama_index.vector_stores.types import VectorStore
from tempfile import TemporaryDirectory
import requests
import nest_asyncio
from datetime import timedelta
from cachetools import cached, TTLCache
from llama_index.readers.file.docs_reader import PDFReader
from llama_index.schema import Document as LlamaIndexDocument
from llama_index.agent import OpenAIAgent
from llama_index.llms import ChatMessage, OpenAI
from llama_index.embeddings.openai import (
OpenAIEmbedding,
OpenAIEmbeddingMode,
OpenAIEmbeddingModelType,
)
from llama_index.llms.base import MessageRole
from llama_index.callbacks.base import BaseCallbackHandler, CallbackManager
from llama_index.tools import QueryEngineTool, ToolMetadata
from llama_index.query_engine import SubQuestionQueryEngine
from llama_index.indices.query.base import BaseQueryEngine
from llama_index.vector_stores.types import (
MetadataFilters,
ExactMatchFilter,
)
from llama_index.node_parser.simple import SimpleNodeParser
from app.core.config import settings
from app.schema import (
Message as MessageSchema,
Document as DocumentSchema,
Conversation as ConversationSchema,
DocumentMetadataKeysEnum,
SecDocumentMetadata,
)
from app.models.db import MessageRoleEnum, MessageStatusEnum
from app.chat.constants import (
DB_DOC_ID_KEY,
SYSTEM_MESSAGE,
NODE_PARSER_CHUNK_OVERLAP,
NODE_PARSER_CHUNK_SIZE,
)
from app.chat.tools import get_api_query_engine_tool
from app.chat.utils import build_title_for_document
from app.chat.pg_vector import get_vector_store_singleton
from app.chat.qa_response_synth import get_custom_response_synth
logger = logging.getLogger(__name__)
logger.info("Applying nested asyncio patch")
nest_asyncio.apply()
def get_s3_fs() -> AsyncFileSystem:
s3 = s3fs.S3FileSystem(
key=settings.AWS_KEY,
secret=settings.AWS_SECRET,
endpoint_url=settings.S3_ENDPOINT_URL,
)
if not (settings.RENDER or s3.exists(settings.S3_BUCKET_NAME)):
s3.mkdir(settings.S3_BUCKET_NAME)
return s3
def fetch_and_read_document(
document: DocumentSchema,
) -> List[LlamaIndexDocument]:
# Super hacky approach to get this to feature complete on time.
# TODO: Come up with better abstractions for this and the other methods in this module.
with TemporaryDirectory() as temp_dir:
temp_file_path = Path(temp_dir) / f"{str(document.id)}.pdf"
with open(temp_file_path, "wb") as temp_file:
with requests.get(document.url, stream=True) as r:
r.raise_for_status()
for chunk in r.iter_content(chunk_size=8192):
temp_file.write(chunk)
temp_file.seek(0)
reader = PDFReader()
return reader.load_data(
temp_file_path, extra_info={DB_DOC_ID_KEY: str(document.id)}
)
def build_description_for_document(document: DocumentSchema) -> str:
if DocumentMetadataKeysEnum.SEC_DOCUMENT in document.metadata_map:
sec_metadata = SecDocumentMetadata.parse_obj(
document.metadata_map[DocumentMetadataKeysEnum.SEC_DOCUMENT]
)
time_period = (
f"{sec_metadata.year} Q{sec_metadata.quarter}"
if sec_metadata.quarter
else str(sec_metadata.year)
)
return f"A SEC {sec_metadata.doc_type.value} filing describing the financials of {sec_metadata.company_name} ({sec_metadata.company_ticker}) for the {time_period} time period."
return "A document containing useful information that the user pre-selected to discuss with the assistant."
def index_to_query_engine(doc_id: str, index: VectorStoreIndex) -> BaseQueryEngine:
filters = MetadataFilters(
filters=[ExactMatchFilter(key=DB_DOC_ID_KEY, value=doc_id)]
)
kwargs = {"similarity_top_k": 3, "filters": filters}
return index.as_query_engine(**kwargs)
@cached(
TTLCache(maxsize=10, ttl=timedelta(minutes=5).total_seconds()),
key=lambda *args, **kwargs: "global_storage_context",
)
def get_storage_context(
persist_dir: str, vector_store: VectorStore, fs: Optional[AsyncFileSystem] = None
) -> StorageContext:
logger.info("Creating new storage context.")
return StorageContext.from_defaults(
persist_dir=persist_dir, vector_store=vector_store, fs=fs
)
async def build_doc_id_to_index_map(
service_context: ServiceContext,
documents: List[DocumentSchema],
fs: Optional[AsyncFileSystem] = None,
) -> Dict[str, VectorStoreIndex]:
persist_dir = f"{settings.S3_BUCKET_NAME}"
vector_store = await get_vector_store_singleton()
try:
try:
storage_context = get_storage_context(persist_dir, vector_store, fs=fs)
except FileNotFoundError:
logger.info(
"Could not find storage context in S3. Creating new storage context."
)
storage_context = StorageContext.from_defaults(
vector_store=vector_store, fs=fs
)
storage_context.persist(persist_dir=persist_dir, fs=fs)
index_ids = [str(doc.id) for doc in documents]
indices = load_indices_from_storage(
storage_context,
index_ids=index_ids,
service_context=service_context,
)
doc_id_to_index = dict(zip(index_ids, indices))
logger.debug("Loaded indices from storage.")
except ValueError:
logger.error(
"Failed to load indices from storage. Creating new indices.", exc_info=True
)
storage_context = StorageContext.from_defaults(
persist_dir=persist_dir, vector_store=vector_store, fs=fs
)
doc_id_to_index = {}
for doc in documents:
llama_index_docs = fetch_and_read_document(doc)
storage_context.docstore.add_documents(llama_index_docs)
index = VectorStoreIndex.from_documents(
llama_index_docs,
storage_context=storage_context,
service_context=service_context,
)
index.set_index_id(str(doc.id))
index.storage_context.persist(persist_dir=persist_dir, fs=fs)
doc_id_to_index[str(doc.id)] = index
return doc_id_to_index
def get_chat_history(
chat_messages: List[MessageSchema],
) -> List[ChatMessage]:
"""
Given a list of chat messages, return a list of ChatMessage instances.
Failed chat messages are filtered out and then the remaining ones are
sorted by created_at.
"""
# pre-process chat messages
chat_messages = [
m
for m in chat_messages
if m.content.strip() and m.status == MessageStatusEnum.SUCCESS
]
# TODO: could be a source of high CPU utilization
chat_messages = sorted(chat_messages, key=lambda m: m.created_at)
chat_history = []
for message in chat_messages:
role = (
MessageRole.ASSISTANT
if message.role == MessageRoleEnum.assistant
else MessageRole.USER
)
chat_history.append(ChatMessage(content=message.content, role=role))
return chat_history
def get_tool_service_context(
callback_handlers: List[BaseCallbackHandler],
) -> ServiceContext:
llm = OpenAI(
temperature=0,
model="gpt-3.5-turbo-0613",
streaming=False,
api_key=settings.OPENAI_API_KEY,
additional_kwargs={"api_key": settings.OPENAI_API_KEY},
)
callback_manager = CallbackManager(callback_handlers)
embedding_model = OpenAIEmbedding(
mode=OpenAIEmbeddingMode.SIMILARITY_MODE,
model_type=OpenAIEmbeddingModelType.TEXT_EMBED_ADA_002,
api_key=settings.OPENAI_API_KEY,
)
# Use a smaller chunk size to retrieve more granular results
node_parser = SimpleNodeParser.from_defaults(
chunk_size=NODE_PARSER_CHUNK_SIZE,
chunk_overlap=NODE_PARSER_CHUNK_OVERLAP,
callback_manager=callback_manager,
)
service_context = ServiceContext.from_defaults(
callback_manager=callback_manager,
llm=llm,
embed_model=embedding_model,
node_parser=node_parser,
)
return service_context
async def get_chat_engine(
callback_handler: BaseCallbackHandler,
conversation: ConversationSchema,
) -> OpenAIAgent:
service_context = get_tool_service_context([callback_handler])
s3_fs = get_s3_fs()
doc_id_to_index = await build_doc_id_to_index_map(
service_context, conversation.documents, fs=s3_fs
)
id_to_doc: Dict[str, DocumentSchema] = {
str(doc.id): doc for doc in conversation.documents
}
vector_query_engine_tools = [
QueryEngineTool(
query_engine=index_to_query_engine(doc_id, index),
metadata=ToolMetadata(
name=doc_id,
description=build_description_for_document(id_to_doc[doc_id]),
),
)
for doc_id, index in doc_id_to_index.items()
]
response_synth = get_custom_response_synth(service_context, conversation.documents)
qualitative_question_engine = SubQuestionQueryEngine.from_defaults(
query_engine_tools=vector_query_engine_tools,
service_context=service_context,
response_synthesizer=response_synth,
verbose=settings.VERBOSE,
use_async=True,
)
api_query_engine_tools = [
get_api_query_engine_tool(doc, service_context)
for doc in conversation.documents
if DocumentMetadataKeysEnum.SEC_DOCUMENT in doc.metadata_map
]
quantitative_question_engine = SubQuestionQueryEngine.from_defaults(
query_engine_tools=api_query_engine_tools,
service_context=service_context,
response_synthesizer=response_synth,
verbose=settings.VERBOSE,
use_async=True,
)
top_level_sub_tools = [
QueryEngineTool(
query_engine=qualitative_question_engine,
metadata=ToolMetadata(
name="qualitative_question_engine",
description="""
A query engine that can answer qualitative questions about a set of SEC financial documents that the user pre-selected for the conversation.
Any questions about company-related headwinds, tailwinds, risks, sentiments, or administrative information should be asked here.
""".strip(),
),
),
QueryEngineTool(
query_engine=quantitative_question_engine,
metadata=ToolMetadata(
name="quantitative_question_engine",
description="""
A query engine that can answer quantitative questions about a set of SEC financial documents that the user pre-selected for the conversation.
Any questions about company-related financials or other metrics should be asked here.
""".strip(),
),
),
]
chat_llm = OpenAI(
temperature=0,
model="gpt-3.5-turbo-0613",
streaming=True,
api_key=settings.OPENAI_API_KEY,
additional_kwargs={"api_key": settings.OPENAI_API_KEY},
)
chat_messages: List[MessageSchema] = conversation.messages
chat_history = get_chat_history(chat_messages)
logger.debug("Chat history: %s", chat_history)
if conversation.documents:
doc_titles = "\n".join(
"- " + build_title_for_document(doc) for doc in conversation.documents
)
else:
doc_titles = "No documents selected."
curr_date = datetime.utcnow().strftime("%Y-%m-%d")
chat_engine = OpenAIAgent.from_tools(
tools=top_level_sub_tools,
llm=chat_llm,
chat_history=chat_history,
verbose=settings.VERBOSE,
system_prompt=SYSTEM_MESSAGE.format(doc_titles=doc_titles, curr_date=curr_date),
callback_manager=service_context.callback_manager,
max_function_calls=3,
)
return chat_engine