File size: 5,978 Bytes
1d0855f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{#- 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 -%}