Skip to content

Commit

Permalink
add chat template directly
Browse files Browse the repository at this point in the history
Signed-off-by: jerryzhuang <[email protected]>
  • Loading branch information
zhuangqh committed Oct 29, 2024
1 parent 6dec9f6 commit a62c6ff
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 7 deletions.
8 changes: 1 addition & 7 deletions docker/presets/models/tfs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ COPY kaito/presets/inference/${MODEL_TYPE}/inference_api.py \
COPY kaito/presets/inference/vllm/inference_api.py /workspace/vllm/inference_api.py

# Chat template
RUN apt update && apt install -y git && \
rm /var/lib/apt/lists/* -r
RUN git clone https://github.com/chujiezheng/chat_templates /tmp/chat_templates && \
cd /tmp/chat_templates && \
git reset --hard 670a2eb && \
cp -r ./chat_templates/ /workspace/ && \
rm -rf /tmp/chat_templates
ADD kaito/presets/inference/chat_templates /workspace/chat_templates

# Model weights
COPY ${WEIGHTS_PATH} /workspace/weights
Expand Down
20 changes: 20 additions & 0 deletions presets/inference/chat_templates/falcon-instruct.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% if messages[0]['role'] == 'system' %}
{% set system_message = messages[0]['content'] %}
{% set messages = messages[1:] %}
{% else %}
{% set system_message = '' %}
{% endif %}

{{ system_message | trim }}
{% for message in messages %}
{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
{% endif %}

{% set content = message['content'].replace('\r\n', '\n').replace('\n\n', '\n') %}
{{ '\n\n' + message['role'] | capitalize + ': ' + content | trim }}
{% endfor %}

{% if add_generation_prompt %}
{{ '\n\nAssistant:' }}
{% endif %}
24 changes: 24 additions & 0 deletions presets/inference/chat_templates/llama-2-chat.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% if messages[0]['role'] == 'system' %}
{% set system_message = '<<SYS>>\n' + messages[0]['content'] | trim + '\n<</SYS>>\n\n' %}
{% set messages = messages[1:] %}
{% else %}
{% set system_message = '' %}
{% endif %}

{% for message in messages %}
{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
{% endif %}

{% if loop.index0 == 0 %}
{% set content = system_message + message['content'] %}
{% else %}
{% set content = message['content'] %}
{% endif %}

{% if message['role'] == 'user' %}
{{ bos_token + '[INST] ' + content | trim + ' [/INST]' }}
{% elif message['role'] == 'assistant' %}
{{ ' ' + content | trim + ' ' + eos_token }}
{% endif %}
{% endfor %}
18 changes: 18 additions & 0 deletions presets/inference/chat_templates/llama-3-instruct.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% if messages[0]['role'] == 'system' %}
{% set offset = 1 %}
{% else %}
{% set offset = 0 %}
{% endif %}

{{ bos_token }}
{% for message in messages %}
{% if (message['role'] == 'user') != (loop.index0 % 2 == offset) %}
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
{% endif %}

{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] | trim + '<|eot_id|>' }}
{% endfor %}

{% if add_generation_prompt %}
{{ '<|start_header_id|>' + 'assistant' + '<|end_header_id|>\n\n' }}
{% endif %}
19 changes: 19 additions & 0 deletions presets/inference/chat_templates/mistral-instruct.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% if messages[0]['role'] == 'system' %}
{% set system_message = messages[0]['content'] | trim + '\n\n' %}
{% set messages = messages[1:] %}
{% else %}
{% set system_message = '' %}
{% endif %}

{{ bos_token + system_message}}
{% for message in messages %}
{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
{% endif %}

{% if message['role'] == 'user' %}
{{ '[INST] ' + message['content'] | trim + ' [/INST]' }}
{% elif message['role'] == 'assistant' %}
{{ ' ' + message['content'] | trim + eos_token }}
{% endif %}
{% endfor %}
18 changes: 18 additions & 0 deletions presets/inference/chat_templates/phi-3-small.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% if messages[0]['role'] == 'system' %}
{% set offset = 1 %}
{% else %}
{% set offset = 0 %}
{% endif %}

{{ bos_token }}
{% for message in messages %}
{% if (message['role'] == 'user') != (loop.index0 % 2 == offset) %}
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
{% endif %}

{{ '<|' + message['role'] + '|>\n' + message['content'] | trim + '<|end|>' + '\n' }}
{% endfor %}

{% if add_generation_prompt %}
{{ '<|assistant|>\n' }}
{% endif %}
17 changes: 17 additions & 0 deletions presets/inference/chat_templates/phi-3.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% if messages[0]['role'] == 'system' %}
{% set offset = 1 %}
{% else %}
{% set offset = 0 %}
{% endif %}

{% for message in messages %}
{% if (message['role'] == 'user') != (loop.index0 % 2 == offset) %}
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
{% endif %}

{{ '<|' + message['role'] + '|>\n' + message['content'] | trim + '<|end|>' + '\n' }}
{% endfor %}

{% if add_generation_prompt %}
{{ '<|assistant|>\n' }}
{% endif %}

0 comments on commit a62c6ff

Please sign in to comment.