54 lines
2.4 KiB
Django/Jinja
54 lines
2.4 KiB
Django/Jinja
{%- if messages[0]['role'] == 'system' -%}
|
|
{%- set system_message = messages[0]['content'] | trim -%}
|
|
{%- set messages = messages[1:] -%}
|
|
{%- else -%}
|
|
{%- set system_message = '' -%}
|
|
{%- endif -%}
|
|
|
|
{%- if tools is not none -%}
|
|
{{- '<|begin_of_text|><|start_header_id|>system<|end_header_id|>' + '\n\n' + system_message -}}
|
|
{{- '\n\n' if system_message else '' -}}
|
|
{{- '<AVAILABLE_TOOLS>[' -}}
|
|
{% for t in tools %}
|
|
{{- (t.function if t.function is defined else t) | tojson() -}}
|
|
{{- ', ' if not loop.last else '' -}}
|
|
{%- endfor -%}
|
|
{{- ']</AVAILABLE_TOOLS>' -}}
|
|
{{- '<|eot_id|>' -}}
|
|
{%- else -%}
|
|
{{- '<|begin_of_text|><|start_header_id|>system<|end_header_id|>' + '\n\n' + system_message + '<|eot_id|>' -}}
|
|
{%- endif -%}
|
|
|
|
{%- for message in messages -%}
|
|
{%- if (message['role'] in ['user', 'tool']) != (loop.index0 % 2 == 0) -%}
|
|
{{- raise_exception('Conversation roles must alternate between user/tool and assistant') -}}
|
|
{%- elif message['role'] == 'user' -%}
|
|
{{- '<|start_header_id|>user<|end_header_id|>' + '\n\n' + message['content'] | trim + '<|eot_id|>' -}}
|
|
{%- elif message['role'] == 'tool' -%}
|
|
{%- set tool_response = '<TOOL_RESPONSE>[' + message['content'] | trim + ']</TOOL_RESPONSE>' -%}
|
|
{{- '<|start_header_id|>user<|end_header_id|>' + '\n\n' + tool_response + '<|eot_id|>' -}}
|
|
{%- elif message['role'] == 'assistant' and message.get('tool_calls') is not none -%}
|
|
{%- set tool_calls = message['tool_calls'] -%}
|
|
{% generation %}
|
|
{{- '<|start_header_id|>assistant<|end_header_id|>' + '\n\n' + '<TOOLCALL>[' -}}
|
|
{%- for tool_call in tool_calls -%}
|
|
{{ '{' + '"name": "' + tool_call.function.name + '", "arguments": ' + tool_call.function.arguments | tojson + '}' }}
|
|
{%- if not loop.last -%}
|
|
{{ ', ' }}
|
|
{%- else -%}
|
|
{{ ']</TOOLCALL>' + '<|eot_id|>' }}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
{% endgeneration %}
|
|
{%- elif message['role'] == 'assistant' -%}
|
|
{% generation %}
|
|
{{- '<|start_header_id|>assistant<|end_header_id|>' + '\n\n' + message['content'] | trim + '<|eot_id|>' -}}
|
|
{% endgeneration %}
|
|
{%- endif -%}
|
|
{%- endfor -%}
|
|
|
|
{%- if add_generation_prompt -%}
|
|
{% generation %}
|
|
{{ '<|start_header_id|>assistant<|end_header_id|>' + '\n\n' }}
|
|
{% endgeneration %}
|
|
{%- endif -%} |