Spaces:
Running
Running
| {# --- MACRO DEFINITION --- #} | |
| {% macro format_parameters(properties, required) -%} | |
| {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%} | |
| {%- set ns = namespace(found_first=false) -%} | |
| {%- for key, value in properties.items() -%} | |
| {%- if key not in standard_keys -%} | |
| {%- if ns.found_first %},{% endif -%} | |
| {%- set ns.found_first = true -%} | |
| {{- key }}:{description:<escape>{{ value['description'] }}<escape>,nullable:{{ 'false' if key in required else 'true' }},type:<escape>{{ value['type'] | upper }}<escape> | |
| {%- if value['type'] | upper == 'STRING' -%} | |
| {%- if value['enum'] is not none -%} | |
| ,enum:{{ format_argument(value['enum']) }} | |
| {%- endif -%} | |
| {%- elif value['type'] | upper == 'OBJECT' -%} | |
| ,properties:{ | |
| {%- if value['properties'] is defined and value['properties'] is mapping -%} | |
| {{- format_parameters(value['properties'], value['required'] | default([])) -}} | |
| {%- elif value is mapping -%} | |
| {{- format_parameters(value, value['required'] | default([])) -}} | |
| {%- endif -%} | |
| },required:[ | |
| {%- for item in value['required'] | default([]) -%} | |
| <escape>{{- item -}}<escape> | |
| {%- if not loop.last %},{% endif -%} | |
| {%- endfor -%} | |
| ] | |
| {%- elif value['type'] | upper == 'ARRAY' -%} | |
| {%- if value['items'] is mapping and value['items'] -%} | |
| ,items:{ | |
| {%- set ns_items = namespace(found_first=false) -%} | |
| {%- for item_key, item_value in value['items'].items() -%} | |
| {%- if item_value is not none -%} | |
| {%- if ns_items.found_first %},{% endif -%} | |
| {%- set ns_items.found_first = true -%} | |
| {%- if item_key == 'properties' -%} | |
| properties:{ | |
| {%- if item_value is mapping -%} | |
| {{- format_parameters(item_value, value['items']['required'] | default([])) -}} | |
| {%- endif -%} | |
| } | |
| {%- elif item_key == 'required' -%} | |
| required:[ | |
| {%- for req_item in item_value -%} | |
| <escape>{{- req_item -}}<escape> | |
| {%- if not loop.last %},{% endif -%} | |
| {%- endfor -%} | |
| ] | |
| {%- elif item_key == 'type' -%} | |
| type:<escape>{{ item_value | upper }}<escape> | |
| {%- else -%} | |
| {{ item_key }}:{{ item_value }} | |
| {%- endif -%} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| } | |
| {%- endif -%} | |
| {%- endif -%} | |
| } | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- endmacro %} | |
| {% macro format_function_declaration(tool_data) -%} | |
| declaration:{{- tool_data['function']['name'] -}} | |
| {description:<escape>{{- tool_data['function']['description'] -}} | |
| <escape>,parameters:{properties:{ | |
| {{- format_parameters(tool_data['function']['parameters']['properties'], tool_data['function']['parameters']['required']) -}}},required:[ | |
| {%- for item in tool_data['function']['parameters']['required'] -%} | |
| <escape>{{- item -}}<escape> | |
| {%- if not loop.last %},{% endif -%} | |
| {%- endfor -%} ],type:<escape>{{ tool_data['function']['parameters']['type'] | upper }}<escape>}} | |
| {%- endmacro %} | |
| {% macro format_argument(argument) -%} | |
| {%- if argument is string -%} | |
| {{- '<escape>' + argument + '<escape>' -}} | |
| {%- elif argument is boolean -%} | |
| {%- if argument -%} | |
| {{- '<escape>true<escape>' -}} | |
| {%- else -%} | |
| {{- '<escape>false<escape>' -}} | |
| {%- endif -%} | |
| {%- elif argument is mapping -%} | |
| {{- '{' -}} | |
| {%- set ns = namespace(found_first=false) -%} | |
| {%- for key, value in argument.items() -%} | |
| {%- if ns.found_first %},{% endif -%} | |
| {%- set ns.found_first = true -%} | |
| {{- '<escape>' + key + '<escape>' -}}:{{- format_argument(value) -}} | |
| {%- endfor -%} | |
| {{- '}' -}} | |
| {%- elif argument is sequence -%} | |
| {{- '[' -}} | |
| {%- for item in argument -%} | |
| {{- format_argument(item) -}} | |
| {%- if not loop.last %},{% endif -%} | |
| {%- endfor -%} | |
| {{- ']' -}} | |
| {%- else -%} | |
| {{- argument -}} | |
| {%- endif -%} | |
| {%- endmacro %} | |
| {{ bos_token }} | |
| {%- set ns = namespace(prev_message_type=None) -%} | |
| {#- Tool Declarations -#} | |
| {%- set loop_messages = messages -%} | |
| {%- if tools or messages[0]['role'] == 'system' or messages[0]['role'] == 'developer' -%} | |
| {{- '<start_of_turn>developer | |
| ' -}} | |
| {%- if messages[0]['role'] == 'system' or messages[0]['role'] == 'developer' -%} | |
| {%- if messages[0]['content'] is string -%} | |
| {{- messages[0]['content'] | trim -}} | |
| {%- elif messages[0]['content'] is sequence -%} | |
| {%- for item in messages[0]['content'] -%} | |
| {%- if item['type'] == 'text' -%} | |
| {{- item['text'] | trim -}} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- endif -%} | |
| {%- set loop_messages = messages[1:] -%} | |
| {%- endif -%} | |
| {%- for tool in tools %} | |
| {{- '<start_function_declaration>' -}} | |
| {{- format_function_declaration(tool) | trim }} | |
| {{- '<end_function_declaration>' -}} | |
| {%- endfor %} | |
| {{- '<end_of_turn> | |
| '}} | |
| {%- 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 + ' | |
| '}} | |
| {%- endif -%} | |
| {%- set ns.prev_message_type = None -%} | |
| {%- if 'content' in message and message['content'] is not none -%} | |
| {%- if message['content'] is string -%} | |
| {{ message['content'] | trim }} | |
| {%- elif message['content'] is sequence -%} | |
| {%- for item in message['content'] -%} | |
| {%- if item['type'] == 'image' -%} | |
| {{ '<start_of_image>' }} | |
| {%- elif item['type'] == 'text' -%} | |
| {{ item['text'] | trim }} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- else -%} | |
| {{ raise_exception("Invalid content type in user/assistant message") }} | |
| {%- 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'] -%} | |
| {% set function = tool_call['function'] %} | |
| {{- '<start_function_call>call:' + function['name'] + '{' -}} | |
| {%- if 'arguments' in function -%} | |
| {%- if function['arguments'] is mapping -%} | |
| {%- set ns = namespace(found_first=false) -%} | |
| {%- for key, value in function['arguments'].items() -%} | |
| {%- if ns.found_first %},{% endif -%} | |
| {%- set ns.found_first = true -%} | |
| {{- key -}}:{{- format_argument(value) -}} | |
| {%- endfor -%} | |
| {%- elif function['arguments'] is string -%} | |
| {# This handles string-JSON, just in case #} | |
| {{ function['arguments'] }} | |
| {%- endif %} | |
| {{- '}<end_function_call>' -}} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- if loop.last -%} | |
| {{ '<start_function_response>' }} | |
| {%- endif -%} | |
| {%- set ns.prev_message_type = 'tool_call' -%} | |
| {%- endif -%} | |
| {%- else -%} | |
| {#- Tool Responses -#} | |
| {%- if 'content' in message and message['content'] is not none -%} | |
| {%- if message['content'] is mapping -%} | |
| {%- if 'name' in message['content'] and 'response' in message['content'] -%} | |
| {{ '<start_function_response>response:' + message['content']['name'] | trim + '{' }} | |
| {%- set response_ns = namespace(found_first=false) -%} | |
| {%- for key, value in message['content']['response'].items() -%} | |
| {%- if response_ns.found_first %},{% endif -%} | |
| {%- set response_ns.found_first = true -%} | |
| {{- key -}}:{{- format_argument(value) -}} | |
| {%- endfor -%} | |
| {{- '}<end_function_response>' -}} | |
| {%- else -%} | |
| {{ raise_exception("Invalid tool response mapping: must contain 'name' and 'response' keys.") }} | |
| {%- endif -%} | |
| {%- elif message['content'] is string -%} | |
| {{ '<start_function_response>' + message['content'] | trim + '<end_function_response>' }} | |
| {%- elif message['content'] is sequence -%} | |
| {%- for item in message['content'] -%} | |
| {%- if item is mapping -%} | |
| {%- if 'name' in item and 'response' in item -%} | |
| {{ '<start_function_response>response:' + item['name'] | trim + '{' }} | |
| {%- set response_ns = namespace(found_first=false) -%} | |
| {%- for key, value in item['response'].items() -%} | |
| {%- if response_ns.found_first %},{% endif -%} | |
| {%- set response_ns.found_first = true -%} | |
| {{- key -}}:{{- format_argument(value) -}} | |
| {%- endfor -%} | |
| {{- '}<end_function_response>' -}} | |
| {%- else -%} | |
| {{ raise_exception("Invalid tool response mapping in sequence: must contain 'name' and 'response' keys.") }} | |
| {%- endif -%} | |
| {%- elif item is string -%} | |
| {{ '<start_function_response>' + item | trim + '<end_function_response>' }} | |
| {%- else -%} | |
| {{ raise_exception("Invalid item type in tool response sequence: must be mapping or string.") }} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- else -%} | |
| {{ raise_exception("Invalid content type in tool message") }} | |
| {%- endif -%} | |
| {%- endif -%} | |
| {%- set ns.prev_message_type = 'tool_response' -%} | |
| {%- endif -%} | |
| {%- if ns.prev_message_type not in ['tool_call', 'tool_response'] -%} | |
| {{ '<end_of_turn> | |
| ' }} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- if add_generation_prompt -%} | |
| {%- if ns.prev_message_type != 'tool_response' -%} | |
| {{- '<start_of_turn>model\n' -}} | |
| {%- endif -%} | |
| {%- endif -%} |