Prompt Embeddings
NVIDIA NIM for Large Language Models supports prompt embeddings, also known as prompt embeds, as a secure alternative to traditional text prompts. Applications can use precomputed embeddings for inference to support more flexible prompt engineering and improve privacy and data security. With prompt embeddings, 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.
Prompt embeddings support the following use cases:
- Privacy-Preserving AI: Convert sensitive prompts to embeddings before sending them to the server.
- Custom Embedding Models: Use specialized, domain-specific embedding models.
- Embedding Caching: Precompute and cache frequently used embeddings.
- Advanced Prompt Engineering: Implement sophisticated preprocessing pipelines.
- Multistage Pipelines: Integrate with proxy services that operate on embeddings.
For background information about prompt embeddings, refer to the vLLM Prompt Embeds documentation.
Architecture
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.
Prerequisites
Before you begin, make sure you have the following:
- A compatible NIM container and backend.
- Access to the model’s embedding layer or a compatible embedding model.
- Python packages:
torch,transformers,openai.
Prompt embeddings are currently supported only with the vLLM backend (V1 engine). The following backends are not supported: TRT-LLM and SGLang.
Run NIM
Enable prompt embeddings by launching NIM with the --enable-prompt-embeds flag:
API Request Format
Prompt embeddings are supported through the Completions API (/v1/completions).
Request Parameters
The following table describes the request parameters:
Either prompt or prompt_embeds must be provided. They cannot be used together.
The OpenAI client does not accept None for prompt. Use an empty string "" when
sending prompt_embeds.
Response Format
The response follows the standard OpenAI completions schema, as shown in the following example:
prompt_tokens might be 0 when using embeddings because the token count is not computed from
embeddings.
cURL Example
The following curl command sends a Completions API request with prompt embeddings:
Python Examples
The following Python examples show common ways to send prompt embeddings to NIM:
Privacy-Preserving Pattern
Use this pattern to send precomputed embeddings without including the original prompt text:
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.