-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoracle_vector_db_lc.py
452 lines (358 loc) · 13.3 KB
/
oracle_vector_db_lc.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
"""
File name: oracle_vector_db_lc.py
Author: Luigi Saetta
Date created: 2024-01-17
Date last modified: 2024-05-03
Python Version: 3.9
Description:
This module provides the class to integrate Oracle
DB Vector Store in LangChain.
This version uses only one table (chunks + vecs)
Inspired by:
Usage:
Import this module into other scripts to use its functions.
Example:
from oracle_vector_db_lc import OracleVectorStore
v_store = OracleVectorStore(embedding=embed_model,
verbose=True)
License:
This code is released under the MIT License.
Notes:
This is a part of a set of demo showing how to use Oracle Vector DB,
OCI GenAI service, Oracle GenAI Embeddings, to build a RAG solution,
where all the data (text + embeddings) are stored in Oracle DB 23c
Modified (25/02) to pass the Embed model and not the fuction
Warnings:
This module is in development, may change in future versions.
"""
from __future__ import annotations
import time
import array
import logging
import traceback
from typing import (
Any,
Callable,
Iterable,
List,
Optional,
Type,
TypeVar,
)
import numpy as np
from tqdm.auto import tqdm
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_core.vectorstores import VectorStore
import oracledb
# load configs from here
from config_private import DB_USER, DB_PWD, DB_HOST_IP, DB_SERVICE
# 64 or 32
from config import EMBEDDINGS_BITS, TOP_K, VERBOSE
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
VST = TypeVar("VST", bound="VectorStore")
#
# supporting functions
#
def make_dsn():
"""
create the DSN
"""
dsn = f"{DB_HOST_IP}/{DB_SERVICE}"
return dsn
def get_type_from_bits():
"""
set type
"""
the_type = "d" if EMBEDDINGS_BITS == 64 else "f"
return the_type
def oracle_query(
embed_query: List[float], collection_name: str, top_k: int = TOP_K, verbose=VERBOSE
) -> List[Document]:
"""
Executes a query against an Oracle database to find the top_k closest vectors
to the given embedding.
History:
23/12/2023: modified to return some metadata (ref)
Args:
embed_query (List[float]): A list of floats representing the query vector embedding.
top_k (int, optional): The number of closest vectors to retrieve. Defaults to TOP_K.
verbose (bool, optional): If set to True, additional information about the query
and execution time will be printed. Defaults to False.
Returns:
List[Document]
"""
t_start = time.time()
# build the DSN from data taken from config.py
dsn = make_dsn()
try:
with oracledb.connect(user=DB_USER, password=DB_PWD, dsn=dsn) as connection:
with connection.cursor() as cursor:
# 'f' single precision 'd' double precision
btype = get_type_from_bits()
array_query = array.array(btype, embed_query)
# changed select adding books (29/12/2023)
select = f"""select C.id, C.CHUNK, C.REF, C.PAG,
VECTOR_DISTANCE(C.VEC, :1, COSINE) as d
from {collection_name} C
order by d
FETCH FIRST {top_k} ROWS ONLY"""
if verbose:
logging.info("select: %s", select)
cursor.execute(select, [array_query])
rows = cursor.fetchall()
result_docs = []
node_ids = []
similarities = []
# prepare output
for row in rows:
clob_pointer = row[1]
full_clob_data = clob_pointer.read()
# 29/12: added book_name to metadata
# 03/03: added pag
result_docs.append(
# pack in the expected format
Document(
page_content=full_clob_data,
metadata={"source": row[2], "page": row[3]},
)
)
# not used, for now
node_ids.append(row[0])
similarities.append(row[4])
except Exception as e:
logging.error("Error occurred in oracle_query: %s", e)
return None
t_elapsed = time.time() - t_start
if verbose:
logging.info("Query duration: %s sec.", round(t_elapsed, 1))
return result_docs
#
# OracleVectorStore
#
class OracleVectorStore(VectorStore):
"""
PreGA implementation of Oracle Vector Store
"""
# To avoid problems with OCI GenAI Embeddings
# where Cohere has a limit on 96
_BATCH_SIZE = 90
_DEFAULT_COLLECTION_NAME = "CHUNKS_VECTORS"
def __init__(
self,
embedding: Embeddings,
*,
collection_name: str = _DEFAULT_COLLECTION_NAME,
relevance_score_fn: Optional[Callable[[float], float]] = None,
verbose: Optional[bool] = False,
) -> None:
# the name for the Oracle DB table
self.collection_name = collection_name
self._embedding_model = embedding
self.override_relevance_score_fn = relevance_score_fn
self.verbose = verbose
def add_texts(
self,
texts: Iterable[str],
metadatas: Optional[List[dict]] = None,
**kwargs: Any,
) -> List[str]:
"""Run more texts through the embeddings and add to the vectorstore.
Args:
texts: Iterable of strings to add to the vectorstore.
embedding: Text embedding model to use.
metadatas: Optional list of metadatas associated with the texts.
kwargs: vectorstore specific parameters
Returns:
List of ids from adding the texts into the vectorstore.
"""
raise NotImplementedError("add_texts method must be implemented...")
@property
def embeddings(self) -> Embeddings:
return self._embedding_model
#
# similarity_search
#
def similarity_search(
self, query: str, k: int = TOP_K, **kwargs: Any
) -> List[Document]:
"""
Return docs most similar to query.
"""
if self.verbose:
logging.info("top_k: %s", k)
logging.info("")
# 1. embed the query
# maybe here we should cast to double if 64 bits
embed_query = self._embedding_model.embed_query(query)
# 2. invoke oracle_query, return List[Document]
result_docs = oracle_query(
embed_query=embed_query,
collection_name=self.collection_name,
top_k=k,
verbose=self.verbose,
)
return result_docs
#
# This function enable to load a table from scratch, with
# texts and embeddings... then you can query
#
@classmethod
def from_documents(
cls: Type[OracleVectorStore],
documents: List[Document],
embedding: Embeddings,
# the name for Oracle DB table
collection_name: str,
verbose=False,
**kwargs: Any,
) -> OracleVectorStore:
"""Return VectorStore initialized from documents and embeddings.
This is a user-friendly interface that:
1. Embeds documents.
2. Adds the documents to a provided Oracle DB with AI
Vetor Search
This is intended to be a quick way to get started.
"""
texts = [d.page_content for d in documents]
metadatas = [d.metadata for d in documents]
refs = [metadata["source"] for metadata in metadatas]
pages = [metadata["page"] for metadata in metadatas]
# compute embeddings
# here we use correctly embed_documents
# (26/02) I'll handle directly inside here the batching
logging.info("Compute embeddings...")
batch_size = cls._BATCH_SIZE
if len(texts) > batch_size:
embeddings = []
# do in batch
for i in tqdm(range(0, len(texts), batch_size)):
batch = texts[i : i + batch_size]
# here we compute embeddings for a batch
embeddings_batch = embedding.embed_documents(batch)
# add to the final list
embeddings.extend(embeddings_batch)
else:
# single batch
embeddings = embedding.embed_documents(texts)
embeddings = np.array(embeddings)
#
# save in db
#
tot_errors = 0
dsn = make_dsn()
with oracledb.connect(user=DB_USER, password=DB_PWD, dsn=dsn) as connection:
with connection.cursor() as cursor:
logging.info("Saving texts, embeddings to DB...")
rec_id = 0
for vector, chunk, ref, pag in zip(
tqdm(embeddings), texts, refs, pages
):
rec_id += 1
# 'f' single precision 'd' double precision
btype = get_type_from_bits()
input_array = array.array(btype, vector)
try:
# insert single row
cursor.execute(
f"""insert into {collection_name} (ID, CHUNK, VEC, REF, PAG)
values (:1, :2, :3, :4, :5)""",
[rec_id, chunk, input_array, ref, pag],
)
except oracledb.DatabaseError as e:
logging.error(
"Database error occurred while saving embeddings:"
)
logging.error("Error message: %s", e)
logging.error("SQL Query: %s", cursor.statement)
logging.error(
"Parameters: %s %s %s %s %s",
rec_id,
chunk,
input_array,
ref,
pag,
)
# Optionally roll back transaction here if necessary
tot_errors += 1
except oracledb.Error as e:
logging.error(
"An unexpected error occurred while saving embeddings:"
)
logging.error("Error message: %s", e)
logging.error(traceback.format_exc())
tot_errors += 1
connection.commit()
logging.info("Tot. errors in save_embeddings: %s", tot_errors)
# beware: here we're passing the model... this cls can be
# after used for query
return cls(
embedding=embedding, collection_name=collection_name, verbose=verbose
)
@classmethod
def from_texts(
cls: Type[OracleVectorStore],
texts: List[str],
embedding: Embeddings,
metadatas: Optional[List[dict]] = None,
**kwargs: Any,
) -> OracleVectorStore:
"""Return VectorStore initialized from texts and embeddings."""
raise NotImplementedError("from_texts method must be implemented...")
@classmethod
def test_connection(cls):
"""
Test the connection to the DB
"""
dsn = make_dsn()
try:
with oracledb.connect(user=DB_USER, password=DB_PWD, dsn=dsn) as connection:
logging.info("Successfully connected !!!")
connection.close()
except oracledb.DatabaseError as e:
logging.error("Error in test connection!")
logging.error("Error message: %s", e)
@classmethod
def create_collection(cls, collection_name: str):
""" "
Create the table in the DB
"""
dsn = make_dsn()
with oracledb.connect(user=DB_USER, password=DB_PWD, dsn=dsn) as connection:
with connection.cursor() as cursor:
create_sql = f"""create table {collection_name}
(ID NUMBER NOT NULL,
CHUNK CLOB,
VEC VECTOR(1024, FLOAT32),
REF VARCHAR2(1000),
PAG NUMBER,
PRIMARY KEY ("ID")
)
"""
sql = f"""
BEGIN
execute immediate '{create_sql}';
END;
"""
cursor.execute(sql)
logging.info("%s created!!!", collection_name)
@classmethod
def drop_collection(cls, collection_name: str):
""" "
Drop the table in the DB
this is an utility function to clean the table during tests
be aware it deletes all the record in the mentioned table
"""
dsn = make_dsn()
with oracledb.connect(user=DB_USER, password=DB_PWD, dsn=dsn) as connection:
with connection.cursor() as cursor:
sql = f"""
BEGIN
execute immediate 'drop table {collection_name}';
END;
"""
cursor.execute(sql)
logging.info("%s dropped!!!", collection_name)