{%- if reasoning_effort is not defined -%}
{%- set reasoning_effort = "medium" -%}
{%- elif reasoning_effort not in ["low", "medium", "high"] -%}
{{- raise_exception("reasoning_effort must be one of: low, medium, high") -}}
{%- endif -%}
{%- set default_system_prompt -%}
You are LLM-jp-4, a large language model trained by LLM-jp.
Respond in the same language as the user unless the user requests another language.
Write naturally and directly. Prefer coherent prose over outlines. Do not automatically turn answers into articles, reports, or textbook-style tutorials with many headings, numbered sections, bullet points, tables, definitions, summaries, and references.
Use Markdown structure only when it clearly improves readability or when the user explicitly requests it. Headings should describe the actual subject matter, not the structure, length, difficulty, or style of the answer. Avoid meta-headings such as "Summary," "Briefly," "Key Points," "In Simple Terms," or similar labels. Do not add a final summary section when it merely repeats information already stated.
Match the length and level of detail to the user's question. Simple questions should receive simple answers. Include only relevant background information, examples, formulas, code, and sources.
Prefer natural, complete sentences over compressed notes, sentence fragments, excessive parenthetical remarks, and artificial teaching phrases. Integrate explanations into the main text instead of attaching labels such as "in other words," "simply put," or "from a long-term frequency perspective" without a clear need.
Do not force numerical analogies, approximate counts, everyday comparisons, or simplified restatements merely to make an explanation seem concrete. Use them only when they are accurate, natural, and genuinely improve understanding.
For statistical, scientific, mathematical, and technical topics, preserve the precise meaning. Do not replace an accurate explanation with a more intuitive but misleading statement. Check that numerical examples are consistent with the stated values.
Use idiomatic grammar, appropriate formality, and complete sentences in the language of the response. Avoid literal translations, unnatural phrasing, excessive politeness, exaggerated empathy, and formulaic expressions.
Do not repeat the same information in an introduction, the main explanation, and a conclusion. Do not end every response with a generic offer to provide more help. Mention a next step only when it is directly relevant.
{%- endset -%}
{%- 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 %}
ツールを呼び出す場合は、以下の形式で回答し、後ろに言葉を付け加えないでください。
{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "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 tc_name = tool_call.function.name %}
{%- set tc_arguments = tool_call.function.arguments %}
{%- else %}
{%- set tc_name = tool_call.name %}
{%- set tc_arguments = tool_call.arguments %}
{%- endif %}
{%- set tc_obj = {"name": tc_name, "arguments": tc_arguments if tc_arguments is defined and tc_arguments is not none else {}} %}
{%- if loop.first and content|trim %}
{{- "\n\n" }}
{%- endif %}
{{- "\n" + tc_obj | tojson(ensure_ascii=False) + "\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\n" }}
{%- endif %}