主题
Oracle AI Vector Search 专为人工智能(AI)工作负载设计,允许你基于语义而非关键字查询数据。Oracle AI Vector Search 的最大优势之一,是可以在单一系统中将非结构化数据的语义搜索与业务数据的关系搜索结合起来。这不仅强大,而且效果显著更好,因为无需添加专门的向量数据库,消除了多系统间数据碎片化的痛苦。
此外,你的向量还可以受益于 Oracle Database 最强大的各项功能,例如:
- 分区支持
- Real Application Clusters 可扩展性
- Exadata 智能扫描
- 跨地理分布式数据库的分片处理
- 事务
- 并行 SQL
- 灾难恢复
- 安全
- Oracle Machine Learning
- Oracle 图数据库
- Oracle Spatial and Graph
- Oracle 区块链
- JSON
本指南演示如何在 Oracle AI Vector Search 中使用 Embedding 能力,通过 OracleEmbeddings 为文档生成嵌入。
如果你刚开始使用 Oracle Database,可以考虑探索免费的 Oracle 23 AI,它提供了设置数据库环境的良好入门。在使用数据库时,通常建议避免默认使用 system 用户;相反,你可以创建自己的用户,以增强安全性和可定制性。有关创建用户的详细步骤,请参阅我们的端到端指南,其中还展示了如何在 Oracle 中设置用户。此外,理解用户权限对于有效管理数据库安全至关重要。你可以在官方的 Oracle 指南 中了解更多关于管理用户账户和安全的内容。
前提条件
请确保已安装 Oracle Python Client 驱动,以便将 llama_index 与 Oracle AI Vector Search 集成。
%pip install llama-index-embeddings-oracleai1
连接 Oracle Database
以下示例代码展示如何连接 Oracle Database。默认情况下,python-oracledb 以“Thin”模式运行,直接连接 Oracle Database。该模式不需要 Oracle Client 库。不过,当 python-oracledb 使用这些库时,可以使用一些额外功能。使用 Oracle Client 库时,python-oracledb 被称为处于“Thick”模式。两种模式都具备支持 Python Database API v2.0 规范的完整功能。请参阅以下指南,了解各模式支持的功能。如果无法使用 thin 模式,你可能需要切换到 thick 模式。
import sys
import oracledb
# Update the following variables with your Oracle database credentials and connection details
username = "<username>"
password = "<password>"
dsn = "<hostname>/<service_name>"
try:
conn = oracledb.connect(user=username, password=password, dsn=dsn)
print("Connection successful!")
except Exception as e:
print("Connection failed!")
sys.exit(1)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
对于嵌入生成,用户有多种提供商选项,包括在数据库内生成嵌入,以及使用 OcigenAI、Hugging Face 和 OpenAI 等第三方服务。选择第三方提供商的用户必须建立凭据,其中包含所需身份验证信息。或者,如果用户选择“database”作为提供商,则需要将 ONNX 模型加载到 Oracle Database 中才能生成嵌入。
加载 ONNX 模型
Oracle 支持多种嵌入提供商,用户可以在专有数据库解决方案与 OCIGENAI、HuggingFace 等第三方服务之间选择。这一选择决定了生成和管理嵌入的方法。
重要:如果用户选择 database 选项,必须将 ONNX 模型上传到 Oracle Database。相反,如果选择第三方提供商生成嵌入,则无需将 ONNX 模型上传到 Oracle Database。
直接在 Oracle 内使用 ONNX 模型的一个显著优势是,它无需将数据传输给外部方,从而提升了安全性和性能。此外,这种方法还避免了通常与网络或 REST API 调用相关的延迟。
以下是将 ONNX 模型上传到 Oracle Database 的示例代码:
from llama_index.embeddings.oracleai import OracleEmbeddings
# please update with your related information
# make sure that you have onnx file in the system
onnx_dir = "DEMO_DIR"
onnx_file = "tinybert.onnx"
model_name = "demo_model"
try:
OracleEmbeddings.load_onnx_model(conn, onnx_dir, onnx_file, model_name)
print("ONNX model loaded.")
except Exception as e:
print("ONNX model loading failed!")
sys.exit(1)1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
创建凭据
在选择第三方提供商生成嵌入时,用户需要建立凭据,以安全访问提供商的端点。
重要: 选择“database”提供商生成嵌入时无需凭据。不过,如果用户决定使用第三方提供商,则必须创建针对所选提供商的凭据。
以下是一个说明性示例:
try:
cursor = conn.cursor()
cursor.execute(
"""
declare
jo json_object_t;
begin
-- HuggingFace
dbms_vector_chain.drop_credential(credential_name => 'HF_CRED');
jo := json_object_t();
jo.put('access_token', '<access_token>');
dbms_vector_chain.create_credential(
credential_name => 'HF_CRED',
params => json(jo.to_string));
-- OCIGENAI
dbms_vector_chain.drop_credential(credential_name => 'OCI_CRED');
jo := json_object_t();
jo.put('user_ocid','<user_ocid>');
jo.put('tenancy_ocid','<tenancy_ocid>');
jo.put('compartment_ocid','<compartment_ocid>');
jo.put('private_key','<private_key>');
jo.put('fingerprint','<fingerprint>');
dbms_vector_chain.create_credential(
credential_name => 'OCI_CRED',
params => json(jo.to_string));
end;
"""
)
cursor.close()
print("Credentials created.")
except Exception as ex:
cursor.close()
raise1
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
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
生成嵌入
Oracle AI Vector Search 提供多种生成嵌入的方法,可使用本地托管的 ONNX 模型或第三方 API。有关配置这些替代方案的完整说明,请参阅 Oracle AI Vector Search 指南。
注意: 使用第三方嵌入生成提供商时,用户可能需要配置代理;使用 ONNX 模型的“database”提供商除外。
# proxy to be used when we instantiate summary and embedder object
proxy = "<proxy>"1
2
2
以下示例代码将展示如何生成嵌入:
from llama_index.embeddings.oracleai import OracleEmbeddings
"""
# using ocigenai
embedder_params = {
"provider": "ocigenai",
"credential_name": "OCI_CRED",
"url": "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/embedText",
"model": "cohere.embed-english-light-v3.0",
}
# using huggingface
embedder_params = {
"provider": "huggingface",
"credential_name": "HF_CRED",
"url": "https://api-inference.huggingface.co/pipeline/feature-extraction/",
"model": "sentence-transformers/all-MiniLM-L6-v2",
"wait_for_model": "true"
}
"""
# using ONNX model loaded to Oracle Database
embedder_params = {"provider": "database", "model": "demo_model"}
# Remove proxy if not required
embedder = OracleEmbeddings(conn=conn, params=embedder_params, proxy=proxy)
embed = embedder._get_text_embedding("Hello World!")
""" verify """
print(f"Embedding generated by OracleEmbeddings: {embed}")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
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
端到端演示
请参阅我们的完整演示指南 Oracle AI Vector Search 端到端演示指南,借助 Oracle AI Vector Search 构建端到端 RAG 流水线。