Spaces:
Running
Running
| {{ bos_token }} | |
| {%- set ns = namespace(prev_message_type=None) -%} | |
| {#- Tool Declarations -#} | |
| {%- set loop_messages = messages -%} | |
| {%- if tools or messages[0]['role'] == 'system' -%} | |
| {{- '<start_of_turn>developer\n' -}} | |
| {%- if messages[0]['role'] == 'system' -%} | |
| {%- if messages[0]['content'] is string -%} | |
| {{- messages[0]['content'] | trim -}} | |
| {%- else -%} | |
| {%- for item in messages[0]['content'] -%} | |
| {%- if item['type'] == 'text' -%} | |
| {{- item['text'] | trim -}} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- endif -%} | |
| {%- set loop_messages = messages[1:] -%} | |
| {%- if tools -%} | |
| {{- '\n\n' -}} | |
| {%- endif -%} | |
| {%- endif -%} | |
| {%- for tool in tools %} | |
| {{- '<start_function_declaration>' -}} | |
| {{- tool | trim }} | |
| {{- '<end_function_declaration>' -}} | |
| {%- endfor %} | |
| {{- '<end_of_turn>\n'}} | |
| {%- endif %} | |
| {#- Loop through messages. -#} | |
| {%- for message in loop_messages -%} | |
| {%- if (message['role'] == 'assistant') -%} | |
| {#- Rename "assistant" to "model". -#} | |
| {%- set role = "model" -%} | |
| {%- else -%} | |
| {%- set role = message['role'] -%} | |
| {%- endif -%} | |
| {%- if role != 'tool' -%} | |
| {%- if ns.prev_message_type == 'tool_call' -%} | |
| {{ raise_exception("Tool call must be followed by a tool response.") }} | |
| {%- endif -%} | |
| {%- if ns.prev_message_type != 'tool_response' -%} | |
| {{- '<start_of_turn>' + role + '\n'}} | |
| {%- endif -%} | |
| {%- set ns.prev_message_type = None -%} | |
| {%- if 'content' in message -%} | |
| {%- if message['content'] is string -%} | |
| {{ message['content'] | trim }} | |
| {%- elif message['content'] is iterable -%} | |
| {%- for item in message['content'] -%} | |
| {%- if item['type'] == 'text' -%} | |
| {{ item['text'] | trim }} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- else -%} | |
| {{ raise_exception("Invalid content type") }} | |
| {%- endif -%} | |
| {%- set ns.prev_message_type = 'content' -%} | |
| {%- endif -%} | |
| {%- if 'tool_calls' in message and message['tool_calls'] and message['tool_calls'] is iterable -%} | |
| {#- Tool Calls -#} | |
| {%- for tool_call in message['tool_calls'] -%} | |
| {%- if 'function' in tool_call -%} | |
| {%- set tool_call = tool_call['function'] -%} | |
| {%- endif -%} | |
| {{- '<start_function_call>call:' + tool_call['name'] + '{' -}} | |
| {%- if 'arguments' in tool_call -%} | |
| {%- for key in tool_call['arguments'] -%} | |
| {{- key + ':' + tool_call['arguments'][key] -}} | |
| {% if not loop.last %} | |
| {{- ',' -}} | |
| {% endif %} | |
| {%- endfor %} | |
| {%- endif -%} | |
| {{- '}' + '<end_function_call>' -}} | |
| {%- endfor -%} | |
| {%- if loop.last -%} | |
| {{ '<start_function_response>' }} | |
| {%- endif -%} | |
| {%- set ns.prev_message_type = 'tool_call' -%} | |
| {%- endif -%} | |
| {%- else -%} | |
| {#- Tool Responses -#} | |
| {%- if 'content' in message -%} | |
| {%- if message['content'] is string -%} | |
| {{- '<start_function_response>response:' -}} | |
| {{ message['content'] | trim }} | |
| {{- '<end_function_response>' -}} | |
| {%- elif message['content'] is iterable -%} | |
| {%- for item in message['content'] -%} | |
| {%- if item['type'] == 'text' -%} | |
| {{ '<start_function_response>response:' + item['text'] + '<end_function_response>' }} | |
| {%- else -%} | |
| {{ raise_exception("Invalid content type for tool response.") }} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- else -%} | |
| {{ raise_exception("Invalid content type") }} | |
| {%- endif -%} | |
| {%- endif -%} | |
| {%- set ns.prev_message_type = 'tool_response' -%} | |
| {%- endif -%} | |
| {%- if ns.prev_message_type not in ['tool_call', 'tool_response'] -%} | |
| {{ '<end_of_turn>\n' }} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- if add_generation_prompt -%} | |
| {%- if ns.prev_message_type == 'tool_call' -%} | |
| {{- '<start_function_response>' -}} | |
| {%- elif ns.prev_message_type != 'tool_response' -%} | |
| {{- '<start_of_turn>model\n' -}} | |
| {%- endif -%} | |
| {%- endif -%} |