-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_store.py
More file actions
281 lines (228 loc) · 8.21 KB
/
vector_store.py
File metadata and controls
281 lines (228 loc) · 8.21 KB
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
from dotenv import load_dotenv
load_dotenv()
import logging
import multiprocessing
import os
from io import BytesIO
from prompt import prompts
from pydantic import BaseModel, Field
from langchain.prompts import ChatPromptTemplate
import pathway as pw
from pathway.udfs import DiskCache, ExponentialBackoffRetryStrategy
from pathway.xpacks.llm import embedders, llms
from pathway.xpacks.llm.parsers import OpenParse
from pathway.stdlib.indexing import (
BruteForceKnnFactory,
HybridIndexFactory,
UsearchKnnFactory,
)
from pathway.stdlib.indexing.bm25 import TantivyBM25Factory
from pathway.xpacks.llm.document_store import DocumentStore
from pathway.xpacks.llm.servers import DocumentStoreServer
import config
from llm import llm
from workflows.repeater import repeater
from workflows.rag_e2e import rag_e2e
from workflows.post_processing import visual_workflow
os.environ["TESSDATA_PREFIX"] = "/home/ganeshr/miniconda3/envs/harshit/share/tessdata/"
from typing import Callable
import pathway as pw
from pathway.internals.udfs.utils import coerce_async
from pathway.xpacks.llm.servers import BaseRestServer
import config
import uuid
def serve_callable(
server,
route: str,
schema: type[pw.Schema],
callable_func: Callable,
**additional_endpoint_kwargs,
):
def func_to_transformer(fn):
HTTP_CONN_RESPONSE_KEY = "result"
async_fn = coerce_async(fn)
class FuncAsyncTransformer(
pw.AsyncTransformer, output_schema=pw.schema_from_types(result=dict)
):
async def invoke(self, *args, **kwargs) -> dict:
args = tuple(
(
arg.value
if isinstance(arg, (pw.Json, pw.PyObjectWrapper))
else arg
)
for arg in args
)
kwargs = {
k: (v.value if isinstance(v, (pw.Json, pw.PyObjectWrapper)) else v)
for k, v in kwargs.items()
}
result = await async_fn(*args, **kwargs)
return {HTTP_CONN_RESPONSE_KEY: result}
def table_transformer(table: pw.Table) -> pw.Table:
return FuncAsyncTransformer(input_table=table).successful
return table_transformer
server.serve(
route,
schema,
handler=func_to_transformer(callable_func),
**additional_endpoint_kwargs,
)
return callable_func
class InputSchema(pw.Schema):
question: str = pw.column_definition(
description="Your question that you want the answer to",
)
def handler(question: str, prev_node):
question_group_id = str(uuid.uuid4())
res = rag_e2e.invoke(
{
"question": question,
"question_group_id": question_group_id,
"prev_node": prev_node,
}
)
print(f"{res}")
documents = [doc.model_dump() for doc in res["documents"]]
documents_after_metada = [
doc.model_dump() for doc in res["documents_after_metadata_filter"]
]
res["documents"] = documents
res["documents_after_metadata_filter"] = documents_after_metada
return res
rest_kwargs = {"methods": ("GET", "POST")}
class FinancialStatementSchema(BaseModel):
"""
The schema for the financial statement metadata.
"""
company_name: str = Field(description="The name of the company.")
year: str = Field(description="The year of the financial statement.")
_system_prompt = prompts.extract_compamy_system_prompt
company_name_and_year_extractor_prompt = ChatPromptTemplate.from_messages(
[
("system", _system_prompt),
("human", "Text: \n\n {text}"),
]
)
company_name_and_year_extractor = (
company_name_and_year_extractor_prompt
| llm.with_structured_output(FinancialStatementSchema)
)
def extract_company_name_and_year_from_nodes(nodes) -> FinancialStatementSchema:
"""
Extracts the 'Company Name' and 'Year of Report' from a list of nodes.
"""
# Combine text from nodes to form the document content
nodes_first_three_pages = []
for node in nodes:
if len(node.bbox) > 0 and node.bbox[0].page < 3:
nodes_first_three_pages.append(node)
document_text = "\n".join(node.text for node in nodes_first_three_pages)
res = company_name_and_year_extractor.invoke({"text": document_text})
return res # type: ignore
class CustomOpenParse(OpenParse):
"""
Custom OpenParse class with modified __wrapped__ behavior.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __wrapped__(self, contents: bytes) -> list[tuple[str, dict]]:
# Import dependencies locally to handle optional imports gracefully
try:
import openparse
from pypdf import PdfReader
except ImportError as e:
raise ImportError(
"Required library not found. Please ensure openparse and pypdf are installed."
) from e
reader = PdfReader(stream=BytesIO(contents))
doc = openparse.Pdf(file=reader)
# Original document parsing with custom modifications
parsed_content = self.doc_parser.parse(doc)
nodes = list(parsed_content.nodes)
extracted_statement_schema = extract_company_name_and_year_from_nodes(
parsed_content.nodes
)
company_name = extracted_statement_schema.company_name.lower().strip()
if company_name.endswith(" inc"):
company_name = company_name.replace(" inc", "")
elif company_name.endswith(" inc."):
company_name = company_name.replace(" inc.", "")
docs = [
(
node.text,
{
"company_name": company_name,
"year": extracted_statement_schema.year.strip(),
"page_no": node.bbox[0].page if len(node.bbox) > 0 else -1,
"variant": str(node.variant),
},
)
for node in nodes
]
return docs
folder = pw.io.fs.read(
path="./data/",
format="binary",
with_metadata=True,
)
sources = [folder]
vision_llm = llms.OpenAIChat(
model="gpt-4o-mini",
cache_strategy=DiskCache(),
retry_strategy=ExponentialBackoffRetryStrategy(max_retries=4),
verbose=True,
)
TABLE_PARSE_PROMPT = prompts.TABLE_PARSE_PROMPT
parser = CustomOpenParse(
table_args={
"parsing_algorithm": "llm",
"llm": vision_llm,
"prompt": TABLE_PARSE_PROMPT,
},
parse_images=False,
cache_strategy=DiskCache(),
)
embedder = embedders.OpenAIEmbedder(
# model="text-embedding-3-large",
cache_strategy=DiskCache()
)
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s %(levelname)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
knn_index = BruteForceKnnFactory(
reserved_space=1000,
embedder=embedder,
metric=pw.engine.BruteForceKnnMetricKind.COS,
dimensions=1536,
)
bm25_index = TantivyBM25Factory(
ram_budget=5000 * 1024 * 1024, in_memory_index=False
)
# usearch_knn_index = UsearchKnnFactory(embedder=embedder)
hybrid_index_factory = HybridIndexFactory(
retriever_factories=[bm25_index, knn_index],
)
doc_store = DocumentStore(
*sources,
retriever_factory=knn_index,
splitter=None, # OpenParse parser handles the chunking
parser=parser,
)
server = DocumentStoreServer(
host=config.VECTOR_STORE_HOST,
port=config.VECTOR_STORE_PORT,
document_store=doc_store,
)
##ADDING IN SERVE CALLABLE FOR OTHER END POINTS
serve_callable(server, "/answer", InputSchema, handler, **rest_kwargs)
def run_server(server):
server.run()
while True:
process = multiprocessing.Process(target=run_server, args=(server,))
process.start()
process.join()
print("server ended...")