Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mtasic85/ca69af6891279380cff219620c58fb04 to your computer and use it in GitHub Desktop.

Select an option

Save mtasic85/ca69af6891279380cff219620c58fb04 to your computer and use it in GitHub Desktop.
rwkv7-chat-template-tool-calling-21.jinja
{%- if not add_generation_prompt is defined %}
{%- set add_generation_prompt = true %}
{%- endif %}
{%- set ns = namespace(system_prompt='', has_tools=false) %}
{%- for message in messages %}
{%- if message.role == 'system' %}
{%- set ns.system_prompt = message.content %}
{%- endif %}
{%- endfor %}
{%- if tools is defined and tools | length > 0 %}
{%- set ns.has_tools = true %}
{%- endif %}
{{bos_token}}
{%- if ns.system_prompt != '' or ns.has_tools %}
{{- 'System: ' }}
{%- if ns.system_prompt != '' %}
{{- ns.system_prompt|trim }}
{%- if ns.has_tools %}
{{- '\n' }}
{%- endif %}
{%- endif %}
{%- if ns.has_tools %}
{{- 'You may call one or more functions to assist with the user query.\n' }}
{{- 'You are provided with function signatures within <tools></tools> XML tags:\n' }}
{{- '<tools>\n' }}
{%- for tool in tools %}
{{- tool | tojson }}
{%- if not loop.last %}
{{- '\n' }}
{%- endif %}
{%- endfor %}
{{- '</tools>\n' }}
{{- 'For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n' }}
{{- '<tool_call>\n' }}
{{- '{"name": "<function-name>", "arguments": "<args-json-object-as-string>"}\n' }}
{{- '</tool_call>\n' }}
{{- 'IMPORTANT: "arguments" MUST be a valid JSON string (escaped and quoted), do not output raw JSON object for arguments.' }}
{%- endif %}
{#{- '\n\n' }#}
{%- endif %}
{%- for message in messages %}
{%- if message.role == 'user' %}
{{- 'User: ' }}
{{- message.content|trim }}
{%- elif message.role == 'assistant' %}
{{- 'Assistant: ' }}
{%- set content = message.content.strip() %}
{%- set reasoning_content = '' %}
{%- if content %}
{%- if '<think>' in content and '</think>' in content %}
{%- set temp = content %}
{%- set content = temp.split('</think>')[-1].strip() %}
{%- set reasoning_content = temp.split('<think>')[-1].split('</think>')[0].strip() %}
{%- elif '</think>' in content %}
{%- set temp = content %}
{%- set content = temp.split('</think>')[-1].strip() %}
{%- set reasoning_content = temp.split('</think>')[0].strip() %}
{%- endif %}
{%- endif %}
{%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls is not mapping and message.tool_calls | length > 0 %}
{%- for tool_call in message.tool_calls %}
{%- set func = tool_call.get('function', {}) %}
{%- set name = func.get('name', '') %}
{%- set args = func.get('arguments', {}) %}
{{- '<tool_call>\n{"name": "' + name + '", "arguments": ' }}
{%- if args is string %}
{{- args }}
{%- else %}
{{- args | tojson }}
{%- endif %}
{{- '}\n</tool_call>' }}
{%- if not loop.last %}
{{- '\n' }}
{%- endif %}
{%- endfor %}
{%- else %}
{%- if content.strip() != '' %}
{{- content|trim }}
{%- endif %}
{%- endif %}
{%- elif message['role'] == 'tool' %}
{{- 'Assistant:' }}
{{- '\n<tool_response>\n' }}
{{- message['content']|trim }}
{{- '\n</tool_response>' }}
{%- endif -%}
{{- '\n\n' }}
{%- endfor %}
{%- if add_generation_prompt %}
{{- 'Assistant: ' -}}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '<think>\n</think>' }}
{%- endif %}
{%- if enable_thinking is defined and enable_thinking is true %}
{{- '<think>\n' }}
{%- endif %}
{%- endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment