Oracle AI 向量搜尋:產生摘要
Oracle AI 向量搜尋專為人工智慧 (AI) 工作負載設計,讓您可以根據語意而非關鍵字查詢資料。Oracle AI 向量搜尋的最大優勢之一是,非結構化資料的語意搜尋可以與單一系統中業務資料的關聯式搜尋結合。這不僅功能強大,而且效率更高,因為您無需新增專門的向量資料庫,從而消除了多個系統之間資料分散的痛苦。
此外,您的向量可以受益於 Oracle Database 最強大的所有功能,例如以下功能:
- 分割支援
- Real Application Clusters 可擴展性
- Exadata 智慧掃描
- 跨地理分散式資料庫的分片處理
- 交易
- 平行 SQL
- 災難復原
- 安全性
- Oracle Machine Learning
- Oracle Graph Database
- Oracle Spatial and Graph
- Oracle Blockchain
- JSON
本指南示範如何在 Oracle AI 向量搜尋中使用摘要功能,以使用 OracleSummary 為您的文件產生摘要。
如果您剛開始使用 Oracle Database,請考慮探索免費的 Oracle 23 AI,它為設定資料庫環境提供了絕佳的入門介紹。在使用資料庫時,通常建議避免預設使用系統使用者;相反地,您可以建立自己的使用者以增強安全性和自訂性。如需使用者建立的詳細步驟,請參閱我們的端對端指南,其中也示範如何在 Oracle 中設定使用者。此外,了解使用者權限對於有效地管理資料庫安全性至關重要。您可以在官方 Oracle 指南中,深入了解有關管理使用者帳戶和安全性的主題。
先決條件
請安裝 Oracle Python Client 驅動程式,以將 Langchain 與 Oracle AI 向量搜尋搭配使用。
# pip install oracledb
連線到 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
# please update with your username, password, hostname and service_name
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)
產生摘要
Oracle AI 向量搜尋 Langchain 程式庫提供了一套 API,專為文件摘要而設計。它支援多個摘要供應商,例如 Database、OCIGENAI、HuggingFace 等,讓使用者可以選擇最符合其需求的供應商。若要使用這些功能,使用者必須設定指定的摘要參數。如需這些參數的詳細資訊,請參閱Oracle AI 向量搜尋指南。
注意: 如果使用者想要使用 Oracle 內部和預設供應商「database」以外的某些第三方摘要產生供應商,則可能需要設定 Proxy。如果您沒有 Proxy,請在例項化 OracleSummary 時移除 proxy 參數。
# proxy to be used when we instantiate summary and embedder object
proxy = "<proxy>"
以下範例程式碼將示範如何產生摘要
from langchain_community.utilities.oracleai import OracleSummary
from langchain_core.documents import Document
"""
# using 'ocigenai' provider
summary_params = {
"provider": "ocigenai",
"credential_name": "OCI_CRED",
"url": "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/summarizeText",
"model": "cohere.command",
}
# using 'huggingface' provider
summary_params = {
"provider": "huggingface",
"credential_name": "HF_CRED",
"url": "https://api-inference.huggingface.co/models/",
"model": "facebook/bart-large-cnn",
"wait_for_model": "true"
}
"""
# using 'database' provider
summary_params = {
"provider": "database",
"glevel": "S",
"numParagraphs": 1,
"language": "english",
}
# get the summary instance
# Remove proxy if not required
summ = OracleSummary(conn=conn, params=summary_params, proxy=proxy)
summary = summ.get_summary(
"In the heart of the forest, "
+ "a lone fox ventured out at dusk, seeking a lost treasure. "
+ "With each step, memories flooded back, guiding its path. "
+ "As the moon rose high, illuminating the night, the fox unearthed "
+ "not gold, but a forgotten friendship, worth more than any riches."
)
print(f"Summary generated by OracleSummary: {summary}")
端對端示範
請參閱我們完整的示範指南Oracle AI 向量搜尋端對端示範指南,以在 Oracle AI 向量搜尋的協助下,建置端對端 RAG 管道。