|
|
|
|
|
|
|
|
|
|
|
|
|
{%- if not enable_thinking is defined %}{% set enable_thinking = false %}{% endif -%} |
|
{%- if not tools is defined %}{% set tools = none %}{% endif -%} |
|
{%- if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif -%} |
|
|
|
|
|
{%- set ns = namespace(system_prompt='', is_last_user=false, outputs_open=false, first_output=true, length=0) -%} |
|
|
|
{%- if messages and messages[0].role == 'system' -%} |
|
{%- set raw = messages[0].content -%} |
|
{%- set ns.system_prompt = raw if raw is string else raw[0].text -%} |
|
{%- set messages = messages[1:] -%} |
|
{%- endif -%} |
|
|
|
{%- set ns.length = messages|length - 1 -%} |
|
|
|
|
|
{%- if enable_thinking -%} |
|
{%- set ns.system_prompt = ns.system_prompt and 'Enable deep thinking subroutine. |
|
|
|
' + ns.system_prompt or 'Enable deep thinking subroutine.' -%} |
|
{%- endif -%} |
|
|
|
|
|
{%- if tools is not none -%} |
|
{%- if ns.system_prompt -%} |
|
{%- set ns.system_prompt = ns.system_prompt + ' |
|
|
|
You have the following functions available: |
|
|
|
' -%} |
|
{%- else -%} |
|
{%- set ns.system_prompt = 'You have the following functions available: |
|
|
|
' -%} |
|
{%- endif -%} |
|
{%- for t in tools -%} |
|
{%- set ns.system_prompt = ns.system_prompt + "```json |
|
" + (t | tojson(indent=4)) + " |
|
``` |
|
|
|
" -%} |
|
{%- endfor -%} |
|
{%- endif -%} |
|
|
|
{{- bos_token -}}{{- ns.system_prompt -}} |
|
|
|
|
|
{%- for m in messages -%} |
|
|
|
{%- if m.role == 'user' -%} |
|
{%- set ns.is_last_user = true -%} |
|
{%- set txt = m.content if m.content is string else m.content | selectattr('type','equalto','text') | map(attribute='text') | join('') -%} |
|
{{- "<|User|>" -}}{{- txt -}} |
|
{%- if loop.index0 != ns.length -%} |
|
{%- set ns.is_last_user = false -%} |
|
{{- "<|Assistant|>" -}} |
|
{%- endif -%} |
|
{%- endif -%} |
|
|
|
|
|
{%- if m.role == 'assistant' and m.tool_calls is defined and m.tool_calls -%} |
|
{%- set ns.is_last_user = false -%} |
|
{%- set lead = m.content is string and m.content|trim or (m.content and m.content | selectattr('type','equalto','text') | map(attribute='text') | join('')) or '' -%} |
|
{{- lead -}}{{- "<|tool▁calls▁begin|>" -}} |
|
{%- for call in m.tool_calls -%} |
|
{{- "<|tool▁call▁begin|>" -}}{{- call.type -}}{{- "<|tool▁sep|>" -}}{{- call.function.name -}} |
|
{{- " |
|
```json |
|
" -}}{{- call.function.arguments -}}{{- " |
|
```" -}}{{- "<|tool▁call▁end|>" -}} |
|
{%- if not loop.last -%}{{- " |
|
" -}}{%- endif -%} |
|
{%- endfor -%} |
|
{{- "<|tool▁calls▁end|>" -}}{{- "<|end▁of▁sentence|>" -}} |
|
{%- endif -%} |
|
|
|
|
|
{%- if m.role == 'assistant' and (m.tool_calls is not defined or not m.tool_calls) -%} |
|
{%- set ns.is_last_user = false -%} |
|
{%- set txt = m.content if m.content is string else m.content | selectattr('type','equalto','text') | map(attribute='text') | join('') -%} |
|
{{- txt -}}{{- "<|end▁of▁sentence|>" -}} |
|
{%- endif -%} |
|
|
|
|
|
{%- if m.role == 'tool' -%} |
|
{%- set ns.is_last_user = false -%} |
|
{%- set out_txt = m.content if m.content is string else m.content | selectattr('type','equalto','text') | map(attribute='text') | join('') -%} |
|
{%- if not ns.outputs_open -%} |
|
{{- "<|tool▁outputs▁begin|>" -}} |
|
{%- set ns.outputs_open = true -%} |
|
{%- endif -%} |
|
{{- "<|tool▁output▁begin|>" -}}{{- out_txt -}}{{- "<|tool▁output▁end|>" -}} |
|
{%- if loop.nextitem is defined and loop.nextitem.role == 'tool' -%} |
|
{{- " |
|
" -}} |
|
{%- endif -%} |
|
{%- if loop.nextitem is not defined or loop.nextitem.role != 'tool' -%} |
|
{{- "<|tool▁outputs▁end|>" -}} |
|
{%- set ns.outputs_open = false -%} |
|
{%- endif -%} |
|
{%- endif -%} |
|
{%- endfor -%} |
|
|
|
{%- if ns.outputs_open -%} |
|
{{- "<|tool▁outputs▁end|>" -}} |
|
{%- endif -%} |
|
|
|
{%- if add_generation_prompt and ns.is_last_user -%} |
|
{{- "<|Assistant|>" -}} |
|
{%- endif -%} |
|
|
|
{%- if add_generation_prompt and enable_thinking -%} |
|
{{- '<think>\n' -}} |
|
{%- endif -%} |
|
|
|
|