mirror of
https://github.com/ikawrakow/ik_llama.cpp.git
synced 2026-01-26 17:20:01 +00:00
176 lines
11 KiB
Django/Jinja
176 lines
11 KiB
Django/Jinja
{#- ========== MiroThinker System Message ========== #}
|
|
|
|
{%- set system_message = namespace(role='system', content='') %}
|
|
{%- if date_string is string %}
|
|
{%- set date_string = 'Today is: ' + date_string %}
|
|
{%- else %}
|
|
{%- set date_string = '' %}
|
|
{%- endif %}
|
|
{%- if tools %}
|
|
{%- set system_message.content = "In this environment you have access to a set of tools you can use to answer the user's question. \n\nYou only have access to the tools provided below. You can only use one tool per message, and will receive the result of that tool in the user's next response. You use tools step-by-step to accomplish a given task, with each tool-use informed by the result of the previous tool-use. " + date_string + "\n\n# Tool-Use Formatting Instructions \n\nTool-use is formatted using XML-style tags. The tool-use is enclosed in <use_mcp_tool></use_mcp_tool> and each parameter is similarly enclosed within its own set of tags.\n\nThe Model Context Protocol (MCP) connects to servers that provide additional tools and resources to extend your capabilities. You can use the server's tools via the `use_mcp_tool`.\n\nDescription: \nRequest to use a tool provided by a MCP server. Each MCP server can provide multiple tools with different capabilities. Tools have defined input schemas that specify required and optional parameters.\n\nParameters:\n- server_name: (required) The name of the MCP server providing the tool\n- tool_name: (required) The name of the tool to execute\n- arguments: (required) A JSON object containing the tool's input parameters, following the tool's input schema, quotes within string must be properly escaped, ensure it's valid JSON\n\nUsage:\n<use_mcp_tool>\n<server_name>server name here</server_name>\n<tool_name>tool name here</tool_name>\n<arguments>\n{\n\"param1\": \"value1\",\n\"param2\": \"value2 \\\"escaped string\\\"\"\n}\n</arguments>\n</use_mcp_tool>\n\nImportant Notes:\n- Tool-use must be placed **at the end** of your response, **top-level**, and not nested within other tags.\n- Always adhere to this format for the tool use to ensure proper parsing and execution.\n\nString and scalar parameters should be specified as is, while lists and objects should use JSON format. Note that spaces for string values are not stripped. The output is not expected to be valid XML and is parsed with regular expressions.\nHere are the functions available in JSONSchema format:\n\n" %}
|
|
{%- set ns = namespace(formatted_tools='', last_server='') %}
|
|
{%- for tool in tools %}
|
|
{%- set tool_name = tool.function.name.split('_') %}
|
|
{%- if tool_name | length > 1 %}
|
|
{%- set curr_server = tool_name[0] %}
|
|
{%- set tool_name = tool_name[1:] | join('_') %}
|
|
{%- else %}
|
|
{%- set curr_server = 'system_default' %}
|
|
{%- set tool_name = tool.function.name %}
|
|
{%- endif %}
|
|
{%- if curr_server != ns.last_server %}
|
|
{%- set ns.formatted_tools = ns.formatted_tools + "\n## Server name: " + curr_server + "\n" %}
|
|
{%- set ns.last_server = curr_server %}
|
|
{%- endif %}
|
|
{%- set ns.formatted_tools = ns.formatted_tools + "### Tool name: " + tool_name + "\n" %}
|
|
{%- set ns.formatted_tools = ns.formatted_tools + "Description: " + tool.function.description + "\n" %}
|
|
{%- set ns.formatted_tools = ns.formatted_tools + "Input JSON schema: " + ( tool.parameters | tojson(ensure_ascii=False) ) + "\n" %}
|
|
{%- set ns.formatted_tools = ns.formatted_tools + '\n' %}
|
|
{%- endfor %}
|
|
{%- set system_message.content = system_message.content + ns.formatted_tools + "\n# General Objective\n\nYou accomplish a given task iteratively, breaking it down into clear steps and working through them methodically.\n\n" %}
|
|
{%- set tools = None %}
|
|
{%- if messages[0].role == 'system' %}
|
|
{%- if messages[0].content.split('</use_mcp_tool>') | length > 1 %}
|
|
{%- set system_message = messages[0] %}
|
|
{%- set messages = messages[1:] %}
|
|
{%- endif %}
|
|
{%- if messages[0].content.split('</tools>') | length > 1 %}
|
|
{%- set messages = messages[1:] %}
|
|
{%- endif %}
|
|
{%- endif %}
|
|
{%- else %}
|
|
{%- set system_message.content = "In this environment you have access to a set of tools you can use to answer the user's question. " + date_string + "\n\nImportant Notes:\n- Tool-use must be placed **at the end** of your response, **top-level**, and not nested within other tags.\n- Always adhere to this format for the tool use to ensure proper parsing and execution.\n\nString and scalar parameters should be specified as is, while lists and objects should use JSON format. Note that spaces for string values are not stripped. The output is not expected to be valid XML and is parsed with regular expressions.\n\n# General Objective\n\nYou accomplish a given task iteratively, breaking it down into clear steps and working through them methodically.\n\n" %}
|
|
{%- if messages[0].role == 'system' %}
|
|
{%- set system_message = messages[0] %}
|
|
{%- set messages = messages[1:] %}
|
|
{%- endif %}
|
|
{%- endif %}
|
|
{%- set messages = [system_message] + messages %}
|
|
|
|
{#- ========== MiroThinker Tool Response ========== #}
|
|
|
|
{%- for message in messages %}
|
|
{%- if message.role == 'assistant' %}
|
|
{%- if message.tool_calls %}
|
|
{%- set message.content = message.content + "\n<use_mcp_tool>" %}
|
|
{%- for tool_call in message.tool_calls %}
|
|
{%- set tool = tool_call.function %}
|
|
{%- set tool_name = tool.name.split('_') %}
|
|
{%- if tool_name | length > 1 %}
|
|
{%- set server_name = tool_name[0] %}
|
|
{%- set tool_name = tool_name[1:] | join('_') %}
|
|
{%- else %}
|
|
{%- set server_name = 'system_default' %}
|
|
{%- set tool_name = tool.name %}
|
|
{%- endif %}
|
|
{%- set message.content = message.content + "\n<server_name>" + server_name + "</server_name>\n<tool_name>" + tool_name + "</tool_name>\n<arguments>\n" + (tool.arguments | tojson(ensure_ascii=False)) + "\n</arguments>" %}
|
|
{%- endfor %}
|
|
{%- set message.content = message.content + "\n</use_mcp_tool>" %}
|
|
{%- set message.tool_calls = [] %}
|
|
{%- endif %}
|
|
{%- elif message.role == 'user' %}
|
|
{%- if message.content.startswith('<tool_response>') %}
|
|
{%- set message.content = message.content[15:] | trim %}
|
|
{%- endif %}
|
|
{%- elif message.role == 'tool' %}
|
|
{%- set message.role = 'user' %}
|
|
{%- endif %}
|
|
{%- endfor %}
|
|
|
|
{#- ========== MiroThinker Tool Usage Patched ========== #}
|
|
|
|
{%- if tools %}
|
|
{{- '<|im_start|>system\n' }}
|
|
{%- if messages[0].role == 'system' %}
|
|
{{- messages[0].content + '\n\n' }}
|
|
{%- endif %}
|
|
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
|
|
{%- for tool in tools %}
|
|
{{- "\n" }}
|
|
{{- tool | tojson }}
|
|
{%- endfor %}
|
|
{{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
|
|
{%- else %}
|
|
{%- if messages[0].role == 'system' %}
|
|
{{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
|
|
{%- endif %}
|
|
{%- endif %}
|
|
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
|
{%- for forward_message in messages %}
|
|
{%- set index = (messages|length - 1) - loop.index0 %}
|
|
{%- set message = messages[index] %}
|
|
{%- set current_content = message.content if message.content is not none else '' %}
|
|
{%- set tool_start = '<tool_response>' %}
|
|
{%- set tool_start_length = tool_start|length %}
|
|
{%- set start_of_message = current_content[:tool_start_length] %}
|
|
{%- set tool_end = '</tool_response>' %}
|
|
{%- set tool_end_length = tool_end|length %}
|
|
{%- set start_pos = (current_content|length) - tool_end_length %}
|
|
{%- if start_pos < 0 %}
|
|
{%- set start_pos = 0 %}
|
|
{%- endif %}
|
|
{%- set end_of_message = current_content[start_pos:] %}
|
|
{%- if ns.multi_step_tool and message.role == "user" and not(start_of_message == tool_start and end_of_message == tool_end) %}
|
|
{%- set ns.multi_step_tool = false %}
|
|
{%- set ns.last_query_index = index %}
|
|
{%- endif %}
|
|
{%- endfor %}
|
|
{%- for message in messages %}
|
|
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
|
|
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
|
|
{%- elif message.role == "assistant" %}
|
|
{%- set content = message.content %}
|
|
{%- set reasoning_content = '' %}
|
|
{%- if message.reasoning_content is defined and message.reasoning_content is not none %}
|
|
{%- set reasoning_content = message.reasoning_content %}
|
|
{%- else %}
|
|
{%- if '</think>' in message.content %}
|
|
{%- set content = (message.content.split('</think>')|last).lstrip('\n') %}
|
|
{%- set reasoning_content = (message.content.split('</think>')|first).rstrip('\n') %}
|
|
{%- set reasoning_content = (reasoning_content.split('<think>')|last).lstrip('\n') %}
|
|
{%- endif %}
|
|
{%- endif %}
|
|
{%- if loop.index0 > ns.last_query_index %}
|
|
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
|
|
{%- else %}
|
|
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
|
|
{%- endif %}
|
|
{%- if message.tool_calls %}
|
|
{%- for tool_call in message.tool_calls %}
|
|
{%- if (loop.first and content) or (not loop.first) %}
|
|
{{- '\n' }}
|
|
{%- endif %}
|
|
{%- if tool_call.function %}
|
|
{%- set tool_call = tool_call.function %}
|
|
{%- endif %}
|
|
{{- '<tool_call>\n{"name": "' }}
|
|
{{- tool_call.name }}
|
|
{{- '", "arguments": ' }}
|
|
{%- if tool_call.arguments is string %}
|
|
{{- tool_call.arguments }}
|
|
{%- else %}
|
|
{{- tool_call.arguments | tojson }}
|
|
{%- endif %}
|
|
{{- '}\n</tool_call>' }}
|
|
{%- endfor %}
|
|
{%- endif %}
|
|
{{- '<|im_end|>\n' }}
|
|
{%- elif message.role == "tool" %}
|
|
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
|
|
{{- '<|im_start|>user' }}
|
|
{%- endif %}
|
|
{{- '\n<tool_response>\n' }}
|
|
{{- message.content }}
|
|
{{- '\n</tool_response>' }}
|
|
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
|
|
{{- '<|im_end|>\n' }}
|
|
{%- endif %}
|
|
{%- endif %}
|
|
{%- endfor %}
|
|
{%- if add_generation_prompt %}
|
|
{{- '<|im_start|>assistant\n' }}
|
|
{%- if enable_thinking is defined and enable_thinking is false %}
|
|
{{- '<think>\n\n</think>\n\n' }}
|
|
{%- endif %}
|
|
{%- endif %}
|