Contextual AI 的指令遵循重排序器是全球首个旨在遵循自定义指令的重排序器,可根据特定标准(如时效性、来源和元数据)优先排序文档。在 BEIR 基准测试中表现优异(得分 61.2,大幅超越竞争对手),为 企业 RAG 应用提供前所未有的控制力和准确性。
主要功能
- 指令遵循:通过自然语言命令动态控制文档排序
- 冲突解决:智能处理来自多个知识源的矛盾信息
- 卓越准确性:在行业基准测试中实现最先进的性能
- 无缝集成:可作为 RAG 流程中现有重排序器的直接替代方案
该重排序器擅长解决企业知识库中的实际挑战,例如优先选择最新文档而非过时文档,或优先选择内部文档而非外部来源。
要详细了解我们的指令遵循重排序器并查看实际应用示例,请访问我们的 产品概览.
如需全面了解 Contextual AI 产品的详细文档,请访问我们的 开发者门户.
此集成需要 contextual-client Python SDK。了解更多请访问 contextual-client-python 代码仓库.
概述
此集成调用 Contextual AI 的基础语言模型。
集成详情
| 类 | 包 | 本地 | 可序列化 | JS 支持 | 下载量 | 版本 |
|---|---|---|---|---|---|---|
ContextualRerank | langchain-contextual | ❌ | beta | ❌ | !PyPI - 下载量 | !PyPI - 版本 |
设置
To access Contextual's reranker models you'll need to create a/an Contextual AI account, get an API key, and install the langchain-contextual 集成包。
凭证
前往 app.contextual.ai 注册 Contextual 并生成 API 密钥。完成后设置 CONTEXTUAL_AI_API_KEY 环境变量:
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 package:
pip install -qU langchain-contextual
实例化
Contextual 重排序器参数如下:
| 参数 | 类型 | 描述 |
|---|---|---|
| documents | list[Document] | 要重排序的文档序列。文档中包含的任何元数据也将用于重排序。 |
| query | str | 用于重排序的查询。 |
| model | str | 要使用的重排序器版本。当前仅有 "ctxl-rerank-en-v1-instruct"。 |
| top_n | Optional[int] | 返回的结果数量。如果为 None 则返回所有结果。默认为 self.top_n. |
| instruction | Optional[str] | 用于重排序器的指令。 |
| callbacks | Optional[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 参考
有关所有功能的详细文档 ChatContextual 和配置,请访问 GitHub 页面: github.com/ContextualAI//langchain-contextual