gpt-oss-120b-bf16 / chat_template.json
zhyncs's picture
Upload folder using huggingface_hub
4c9160f verified
{
"chat_template": "{#-\n In addition to the normal inputs of `messages` and `tools`, this template also accepts the\n following kwargs:\n - \"builtin_tools\": A list, can contain \"browser\" and/or \"python\".\n - \"model_identity\": A string that optionally describes the model identity.\n - \"reasoning_effort\": A string that describes the reasoning effort, defaults to \"medium\".\n #}\n\n{#- Tool Definition Rendering ============================================== #}\n{%- macro render_typescript_type(param_spec, required_params, is_nullable=false) -%}\n {%- if param_spec.type == \"array\" -%}\n {%- if param_spec['items'] -%}\n {%- if param_spec['items']['type'] == \"string\" -%}\n {{- \"string[]\" }}\n {%- elif param_spec['items']['type'] == \"number\" -%}\n {{- \"number[]\" }}\n {%- elif param_spec['items']['type'] == \"integer\" -%}\n {{- \"number[]\" }}\n {%- elif param_spec['items']['type'] == \"boolean\" -%}\n {{- \"boolean[]\" }}\n {%- else -%}\n {%- set inner_type = render_typescript_type(param_spec['items'], required_params) -%}\n {%- if inner_type == \"object | object\" or inner_type|length > 50 -%}\n {{- \"any[]\" }}\n {%- else -%}\n {{- inner_type + \"[]\" }}\n {%- endif -%}\n {%- endif -%}\n {%- if param_spec.nullable -%}\n {{- \" | null\" }}\n {%- endif -%}\n {%- else -%}\n {{- \"any[]\" }}\n {%- if param_spec.nullable -%}\n {{- \" | null\" }}\n {%- endif -%}\n {%- endif -%}\n {%- elif param_spec.type is defined and param_spec.type is iterable and param_spec.type is not string and param_spec.type is not mapping and param_spec.type[0] is defined -%}\n {#- Handle array of types like [\"object\", \"object\"] from Union[dict, list] #}\n {%- if param_spec.type | length > 1 -%}\n {{- param_spec.type | join(\" | \") }}\n {%- else -%}\n {{- param_spec.type[0] }}\n {%- endif -%}\n {%- elif param_spec.oneOf -%}\n {#- Handle oneOf schemas - check for complex unions and fallback to any #}\n {%- set has_object_variants = false -%}\n {%- for variant in param_spec.oneOf -%}\n {%- if variant.type == \"object\" -%}\n {%- set has_object_variants = true -%}\n {%- endif -%}\n {%- endfor -%}\n {%- if has_object_variants and param_spec.oneOf|length > 1 -%}\n {{- \"any\" }}\n {%- else -%}\n {%- for variant in param_spec.oneOf -%}\n {{- render_typescript_type(variant, required_params) -}}\n {%- if variant.description %}\n {{- \"// \" + variant.description }}\n {%- endif -%}\n {%- if variant.default is defined %}\n {{ \"// default: \" + variant.default|tojson }}\n {%- endif -%}\n {%- if not loop.last %}\n {{- \" | \" }}\n {% endif -%}\n {%- endfor -%}\n {%- endif -%}\n {%- elif param_spec.type == \"string\" -%}\n {%- if param_spec.enum -%}\n {{- '\"' + param_spec.enum|join('\" | \"') + '\"' -}}\n {%- else -%}\n {{- \"string\" }}\n {%- if param_spec.nullable %}\n {{- \" | null\" }}\n {%- endif -%}\n {%- endif -%}\n {%- elif param_spec.type == \"number\" -%}\n {{- \"number\" }}\n {%- elif param_spec.type == \"integer\" -%}\n {{- \"number\" }}\n {%- elif param_spec.type == \"boolean\" -%}\n {{- \"boolean\" }}\n\n {%- elif param_spec.type == \"object\" -%}\n {%- if param_spec.properties -%}\n {{- \"{\n\" }}\n {%- for prop_name, prop_spec in param_spec.properties.items() -%}\n {{- prop_name -}}\n {%- if prop_name not in (param_spec.required or []) -%}\n {{- \"?\" }}\n {%- endif -%}\n {{- \": \" }}\n {{ render_typescript_type(prop_spec, param_spec.required or []) }}\n {%- if not loop.last -%}\n {{-\", \" }}\n {%- endif -%}\n {%- endfor -%}\n {{- \"}\" }}\n {%- else -%}\n {{- \"object\" }}\n {%- endif -%}\n {%- else -%}\n {{- \"any\" }}\n {%- endif -%}\n{%- endmacro -%}\n\n{%- macro render_tool_namespace(namespace_name, tools) -%}\n {{- \"## \" + namespace_name + \"\n\n\" }}\n {{- \"namespace \" + namespace_name + \" {\n\n\" }}\n {%- for tool in tools %}\n {%- set tool = tool.function %}\n {{- \"// \" + tool.description + \"\n\" }}\n {{- \"type \"+ tool.name + \" = (\" }}\n {%- if tool.parameters and tool.parameters.properties -%}\n {{- \"_: \" }}\n {{- \"{\n\" }}\n {%- for param_name, param_spec in tool.parameters.properties.items() %}\n {%- if param_spec.description -%}\n {{- \"// \" + param_spec.description + \"\n\" }}\n {%- endif -%}\n {{- param_name }}\n {%- if param_name not in (tool.parameters.required or []) -%}\n {{- \"?\" }}\n {%- endif -%}\n {{- \": \" }}\n {{- render_typescript_type(param_spec, tool.parameters.required or []) }}\n {%- if param_spec.default is defined -%}\n {%- if param_spec.oneOf %}\n {{- \"// default: \" + param_spec.default }}\n {%- else %}\n {{- \", // default: \" + param_spec.default|tojson }}\n {%- endif -%}\n {%- endif -%}\n {%- if not loop.last %}\n {{- \",\n\" }}\n {%- endif -%}\n {%- endfor %}\n {{- \",\n}) => any;\n\" }}\n {%- else -%}\n {{- \"\n}) => any;\n\" }}\n {%- endif -%}\n {%- endfor %}\n {{- \"\n} // namespace \" + namespace_name }}\n{%- endmacro -%}\n\n{%- macro render_builtin_tools(browser_tool, python_tool) -%}\n {%- if browser_tool %}\n {{- \"## browser\n\n\" }}\n {{- \"// Tool for browsing.\n\" }}\n {{- \"// The `cursor` appears in brackets before each browsing display: `[{cursor}]`.\n\" }}\n {{- \"// Cite information from the tool using the following format:\n\" }}\n {{- \"// `\u3010{cursor}\u2020L{line_start}(-L{line_end})?\u3011`, for example: `\u30106\u2020L9-L11\u3011` or `\u30108\u2020L3\u3011`.\n\" }}\n {{- \"// Do not quote more than 10 words directly from the tool output.\n\" }}\n {{- \"// sources=web (default: web)\n\" }}\n {{- \"namespace browser {\n\n\" }}\n {{- \"// Searches for information related to `query` and displays `topn` results.\n\" }}\n {{- \"type search = (_: {\n\" }}\n {{- \"query: string,\n\" }}\n {{- \"topn?: number, // default: 10\n\" }}\n {{- \"source?: string,\n\" }}\n {{- \"}) => any;\n\n\" }}\n {{- \"// Opens the link `id` from the page indicated by `cursor` starting at line number `loc`, showing `num_lines` lines.\n\" }}\n {{- \"// Valid link ids are displayed with the formatting: `\u3010{id}\u2020.*\u3011`.\n\" }}\n {{- \"// If `cursor` is not provided, the most recent page is implied.\n\" }}\n {{- \"// If `id` is a string, it is treated as a fully qualified URL associated with `source`.\n\" }}\n {{- \"// If `loc` is not provided, the viewport will be positioned at the beginning of the document or centered on the most relevant passage, if available.\n\" }}\n {{- \"// Use this function without `id` to scroll to a new location of an opened page.\n\" }}\n {{- \"type open = (_: {\n\" }}\n {{- \"id?: number | string, // default: -1\n\" }}\n {{- \"cursor?: number, // default: -1\n\" }}\n {{- \"loc?: number, // default: -1\n\" }}\n {{- \"num_lines?: number, // default: -1\n\" }}\n {{- \"view_source?: boolean, // default: false\n\" }}\n {{- \"source?: string,\n\" }}\n {{- \"}) => any;\n\n\" }}\n {{- \"// Finds exact matches of `pattern` in the current page, or the page given by `cursor`.\n\" }}\n {{- \"type find = (_: {\n\" }}\n {{- \"pattern: string,\n\" }}\n {{- \"cursor?: number, // default: -1\n\" }}\n {{- \"}) => any;\n\n\" }}\n {{- \"} // namespace browser\n\n\" }}\n {%- endif -%}\n\n {%- if python_tool %}\n {{- \"## python\n\n\" }}\n {{- \"Use this tool to execute Python code in your chain of thought. The code will not be shown to the user. This tool should be used for internal reasoning, but not for code that is intended to be visible to the user (e.g. when creating plots, tables, or files).\n\n\" }}\n {{- \"When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 120.0 seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is UNKNOWN. Depends on the cluster.\n\n\" }}\n {%- endif -%}\n{%- endmacro -%}\n\n{#- System Message Construction ============================================ #}\n{%- macro build_system_message() -%}\n {%- if model_identity is not defined %}\n {{- \"You are ChatGPT, a large language model trained by OpenAI.\n\" -}}\n {%- else %}\n {{- model_identity }}\n {%- endif %}\n {{- \"Knowledge cutoff: 2024-06\n\" }}\n {{- \"Current date: \" + strftime_now(\"%Y-%m-%d\") + \"\n\n\" }}\n {%- if reasoning_effort is not defined %}\n {%- set reasoning_effort = \"medium\" %}\n {%- endif %}\n {{- \"reasoning: \" + reasoning_effort + \"\n\n\" }}\n {%- if builtin_tools %}\n {{- \"# Tools\n\n\" }}\n {%- set available_builtin_tools = namespace(browser=false, python=false) %}\n {%- for tool in builtin_tools %}\n {%- if tool == \"browser\" %}\n {%- set available_builtin_tools.browser = true %}\n {%- elif tool == \"python\" %}\n {%- set available_builtin_tools.python = true %}\n {%- endif %}\n {%- endfor %}\n {{- render_builtin_tools(available_builtin_tools.browser, available_builtin_tools.python) }}\n {%- endif -%}\n {{- \"# Valid channels: analysis, commentary, final. Channel must be included for every message.\n\" }}\n {{- \"Calls to these tools must go to the commentary channel: 'functions'.\" }}\n{%- endmacro -%}\n\n{#- CoT Dropping Logic ================================================== #}\n{%- set cot_final_indices = [] -%}\n{%- for idx in range(messages|length) -%}\n {%- set m = messages[idx] -%}\n {%- if m.role == 'assistant' and m.get('channel', '') == 'final' -%}\n {%- if cot_final_indices.append(idx) -%}{%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n{%- set cot_last_final_idx = cot_final_indices[-1] if cot_final_indices else none -%}\n{%- set cot_last_user_idx = none -%}\n{%- if cot_last_final_idx is not none -%}\n {%- for idx in range(cot_last_final_idx - 1, -1, -1) -%}\n {%- if messages[idx].role == 'user' and cot_last_user_idx is none -%}\n {%- set cot_last_user_idx = idx -%}\n {%- endif -%}\n {%- endfor -%}\n{%- endif -%}\n\n{#- Main Template Logic ================================================= #}\n{#- Set defaults #}\n{%- set auto_drop = auto_drop_analysis if auto_drop_analysis is defined else true -%}\n\n{#- Render system message #}\n{{- \"<|start|>system<|message|>\" }}\n{{- build_system_message() }}\n{{- \"<|end|>\" }}\n\n{#- Extract developer message #}\n{%- if messages[0].role == \"developer\" or messages[0].role == \"system\" %}\n {%- set developer_message = messages[0].content %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set developer_message = \"\" %}\n {%- set loop_messages = messages %}\n{%- endif %}\n\n{#- Render developer message #}\n{%- if developer_message or tools %}\n {{- \"<|start|>developer<|message|>\" }}\n {%- if developer_message %}\n {{- \"# Instructions\n\n\" }}\n {{- developer_message }}\n {%- endif %}\n {%- if tools -%}\n {{- \"\n\n\" }}\n {{- \"# Tools\n\n\" }}\n {{- render_tool_namespace(\"functions\", tools) }}\n {%- endif -%}\n {{- \"<|end|>\" }}\n{%- endif %}\n\n{#- Render messages #}\n{%- set last_tool_call = namespace(name=none) %}\n{%- for message in loop_messages -%}\n {%- set skip = false -%}\n \n {# Apply CoT dropping logic #}\n {%- if auto_drop and cot_last_final_idx is not none and loop.index0 < cot_last_final_idx -%}\n {%- if message.role == 'assistant' and message.get('channel', '') != 'final' -%}\n {%- if cot_last_user_idx is none or loop.index0 > cot_last_user_idx -%}\n {%- set skip = true -%}\n {%- endif -%}\n {%- elif message.role == 'user' and message.get('channel', '') == 'analysis' -%}\n {%- set skip = true -%}\n {%- endif -%}\n {%- endif -%}\n \n {%- if not skip -%}\n {#- At this point only assistant/user/tool messages should remain #}\n {%- if message.role == 'assistant' -%}\n {%- if \"tool_calls\" in message %}\n {# I'm assuming max 1 tool call per message here, which might be wrong #}\n {{- \"<|start|>assistant<|channel|>analysis<|message|>\" + message.content }}\n {{- \"<|end|><|start|>assistant to=\" }}\n {{- \"functions.\" + message.tool_calls[0].name + \"<|channel|>commentary json<|message|>\" }}\n {{- message.tool_calls[0].arguments|tojson }}\n {{- \"<|end|>\" }}\n {%- set last_tool_call.name = message.tool_calls[0].name %}\n {%- elif \"thinking\" in message %}\n {#- CoT is dropped during all model inputs, so we never actually render it #}\n {{- \"<|start|>assistant<|channel|>final<|message|>\" + message.content + \"<|end|>\" }}\n {%- else %}\n {{- \"<|start|>assistant<|message|>\" + message.content + \"<|end|>\" }}\n {%- endif %}\n {%- elif message.role == 'tool' -%}\n {%- if last_tool_call.name is none %}\n {{- raise_exception(\"Message has tool role, but there was no previous assistant message with a tool call!\") }}\n {%- endif %}\n {{- \"<|start|>functions.\" + last_tool_call.name }}\n {{- \" to=assistant<|channel|>commentary<|message|>\" + message.content|tojson + \"<|end|>\" }}\n {%- else -%}\n {{- \"<|start|>user<|message|>\" + message.content + \"<|end|>\" }}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n\n{#- Generation prompt #}\n{%- if add_generation_prompt -%}\n<|start|>assistant\n{%- endif -%}"
}