{# --------------------------- Header: identity + tools --------------------------- #} {{- '<|im_start|>developer\n' -}} {%- if model_identity -%} {{ model_identity }} {%- else -%} You are a large language model trained by Menhera.org. {%- endif -%} {%- if tools and tools|length > 0 -%} {{ "\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n" }} {%- for tool in tools -%} {{ "\n" }}{{ tool | tojson }} {%- endfor -%} {{ "\n\n\nWhen you decide to call a tool, output one or more tool calls using tags.\n\nEach tool call must be a JSON object in the following OpenAI-compatible shape:\n\n{\n \"id\": \"call_...\",\n \"type\": \"function\",\n \"function\": {\n \"name\": \"\",\n \"arguments\": \"\"\n }\n}\n\n\nNotes:\n- \"arguments\" MUST be a JSON-encoded string (not an object).\n- You may emit multiple blocks in a single assistant turn.\n- After tool calls, you will receive tool results in blocks.\n- Then continue the conversation.\n" }} {%- endif -%} {%- if schema -%} {{ '\n\n# Schema\n\nYou must follow the following JSON schema when you output the result. Only output the plain JSON.\n\n\n'}} {{- schema }} {{ '\n' }} {%- endif -%} {{- '<|im_end|>\n' -}} {# --------------------------- Conversation history --------------------------- #} {%- for message in messages -%} {# 1) system/user/assistant without tool_calls #} {%- if (message.role == "system") or (message.role == "user") or (message.role == "assistant" and not message.tool_calls) -%} {%- set speaker = (message.name if message.name is defined else message.role) -%} {{- '<|im_start|>' ~ message.role ~ '\n[' ~ speaker ~ ']\n' -}} {{- message.content -}} {{- '<|im_end|>\n' -}} {# 2) assistant WITH tool_calls (OpenAI shape) #} {%- elif message.role == "assistant" -%} {%- set speaker = (message.name if message.name is defined else "assistant") -%} {{- '<|im_start|>assistant\n[' ~ speaker ~ ']\n' -}} {%- if message.content -%} {{- message.content -}} {%- endif -%} {%- for tc in message.tool_calls -%} {%- set fn = (tc.function if tc.function is defined else none) -%} {%- if fn is not none -%} {{- '\n\n' -}} {{- '{' -}} {{- '"id": ' ~ (tc.id | tojson) ~ ', ' -}} {{- '"type": "function", ' -}} {{- '"function": {' -}} {{- '"name": ' ~ (fn.name | tojson) ~ ', ' -}} {# fn.arguments should already be a JSON STRING per OpenAI spec #} {{- '"arguments": ' ~ (fn.arguments | tojson) -}} {{- '}' -}} {{- '}' -}} {{- '\n' -}} {%- else -%} {# If someone stored a simplified toolcall shape by mistake, fail safe #} {{- '\n\n' -}} {{- tc | tojson -}} {{- '\n' -}} {%- endif -%} {%- endfor -%} {{- '<|im_end|>\n' -}} {# 3) tool messages: must include tool_call_id (OpenAI style) #} {%- elif message.role == "tool" -%} {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") -%} {{- '<|im_start|>tool\n' -}} {%- endif -%} {{- '\n' -}} {{- '{' -}} {%- if message.tool_call_id is defined -%} {{- '"tool_call_id": ' ~ (message.tool_call_id | tojson) ~ ', ' -}} {%- endif -%} {{- '"content": ' ~ (message.content | tojson) -}} {{- '}' -}} {{- '\n\n' -}} {%- if loop.last or (messages[loop.index0 + 1].role != "tool") -%} {{- '<|im_end|>\n' -}} {%- endif -%} {%- endif -%} {%- endfor -%} {# --------------------------- Generation prompt --------------------------- #} {%- if add_generation_prompt -%} {{- '<|im_start|>assistant\n[assistant]\n' -}} {%- endif -%}