Skip to main content
NVIDIARAGRetriever 将 LangChain 连接到正在运行的 NVIDIA RAG Blueprint 服务器,并通过 /v1/search 端点检索相关文档。它支持同步和异步检索、重排序、查询重写和元数据过滤。

概述

集成详情

本地可序列化JS 支持下载量版本
NVIDIARAGRetrieverlangchain-nvidia-ai-endpointsbetaPyPI - DownloadsPyPI - Version

设置

NVIDIARAGRetriever 需要一个正在运行的 NVIDIA RAG Blueprint 服务器。有关部署说明,请参阅 NVIDIA RAG Blueprint 文档。默认情况下,服务器监听 http://localhost:8081,并期望其向量数据库中至少有一个已摄入的集合。 检索器不需要 API 密钥;身份验证由 RAG 服务器处理。

安装

pip install -qU langchain-nvidia-ai-endpoints

实例化

from langchain_nvidia_ai_endpoints import NVIDIARAGRetriever

retriever = NVIDIARAGRetriever(
    base_url="http://localhost:8081",
    k=4,
    collection_names=["my_collection"],
)
关键参数:
参数类型默认值描述
base_urlstrRAG Blueprint 服务器的基础 URL
kint10返回的文档数量 (0–25)
collection_nameslist[str]["multimodal_data"]要搜索的向量数据库集合
vdb_top_kint100重排序之前检索的结果 (0–400)
enable_rerankerboolTrue启用检索结果的重排序
enable_query_rewritingboolFalse在搜索前启用查询重写
confidence_thresholdfloat0.0包含文档的最小相关性分数 (0.0–1.0)
timeoutfloat60HTTP 请求超时时间(秒)

用法

docs = retriever.invoke("What is NVIDIA NIM?")
for doc in docs:
    print(doc.page_content)
也支持异步检索:
docs = await retriever.ainvoke("What is NVIDIA NIM?")

在链中使用

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_nvidia_ai_endpoints import ChatNVIDIA, NVIDIARAGRetriever

retriever = NVIDIARAGRetriever(base_url="http://localhost:8081", k=4)
llm = ChatNVIDIA(model="meta/llama3-8b-instruct")

prompt = ChatPromptTemplate.from_template(
    "Answer the question based only on the following context:\n{context}\n\nQuestion: {question}"
)


def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)


chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

chain.invoke("What is NVIDIA NIM?")

API 参考

有关所有 NVIDIARAGRetriever 功能和配置的详细文档,请前往 API 参考

相关主题