{%- macro render_extra_keys(json_dict, handled_keys) -%} {%- if json_dict is mapping -%} {%- for json_key in json_dict if json_key not in handled_keys -%} {%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) -%} {{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '' -}} {%- else -%} {{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '' -}} {%- endif -%} {%- endfor -%} {%- endif -%} {%- endmacro -%} {%- if not add_generation_prompt is defined -%}{%- set add_generation_prompt = false -%}{%- endif -%} {%- set ns = namespace(system_prompt='', is_first_sp=true, is_last_user=false) -%} {%- set default_system = "You are JoyAI , a large language model trained by JD(京东)that can interact with a computer to solve tasks. Answer as concisely as possible." -%} {%- set ns.system_prompt = default_system -%} {%- for message in messages -%} {%- if message['role'] == 'system' -%} {%- if ns.is_first_sp -%} {%- set ns.system_prompt = message['content'] -%} {%- set ns.is_first_sp = false -%} {%- else -%} {%- set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] -%} {%- endif -%} {%- endif -%} {%- endfor -%} {{- bos_token -}}{{- ns.system_prompt -}} {%- if tools is iterable and tools | length > 0 -%} {{- "\n\n# Tools\n\nYou have access to the following functions:\n\n" }} {{- "" }} {%- for tool in tools %} {%- if tool.function is defined %} {%- set tool = tool.function %} {%- endif %} {{- "\n\n" ~ tool.name ~ "" }} {%- if tool.description is defined %} {{- '\n' ~ (tool.description | trim) ~ '' }} {%- endif %} {{- '\n' }} {%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %} {%- for param_name, param_fields in tool.parameters.properties|items %} {{- '\n' }} {{- '\n' ~ param_name ~ '' }} {%- if param_fields.type is defined %} {{- '\n' ~ (param_fields.type | string) ~ '' }} {%- endif %} {%- if param_fields.description is defined %} {{- '\n' ~ (param_fields.description | trim) ~ '' }} {%- endif %} {%- set handled_keys = ['name', 'type', 'description'] %} {{- render_extra_keys(param_fields, handled_keys) }} {{- '\n' }} {%- endfor %} {%- endif %} {% set handled_keys = ['type', 'properties'] %} {{- render_extra_keys(tool.parameters, handled_keys) }} {{- '\n' }} {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %} {{- render_extra_keys(tool, handled_keys) }} {{- '\n' }} {%- endfor %} {{- "\n" }} {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n\n\n\nvalue_1\n\n\nThis is the value for the second parameter\nthat can span\nmultiple lines\n\n\n\n\n\nReminder:\n- Function calls MUST follow the specified format: an inner block must be nested within XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n' }} {%- endif %} {%- for message in messages -%} {%- if message['role'] == 'user' -%} {%- set ns.is_last_user = true -%} {{- '<|User|>' + message['content'] -}} {%- elif message['role'] == 'assistant' -%} {%- if ns.is_last_user -%} {{ '<|Assistant|>' }} {%- endif -%} {%- set ns.is_last_user = false -%} {%- set content = message.get('content') | default('', true) -%} {{ '<|end_of_thought|>' + content }} {%- if message['tool_calls'] is defined and message['tool_calls'] is not none -%} {%- for tool in message['tool_calls'] -%} {%- if tool.function is defined %}{% set tool = tool.function %}{% endif -%} {{- '\n\n\n' -}} {%- if tool.arguments is defined -%} {%- if tool.arguments is string -%}{%- set args_data = tool.arguments | from_json -%}{%- else -%}{%- set args_data = tool.arguments -%}{%- endif -%} {%- for args_name, args_value in args_data.items() -%} {{- '\n' -}} {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string -%} {{- args_value -}}{{- '\n\n' -}} {%- endfor -%} {%- endif -%} {{- '\n' -}} {%- endfor -%} {%- endif -%} {{ '<|end▁of▁sentence|>' }} {%- elif message['role'] == 'tool' -%} {%- set ns.is_last_user = true -%} {{ '\n\n' + message['content'] + '\n' }} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt -%} {{ '<|Assistant|>' }}{{ '<|end_of_thought|>' }} {%- endif -%}