diff --git a/models/templates/MiroThinker.jinja b/models/templates/MiroThinker.jinja index fee4a57d5..34c6c0257 100644 --- a/models/templates/MiroThinker.jinja +++ b/models/templates/MiroThinker.jinja @@ -1,3 +1,69 @@ +{#- ========== MiroThinker Tool Parsing Macro ========== #} + +{%- macro function_name() %} + {%- if tool.function is defined %} + {{- tool.function.name }} + {%- elif tool.name is defined and tool.description is defined %} + {{- tool.name }} + {%- endif %} +{%- endmacro %} +{%- macro function_description() %} + {%- if tool.function is defined %} + {{- tool.function.description }} + {%- elif tool.name is defined and tool.description is defined %} + {{- tool.description }} + {%- endif %} +{%- endmacro %} +{%- macro function_parameters() %} + {%- if tool.function is defined %} + {{- tool.function.parameters }} + {%- elif tool.name is defined and tool.description is defined %} + {{- tool.parameters }} + {%- endif %} +{%- endmacro %} + +{%- macro render_tool(server_name) %} + {%- if tool.mt_visited is not defined %} + {%- if server_name != ns.last_server %} + {{- "\n## Server name: " + server_name + "\n" }} + {%- set ns.last_server = server_name %} + {%- endif %} + {{- "### Tool name: " + function_name() + "\n" }} + {{- "Description: " + function_description() + "\n" }} + {{- "Input JSON schema: " + (function_parameters() | tojson(ensure_ascii=False)) + "\n" }} + {{- "\n" }} + {%- endif %} +{%- endmacro %} + +{%- macro render_tool_server() %} + {%- if (function_name().split('_sandbox') | length > 1) or function_name().startswith('run_') or (function_name().split('python') | length > 1) %} + {{- "tool-python" }} + {%- elif function_name().split('_search') | length > 1 %} + {{- "search_and_scrape_webpage" }} + {%- elif function_name() == 'scrape_and_extract_info' %} + {{- "jina_scrape_llm_summary" }} + {%- else %} + {{- "generic-extras" }} + {%- endif %} +{%- endmacro %} + +{%- macro render_tool_call() %} + {%- if message.tool_calls %} + {{- "\n" }} + {%- for tool_call in message.tool_calls %} + {%- set function = tool_call.function %} + {{- "\n" }} + {{- render_tool_server() }} + {{- "\n" }} + {{- function.name }} + {{- "\n\n" }} + {{- function.arguments | tojson(ensure_ascii=False) }} + {{- "\n" }} + {%- endfor %} + {{- "\n" }} + {%- endif %} +{%- endmacro %} + {#- ========== MiroThinker System Message ========== #} {%- set system_message = namespace(role='system', content='') %} @@ -9,33 +75,14 @@ {%- 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 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\nserver name here\ntool name here\n\n{\n\"param1\": \"value1\",\n\"param2\": \"value2 \\\"escaped string\\\"\"\n}\n\n\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=None) %} - {%- for tool in tools %} - {%- set function = namespace(name=None, description=None, parameters=None) %} - {%- if tool.function is defined %} - {%- set function.name = tool.function.name %} - {%- set function.description = tool.function.description %} - {%- set function.parameters = tool.function.parameters %} - {%- elif tool.name is defined and tool.description is defined %} - {%- set function.name = tool.name %} - {%- set function.description = tool.description %} - {%- set function.parameters = tool.parameters %} - {%- endif %} - {%- set tool_name = function.name.split('_') %} - {%- if tool_name | length > 2 %} - {%- set curr_server = tool_name[0] %} - {%- set tool_name = tool_name[1:] | join('_') %} - {%- else %} - {%- set curr_server = 'system_default' %} - {%- set tool_name = 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: " + function.description + "\n" %} - {%- set ns.formatted_tools = ns.formatted_tools + "Input JSON schema: " + ( function.parameters | tojson(ensure_ascii=False) ) + "\n" %} - {%- set ns.formatted_tools = ns.formatted_tools + '\n' %} + {%- for tool_server in ['tool-python', 'search_and_scrape_webpage', 'jina_scrape_llm_summary', 'generic-extras'] %} + {%- for tool in tools %} + {%- set this_server = render_tool_server() %} + {%- if this_server == tool_server %} + {%- set ns.formatted_tools = ns.formatted_tools + render_tool(tool_server) %} + {%- set tool.mt_visited = 1 %} + {%- endif %} + {%- endfor %} {%- 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 %} @@ -113,20 +160,7 @@ {%- for message in messages %} {%- if message.role == 'assistant' %} {%- if message.tool_calls %} - {%- set message.content = message.content + "\n" %} - {%- 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 + "\n" + tool_name + "\n\n" + (tool.arguments | tojson(ensure_ascii=False)) + "\n" %} - {%- endfor %} - {%- set message.content = message.content + "\n" %} + {%- set message.content = message.content + render_tool_call() %} {%- set message.tool_calls = [] %} {%- endif %} {%- elif message.role == 'user' %} @@ -237,3 +271,15 @@ {{- '\n\n\n\n' }} {%- endif %} {%- endif %} + +{#- ========== Workaround for llama.cpp crashing ========== #} +{%- for message in messages %} + {%- if message.role == "assistant" %} + {%- if message.tool_calls | length == 0 %} + {%- set fake_function = namespace(name='fake_name', arguments='{}') %} + {%- set fake_function = namespace(function=fake_function) %} + {%- set message.tool_calls = [fake_function, fake_function] %} + {%- endif %} + {%- endif %} +{%- endfor %} +{#- ========== Workaround for llama.cpp crashing ========== #}