{%- if tools is iterable and tools | length > 0 %} {{- '<|im_start|>system 당신은 도구 호출 기능을 갖춘 유용한 도우미입니다. 사용자의 요청을 처리하기 위해서 필요한 도구가 주어진 목록에 있는 경우 도구 호출로 응답하세요. 필요한 도구가 목록에 없는 경우에는 도구 호출 없이 사용자가 요구한 정보를 제공하세요. 필요한 도구가 목록에 있지만 해당 도구를 호출하는데 필요한 argument 정보가 부족한 경우 해당 정보를 사용자에게 요청하세요. 사용자의 요청을 처리하기 위해 여러번 도구를 호출할 수 있어야 합니다. 도구 호출 이후 도구 실행 결과를 입력으로 받으면 해당 결과를 활용하여 답변을 생성하세요. 다음은 접근할 수 있는 도구들의 목록 입니다: ' }} {%- for t in tools %} {{- t | tojson }} {{- '\n' }} {%- endfor %} {{- ' 도구를 호출하려면 아래의 JSON으로 응답하세요. 도구 호출 형식: {"name": 도구 이름, "arguments": dictionary 형태의 도구 인자값}<|im_end|> ' }} {%- endif %} {%- macro render_content(content) -%} {#- `content` may be a plain string, none, or a list of parts as sent by OpenAI-compatible clients: [{"type": "text", "text": "..."}] -#} {%- if content is none -%} {%- elif content is string -%} {{- content -}} {%- elif content is mapping -%} {{- content.text if content.text is defined else content -}} {%- elif content is sequence -%} {%- for part in content -%} {%- if part is mapping and part.text is defined -%} {{- part.text -}} {%- elif part is string -%} {{- part -}} {%- endif -%} {%- endfor -%} {%- else -%} {{- content -}} {%- endif -%} {%- endmacro -%} {%- macro render_tool_calls(tool_calls) -%} {%- for tool_call in tool_calls -%} {%- if tool_call.function is defined -%} {%- set tool_call = tool_call.function -%} {%- endif -%} {{- '\n' -}} {{- '{"name": "' + tool_call.name + '"' -}} {%- if tool_call.arguments is defined -%} {{- ', "arguments": ' -}} {#- OpenAI-compatible clients send `arguments` as a JSON string; pass it through -#} {%- if tool_call.arguments is string -%} {{- tool_call.arguments -}} {%- else -%} {{- tool_call.arguments | tojson -}} {%- endif -%} {%- endif -%} {{- '}\n' -}} {%- endfor -%} {%- endmacro -%} {%- set ns = namespace(last_user_index=-1) %} {%- for m in messages %} {%- if m.role == 'user' %} {%- set ns.last_user_index = loop.index0 %} {%- endif %} {%- endfor %} {%- for message in messages %} {%- if message.role == 'system' or message.role == 'user' %} {{- '<|im_start|>' + message.role + '\n' }} {%- set reasoning_content = '' %} {%- if message.reasoning_content is defined and message.reasoning_content is not none %} {%- set reasoning_content = message.reasoning_content %} {%- elif message.reasoning is defined and message.reasoning is not none %} {%- set reasoning_content = message.reasoning %} {%- endif %} {%- if reasoning_content %} {{- '' + reasoning_content.strip() + '' }} {%- endif %} {{- render_content(message.content) }} {%- if message.tool_calls is defined and message.tool_calls is not none %} {{- render_tool_calls(message.tool_calls) }} {%- endif %} {{- '<|im_end|>\n' }} {%- elif message.role == 'assistant' %} {{- '<|im_start|>assistant\n' }} {#- Extract reasoning and content -#} {%- set reasoning_content = '' %} {%- set content = '' %} {%- if message.content is defined and message.content is not none %} {%- set content = render_content(message.content) %} {%- endif %} {%- if message.reasoning_content is defined and message.reasoning_content is not none %} {%- set reasoning_content = message.reasoning_content %} {%- elif message.reasoning is defined and message.reasoning is not none %} {%- set reasoning_content = message.reasoning %} {%- elif '' in content %} {%- set reasoning_content = content.split('')[0].split('')[-1] %} {%- set content = content.split('')[-1].lstrip('\n') %} {%- endif %} {#- Determine whether to render thinking -#} {%- set render_thinking = false %} {%- if reasoning_content %} {%- if show_thinking is defined and show_thinking %} {%- set render_thinking = true %} {%- elif not (show_thinking is defined and not show_thinking) %} {#- show_thinking undefined: show if last turn or tool-call turn after last user -#} {%- if loop.last or (message.tool_calls is defined and loop.index0 > ns.last_user_index) %} {%- set render_thinking = true %} {%- endif %} {%- endif %} {%- endif %} {%- if render_thinking %} {{- '' + reasoning_content.strip() + '' }} {%- else %} {{- '' }} {%- endif %} {{- content }} {%- if message.tool_calls is defined and message.tool_calls is not none %} {{- render_tool_calls(message.tool_calls) }} {%- endif %} {{- '<|im_end|>' + ('\n' if (not loop.last or add_generation_prompt) else '') }} {%- elif message.role == 'tool' %} {{- '<|im_start|>tool\n' }} {{- '\n' }} {{- render_content(message.content) }} {{- '\n' }} {{- '<|im_end|>\n' }} {%- endif %} {%- endfor %} {%- if add_generation_prompt %} {{- '<|im_start|>assistant\n' }} {%- if enable_thinking is defined and enable_thinking is true %} {{- '' }} {%- else %} {{- '' }} {%- endif %} {%- endif %}