Skip to content

Commit 21b3ef8

Browse files
TankNeeDarkLight1337
authored andcommitted
Add chat doc in quick start (vllm-project#21213)
Co-authored-by: Cyrus Leung <cyrus.tl.leung@gmail.com> Signed-off-by: Diego-Castan <diego.castan@ibm.com>
1 parent c3cae61 commit 21b3ef8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/getting_started/quickstart.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,43 @@ for output in outputs:
9898
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
9999
```
100100

101+
!!! note
102+
The `llm.generate` method does not automatically apply the model's chat template to the input prompt. Therefore, if you are using an Instruct model or Chat model, you should manually apply the corresponding chat template to ensure the expected behavior. Alternatively, you can use the `llm.chat` method and pass a list of messages which have the same format as those passed to OpenAI's `client.chat.completions`:
103+
104+
??? code
105+
106+
```python
107+
# Using tokenizer to apply chat template
108+
from transformers import AutoTokenizer
109+
110+
tokenizer = AutoTokenizer.from_pretrained("/path/to/chat_model")
111+
messages_list = [
112+
[{"role": "user", "content": prompt}]
113+
for prompt in prompts
114+
]
115+
texts = tokenizer.apply_chat_template(
116+
messages_list,
117+
tokenize=False,
118+
add_generation_prompt=True,
119+
)
120+
121+
# Generate outputs
122+
outputs = llm.generate(texts, sampling_params)
123+
124+
# Print the outputs.
125+
for output in outputs:
126+
prompt = output.prompt
127+
generated_text = output.outputs[0].text
128+
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
129+
130+
# Using chat interface.
131+
outputs = llm.chat(messages_list, sampling_params)
132+
for idx, output in enumerate(outputs):
133+
prompt = prompts[idx]
134+
generated_text = output.outputs[0].text
135+
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
136+
```
137+
101138
[](){ #quickstart-online }
102139

103140
## OpenAI-Compatible Server

0 commit comments

Comments
 (0)