NVIDIA NIMs
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โ
%pip install --upgrade --quiet langchain-nvidia-ai-endpoints
Setupโ
To get started:
-
Create a free account with NVIDIA, which hosts NVIDIA AI Foundation models.
-
Select the
Retrieval
tab, then select your model of choice. -
Under
Input
select thePython
tab, and clickGet API Key
. Then clickGenerate Key
. -
Copy and save the generated key as
NVIDIA_API_KEY
. From there, you should have access to the endpoints.
import getpass
import os
# del os.environ['NVIDIA_API_KEY'] ## delete key and reset
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
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.
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings
embedder = NVIDIAEmbeddings(model="NV-Embed-QA")
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.