tiny-ErgoForCausalLM / chat_template.jinja
qgallouedec's picture
qgallouedec HF Staff
Upload ErgoForCausalLM
1d0855f verified
Raw History Blame Contribute Delete
5.98 kB
{#- Ergo chat format. Each turn is `Label<ctrl0>...<ctrl100>` and turns are joined by a newline. -#}
{%- set labels = {'system': 'System', 'developer': 'System', 'user': 'User', 'assistant': 'Assistant', 'tool': 'CodeExecutor', 'function': 'CodeExecutor'} -%}
{%- set default_system_prompt = 'You are Ergo, a model built by Amazon. You are a helpful agent that is good at everything. Do not present yourself as a real human being; act as a helpful assistant.' -%}
{#- Message content is a string or a list of text parts. -#}
{%- macro text_of(content) -%}
{%- if content is string -%}
{{- content -}}
{%- elif content is defined and content is not none -%}
{%- for part in content -%}
{%- if part is string -%}
{{- part -}}
{%- elif part.type == 'text' -%}
{{- part.text -}}
{%- else -%}
{{- raise_exception('Ergo supports text content only, got ' ~ part.type) -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{#- Preserve complete tool schemas in client key order, adding missing root defaults. -#}
{%- macro tool_definitions(tools) -%}
{%- set out = namespace(definitions=[]) -%}
{%- for tool in tools -%}
{%- set function = tool.function -%}
{%- set parameters = function.get('parameters') -%}
{%- if parameters is none -%}
{%- set parameters = {} -%}
{%- endif -%}
{%- if parameters is not mapping -%}
{{- raise_exception('Tool parameters must be a JSON object') -}}
{%- endif -%}
{%- set schema = namespace(value={}) -%}
{%- if 'type' not in parameters -%}
{%- set schema.value = {'type': 'object'} -%}
{%- if 'properties' not in parameters -%}
{%- set schema.value = dict(schema.value, properties={}) -%}
{%- endif -%}
{%- endif -%}
{%- for key, value in parameters.items() -%}
{%- set schema.value = dict(schema.value, **{key: value}) -%}
{%- if key == 'type' and 'properties' not in parameters -%}
{%- set schema.value = dict(schema.value, properties={}) -%}
{%- endif -%}
{%- endfor -%}
{%- set definition = {'type': 'function', 'function': {'name': function.name, 'description': function.get('description') or '', 'parameters': schema.value}} -%}
{%- set out.definitions = out.definitions + [definition] -%}
{%- endfor -%}
{{- '\nThe tool definition is:\n\n```json\n' ~ (out.definitions | tojson(ensure_ascii=false, separators=(',', ':'), sort_keys=false)) ~ '\n```' -}}
{%- endmacro -%}
{%- macro turn(message, state) -%}
{%- if message.role not in labels -%}
{{- raise_exception('Unsupported Ergo chat role: ' ~ message.role) -}}
{%- endif -%}
{{- ('\n' if state.started else '') ~ labels[message.role] ~ '<ctrl0>' -}}
{%- set state.started = true -%}
{%- if message.role == 'assistant' -%}
{#- Every assistant turn has a reasoning block, empty when there is no reasoning. -#}
{{- '<ctrl1>' ~ text_of(message.reasoning_content) ~ '<ctrl2>' ~ text_of(message.content) -}}
{%- for call in message.tool_calls or [] -%}
{%- set arguments = call.function.arguments or {} -%}
{%- if arguments is string -%}
{{- '<ctrl3>{"name":' ~ (call.function.name | tojson) ~ ',"arguments":' ~ arguments ~ '}<ctrl4>' -}}
{%- else -%}
{{- '<ctrl3>' ~ ({'name': call.function.name, 'arguments': arguments} | tojson(separators=(',', ':'))) ~ '<ctrl4>' -}}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{- text_of(message.content) -}}
{#- The tool definitions go at the end of the first system turn. -#}
{%- if labels[message.role] == 'System' and state.tools -%}
{{- tool_definitions(state.tools) -}}
{%- set state.tools = none -%}
{%- endif -%}
{%- endif -%}
{{- '<ctrl100>' -}}
{%- endmacro -%}
{%- set state = namespace(started=false, tools=tools, answered=false) -%}
{%- if messages | selectattr('role', 'in', ['system', 'developer']) | list | length == 0 -%}
{{- turn({'role': 'system', 'content': default_system_prompt}, state) -}}
{%- endif -%}
{%- for message in messages -%}
{%- if message.role in ['tool', 'function'] and state.answered -%}
{#- Already written right after the assistant turn it answers. -#}
{%- continue -%}
{%- endif -%}
{%- set state.answered = false -%}
{{- turn(message, state) -}}
{%- if message.role == 'assistant' and message.tool_calls -%}
{#- The format has no call id: the n-th tool result answers the n-th call. Write the results that follow
in the order of the calls, then the results that match no call. -#}
{%- set state.answered = true -%}
{%- set results = namespace(messages=[]) -%}
{%- for result in messages[loop.index:] -%}
{%- if result.role not in ['tool', 'function'] -%}
{%- break -%}
{%- endif -%}
{%- set results.messages = results.messages + [result] -%}
{%- endfor -%}
{%- set call_ids = message.tool_calls | selectattr('id', 'defined') | map(attribute='id') | unique | list -%}
{%- for call_id in call_ids -%}
{%- for result in results.messages if result.tool_call_id == call_id -%}
{{- turn(result, state) -}}
{%- endfor -%}
{%- endfor -%}
{%- for result in results.messages if result.tool_call_id not in call_ids -%}
{{- turn(result, state) -}}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{- '\nAssistant<ctrl0><ctrl1>' -}}
{%- if enable_thinking is defined and not enable_thinking -%}
{{- '<ctrl2>' -}}
{%- endif -%}
{%- endif -%}