Skip to main content
The langchain-nvidia-ai-endpoints package contains LangChain integrations building applications with models on NVIDIA NIM inference microservice. NIM supports models across domains like chat, embedding, and re-ranking models from the community as well as NVIDIA. These models are optimized by NVIDIA to deliver the best performance on NVIDIA accelerated infrastructure and deployed as a NIM, an easy-to-use, prebuilt containers that deploy anywhere using a single command on NVIDIA accelerated infrastructure. NVIDIA hosted deployments of NIMs are available to test on the NVIDIA API catalog. After testing, NIMs can be exported from NVIDIA’s API catalog using the NVIDIA AI Enterprise license and run on-premises or in the cloud, giving enterprises ownership and full control of their IP and AI application. NIMs are packaged as container images on a per model basis and are distributed as NGC container images through the NVIDIA NGC Catalog. At their core, NIMs provide easy, consistent, and familiar APIs for running inference on an AI model. This example goes over how to use LangChain to interact with the supported NVIDIA Retrieval QA Embedding Model for retrieval-augmented generation via the NVIDIAEmbeddings class. For more information on accessing the chat models through this API, check out the ChatNVIDIA documentation.

Installation

Setup

To get started:
  1. Create a free account with NVIDIA, which hosts NVIDIA AI Foundation models.
  2. Select the Retrieval tab, then select your model of choice.
  3. Under Input select the Python tab, and click Get API Key. Then click Generate Key.
  4. Copy and save the generated key as NVIDIA_API_KEY. From there, you should have access to the endpoints.
We should be able to see an embedding model among that list which can be used in conjunction with an LLM for effective RAG solutions. We can interface with this model as well as other embedding models supported by NIM through the NVIDIAEmbeddings class.

Working with NIMs on the NVIDIA API Catalog

When initializing an embedding model you can select a model by passing it, e.g. NV-Embed-QA below, or use the default by not passing any arguments.
This model is a fine-tuned E5-large model which supports the expected Embeddings methods including:
  • embed_query: Generate query embedding for a query sample.
  • embed_documents: Generate passage embeddings for a list of documents which you would like to search over.
  • aembed_query/aembed_documents: Asynchronous versions of the above.

Working with self-hosted NVIDIA NIMs

When ready to deploy, you can self-host models with NVIDIA NIM—which is included with the NVIDIA AI Enterprise software license—and run them anywhere, giving you ownership of your customizations and full control of your intellectual property (IP) and AI applications. Learn more about NIMs

Similarity

The following is a quick test of the similarity for these data points: Queries:
  • What’s the weather like in Komchatka?
  • What kinds of food is Italy known for?
  • What’s my name? I bet you don’t remember…
  • What’s the point of life anyways?
  • The point of life is to have fun :D
Documents:
  • Komchatka’s weather is cold, with long, severe winters.
  • Italy is famous for pasta, pizza, gelato, and espresso.
  • I can’t recall personal names, only provide information.
  • Life’s purpose varies, often seen as personal fulfillment.
  • Enjoying life’s moments is indeed a wonderful approach.

Embedding Runtimes

Document Embedding

Now that we’ve generated our embeddings, we can do a simple similarity check on the results to see which documents would have triggered as reasonable answers in a retrieval task:
As a reminder, the queries and documents sent to our system were: Queries:
  • What’s the weather like in Komchatka?
  • What kinds of food is Italy known for?
  • What’s my name? I bet you don’t remember…
  • What’s the point of life anyways?
  • The point of life is to have fun :D
Documents:
  • Komchatka’s weather is cold, with long, severe winters.
  • Italy is famous for pasta, pizza, gelato, and espresso.
  • I can’t recall personal names, only provide information.
  • Life’s purpose varies, often seen as personal fulfillment.
  • Enjoying life’s moments is indeed a wonderful approach.

Truncation

Embedding models typically have a fixed context window that determines the maximum number of input tokens that can be embedded. This limit could be a hard limit, equal to the model’s maximum input token length, or an effective limit, beyond which the accuracy of the embedding decreases. Since models operate on tokens and applications usually work with text, it can be challenging for an application to ensure that its input stays within the model’s token limits. By default, an exception is thrown if the input is too large. To assist with this, NVIDIA’s NIMs (API Catalog or local) provide a truncate parameter that truncates the input on the server side if it’s too large. The truncate parameter has three options:
  • “NONE”: The default option. An exception is thrown if the input is too large.
  • “START”: The server truncates the input from the start (left), discarding tokens as necessary.
  • “END”: The server truncates the input from the end (right), discarding tokens as necessary.

RAG Retrieval

The following is a repurposing of the initial example of the LangChain Expression Language Retrieval Cookbook entry, but executed with the AI Foundation Models’ Mixtral 8x7B Instruct and NVIDIA Retrieval QA Embedding models available in their playground environments. The subsequent examples in the cookbook also run as expected, and we encourage you to explore with these options. TIP: We would recommend using Mixtral for internal reasoning (i.e. instruction following for data extraction, tool selection, etc.) and Llama-Chat for a single final “wrap-up by making a simple response that works for this user based on the history and context” response.

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.