-
Notifications
You must be signed in to change notification settings - Fork 17
/
adv_rag.py
289 lines (248 loc) · 9.86 KB
/
adv_rag.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
import os
import uuid
from pathlib import Path
from typing import Any
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.prompts.chat import ChatPromptTemplate
from langchain.retrievers.multi_vector import MultiVectorRetriever
from langchain.schema.document import Document
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough
from langchain.storage import InMemoryStore
from langchain.vectorstores import Chroma
from langchain_core.retrievers import BaseRetriever
from loguru import logger
from pydantic import BaseModel, FilePath
from unstructured.documents.elements import Element
from unstructured.partition.pdf import partition_pdf
class DocElement(BaseModel):
type: str
content: Any
class DocElements(BaseModel):
text_doc_elements: list[DocElement]
table_doc_elements: list[DocElement]
class DocSummaries(BaseModel):
text_doc_summaries: list[str]
table_doc_summaries: list[str]
class SummaryCreator:
def __call__(self, elements: list[Element]) -> DocElements:
return self.build(elements)
def create(self, elements: list[Element]) -> DocElements:
text_doc_elements, table_doc_elements = list(), list()
for element in elements:
if "unstructured.documents.elements.Table" in str(type(element)):
# logger.debug(f"Table: {element}")
table_doc_elements.append(
DocElement(type="table", content=str(element))
)
elif "unstructured.documents.elements.CompositeElement" in str(
type(element)
):
# logger.debug(f"Composite element: {element}")
text_doc_elements.append(DocElement(type="text", content=str(element)))
return DocElements(
text_doc_elements=text_doc_elements, table_doc_elements=table_doc_elements
)
class SummaryChain:
_prompt_text = """
You should concisely summary content chunk:
content: {content}
"""
def __init__(self):
prompt = ChatPromptTemplate.from_template(self._prompt_text)
self._summarize_chain = (
{"content": RunnablePassthrough()}
| prompt
| ChatOpenAI(temperature=0, model="gpt-4-1106-preview")
| StrOutputParser()
)
def __call__(
self, doc_elements: DocElements, max_con_curr: int = 5
) -> DocSummaries:
return self.execute(doc_elements, max_con_curr)
def execute(
self, doc_elements: DocElements, max_con_curr: int = 10
) -> DocSummaries:
texts = list(map(lambda e: e.content, doc_elements.text_doc_elements))
text_doc_summaries = self._summarize_chain.batch(
texts, {"max_concurrency": max_con_curr}
)
tables = list(map(lambda e: e.content, doc_elements.table_doc_elements))
table_doc_summaries = self._summarize_chain.batch(
tables, {"max_concurrency": max_con_curr}
)
return DocSummaries(
text_doc_summaries=text_doc_summaries,
table_doc_summaries=table_doc_summaries,
)
class RetrieverBuilder:
_key = "doc_key"
_doc_store = InMemoryStore()
def __call__(
self, doc_elements: DocElements, doc_summaries: DocSummaries
) -> BaseRetriever:
return self.build(doc_elements, doc_summaries)
def build(
self, doc_elements: DocElements, doc_summaries: DocSummaries
) -> BaseRetriever:
assert len(doc_elements.text_doc_elements) == len(
doc_summaries.text_doc_summaries
), "Text elements and summaries must be same length"
assert len(doc_elements.table_doc_elements) == len(
doc_summaries.table_doc_summaries
), "Table elements and summaries must be same length"
text_keys = [str(uuid.uuid4()) for _ in doc_elements.text_doc_elements]
table_keys = [str(uuid.uuid4()) for _ in doc_elements.table_doc_elements]
retriever = MultiVectorRetriever(
vectorstore=Chroma(
collection_name="summaries",
embedding_function=OpenAIEmbeddings(),
),
docstore=self._doc_store,
id_key=self._key,
)
retriever.vectorstore.add_documents(
list(
map(
lambda x: Document(metadata={self._key: x[0]}, page_content=x[1]),
list(zip(text_keys, doc_summaries.text_doc_summaries)),
)
)
)
retriever.docstore.mset(
list(
zip(
text_keys,
list(map(lambda x: x.content, doc_elements.text_doc_elements)),
)
)
)
retriever.vectorstore.add_documents(
list(
map(
lambda x: Document(metadata={self._key: x[0]}, page_content=x[1]),
list(zip(table_keys, doc_summaries.table_doc_summaries)),
)
)
)
retriever.docstore.mset(
list(
zip(
table_keys,
list(map(lambda x: x.content, doc_elements.table_doc_elements)),
)
)
)
return retriever
class QueryChain:
_prompt_text = """
Answer the question based on the context:
context: {context}
question: {question}
"""
def __init__(self, retriever: BaseRetriever):
prompt = ChatPromptTemplate.from_template(self._prompt_text)
self._summarize_chain = (
{"question": RunnablePassthrough(), "context": retriever}
| prompt
| ChatOpenAI(temperature=0, model="gpt-4-1106-preview")
| StrOutputParser()
)
def __call__(self, query: str) -> str:
return self.invoke(query)
def invoke(self, query: str) -> str:
return self._summarize_chain.invoke(query)
class App:
def _read_pdf(
self,
filepath: FilePath | None,
extract_images_in_pdf: bool,
infer_table_structure: bool,
) -> list[Element] | None:
if not filepath:
return None
raw_pdf_elements = partition_pdf(
filename=filepath.absolute(),
extract_images_in_pdf=extract_images_in_pdf,
infer_table_structure=infer_table_structure,
chunking_strategy="by_title",
max_characters=4000,
new_after_n_chars=3800,
combine_text_under_n_chars=2000,
image_output_dir_path="./tmp",
)
return raw_pdf_elements
def _upload_doc(self) -> FilePath | None:
with st.sidebar:
uploaded_doc = st.file_uploader("# Upload one PDF", key="doc_uploader")
if not uploaded_doc:
return None
if uploaded_doc:
tmp_dir = "tmp/"
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
temp_file_path = os.path.join(tmp_dir, f"{uploaded_doc.name}")
with open(temp_file_path, "wb") as file:
file.write(uploaded_doc.getvalue())
file_name = uploaded_doc.name
logger.debug(f"Uploaded {file_name}")
uploaded_doc.flush()
uploaded_doc.close()
# os.remove(temp_file_path)
return Path(temp_file_path)
return None
def __call__(self):
st.title("# Advanced RAG")
st.write(
f"Data source: [Baidu Inc. 2023 Q3 Financial Reports](https://ir.baidu.com/static-files/f4006310-1a98-4d86-89a1-edfea1ef5d0e)"
)
st.write(
f"Data source: [Tesla Investor Shareholder Reports](https://ir.tesla.com/#quarterly-disclosure)"
)
filepath = self._upload_doc()
logger.debug(f"Loaded Filepath: {filepath}")
if filepath is not None:
st.sidebar.info(
"For None-GPU machine it'll take longer time to extract PDF."
)
element_list = (
self._read_pdf(
filepath=filepath,
extract_images_in_pdf=True,
infer_table_structure=True,
)
if "element_list" not in st.session_state
else st.session_state["element_list"]
)
st.session_state["element_list"] = element_list
with st.sidebar.expander("Element types"):
st.write(set([type(element) for element in element_list]))
if "retriever" not in st.session_state:
logger.debug("Creating retriever")
doc_elements = SummaryCreator().create(element_list)
doc_summaries = SummaryChain().execute(doc_elements)
retriever = RetrieverBuilder().build(doc_elements, doc_summaries)
st.session_state["retriever"] = retriever
with st.expander("Doc elements"):
st.write("Text elements")
st.write(doc_elements.text_doc_elements)
st.write("Table elements")
st.write(doc_elements.table_doc_elements)
with st.expander("Doc summaries"):
st.write("Text summaries")
st.write(doc_summaries.text_doc_summaries)
st.write("Table summaries")
st.write(doc_summaries.table_doc_summaries)
query = st.text_input("Query")
if (
query is not None
and len(query.strip()) > 0
and "retriever" in st.session_state
):
logger.debug(f"Query: {query}")
retriever = st.session_state["retriever"]
st.write(QueryChain(retriever).invoke(query.strip()))
if __name__ == "__main__":
App()()