| |
| |
| |
| {%- 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 default_prompt = "You are Cogito, an AI assistant created by Deep Cogito, which is an AI research lab based in San Francisco." -%} |
| |
| |
| {%- set ns = namespace(system_prompt='', is_last_user=false, outputs_open=false, first_output=true) -%} |
| |
| {%- 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 user_prompt = ns.system_prompt -%} |
| {%- if enable_thinking -%} |
| |
| {%- set ns.system_prompt = "Enable deep thinking subroutine.\n\n" ~ default_prompt -%} |
| {%- if user_prompt -%} |
| {%- set ns.system_prompt = ns.system_prompt ~ "\n\n" ~ user_prompt ~ "\n\n" -%} |
| {%- endif -%} |
| {%- else -%} |
| |
| {%- set ns.system_prompt = default_prompt -%} |
| {%- if user_prompt -%} |
| {%- set ns.system_prompt = ns.system_prompt ~ "\n\n" ~ user_prompt -%} |
| {%- endif -%} |
| {%- 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 -}}{{- "<|Assistant|>" -}} |
| {%- 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 undefined 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 not ns.is_last_user -%} |
| {{- "<|Assistant|>" -}} |
| {%- endif -%} |
| |
| {%- if add_generation_prompt and enable_thinking -%} |
| {{- '<think>\n' -}} |
| {%- endif -%} |