|
|
|
{{- bos_token -}} |
|
{%- set system_message = None %} |
|
{%- set loop_messages = messages %} |
|
{%- set ns = namespace(first_user=True) %} |
|
{%- 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 %} |
|
|
|
{%- for message in loop_messages -%} |
|
{%- set idx = loop.index0 %} |
|
{%- if message['role'] == 'user' %} |
|
{%- if ns.first_user -%} |
|
{{- "<start_of_turn>user" }}{{- " |
|
" -}} |
|
{%- set ns.first_user = False %} |
|
{%- else -%} |
|
{{- " |
|
" -}}{{ "<start_of_turn>user" }}{{- " |
|
" -}} |
|
{%- endif %} |
|
{%- if idx == 0 and system_message is not none -%} |
|
{{- system_message }} |
|
{{-" |
|
"-}} |
|
{%- if tools is not none %} |
|
{%- for t in tools %} |
|
{{- t | tojson() }} |
|
{{-","-}} |
|
{%- endfor %} |
|
{{-" |
|
"-}} |
|
{%- endif %} |
|
{{- message.content -}} |
|
{%- else -%} |
|
{{- message.content -}} |
|
{%- endif -%} |
|
{{-"<end_of_turn>"-}} |
|
{%- elif message['role'] == 'assistant' %} |
|
{{- " |
|
" -}}{{"<start_of_turn>model"}}{{- " |
|
" -}} |
|
{%- if message.get("content") %} |
|
{%- if message.get("tool_calls") %} |
|
{{- message.content }}{{- " |
|
" -}} |
|
{%- else -%} |
|
{{- message.content }} |
|
{%- endif -%} |
|
{%- endif %} |
|
{%- if message.get("tool_calls") %} |
|
{{- "<code>" }}{{- " |
|
" -}} |
|
{%- 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") -%} |
|
{{- 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 -%} |
|
{{- "</code>" }} |
|
{%- endif %} |
|
{%- for follow_idx in range(idx + 1, loop_messages|length) %} |
|
{%- if loop_messages[follow_idx]['role'] in ['tool', 'tool_results'] %} |
|
{{- " |
|
" -}}{{- "<result>" }}{{- " |
|
" -}} |
|
{{- loop_messages[follow_idx]['content'] -}} |
|
{{- " |
|
" -}}{{- "</result>" }} |
|
{%- else -%} |
|
{%- break %} |
|
{%- endif %} |
|
{%- endfor %} |
|
{{- "<end_of_turn>" -}} |
|
{%- endif %} |
|
{%- endfor %} |
|
|
|
{%- if add_generation_prompt %} |
|
{{- " |
|
" -}}{{ "<start_of_turn>model" }}{{- " |
|
" -}} |
|
{%- endif %} |
|
|