Oracle AI 数据库通过将非结构化内容的语义搜索与业务数据的关联查询结合在单一系统中,支持以文档为中心的 AI 工作流。这使得构建检索工作流(如 RAG)变得更加容易,无需将数据和向量分散到多个数据库中。
在本指南中,您将学习如何:
- - 从 Python 连接到 Oracle 数据库
- - 使用以下方式加载文档
OracleDocLoader(从数据库表、文件或目录中) - - 使用以下工具分块内容
OracleTextSplitter以准备生成嵌入和检索
> **为什么选择数据库内文档处理?** > 您可以将 Oracle 数据库的功能(包括安全性、事务、可扩展性和高可用性)应用到加载、分块和存储用于 AI 搜索和检索内容的同一管道中。
如果您刚开始使用 Oracle 数据库,建议探索 免费 Oracle 26 AI,它提供了一种简单的设置方法。在使用数据库时,通常建议避免使用 SYSTEM 用户进行应用程序工作负载;而是创建具有最小所需权限的专用用户。关于端到端设置演练,请参阅 Oracle AI 向量搜索演示笔记本。关于用户管理的背景信息,请参阅官方 Oracle 指南.
前提条件
安装 langchain-oracledb。 python-oracledb 驱动程序将作为依赖项自动安装。
pip install -qU langchain-oracledb
uv add langchain-oracledb
连接到 Oracle 数据库
以下示例代码将展示如何连接到 Oracle 数据库。默认情况下,python-oracledb 以“精简”模式运行,直接连接到 Oracle 数据库。此模式不需要 Oracle Client 库。但是,当 python-oracledb 使用 Oracle Client 库时,某些额外功能可用。当使用 Oracle Client 库时,Python-oracledb 被称为处于“厚”模式。两种模式都全面支持 Python 数据库 API v2.0 规范。请参阅以下 指南 ,了解每种模式支持的功能。如果您无法使用精简模式,可能需要切换到厚模式。
# Please update with your username, password, hostname, port and service_name
username = "<username>"
password = "<password>"
dsn = "<hostname>:<port>/<service_name>"
connection = oracledb.connect(user=username, password=password, dsn=dsn)
print("Connection successful!")
现在让我们创建一个表并插入一些示例文档进行测试。
try:
cursor = conn.cursor()
drop_table_sql = """drop table if exists demo_tab"""
cursor.execute(drop_table_sql)
create_table_sql = """create table demo_tab (id number, data clob)"""
cursor.execute(create_table_sql)
insert_row_sql = """insert into demo_tab values (:1, :2)"""
rows_to_insert = [
(
1,
"If the answer to any preceding questions is yes, then the database stops the search and allocates space from the specified tablespace; otherwise, space is allocated from the database default shared temporary tablespace.",
),
(
2,
"A tablespace can be online (accessible) or offline (not accessible) whenever the database is open.\nA tablespace is usually online so that its data is available to users. The SYSTEM tablespace and temporary tablespaces cannot be taken offline.",
),
(
3,
"The database stores LOBs differently from other data types. Creating a LOB column implicitly creates a LOB segment and a LOB index. The tablespace containing the LOB segment and LOB index, which are always stored together, may be different from the tablespace containing the table.\nSometimes the database can store small amounts of LOB data in the table itself rather than in a separate LOB segment.",
),
]
cursor.executemany(insert_row_sql, rows_to_insert)
conn.commit()
print("Table created and populated.")
cursor.close()
except Exception as e:
print("Table creation failed.")
cursor.close()
conn.close()
sys.exit(1)
加载文档
用户可以灵活地从 Oracle 数据库、文件系统或两者加载文档,只需适当配置加载器参数。有关这些参数的详细信息,请参阅 Oracle AI 向量搜索指南.
使用 OracleDocLoader 的一个重要优势是它能够处理 150 多种不同的文件格式,无需为不同文档类型使用多个加载器。有关支持格式的完整列表,请参阅 Oracle Text 支持的文档格式.
以下是示例代码片段,演示如何使用 OracleDocLoader:
from langchain_oracledb.document_loaders.oracleai import OracleDocLoader
from langchain_core.documents import Document
"""
# loading a local file
loader_params = {}
loader_params["file"] = "<file>"
# loading from a local directory
loader_params = {}
loader_params["dir"] = "<directory>"
"""
# loading from Oracle Database table
loader_params = {
"owner": "<owner>",
"tablename": "demo_tab",
"colname": "data",
}
""" load the docs """
loader = OracleDocLoader(conn=conn, params=loader_params)
docs = loader.load()
""" verify """
print(f"Number of docs loaded: {len(docs)}")
# print(f"Document-0: {docs[0].page_content}") # content
拆分文档
文档大小可能有所不同,从小型到大型不等。用户通常更喜欢将文档分块成较小的部分,以方便生成嵌入。此拆分过程有多种自定义选项可用。有关这些参数的详细信息,请参阅 Oracle AI 向量搜索指南.
以下是一个示例代码,展示了如何实现此功能:
from langchain_oracledb.document_loaders.oracleai import OracleTextSplitter
from langchain_core.documents import Document
"""
# Some examples
# split by chars, max 500 chars
splitter_params = {"split": "chars", "max": 500, "normalize": "all"}
# split by words, max 100 words
splitter_params = {"split": "words", "max": 100, "normalize": "all"}
# split by sentence, max 20 sentences
splitter_params = {"split": "sentence", "max": 20, "normalize": "all"}
"""
# split by default parameters
splitter_params = {"normalize": "all"}
# get the splitter instance
splitter = OracleTextSplitter(conn=conn, params=splitter_params)
list_chunks = []
for doc in docs:
chunks = splitter.split_text(doc.page_content)
list_chunks.extend(chunks)
""" verify """
print(f"Number of Chunks: {len(list_chunks)}")
# print(f"Chunk-0: {list_chunks[0]}") # content
端到端演示
请参阅我们完整的演示指南 Oracle AI 向量搜索端到端演示指南 以在 Oracle AI 向量搜索的帮助下构建端到端 RAG 管道。