{%- set allowed_effort = ["none", "low", "medium", "high"] -%} {%- set reasoning_effort = reasoning_effort if reasoning_effort is defined and reasoning_effort in allowed_effort else "medium" -%} {%- set default_system_prompt = "You are LLM-jp-4, a large language model trained by LLM-jp." -%} {%- set message_end = "<|im_end|>\n" -%} {%- set assistant_end = eos_token if eos_token is defined else "<|return|>" -%} {%- macro tool_to_json(tool) -%} {%- set ns_tool = namespace(first=true) -%} {{ "{" -}} {%- for k, v in tool.items() -%} {%- if k != "defer_loading" and k != "strict" -%} {%- if not ns_tool.first -%}{{- ", " -}}{%- endif -%} {%- set ns_tool.first = false -%} "{{ k }}": {{ v | tojson(ensure_ascii=False) }} {%- endif -%} {%- endfor -%} {{- "}" -}} {%- endmacro -%} {%- macro render_content(content) -%} {%- if content is string -%} {{- content -}} {%- elif content is mapping -%} {%- if content.text is defined -%} {{- content.text -}} {%- else -%} {{- content | tojson(ensure_ascii=False) -}} {%- endif -%} {%- elif content is iterable -%} {%- for item in content -%} {%- if item is string -%} {{- item -}} {%- elif item is mapping -%} {%- if item.type is defined and item.type == "text" and item.text is defined -%} {{- item.text -}} {%- elif item.text is defined -%} {{- item.text -}} {%- else -%} {{- item | tojson(ensure_ascii=False) -}} {%- endif -%} {%- else -%} {{- item | string -}} {%- endif -%} {%- endfor -%} {%- elif content is none or content is undefined -%} {%- else -%} {{- content | string -}} {%- endif -%} {%- endmacro -%} {%- if tools -%} <|im_start|>system # ツール タスクを解決するために、1つまたは複数のツールを呼び出すことができます。 ツールのシグネチャは、 XMLタグ内に記載されています。 {% for tool in tools %} {%- if "function" in tool -%} {%- set tool = tool["function"] -%} {%- endif -%} {%- if tool.defer_loading is not defined or not tool.defer_loading -%} {{ tool_to_json(tool) }} {{- "\n" }} {%- endif -%} {% endfor %} ツールを呼び出す場合は、以下の形式で回答し、後ろに言葉を付け加えないでください。 value_1 This is the value for the second parameter {%- if messages and messages[0].role == "system" %} {{- "\n\n" + (render_content(messages[0].content)|trim) }} {%- else %} {{- "\n\n" + default_system_prompt }} {%- endif %} {{- "\n\nReasoning effort: " + reasoning_effort }} {{- message_end }} {%- elif messages and messages[0].role == "system" -%} {{- "<|im_start|>system\n" + (render_content(messages[0].content)|trim) + "\n\nReasoning effort: " + reasoning_effort + message_end }} {%- else -%} {{- "<|im_start|>system\n" + default_system_prompt + "\n\nReasoning effort: " + reasoning_effort + message_end }} {%- endif -%} {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) -%} {%- for message in messages[::-1] -%} {%- set index = (messages|length - 1) - loop.index0 -%} {%- if ns.multi_step_tool and message.role == "user" -%} {%- set content = render_content(message.content)|trim -%} {%- if not(content.startswith("") and content.endswith("")) -%} {%- set ns.multi_step_tool = false -%} {%- set ns.last_query_index = index -%} {%- endif -%} {%- endif -%} {%- endfor -%} {%- if ns.multi_step_tool %} {{- raise_exception("No user query found in messages.") }} {%- endif -%} {%- for message in messages -%} {%- set content = render_content(message.content)|trim -%} {%- if message.role == "system" %} {%- if not loop.first %} {{- raise_exception("System message must be at the beginning.") }} {%- endif %} {%- elif message.role == "user" -%} {{- "<|im_start|>user\n" + content + message_end }} {%- elif message.role == "assistant" %} {%- set reasoning_content = "" -%} {%- if message.reasoning_content is string -%} {%- set reasoning_content = message.reasoning_content -%} {%- else -%} {%- if "" in content -%} {%- set reasoning_content = content.split("")[0].rstrip("\n").split("")[-1].lstrip("\n") -%} {%- set content = content.split("")[-1].lstrip("\n") -%} {%- endif -%} {%- endif -%} {%- set reasoning_content = reasoning_content|trim -%} {%- if loop.index0 > ns.last_query_index %} {{- "<|im_start|>assistant\n\n" + reasoning_content + "\n\n\n" + content }} {%- else %} {{- "<|im_start|>assistant\n" + content }} {%- endif %} {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %} {%- for tool_call in message.tool_calls %} {%- if tool_call.function is defined %} {%- set tool_call = tool_call.function %} {%- endif %} {%- if loop.first %} {%- if content|trim %} {{- "\n\n\n\n" }} {%- else %} {{- "\n\n" }} {%- endif %} {%- else %} {{- "\n\n\n" }} {%- endif %} {%- if tool_call.arguments is defined and tool_call.arguments is not none %} {%- if tool_call.arguments is mapping %} {%- for args_name, args_value in tool_call.arguments|items %} {{- "\n" }} {%- set args_value = args_value | tojson(ensure_ascii=False) if args_value is mapping or (args_value is iterable and args_value is not string and args_value is not mapping) else args_value | string %} {{- args_value }} {{- "\n\n" }} {%- endfor %} {%- elif tool_call.arguments is string %} {{- raise_exception("tool_call.arguments must be a mapping.") }} {%- else %} {{- raise_exception("tool_call.arguments must be a mapping.") }} {%- endif %} {%- endif %} {{- "\n\n" }} {%- endfor %} {%- endif %} {{- assistant_end }} {%- elif message.role == "tool" %} {%- if loop.previtem and loop.previtem.role != "tool" %} {{- "<|im_start|>user" }} {%- endif %} {{- "\n\n" }} {{- content }} {{- "\n" }} {%- if loop.last or loop.nextitem.role != "tool" %} {{- message_end }} {%- endif %} {%- else %} {{- raise_exception("Unexpected message role.") }} {%- endif %} {%- endfor -%} {%- if add_generation_prompt %} {{- "<|im_start|>assistant\n" }} {%- if reasoning_effort == "none" %} {{- "\n\n\n\n" }} {%- else %} {{- "\n" }} {%- endif %} {%- endif %}