Prompt Embeddings
Prompt embeddings through NIM require the vLLM backend. The --enable-prompt-embeds flag and the prompt_embeds request field in this guide do not apply to SGLang.
NVIDIA NIM for Large Language Models (LLM) and Vision Language Models (VLM) supports prompt embeddings, also known as prompt embeds, as a secure alternative to traditional text prompts. Applications transform sensitive user data into embeddings before sending requests to the inference server, which reduces the risk of exposing confidential information during the AI workflow.
Because the request carries a precomputed tensor instead of text, prompt embeddings also enable prompt reuse and preprocessing patterns that are not possible with plain text prompts. Prompt embeddings support the following use cases:
- Privacy-Preserving AI: Convert sensitive prompts to embeddings before sending them to the server.
- Custom Embedding Models: Compute embeddings with specialized, domain-specific models.
- Embedding Caching: Precompute and cache frequently used embeddings.
- Advanced Prompt Engineering: Implement sophisticated preprocessing pipelines.
- Multistage Pipelines: Integrate with proxy services that validate requests and operate on embeddings.
For background information about prompt embeddings, refer to the vLLM Prompt Embeds documentation.
How Prompt Embeddings Work
A client, or a proxy acting on behalf of the client, converts prompt text into an embedding tensor and sends that tensor to NIM. NIM dispatches the tensor to the vLLM backend, which generates tokens and returns a standard OpenAI completions response. The following diagram shows how prompt embeddings flow through the system:
The system uses three main components:
- Client: Sends OpenAI API-compliant requests by using
/v1/completions. - Transform Proxy: Acts as an intermediary that:
- Validates incoming requests
- Transforms text prompts into embeddings
- Forwards the transformed embeddings to the upstream inference server
- Processes and streams responses back to the client in the standard OpenAI API format
- NIM Inference Server: Receives inference requests with prompt embeddings and returns generated responses to the Transform Proxy.
The transform proxy is optional. A client that computes its own embeddings can call NIM directly.
Send Prompt Embeddings to NIM
This procedure starts a NIM container with prompt embeddings enabled, computes an embedding tensor from a model’s embedding layer, and sends it to the Completions API. The end result is a generated completion produced without sending prompt text to the server.
Before you start, complete the following prerequisites:
- Select a NIM container that uses the vLLM backend.
- Obtain access to the model’s embedding layer or a compatible embedding model.
- Install the
torch,transformers, andopenaiPython packages.
To send prompt embeddings to NIM, complete the following steps:
-
Start the container with the
--enable-prompt-embedsflag. -
Compute an embedding tensor from the model’s embedding layer, serialize it with
torch.save, and base64-encode the result. For a complete script, refer to Complete Example. -
Send a Completions API request that sets
prompt_embedsand passes an empty string forprompt. -
Confirm that the response follows the standard OpenAI completions schema. For field details, refer to Request and Response Reference.
prompt_tokensmight be0when using embeddings because the token count is not computed from embeddings.
Complete Example
The following example demonstrates the full workflow: tokenizing a chat prompt, computing embeddings from the model’s embedding layer, serializing the tensor, and sending the request to NIM.
cURL Example
The following curl command sends a Completions API request with prompt embeddings:
Request and Response Reference
Prompt embeddings are supported through the Completions API (/v1/completions). The following example shows a complete request body:
The following table describes the request parameters:
The response follows the standard OpenAI completions schema, as shown in the following example:
Limitations and Considerations
The following limitations apply to prompt embeddings:
- Prompt or Embeddings: Either
promptorprompt_embedsmust be provided. They cannot be used together. The OpenAI client does not acceptNoneforprompt. Use an empty string""when sendingprompt_embeds. - Token Counts:
prompt_tokensmight be0when using embeddings because the token count is not computed from embeddings.
Related Topics
Refer to the following pages for related reference and configuration details:
- API Reference for the Completions API schema and other inference endpoints
- Advanced Configuration for passing
--enable-prompt-embedsthrough container arguments or HelmcustomArgs