custom_chat_template.md 1.74 KB
Newer Older
1
# Custom Chat Template
Ying Sheng's avatar
Ying Sheng committed
2

Lianmin Zheng's avatar
Lianmin Zheng committed
3
**NOTE**: There are two chat template systems in SGLang project. This document is about setting a custom chat template for the OpenAI-compatible API server (defined at [conversation.py](https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/conversation.py)). It is NOT related to the chat template used in the SGLang language frontend (defined at [chat_template.py](https://github.com/sgl-project/sglang/blob/main/python/sglang/lang/chat_template.py)).
4
5
6

By default, the server uses the chat template specified in the model tokenizer from Hugging Face.
It should just work for most official models such as Llama-2/Llama-3.
Ying Sheng's avatar
Ying Sheng committed
7
8
9

If needed, you can also override the chat template when launching the server:

10
```bash
11
12
13
14
python -m sglang.launch_server \
  --model-path meta-llama/Llama-2-7b-chat-hf \
  --port 30000 \
  --chat-template llama-2
Ying Sheng's avatar
Ying Sheng committed
15
16
```

17
18
19
If the chat template you are looking for is missing, you are welcome to contribute it or load it from a file.

## JSON Format
20

21
You can load the JSON format, which is defined by `conversation.py`.
Ying Sheng's avatar
Ying Sheng committed
22
23
24
25
26
27
28
29
30
31
32
33
34

```json
{
  "name": "my_model",
  "system": "<|im_start|>system",
  "user": "<|im_start|>user",
  "assistant": "<|im_start|>assistant",
  "sep_style": "CHATML",
  "sep": "<|im_end|>",
  "stop_str": ["<|im_end|>", "<|im_start|>"]
}
```

35
```bash
36
37
38
39
python -m sglang.launch_server \
  --model-path meta-llama/Llama-2-7b-chat-hf \
  --port 30000 \
  --chat-template ./my_model_template.json
40
41
42
43
```

## Jinja Format

44
45
46
You can also use the [Jinja template format](https://huggingface.co/docs/transformers/main/en/chat_templating) as defined by Hugging Face Transformers.

```bash
47
48
49
50
python -m sglang.launch_server \
  --model-path meta-llama/Llama-2-7b-chat-hf \
  --port 30000 \
  --chat-template ./my_model_template.jinja
51
```