Skip to content

Commit

Permalink
including chat templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh committed Jun 10, 2024
1 parent b8e4f37 commit 3186bd7
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ COPY --chown=$MAMBA_USER:$MAMBA_USER requirements.txt /tmp/requirements.txt
# install the python deps
RUN python3 -m pip install --no-cache-dir -r /tmp/requirements.txt

# copy over the chat templates
COPY chat-templates /chat-templates

# copy over the entry point
COPY --chown=$MAMBA_USER:$MAMBA_USER entrypoint.sh /
RUN chmod +x /entrypoint.sh
Expand Down
29 changes: 29 additions & 0 deletions chat-templates/alpacha.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{ (messages|selectattr('role', 'equalto', 'system')|list|last).content|trim if (messages|selectattr('role', 'equalto', 'system')|list) else '' }}

{% for message in messages %}
{% if message['role'] == 'user' %}
### Instruction:
{{ message['content']|trim -}}
{% if not loop.last %}


{% endif %}
{% elif message['role'] == 'assistant' %}
### Response:
{{ message['content']|trim -}}
{% if not loop.last %}


{% endif %}
{% elif message['role'] == 'user_context' %}
### Input:
{{ message['content']|trim -}}
{% if not loop.last %}


{% endif %}
{% endif %}
{% endfor %}
{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}
### Response:
{% endif %}
18 changes: 18 additions & 0 deletions chat-templates/chatglm.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{%- set counter = namespace(index=0) -%}
{%- for message in messages -%}
{%- if message['role'] == 'user' -%}
{{- '[Round ' + counter.index|string + ']\n问:' + message['content'] -}}
{%- set counter.index = counter.index + 1 -%}
{%- endif -%}
{%- if message['role'] == 'assistant' -%}
{{- '\n答:' + message['content'] -}}
{%- if (loop.last and add_generation_prompt) or not loop.last -%}
{{- '\n' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}


{%- if add_generation_prompt and messages[-1]['role'] != 'assistant' -%}
{{- '\n答:' -}}
{%- endif -%}
18 changes: 18 additions & 0 deletions chat-templates/chatglm2.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{%- set counter = namespace(index=1) -%}
{%- for message in messages -%}
{%- if message['role'] == 'user' -%}
{{- '[Round ' + counter.index|string + ']\n\n问:' + message['content'] -}}
{%- set counter.index = counter.index + 1 -%}
{%- endif -%}
{%- if message['role'] == 'assistant' -%}
{{- '\n\n答:' + message['content'] -}}
{%- if (loop.last and add_generation_prompt) or not loop.last -%}
{{- '\n\n' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}


{%- if add_generation_prompt and messages[-1]['role'] != 'assistant' -%}
{{- '\n\n答:' -}}
{%- endif -%}
2 changes: 2 additions & 0 deletions chat-templates/chatml.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content']}}{% if (loop.last and add_generation_prompt) or not loop.last %}{{ '<|im_end|>' + '\n'}}{% endif %}{% endfor %}
{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}{{ '<|im_start|>assistant\n' }}{% endif %}
17 changes: 17 additions & 0 deletions chat-templates/falcon-180b.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{%- for message in messages -%}
{%- if message['role'] == 'system' -%}
{{- 'System: ' + message['content'] -}}
{%- elif message['role'] == 'user' -%}
{{- 'User: ' + message['content'] -}}
{%- elif message['role'] == 'assistant' -%}
{{- 'Falcon: ' + message['content'] -}}
{%- endif -%}
{%- if (loop.last and add_generation_prompt) or not loop.last -%}
{{- '\n' -}}
{%- endif -%}
{%- endfor -%}


{%- if add_generation_prompt and messages[-1]['role'] != 'assistant' -%}
{{- 'Falcon:' -}}
{% endif %}
15 changes: 15 additions & 0 deletions chat-templates/falcon.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{%- for message in messages -%}
{%- if message['role'] == 'user' -%}
{{- 'User: ' + message['content'] -}}
{%- elif message['role'] == 'assistant' -%}
{{- 'Assistant: ' + message['content'] -}}
{%- endif -%}
{%- if (loop.last and add_generation_prompt) or not loop.last -%}
{{- '\n' -}}
{%- endif -%}
{%- endfor -%}


{%- if add_generation_prompt and messages[-1]['role'] != 'assistant' -%}
{{- 'Assistant:' -}}
{% endif %}
24 changes: 24 additions & 0 deletions chat-templates/llama2.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 chat-templates/llama3-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 %}
23 changes: 23 additions & 0 deletions chat-templates/llava.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{%- if messages[0]['role'] == 'system' -%}
{%- set system_message = messages[0]['content'] -%}
{%- 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' -%}
{{ 'USER: ' + message['content'] + '\n' }}
{%- elif message['role'] == 'assistant' -%}
{{ 'ASSISTANT: ' + message['content'] + eos_token + '\n' }}
{%- endif -%}
{%- endfor -%}

{%- if add_generation_prompt -%}
{{ 'ASSISTANT:' }}
{% endif %}
4 changes: 4 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ if [[ -n "$MAX_MODEL_LEN" ]]; then
additional_args="$additional_args --max-model-len $MAX_MODEL_LEN"
fi

if [[ ! -z "${CHAT_TEMPLATE}" ]]; then
additional_args="${additional_args} --chat-template ${CHAT_TEMPLATE}"
fi

# Start the API server with the specified or default settings
python3 -m vllm.entrypoints.openai.api_server \
--tensor-parallel-size $NUM_GPU \
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vllm==0.4.2
vllm==0.4.3
hf_transfer
accelerate

0 comments on commit 3186bd7

Please sign in to comment.