NVIDIA NeMo Agent Toolkit LangChain/LangGraph Integration

1.6.0 · active · verified Thu Apr 16

The `nvidia-nat-langchain` package is a subpackage designed for integrating LangChain and LangGraph with the NVIDIA NeMo Agent Toolkit (NAT). NAT is a flexible, lightweight, and framework-agnostic library that enhances AI agents with enterprise-grade capabilities like observability, profiling, evaluation, and orchestration. It allows users to leverage existing LangChain/LangGraph agents while benefiting from NAT's advanced features without significant code changes. The library is actively maintained, with version 1.6.0 being the current release, and follows a rapid development and release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `nvidia-nat-langchain` for basic profiling with a LangChain chain. It assumes `langchain-nvidia-ai-endpoints` is also installed for `ChatNVIDIA`. The core idea is to wrap your existing LangChain construct with `add_profiling_callbacks` to enable NAT's observability features. In a full NeMo Agent Toolkit workflow, this integration is often handled through YAML configurations and the `nat` CLI.

import os
from langchain_core.prompts import ChatPromptTemplate
from langchain_nvidia_ai_endpoints import ChatNVIDIA
from langchain_core.output_parsers import StrOutputParser
from nvidia_nat.langchain import add_profiling_callbacks

# Ensure NVIDIA_API_KEY is set in your environment
# You can get one from https://build.nvidia.com/
# os.environ["NVIDIA_API_KEY"] = "nvapi-xxxxxxxxxxxxxxxxxxxxxxxx"

if not os.getenv("NVIDIA_API_KEY"):
    print("Please set the NVIDIA_API_KEY environment variable.")
    exit()

# 1. Define your LangChain components
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful AI assistant."),
    ("user", "{input}")
])
llm = ChatNVIDIA(model="meta/llama-3-8b-instruct")
output_parser = StrOutputParser()

# 2. Create your LangChain chain
chain = prompt | llm | output_parser

# 3. Add NeMo Agent Toolkit profiling callbacks to your chain
profiled_chain = add_profiling_callbacks(chain)

# 4. Invoke the profiled chain
response = profiled_chain.invoke({"input": "What is the capital of France?"})
print(response)

# In a real NAT setup, you would typically run this via the `nat` CLI
# with a YAML configuration, which automatically applies these integrations.

view raw JSON →