|
"chat_template": "{% set BEGIN_TOOL_USE_TOKEN = '<tool>' %}{% set END_TOOL_USE_TOKEN = '</tool>' %}{% set IMAGE_RESERVED_TOKEN = '<image>' %}{% set VIDEO_RESERVED_TOKEN = '<video>' %}{% set MEDIA_TOKEN_SEP = '\n' %}{% set DEFAULT_SYSTEM_MSG = 'A chat between a curious User and an artificial intelligence Bot. The Bot gives helpful, detailed, and polite answers to the User\\'s questions.' %}{% set GLOBAL = namespace(TOOL_USE_ORDERED = []) %}{% macro has_system_content_blocks(system) %}{% set result = namespace(value = '') %}{% if system is string %}{% set result.value = 'True' %}{% elif system is iterable and system is not mapping %}{% for item in system %}{% if item is string or 'text' in item or 'image' in item or 'video' in item %}{% set result.value = 'True' %}{% endif %}{% endfor %}{% endif %}{{ result.value }}{% endmacro %}{% macro format_param_schema(param_name, schema, is_required) %}{% if schema is undefined or not schema is mapping %}{{ raise_exception('Invalid json in tool input schema. Expected a valid JSON Schema object for ' ~ param_name ~ ' parameter.') }}{% endif %}{{ '{' }}{% for k,v in schema.items() %}{% if k == \"required\" %}{{ '\"required_properties\": '}}{% else %}{{ '\"'~ k ~ '\": ' }}{% endif %}{{ v | tojson }}{% if not loop.last %}{{ ', ' }}{% elif is_required %}{{ ', \"required\": True' }}{% endif %}{% endfor %}{{ '}' }}{% endmacro %}{% macro format_tool_input_schema(json_schema) %}{% if json_schema is undefined %}{{ '\"arguments\": {}' }}{% else %}{% if not json_schema is mapping %}{{ raise_exception('Invalid json in tool input schema. Expected a valid JSON Schema object.') }}{% endif %}{% for key in json_schema %}{% if not key in ['type', 'properties', 'required'] %}{{ raise_exception('Invalid json in tool input schema. Unexpected key \\'' ~ key ~'\\'. Only [\\'type\\', \\'properties\\', \\'required\\'] are supported.') }}{% endif %}{% endfor %}{% if json_schema['type'] is undefined or json_schema['type'] != 'object' %}{{ raise_exception('Invalid json in tool input schema. Expected key \\'type\\' with value \\'object\\'.') }}{% endif %}{% if json_schema['properties'] is undefined %}{{ '\"arguments\": {}' }}{% else %}{{ '\"arguments\": {' }}{% for k,v in json_schema['properties'].items() %}{{ '\"' ~ k ~ '\": ' }}{% set is_required = true if json_schema['required'] is defined and k in json_schema['required'] %}{{ format_param_schema(k, v, is_required) }}{{ ', ' if not loop.last }}{% endfor %}{{ '}' }}{% endif %}{% endif %}{% endmacro %}{% macro format_tool_def(tool) %}{% set toolDef = tool['toolSpec'] %}{% if toolDef is undefined %}{{ raise_exception('Invalid tool definition. Expected key \\'toolSpec\\' in tool definition.') }}{% endif %}{% if toolDef['name'] is undefined %}{{ raise_exception('Invalid tool specification. Expected key \\'name\\' in tool specification.') }}{% endif %}{% set input_schema = toolDef['inputSchema'] %}{% if input_schema is undefined %}{{ raise_exception('Invalid tool specification. Expected key \\'inputSchema\\' in tool specification.') }}{% endif %}{{ '{\"name\": \"' }}{{ toolDef['name'] }}{{ '\", ' }}{% if toolDef['description'] is defined %}{{ '\"description\": \"' }}{{ toolDef['description'] }}{{ '\", ' }}{% endif %}{{ format_tool_input_schema(input_schema['json']) }}{{ '}' }}{% endmacro %}{% macro format_pythonic_function_params(json_object) %}{% for k,v in json_object.items() %}{{ k }}{{ '=' }}{{ v | tojson }}{{', ' if not loop.last}}{% endfor %}{% endmacro %}{% macro format_tool_use(tool_use_blocks)%}{{ BEGIN_TOOL_USE_TOKEN }}{{ ' [' }}{% for tool_use in tool_use_blocks %}{% set tool_name = tool_use.get('name') %}{% set tool_params = tool_use.get('input') %}{% if not tool_name is defined %}{{ raise_exception('Invalid tool use block. Expected key ''name'' in tool use block.') }}{% endif %}{% if not tool_params is defined %}{{ raise_exception('Invalid tool use block. Expected key ''input'' in tool use block.') }}{% endif %}{{ tool_name }}{{ '(' }}{{ format_pythonic_function_params(tool_params) }}{{ ')' }}{{', ' if not loop.last}}{% endfor %}{{ '] ' }}{{ END_TOOL_USE_TOKEN }}{% endmacro %}{% macro find_tool_name(tool_use_ordered, use_id) %}{% for tool_use in tool_use_ordered %}{% if tool_use['toolUseId'] == use_id %}{{ tool_use['name'] }}{% endif %}{% else %}{{ raise_exception('Invalid tool use id: ' ~ use_id) }}{% endfor %}{% endmacro %}{% macro format_tool_result(tool_result_blocks, tool_use_ordered) %}{% set ordered = namespace(tool_results = []) %}{% for tool_use in tool_use_ordered %}{% for tool_result in tool_result_blocks %}{% if tool_result['toolUseId'] == tool_use['toolUseId'] %}{% set ordered.tool_results = ordered.tool_results + [tool_result] %}{% endif %}{% endfor %}{% endfor %}{% if (ordered.tool_results | length) != (tool_result_blocks | length) %}{{ raise_exception('Invalid tool result blocks. Not every tool result has corresponding tool use.') }}{% endif %}{{ 'Resource: {\"toolResult\": ['}}{% for tool_result in ordered.tool_results %}{{ '{\"tool_name\": \"' }}{{ find_tool_name(tool_use_ordered, tool_result['toolUseId']) }}{{ '\", \"result\": ' }}{% set content = tool_result.get('content') %}{% if content is undefined or content | length == 0 %}{% if tool_result.get('status') == 'error' %}{{ '\"unknown error occured\"' }}{% else %}{{ '\"\"' }}{% endif %}{% else %}{% if content is iterable and content | length == 1 and content[0]['json'] is defined %}{{ content[0]['json'] | tojson }}{% else %}{% if content is iterable and (content | selectattr('json', 'defined') | list | length > 0) %}{{ raise_exception('Invalid tool result content. Expected either single JSON block or array of non-JSON blocks.') }}{% endif %}{% set content_formatted = get_shorthand_content(content) %}{{ '\"' }}{{ content_formatted }}{{ '\"' }}{% endif %}{% endif %}{{ '}' }}{{', ' if not loop.last}}{% endfor %}{{ ']}' }}{% endmacro %}{% macro get_shorthand_content(content) %}{% if content is string %}{{ content | trim }}{% elif content is iterable and content is not mapping %}{% set local=namespace(tool_use_ordered = [], tool_results_ordered = []) %}{% for item in content %}{% if 'text' in item %}{{ item['text'] | trim }}{{' ' if not loop.last}}{% elif 'image' in item %}{{ IMAGE_RESERVED_TOKEN }}{{ MEDIA_TOKEN_SEP }}{% elif 'video' in item %}{{ VIDEO_RESERVED_TOKEN }}{{ MEDIA_TOKEN_SEP }}{% elif 'toolResult' in item %}{% set local.tool_results_ordered = local.tool_results_ordered + [item['toolResult']] %}{% if loop.last or 'toolResult' not in content[loop.index] %}{{ format_tool_result(local.tool_results_ordered, GLOBAL.TOOL_USE_ORDERED) }}{{' ' if not loop.last}}{% set local.tool_results_ordered = [] %}{% endif %}{% elif 'toolUse' in item %}{% set local.tool_use_ordered = local.tool_use_ordered + [item['toolUse']] %}{% set GLOBAL.TOOL_USE_ORDERED = GLOBAL.TOOL_USE_ORDERED + [item['toolUse']] %}{% if loop.last or 'toolUse' not in content[loop.index] %}{{ format_tool_use(local.tool_use_ordered) }}{{' ' if not loop.last}}{% set local.tool_use_ordered = [] %}{% endif %}{% endif %}{% endfor %}{% else %}{{ raise_exception('Invalid type for message[''content'']. Expected types are only String and List') }}{% endif %}{% endmacro %}{% macro get_system_prompt_content(system, toolConfig) %}{% if system is defined and has_system_content_blocks(system) == 'True' %}{% set system_msg = DEFAULT_SYSTEM_MSG + ' ' + get_shorthand_content(system) %}{% else %}{% set system_msg = DEFAULT_SYSTEM_MSG %}{% endif %}{{ system_msg }}{% if toolConfig is defined %}{% set toolChoice = toolConfig['toolChoice'] %}{% if toolChoice is defined and ('any' in toolChoice or 'tool' in toolChoice) %}{{ raise_exception('Invalid tool configuration. Only supported toolChoice value is \\'auto\\'.') }}{% endif %}{% set tools_list = toolConfig['tools'] %}{% if tools_list is undefined %}{{ raise_exception('Invalid tool configuration. Expected key \\'tools\\' in tool configuration.') }}{% endif %}{{ ' ' if system_msg[-1] | trim | length > 0 else '' }}{{ 'In this session, the model has access to external functionalities.\n' }}{{ 'To assist the user, you can reply to the user or invoke an action. Only invoke actions if relevant to the user request.\n\n' }}{{ 'The following actions are available:\n' }}{% for tool in tools_list %}{{ '- ' }}{{ format_tool_def(tool) }}{{ '\n' }}{% endfor %}{{ '\nModel Instructions:\n' }}{{ '- To invoke an action, begin with ' }}{{ BEGIN_TOOL_USE_TOKEN }}{{ ' and end with ' }}{{ END_TOOL_USE_TOKEN }}{{ '. Generate Pythonic action calls and place them in an array, e.g., ' }}{{ BEGIN_TOOL_USE_TOKEN }}{{ ' [action1(arg1=val1, ...), action2(arg1=val1)] ' }}{{ END_TOOL_USE_TOKEN }}{{ '.' }}\n{% endif %}{% endmacro %}{% macro validate_roles(messages) %}{% set roles = messages | map(attribute='role') | unique | list %}{% set allowed_roles = ['user', 'bot', 'system', 'assistant'] %}{% if ('bot' in roles and 'assistant' in roles) or (roles | reject('in', allowed_roles) | list | length > 0) %}{{ raise_exception('Message roles: [' + roles | join(', ') + '] has unexpected role(s)') }}{% endif %}{% endmacro %}{% macro get_prompt() %}{{ validate_roles(messages) }}{% for message in messages %}{% if loop.index == 1 %}{% if message['role'] != 'system' %}{% set SYSTEM_MSG = get_system_prompt_content(system, toolConfig)%}{% elif system is defined %}{{ raise_exception('system field is defined, unexpected system role in messages') }}{% else %}{% set SYSTEM_MSG = get_system_prompt_content(message['content'], toolConfig) %}{% endif %}{{ SYSTEM_MSG }}{{ ' ' if SYSTEM_MSG[-1] | trim | length > 0 else '' }}{% endif %}{% if beliefAugmentation is defined and beliefAugmentation | length != 0 %}{% set BELIEF_AUGMENTATION_SEP = '\n' %}{{get_shorthand_content(beliefAugmentation)}}{{BELIEF_AUGMENTATION_SEP}}{% endif %}{% if message['role'] == 'user' %}{{'User: ' }}{{get_shorthand_content(message['content'])}}{% elif message['role'] == 'bot' or message['role'] == 'assistant' %}{{'Bot: ' }}{{get_shorthand_content(message['content'])}}{{'' if loop.last else ' [EOS]'}}{% elif message['role'] == 'system' and loop.index != 1 %}{{ raise_exception('Unexpected system message in the middle of the messages') }}{% endif %}{{ ' ' if not loop.last and message['role'] != 'system' else '' }}{{ ' Bot:' if loop.last and message['role'] == 'user' else '' }}{% endfor %}{% endmacro %}{% set prompt = get_prompt() %}{{'[BOS] ' + prompt if prompt else prompt}}", |