跳到主要內容
Open In ColabOpen on GitHub

Oracle AI 向量搜尋:產生嵌入向量

Oracle AI 向量搜尋專為人工智慧 (AI) 工作負載設計,讓您可以根據語意而非關鍵字查詢資料。Oracle AI 向量搜尋最大的優勢之一,是能夠在單一系統中將非結構化資料的語意搜尋與業務資料的關聯式搜尋結合。這不僅功能強大,而且更有效率,因為您不需要新增專門的向量資料庫,從而消除了多個系統之間資料分散的困擾。

此外,您的向量可以受益於 Oracle 資料庫所有最強大的功能,例如以下各項

本指南示範如何使用 Oracle AI 向量搜尋中的嵌入功能,透過 OracleEmbeddings 為您的文件產生嵌入向量。

如果您剛開始使用 Oracle 資料庫,請考慮探索免費的 Oracle 23 AI,它為設定資料庫環境提供了絕佳的入門介紹。在使用資料庫時,通常建議避免預設使用系統使用者;相反地,您可以建立自己的使用者以增強安全性和自訂性。如需使用者建立的詳細步驟,請參閱我們的端對端指南,其中也說明如何在 Oracle 中設定使用者。此外,了解使用者權限對於有效管理資料庫安全性至關重要。您可以在官方 Oracle 指南中,找到關於管理使用者帳戶和安全性的更多資訊。

先決條件

請確保您已安裝 Oracle Python Client 驅動程式,以利 Langchain 與 Oracle AI 向量搜尋的整合。

# pip install oracledb

連線至 Oracle 資料庫

以下範例程式碼將示範如何連線至 Oracle 資料庫。預設情況下,python-oracledb 以「Thin」模式執行,直接連線至 Oracle 資料庫。此模式不需要 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)

為了產生嵌入向量,使用者可以使用多種供應商選項,包括在資料庫中產生嵌入向量,以及諸如 OcigenAI、Hugging Face 和 OpenAI 等第三方服務。選擇第三方供應商的使用者必須建立包含必要驗證資訊的憑證。或者,如果使用者選擇「資料庫」作為其供應商,則需要將 ONNX 模型載入 Oracle 資料庫以利嵌入。

載入 ONNX 模型

Oracle 容納各種嵌入供應商,讓使用者可以在專有的資料庫解決方案和第三方服務(例如 OCIGENAI 和 HuggingFace)之間進行選擇。此選擇決定了產生和管理嵌入向量的方法。

重要事項:如果使用者選擇資料庫選項,則必須將 ONNX 模型上傳到 Oracle 資料庫。相反地,如果選擇第三方供應商來產生嵌入向量,則不需要將 ONNX 模型上傳到 Oracle 資料庫。

直接在 Oracle 中使用 ONNX 模型的一個顯著優勢是,它透過消除將資料傳輸到外部方的需求,提供了更高的安全性和效能。此外,此方法避免了通常與網路或 REST API 呼叫相關聯的延遲。

以下是將 ONNX 模型上傳到 Oracle 資料庫的範例程式碼

from langchain_community.embeddings.oracleai import OracleEmbeddings

# Update the directory and file names for your ONNX model
# 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)
API 參考:OracleEmbeddings

建立憑證

當選擇第三方供應商來產生嵌入向量時,使用者需要建立憑證,以安全地存取供應商的端點。

重要事項: 當選擇「資料庫」供應商來產生嵌入向量時,不需要憑證。但是,如果使用者決定使用第三方供應商,則必須建立特定於所選供應商的憑證。

以下是一個說明範例

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()
raise

產生嵌入向量

Oracle AI 向量搜尋提供多種產生嵌入向量的方法,可使用本機託管的 ONNX 模型或第三方 API。如需設定這些替代方案的完整說明,請參閱Oracle AI 向量搜尋指南

注意: 使用者可能需要設定 Proxy 才能使用第三方嵌入向量產生供應商,但不包括使用 ONNX 模型的「資料庫」供應商。

# proxy to be used when we instantiate summary and embedder object
proxy = "<proxy>"

以下範例程式碼將示範如何產生嵌入向量

from langchain_community.embeddings.oracleai import OracleEmbeddings
from langchain_core.documents import Document

"""
# 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"}

# If a proxy is not required for your environment, you can omit the 'proxy' parameter below
embedder = OracleEmbeddings(conn=conn, params=embedder_params, proxy=proxy)
embed = embedder.embed_query("Hello World!")

""" verify """
print(f"Embedding generated by OracleEmbeddings: {embed}")
API 參考:OracleEmbeddings | Document

端對端示範

請參閱我們的完整示範指南 Oracle AI 向量搜尋端對端示範指南,以在 Oracle AI 向量搜尋的協助下建置端對端 RAG 管道。


此頁面是否對您有幫助?