Skip to content

Instantly share code, notes, and snippets.

@nickmitchko
Created September 9, 2025 22:20
Show Gist options
  • Select an option

  • Save nickmitchko/1c6d8525329ba775af3c7571315f1cbf to your computer and use it in GitHub Desktop.

Select an option

Save nickmitchko/1c6d8525329ba775af3c7571315f1cbf to your computer and use it in GitHub Desktop.
{%- if tools is defined and tools is not none -%}
{%- set tools_dict = {} -%}
{%- for tool in tools -%}
{%- set _ = tools_dict.update({tool['function']['name']: tool['function']}) -%}
{%- endfor -%}
{%- endif -%}
{%- set system_message = '' -%}
{%- if messages[0]['role'] == 'system' -%}
{%- set system_message = messages[0]['content'] -%}
{%- set messages = messages[1:] -%}
{%- endif -%}
{%- if tools is defined and tools is not none -%}
{%- if system_message -%}
You are a helpful assistant with tool calling capabilities. {{ system_message }}
{%- else -%}
You are a helpful assistant with tool calling capabilities. When you receive a tool call response, use the output to format an answer to the original user question.
{%- endif -%}
You have access to the following tools:
{%- for tool in tools %}
{{ tool.function | tojson }}
{%- endfor %}
When you need to use a tool, respond with a function call in the following format:
<function_calls>
<invoke name="function_name">
<parameter name="param_name">param_value</parameter>
</invoke>
</function_calls>
{%- else -%}
{%- if system_message -%}
{{ system_message }}
{%- else -%}
You are InternVL3.5, a large multimodal language model created by OpenGVLab. Follow the user's instructions carefully. Respond using markdown.
{%- endif -%}
{%- endif -%}
{%- for message in messages -%}
{%- if message['role'] == 'user' -%}
<|im_start|>user
{{ message['content'] }}<|im_end|>
{%- elif message['role'] == 'assistant' -%}
{%- if message.get('tool_calls') -%}
<|im_start|>assistant
<function_calls>
{%- for tool_call in message['tool_calls'] -%}
<invoke name="{{ tool_call['function']['name'] }}">
{%- if tool_call['function']['arguments'] is string -%}
{%- set args = tool_call['function']['arguments'] | fromjson -%}
{%- else -%}
{%- set args = tool_call['function']['arguments'] -%}
{%- endif -%}
{%- for param, value in args.items() -%}
<parameter name="{{ param }}">{{ value }}</parameter>
{%- endfor -%}
</invoke>
{%- endfor -%}
</function_calls><|im_end|>
{%- else -%}
<|im_start|>assistant
{{ message['content'] }}<|im_end|>
{%- endif -%}
{%- elif message['role'] == 'tool' -%}
<|im_start|>tool
Tool "{{ message.get('name', 'unknown') }}" returned: {{ message['content'] }}<|im_end|>
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
<|im_start|>assistant
{%- endif -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment