Python使用Oracle向量數(shù)據(jù)庫實現(xiàn)文本檢索系統(tǒng)
代碼分析
讓我們逐步分析原始代碼的主要組件和功能:
導(dǎo)入必要的庫:
- 使用
oracledb連接Oracle數(shù)據(jù)庫 - 使用
numpy處理向量 - 使用
pydantic進(jìn)行配置驗證 - 使用
flask和redis進(jìn)行Web應(yīng)用程序集成
- 使用
定義
OracleVectorConfig類:- 使用Pydantic模型驗證Oracle連接配置
創(chuàng)建
OracleVector類:- 實現(xiàn)向量數(shù)據(jù)庫的核心功能
- 使用
contextmanager管理數(shù)據(jù)庫連接 - 實現(xiàn)CRUD操作和向量搜索
實現(xiàn)
OracleVectorFactory類:- 用于初始化向量數(shù)據(jù)庫實例
現(xiàn)在,讓我們基于相同的技術(shù)創(chuàng)建一個新的示例代碼:
import array
import json
import uuid
from contextlib import contextmanager
from typing import List, Dict, Any
import numpy as np
import oracledb
from pydantic import BaseModel, validator
class OracleConfig(BaseModel):
host: str
port: int
user: str
password: str
database: str
@validator('host', 'user', 'password', 'database')
def check_not_empty(cls, v):
if not v:
raise ValueError("Field cannot be empty")
return v
class TextEmbeddingStore:
def __init__(self, config: OracleConfig):
self.pool = self._create_connection_pool(config)
self.table_name = "text_embeddings"
self._create_table()
def _create_connection_pool(self, config: OracleConfig):
return oracledb.create_pool(
user=config.user,
password=config.password,
dsn=f"{config.host}:{config.port}/{config.database}",
min=1,
max=5,
increment=1
)
@contextmanager
def _get_cursor(self):
conn = self.pool.acquire()
conn.inputtypehandler = self._input_type_handler
conn.outputtypehandler = self._output_type_handler
cur = conn.cursor()
try:
yield cur
finally:
cur.close()
conn.commit()
conn.close()
def _input_type_handler(self, cursor, value, arraysize):
if isinstance(value, np.ndarray):
return cursor.var(
oracledb.DB_TYPE_VECTOR,
arraysize=arraysize,
inconverter=self._numpy_to_array
)
def _output_type_handler(self, cursor, metadata):
if metadata.type_code is oracledb.DB_TYPE_VECTOR:
return cursor.var(
metadata.type_code,
arraysize=cursor.arraysize,
outconverter=self._array_to_numpy
)
def _numpy_to_array(self, value):
return array.array('f', value)
def _array_to_numpy(self, value):
return np.array(value, dtype=np.float32)
def _create_table(self):
with self._get_cursor() as cur:
cur.execute(f"""
CREATE TABLE IF NOT EXISTS {self.table_name} (
id VARCHAR2(100) PRIMARY KEY,
text CLOB NOT NULL,
metadata JSON,
embedding VECTOR NOT NULL
)
""")
def add_texts(self, texts: List[str], embeddings: List[List[float]], metadata: List[Dict] = None):
if metadata is None:
metadata = [{} for _ in texts]
values = [
(str(uuid.uuid4()), text, json.dumps(meta), np.array(emb, dtype=np.float32))
for text, emb, meta in zip(texts, embeddings, metadata)
]
with self._get_cursor() as cur:
cur.executemany(
f"INSERT INTO {self.table_name} (id, text, metadata, embedding) VALUES (:1, :2, :3, :4)",
values
)
def search_similar(self, query_vector: List[float], top_k: int = 5) -> List[Dict[str, Any]]:
query_vector = np.array(query_vector, dtype=np.float32)
with self._get_cursor() as cur:
cur.execute(
f"""
SELECT id, text, metadata, vector_distance(embedding, :1) AS distance
FROM {self.table_name}
ORDER BY distance
FETCH FIRST :2 ROWS ONLY
""",
[query_vector, top_k]
)
results = []
for id, text, metadata, distance in cur:
results.append({
"id": id,
"text": text,
"metadata": json.loads(metadata),
"distance": distance,
"similarity": 1 - distance
})
return results
# 使用示例
if __name__ == "__main__":
config = OracleConfig(
host="localhost",
port=1521,
user="your_username",
password="your_password",
database="your_database"
)
store = TextEmbeddingStore(config)
# 添加文本和嵌入
texts = ["Hello world", "Python programming", "Vector database"]
embeddings = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]]
store.add_texts(texts, embeddings)
# 搜索相似文本
query_vector = [0.2, 0.3, 0.4]
results = store.search_similar(query_vector, top_k=2)
for result in results:
print(f"Text: {result['text']}")
print(f"Similarity: {result['similarity']:.4f}")
print("---")
這個新的示例代碼實現(xiàn)了一個簡化版的文本嵌入存儲系統(tǒng),使用Oracle向量數(shù)據(jù)庫。它包含以下主要功能:
- 使用Pydantic進(jìn)行配置驗證
- 創(chuàng)建和管理Oracle連接池
- 使用上下文管理器處理數(shù)據(jù)庫連接
- 處理numpy數(shù)組和Oracle向量類型之間的轉(zhuǎn)換
- 實現(xiàn)添加文本和嵌入的方法
- 實現(xiàn)基于向量相似度的搜索方法
這個示例展示了如何使用Oracle向量數(shù)據(jù)庫來存儲和檢索文本嵌入,可以作為構(gòu)建更復(fù)雜的文本檢索或推薦系統(tǒng)的基礎(chǔ)。
在實際應(yīng)用中,你可能需要添加錯誤處理、日志記錄、性能優(yōu)化等功能。
到此這篇關(guān)于Python使用Oracle向量數(shù)據(jù)庫實現(xiàn)文本檢索系統(tǒng)的文章就介紹到這了,更多相關(guān)Python Oracle文本檢索系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 實現(xiàn)rolling和apply函數(shù)的向下取值操作
這篇文章主要介紹了python 實現(xiàn)rolling和apply函數(shù)的向下取值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python PyQt5中彈出子窗口解決子窗口一閃而過的問題
這篇文章主要介紹了Python PyQt5中彈出子窗口解決子窗口一閃而過的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
pytorch保存和加載模型的方法及如何load部分參數(shù)
本文總結(jié)了pytorch中保存和加載模型的方法,以及在保存的模型文件與新定義的模型的參數(shù)不一一對應(yīng)時,我們該如何加載模型參數(shù),對pytorch保存和加載模型相關(guān)知識感興趣的朋友一起看看吧2024-03-03
TensorFlow2.4完成Word2vec詞嵌入訓(xùn)練方法詳解
這篇文章主要為大家介紹了TensorFlow2.4完成Word2vec詞嵌入訓(xùn)練方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
python中如何用time方法生成當(dāng)前時間年月日時分秒
這篇文章主要給大家介紹了關(guān)于python中如何用time方法生成當(dāng)前時間年月日時分秒的相關(guān)資料,在Python中與時間處理有關(guān)的模塊就包括:time,datetime以及calendar,Time模塊用以取得系統(tǒng)時間相關(guān)的信息和時間的格式化等操作,需要的朋友可以參考下2023-08-08
Python內(nèi)置庫之webbrowser模塊用法詳解
webbrowser模塊是Python自帶的標(biāo)準(zhǔn)庫,無需安裝,可以直接在Python中使用該模塊來打開網(wǎng)頁、PDF文件等,本文給大家詳細(xì)介紹了Python webbrowser模塊用法,需要的朋友可以參考下2023-08-08

