> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# NVIDIA

> 使用 LangChain Python 与 NVIDIA 集成。

LangChain 与 NVIDIA 已合作，通过四种机制加速智能体：

1. [组件](#components)
2. [LangGraph 加速原语](#accelerate-langgraph-with-nvidia)
3. [NeMo Agent Toolkit 优化](#nemo-agent-toolkit-optimizations-with-langsmith-telemetry)
4. [全栈蓝图](#full-stack-blueprints)

## 组件

`langchain-nvidia-ai-endpoints` 包为 LangChain 提供了由 NVIDIA AI 驱动的聊天、嵌入、重排序和检索集成——包括 [Nemotron](https://www.nvidia.com/en-us/ai-data-science/foundation-models/nemotron/)（NVIDIA 为智能体 AI 构建的开放模型系列）以及 [NVIDIA API 目录](https://build.nvidia.com/) 上的数百个社区模型。

模型运行在 NVIDIA NIM 微服务上：这些容器镜像暴露了标准的 OpenAI 兼容 API，并使用 TensorRT-LLM 进行了优化，以在 NVIDIA 硬件上实现峰值吞吐量。它们可以通过托管的 API 目录访问，也可以在本地自托管。

| 组件          | 类                                                     | 描述                                     |
| :---------- | :---------------------------------------------------- | :------------------------------------- |
| 聊天          | [`ChatNVIDIA`](#chat-chatnvidia)                      | 使用任何 NVIDIA 托管模型或本地 NIM 进行聊天补全         |
| 聊天 (Dynamo) | [`ChatNVIDIADynamo`](#chat-chatnvidiadynamo)          | `ChatNVIDIA`，带有用于 Dynamo 部署的 KV 缓存路由提示 |
| 嵌入          | [`NVIDIAEmbeddings`](#embeddings-nvidiaembeddings)    | 用于语义搜索和 RAG 的稠密向量嵌入                    |
| 重排序         | [`NVIDIARerank`](#reranking-nvidiarerank)             | 根据查询相关性对文档进行重排序                        |
| 检索          | [`NVIDIARAGRetriever`](#retrieval-nvidiaragretriever) | 从 NVIDIA RAG 蓝图服务器进行检索                 |

### 聊天：ChatNVIDIA

`ChatNVIDIA` 提供基于 NVIDIA 托管模型和本地 NIM 部署的聊天补全。它支持工具调用、结构化输出、图像输入和流式传输。

#### 安装

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-nvidia-ai-endpoints
```

#### 访问 NVIDIA API 目录

1. 在 [NVIDIA API 目录](https://build.nvidia.com/) 上创建一个免费账户并登录。
2. 点击您的个人资料图标，然后选择 **API Keys** > **Generate API Key**。
3. 复制并保存密钥为 `NVIDIA_API_KEY`。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if os.environ.get("NVIDIA_API_KEY", "").startswith("nvapi-"):
    print("Valid NVIDIA_API_KEY already in environment. Delete to reset")
else:
    nvapi_key = getpass.getpass("NVAPI Key (starts with nvapi-): ")
    assert nvapi_key.startswith(
        "nvapi-"
    ), f"{nvapi_key[:5]}... is not a valid key"
    os.environ["NVIDIA_API_KEY"] = nvapi_key
```

#### Nemotron：用于智能体 AI 的精选模型

[Nemotron](https://www.nvidia.com/en-us/ai-data-science/foundation-models/nemotron/) 是 NVIDIA 为智能体 AI 设计的开放模型系列。这些模型采用混合 Mamba-Transformer 专家混合架构，以高吞吐量提供领先的基准性能，并支持高达 1M token 的上下文窗口。Nemotron 模型权重、训练数据和实现方案均在 NVIDIA 开放模型许可下公开发布。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nvidia_ai_endpoints import ChatNVIDIA

# Nemotron 3 Super — 高效推理和智能体任务
llm = ChatNVIDIA(model="nvidia/nemotron-3-super-120b-a12b")
result = llm.invoke("Plan a three-step research workflow for competitive analysis.")
print(result.content)
```

有关完整文档，包括工具调用、多模态输入和 Nemotron 特定示例，请参阅 [`ChatNVIDIA` 集成页面](/oss/python/integrations/chat/nvidia_ai_endpoints)。

### 聊天：ChatNVIDIADynamo

`ChatNVIDIADynamo` 是 `ChatNVIDIA` 的直接替代品，用于与 [NVIDIA Dynamo](https://developer.nvidia.com/dynamo) 部署配合使用。它会自动将 KV 缓存路由提示注入每个请求，允许 Dynamo 调度器优化内存分配、负载路由和请求优先级。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nvidia_ai_endpoints import ChatNVIDIADynamo

llm = ChatNVIDIADynamo(
    base_url="http://localhost:8099/v1",
    model="nvidia/nemotron-3-super-120b-a12b",
    osl=512,             # 预期输出序列长度（token）
    iat=250,             # 预期到达间隔时间（毫秒）
    latency_sensitivity=1.0,
    priority=1,
)
result = llm.invoke("Summarize KV cache routing in one sentence.")
print(result.content)
```

有关完整的 `ChatNVIDIADynamo` 参考，包括每次调用的覆盖和流式传输，请参阅 [`ChatNVIDIA` 集成页面](/oss/python/integrations/chat/nvidia_ai_endpoints#use-with-nvidia-dynamo)。

### 嵌入：NVIDIAEmbeddings

`NVIDIAEmbeddings` 生成用于语义搜索和 RAG 管道的稠密向量嵌入。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings

embedder = NVIDIAEmbeddings(model="NV-Embed-QA")
embedder.embed_query("What's the temperature today?")
```

有关完整文档，请参阅 [`NVIDIAEmbeddings` 集成页面](/oss/python/integrations/embeddings/nvidia_ai_endpoints)。

### 重排序：NVIDIARerank

`NVIDIARerank` 使用 NeMo Retriever 重排序 NIM 根据查询相关性对文档列表进行重排序。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_core.documents import Document
from langchain_nvidia_ai_endpoints import NVIDIARerank

ranker = NVIDIARerank(model="nvidia/llama-3.2-nv-rerankqa-1b-v1")
docs = ranker.compress_documents(
    query="What is GPU memory bandwidth?",
    documents=[Document(page_content=p) for p in passages],
)
```

### 检索：NVIDIARAGRetriever

`NVIDIARAGRetriever` 将 LangChain 连接到正在运行的 [NVIDIA RAG 蓝图](https://docs.nvidia.com/rag/latest/index.html) 服务器，并通过 `/v1/search` 端点检索相关文档。它支持重排序、查询重写和元数据过滤。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nvidia_ai_endpoints import NVIDIARAGRetriever

retriever = NVIDIARAGRetriever(base_url="http://localhost:8081", k=4)
docs = retriever.invoke("What is NVIDIA NIM?")
```

有关完整文档，请参阅 [`NVIDIARAGRetriever` 集成页面](/oss/python/integrations/retrievers/nvidia)。

### 使用 NVIDIA NIM 微服务自托管

当您准备好部署 AI 应用程序时，可以使用 NVIDIA NIM 自托管模型。有关更多信息，请参阅 [NVIDIA NIM 微服务](https://www.nvidia.com/en-us/ai-data-science/products/nim-microservices/)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nvidia_ai_endpoints import ChatNVIDIA, NVIDIAEmbeddings, NVIDIARerank

# 连接到在 localhost:8000 运行的聊天 NIM，指定模型
llm = ChatNVIDIA(base_url="http://localhost:8000/v1", model="nvidia/nemotron-3-super-120b-a12b")

# 连接到在 localhost:8080 运行的嵌入 NIM
embedder = NVIDIAEmbeddings(base_url="http://localhost:8080/v1")

# 连接到在 localhost:2016 运行的重排序 NIM
ranker = NVIDIARerank(base_url="http://localhost:2016/v1")
```

## 使用 NVIDIA 加速 LangGraph

`langchain-nvidia-langgraph` 包为 LangGraph 图提供了 NVIDIA 优化的执行策略。它在编译时提供两种互补的优化：

* **并行执行**：自动识别独立节点并并发运行，消除不必要的顺序瓶颈。
* **推测执行**：条件边的两个分支同时运行；一旦路由条件确定，错误的分支将被丢弃。

这两种优化都不需要更改节点逻辑或图边。

### 安装

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-nvidia-langgraph
```

### 并行执行

将 LangGraph 中的 `StateGraph` 替换为 `langchain_nvidia_langgraph.graph` 中的 `StateGraph`。图定义的其余部分保持不变。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nvidia_langgraph.graph import StateGraph, OptimizationConfig
from langgraph.graph import END
from typing import TypedDict

class AgentState(TypedDict):
  ...

graph = StateGraph(AgentState)
app = graph.compile(optimization=OptimizationConfig(enable_parallel=True))
```

或者包装现有的 `StateGraph`：

```
from langgraph.graph import StateGraph as LangGraphStateGraph
graph = LangGraphStateGraph(AgentState)
app = with_app_compile(graph).compile(optimization=OptimizationConfig(enable_parallel=True))
```

装饰器可以显式控制哪些节点参与优化：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_nvidia_langgraph.graph import sequential, depends_on, speculation_unsafe

# 防止节点被并行化（例如，它写入共享状态）
@sequential
def write_to_db(state):
    ...

# 声明图边中未表达的依赖关系
@depends_on("write_to_db")
def next_action(state):
    ...
```

### 推测执行

在编译时通过 `OptimizationConfig` 启用推测。执行器并行运行条件分支，并保留与路由决策匹配的结果。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
app = graph.compile(optimization=OptimizationConfig(enable_speculation=True))
```

## NeMo Agent Toolkit 与 LangSmith 遥测集成优化

NVIDIA NeMo Agent Toolkit 是一个开源 AI 工具包，用于构建、分析和优化智能体。开发者可以使用 LangChain 与 NeMo Agent Toolkit，只需最少的代码更改即可启用分析、评估、GPU 容量规划和自动化优化。NeMo Agent Toolkit 与 LangSmith 互操作。

* [使用 NeMo Agent Toolkit 和 LangChain 入门](https://github.com/NVIDIA/NeMo-Agent-Toolkit/blob/develop/examples/frameworks/auto_wrapper/langchain_deep_research/langgraph_deep_research.ipynb)

* [使用 NeMo Agent Toolkit 和 LangSmith 优化 LangChain](https://github.com/NVIDIA/NeMo-Agent-Toolkit/blob/develop/docs/source/run-workflows/observe/observe-workflow-with-langsmith.md)

## 全栈蓝图

NVIDIA 和 LangChain 合作开发了[全栈示例](https://github.com/langchain-ai/deepagents/tree/main/examples)，展示了如何将所有这些组件结合用于两个企业用例，并重点关注生产就绪性：

* [NVIDIA AI-Q](https://github.com/NVIDIA-AI-Blueprints/aiq/tree/develop) 是一个使用 LangChain Deep Agents 跨企业数据源进行深度研究的蓝图
* [NVIDIA VSS](https://github.com/NVIDIA-AI-Blueprints/video-search-and-summarization) 是一个使用 LangChain 和 LangGraph 进行视频搜索和摘要的蓝图

## 附加资源

* [`langchain-nvidia-ai-endpoints` 包 README](https://github.com/langchain-ai/langchain-nvidia/blob/main/libs/ai-endpoints/README.md)
* [`langchain-nvidia-langgraph` 包](https://github.com/langchain-ai/langchain-nvidia/tree/main/libs/langgraph)
* [Nemotron 模型系列](https://www.nvidia.com/en-us/ai-data-science/foundation-models/nemotron/)
* [NVIDIA NIM 大型语言模型 (LLM) 概述](https://docs.nvidia.com/nim/large-language-models/latest/introduction.html)
* [NeMo Retriever 嵌入 NIM 概述](https://docs.nvidia.com/nim/nemo-retriever/text-embedding/latest/overview.html)
* [NeMo Retriever 重排序 NIM 概述](https://docs.nvidia.com/nim/nemo-retriever/text-reranking/latest/overview.html)
* [`ChatNVIDIA` 模型](/oss/python/integrations/chat/nvidia_ai_endpoints)
* [`NVIDIAEmbeddings` 用于 RAG 工作流的模型](/oss/python/integrations/embeddings/nvidia_ai_endpoints)
* [`NVIDIARAGRetriever`](/oss/python/integrations/retrievers/nvidia)
* [NVIDIA Dynamo](https://developer.nvidia.com/dynamo)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/providers/nvidia.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
