跳到主要內容
Open In ColabOpen on GitHub

Contextual AI 重新排序器

Contextual AI 的指令遵循重新排序器是世界首款重新排序器,旨在遵循關於如何根據特定標準(如新近度、來源和元數據)對文件進行優先排序的自訂指令。憑藉在 BEIR 基準測試中的卓越效能(得分 61.2,並以顯著優勢超越競爭對手),它為企業 RAG 應用程式提供前所未有的控制和準確性。

主要功能

  • 指令遵循:透過自然語言命令動態控制文件排名
  • 衝突解決:智慧處理來自多個知識來源的矛盾資訊
  • 卓越的準確性:在產業基準測試中實現最先進的效能
  • 無縫整合:RAG 管道中現有重新排序器的直接替換

此重新排序器擅長解決企業知識庫中的實際挑戰,例如優先處理最近的文件而非過時的文件,或偏好內部文件而非外部來源。

若要深入瞭解我們的指令遵循重新排序器並查看其運作範例,請造訪我們的產品概述

如需 Contextual AI 產品的完整文件,請造訪我們的開發人員入口網站

此整合需要 contextual-client Python SDK。如需更多資訊,請按一下此處

概述

此整合會調用 Contextual AI 的 Grounded Language Model。

整合詳細資訊

類別套件本地可序列化JS 支援套件下載次數套件最新版本
ContextualReranklangchain-contextualbetaPyPI - DownloadsPyPI - Version

設定

若要存取 Contextual 的重新排序器模型,您需要建立 Contextual AI 帳戶、取得 API 金鑰,並安裝 langchain-contextual 整合套件。

憑證

前往 app.contextual.ai 註冊 Contextual 並產生 API 金鑰。完成後,設定 CONTEXTUAL_AI_API_KEY 環境變數

import getpass
import os

if not os.getenv("CONTEXTUAL_AI_API_KEY"):
os.environ["CONTEXTUAL_AI_API_KEY"] = getpass.getpass(
"Enter your Contextual API key: "
)

安裝

LangChain Contextual 整合位於 langchain-contextual 套件中

%pip install -qU langchain-contextual

例項化

Contextual 重新排序器的引數為

參數類型描述
documentslist[Document]要重新排序的文件序列。文件中包含的任何元數據也將用於重新排序。
querystr用於重新排序的查詢。
modelstr要使用的重新排序器版本。目前,我們只有「ctxl-rerank-en-v1-instruct」。
top_nOptional[int]要傳回的結果數。如果為 None,則傳回所有結果。預設為 self.top_n。
instructionOptional[str]用於重新排序器的指令。
callbacksOptional[Callbacks]在壓縮過程中執行的回呼。
from langchain_contextual import ContextualRerank

api_key = ""
model = "ctxl-rerank-en-v1-instruct"

compressor = ContextualRerank(
model=model,
api_key=api_key,
)

用法

首先,我們將設定將使用的全域變數和範例,並例項化重新排序器用戶端。

from langchain_core.documents import Document

query = "What is the current enterprise pricing for the RTX 5090 GPU for bulk orders?"
instruction = "Prioritize internal sales documents over market analysis reports. More recent documents should be weighted higher. Enterprise portal content supersedes distributor communications."

document_contents = [
"Following detailed cost analysis and market research, we have implemented the following changes: AI training clusters will see a 15% uplift in raw compute performance, enterprise support packages are being restructured, and bulk procurement programs (100+ units) for the RTX 5090 Enterprise series will operate on a $2,899 baseline.",
"Enterprise pricing for the RTX 5090 GPU bulk orders (100+ units) is currently set at $3,100-$3,300 per unit. This pricing for RTX 5090 enterprise bulk orders has been confirmed across all major distribution channels.",
"RTX 5090 Enterprise GPU requires 450W TDP and 20% cooling overhead.",
]

metadata = [
{
"Date": "January 15, 2025",
"Source": "NVIDIA Enterprise Sales Portal",
"Classification": "Internal Use Only",
},
{"Date": "11/30/2023", "Source": "TechAnalytics Research Group"},
{
"Date": "January 25, 2025",
"Source": "NVIDIA Enterprise Sales Portal",
"Classification": "Internal Use Only",
},
]

documents = [
Document(page_content=content, metadata=metadata[i])
for i, content in enumerate(document_contents)
]
reranked_documents = compressor.compress_documents(
query=query,
instruction=instruction,
documents=documents,
)
API 參考:Document

在鏈中使用

範例即將推出。

API 參考

如需所有 ChatContextual 功能和組態的詳細文件,請前往 Github 頁面:https://github.com/ContextualAI//langchain-contextual


此頁面是否對您有幫助?