File size: 4,681 Bytes
e80d915 |
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 |
{{- bos_token -}}
{%- set system_message = None %}
{%- set loop_messages = messages %}
{%- if messages[0]['role'] == 'system' -%}
{%- set system_message = messages[0]['content'] %}
{%- set loop_messages = messages[1:] %}
{%- else -%}
{%- set system_message = "You are an expert in composing functions." %}
{%- set loop_messages = messages %}
{%- endif %}
{%- set ns = namespace(in_user_turn=False, skip_until=-1) %}
{%- for message in loop_messages %}
{%- set idx = loop.index0 %}
{%- if idx > ns.skip_until %}
{%- if message['role'] == 'user' or message['role'] in ['tool', 'tool_results'] %}
{%- if not ns.in_user_turn %}
{{- "<start_of_turn>user" }}{{- "\n" -}}
{%- set ns.in_user_turn = True %}
{%- endif %}
{%- if message['role'] == 'user' %}
{%- if idx == 0 and system_message is not none -%}
{{- system_message }}{{- "\n" -}}
{%- if tools is not none %}
{%- for t in tools %}
{{- t | tojson() }}
{{-","-}}
{%- endfor %}
{{-"\n"-}}
{%- endif %}
{%- endif %}
{{- message.content -}}
{%- elif message['role'] in ['tool', 'tool_results'] %}
{{- "\n" -}}{{- "<unused96>" }}{{- "\n" -}}
{{- message.content -}}
{{- "\n" -}}{{- "<unused97>" }}
{%- endif %}
{%- set last_processed_idx = idx %}
{%- for next_idx in range(idx + 1, loop_messages|length) %}
{%- set next_message = loop_messages[next_idx] %}
{%- if next_message['role'] == 'user' or next_message['role'] in ['tool', 'tool_results'] %}
{%- if next_message['role'] == 'user' %}
{{- "\n" -}}{{- next_message.content -}}
{%- elif next_message['role'] in ['tool', 'tool_results'] %}
{{- "\n" -}}{{- "<unused96>" }}{{- "\n" -}}
{{- next_message.content -}}
{{- "\n" -}}{{- "<unused97>" }}
{%- endif %}
{%- set last_processed_idx = next_idx %}
{%- else %}
{%- break %}
{%- endif %}
{%- endfor %}
{%- set ns.skip_until = last_processed_idx %}
{%- elif message['role'] == 'assistant' %}
{%- if ns.in_user_turn %}
{{- "<end_of_turn>" }}{{- "\n" -}}
{%- set ns.in_user_turn = False %}
{%- endif %}
{{- "<start_of_turn>model" }}{{- "\n" -}}
{%- if message.get("content") %}
{{- message.content }}
{%- endif %}
{%- if message.get("tool_calls") %}
{%- if message.get("content") %}{{- "\n" -}}{%- endif %}
{{- "<unused94>" }}{{- "\n" -}}
[
{%- for tool in message.get("tool_calls") -%}
{%- if tool is mapping and tool.get("type") == "function" and tool.get("function") is mapping -%}
{%- set func = tool.get("function") -%}
{%- if not loop.first %}, {% endif -%}
{{- func.get("name", "") }}(
{%- if func.get("arguments") is string -%}
{{- func.get("arguments") -}}
{%- elif func.get("arguments") is mapping -%}
{%- for arg_name, arg_value in func.get("arguments").items() -%}
{%- if not loop.first %}, {% endif -%}
{%- if arg_value is string -%}
{{- arg_name }}='{{ arg_value -}}'
{%- else -%}
{{- arg_name }}={{ arg_value|tojson -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
)
{%- endif -%}
{%- endfor -%}
]{{- "\n" -}}
{{- "<unused95>" }}
{%- endif %}
{{- "<end_of_turn>" }}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if ns.in_user_turn %}
{{- "<end_of_turn>" }}
{%- endif %}
{%- if add_generation_prompt %}
{{- "\n" -}}{{ "<start_of_turn>model" }}{{- "\n" -}}
{%- endif %}
|