昇腾社区首页
中文
注册

类功能

功能描述

提供OpenGauss知识数据库,主要存储切分后的chunk信息。

函数原型

from mx_rag.storage.document_store import OpenGaussDocstore
OpenGaussDocstore(engine, encrypt_fn, decrypt_fn, enable_bm25, index_name)

输入参数说明

参数名

数据类型

可选/必选

说明

engine

Engine

必选

Engine实例。

encrypt_fn

Callable

可选

回调方法,对ChunkModel类的chunk内容加密,输出为字符串。add保存时,数据库保存的是chunk字段经过encrypt_fn方法处理后的数据。

须知:

如果上传的文档涉及银行卡号、身份证号、护照号、口令等个人数据,请配置该参数保证个人数据安全。

decrypt_fn

Callable

可选

回调方法,对ChunkModel类的chunk内容解密,输出为字符串。search返回时,返回的是chunk字段经过decrypt_fn方法处理后的数据。

enable_bm25

bool

可选

配置数据库是否支持bm25稀疏检索,若此参数配置为False,则全文检索功能不可用(full_text_search方法始终返回[ ]),默认为True。

index_name

str

可选

创建的bm25检索的名称,需要满足正则表达式^[a-zA-Z0-9_-]{6,64}$,即只能由大小写字母、数字、下划线组成,且长度为6-64,默认为"chunks_content_bm25"。

调用示例

from sqlalchemy import URL, create_engine
from mx_rag.storage.document_store import MxDocument, OpenGaussDocstore
def encrypt_fn(value):
    # 安全的加密方法
    return value
def decrypt_fn(value):
    # 安全的解密方法
    return value
username = "<username>"
password = "<your password>"
host = "<host>"
port = "<port>"
database = "database"
url = URL.create(
   "opengauss+psycopg2",
   username=username,
   password=password,
   host=host,
   port=port,
   database=database
)
engine = create_engine(url)
chunk_store = OpenGaussDocstore(engine=engine, encrypt_fn=encrypt_fn, decrypt_fn=decrypt_fn)
texts = ["示例", "文本"]
metadatas = [{} for _ in texts]
doc = [MxDocument(page_content=t, metadata=m, document_name="1.docx") for t, m in zip(texts, metadatas)]
document_id = 1
chunk_store.add(doc, document_id)
idx = chunk_store.get_all_chunk_id()
document = chunk_store.search(idx[0])
print(document.page_content)
print(chunk_store.full_text_search("文本", filter_dict={"document_id": [0]}))
print(chunk_store.full_text_search("文本", filter_dict={"document_id": [document_id]}))
chunk_store.update(idx[:2], ["text1", "text2"])
print(chunk_store.delete(document_id))
chunk_store.search_by_document_id(document_id)