shootstuff's picture
Duplicate from NousResearch/Kimi-K2-Thinking-Alternate-Tokenizer
7a154ce
{%- if keep_thinking is not defined -%}
{%- set keep_thinking = true -%}
{%- endif -%}
{%- if tools -%}
{{- bos_token + '<|start_header_id|>system<|end_header_id|>\n' -}}
{%- if messages[0]['role'] == 'system' -%}
{{- messages[0]['content'] + '\n\n' -}}
{%- endif -%}
{{- "You are a function calling AI model. You are provided with function signatures within <tools> </tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions. After calling & executing the functions, you will be provided with function results within <tool_response> </tool_response> XML tags.\n\n<tools>" -}}
{%- for tool in tools -%}
{{- "\n" -}}
{{- tool | tojson -}}
{%- endfor -%}
{{- "\n</tools>\n\nFor each function call return a JSON object, with the following pydantic model json schema:\n\n{\"title\": \"FunctionCall\", \"type\": \"object\", \"properties\": {\"name\": {\"title\": \"Name\", \"type\": \"string\"}, \"arguments\": {\"title\": \"Arguments\", \"type\": \"object\"}}, \"required\": [\"name\", \"arguments\"]}\n\nEach function call should be enclosed within <tool_call> </tool_call> XML tags.\n\nExample:\n<tool_call>\n{\"name\": \"read_file\", \"arguments\": {\"path\": \"/path/to/file.txt\"}}\n</tool_call>\n<|eot_id|>" -}}
{%- else -%}
{%- if messages[0]['role'] == 'system' -%}
{{- bos_token + '<|start_header_id|>system<|end_header_id|>\n\n' + messages[0]['content'] + '<|eot_id|>' -}}
{%- else -%}
{{- bos_token -}}
{%- endif -%}
{%- endif -%}
{%- for message in messages -%}
{%- if (message.role == "user") or (message.role == "system" and not loop.first) -%}
{{- '<|start_header_id|>' + message.role + '<|end_header_id|>\n\n' + message.content + '<|eot_id|>' -}}
{%- elif message.role == "assistant" -%}
{{- '<|start_header_id|>' + message.role + '<|end_header_id|>\n' -}}
{%- if message.content or message.reasoning_content is defined -%}
{#- Extract reasoning and content -#}
{%- set reasoning = '' -%}
{%- set content = message.content | default('', true) -%}
{%- if message.reasoning_content is defined and message.reasoning_content -%}
{#- Use explicit reasoning_content field if provided -#}
{%- set reasoning = message.reasoning_content -%}
{#- Strip any inline think tags from content when reasoning_content exists -#}
{%- if '</think>' in content -%}
{%- set content = content.split('</think>', 1)[1] -%}
{%- endif -%}
{%- elif '</think>' in content -%}
{#- Extract reasoning from <think> tags in content -#}
{%- set thinking_parts = content.split('</think>', 1) -%}
{%- if '<think>' in thinking_parts[0] -%}
{%- set reasoning = thinking_parts[0].split('<think>', 1)[1] -%}
{%- else -%}
{%- set reasoning = thinking_parts[0] -%}
{%- endif -%}
{%- set content = thinking_parts[1] if thinking_parts | length > 1 else '' -%}
{%- endif -%}
{#- Output based on keep_thinking setting -#}
{%- if keep_thinking and reasoning -%}
{{- '\n<think>' + reasoning.strip() + '</think>' -}}
{%- else -%}
{{- '\n' -}}
{%- endif -%}
{%- if content.strip() -%}
{{- content.strip() -}}
{%- endif -%}
{%- endif -%}
{%- if message.tool_calls -%}
{%- for tool_call in message.tool_calls -%}
{%- if tool_call.function is defined -%}
{%- set tool_call = tool_call.function -%}
{%- endif -%}
{{- '\n<tool_call>\n{"name": "' -}}
{{- tool_call.name -}}
{{- '", "arguments": ' -}}
{{- tool_call.arguments | tojson -}}
{{- '}\n</tool_call>' -}}
{%- endfor -%}
{%- endif -%}
{{- '<|eot_id|>' -}}
{%- elif message.role == "tool" -%}
{%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") -%}
{{- '<|start_header_id|>tool<|end_header_id|>\n\n' -}}
{%- endif -%}
{{- message.content -}}
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") -%}
{{- '<|eot_id|>' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}
{%- endif -%}