prompt_embeds.md 2.04 KB
Newer Older
1
2
3
4
5
6
7
8
# Prompt Embedding Inputs

This page teaches you how to pass prompt embedding inputs to vLLM.

## What are prompt embeddings?

The traditional flow of text data for a Large Language Model goes from text to token ids (via a tokenizer) then from token ids to prompt embeddings. For a traditional decoder-only model (such as meta-llama/Llama-3.1-8B-Instruct), this step of converting token ids to prompt embeddings happens via a look-up from a learned embedding matrix, but the model is not limited to processing only the embeddings corresponding to its token vocabulary.

9
10
!!! note
    Prompt embeddings are currently only supported in the v0 engine.
11
12
13

## Offline Inference

14
To input multi-modal data, follow this schema in [vllm.inputs.EmbedsPrompt][]:
15
16
17
18
19
20
21

- `prompt_embeds`: A torch tensor representing a sequence of prompt/token embeddings. This has the shape (sequence_length, hidden_size), where sequence length is the number of tokens embeddings and hidden_size is the hidden size (embedding size) of the model.

### Hugging Face Transformers Inputs

You can pass prompt embeddings from Hugging Face Transformers models to the  `'prompt_embeds'` field of the prompt embedding dictionary, as shown in the following examples:

22
<gh-file:examples/offline_inference/prompt_embed_inference.py>
23
24
25
26
27
28
29
30
31
32
33
34
35
36

## Online Serving

Our OpenAI-compatible server accepts prompt embeddings inputs via the [Completions API](https://platform.openai.com/docs/api-reference/completions). Prompt embeddings inputs are added via a new `'prompt_embeds'` key in the JSON package.

When a mixture of `'prompt_embeds'` and `'prompt'` inputs are provided in a single request, the prompt embeds are always returned first.

Prompt embeddings are passed in as base64 encoded torch tensors.

### Transformers Inputs via OpenAI Client

First, launch the OpenAI-compatible server:

```bash
37
vllm serve meta-llama/Llama-3.2-1B-Instruct --runner generate \
38
39
40
41
42
  --max-model-len 4096 --enable-prompt-embeds
```

Then, you can use the OpenAI client as follows:

43
<gh-file:examples/online_serving/prompt_embed_inference_with_openai_client.py>