| {%- if tools is iterable and tools | length > 0 %} |
| {{- '<|im_start|>system |
| 당신은 도구 호출 기능을 갖춘 유용한 도우미입니다. 사용자의 요청을 처리하기 위해서 필요한 도구가 주어진 목록에 있는 경우 도구 호출로 응답하세요. |
| 필요한 도구가 목록에 없는 경우에는 도구 호출 없이 사용자가 요구한 정보를 제공하세요. |
| 필요한 도구가 목록에 있지만 해당 도구를 호출하는데 필요한 argument 정보가 부족한 경우 해당 정보를 사용자에게 요청하세요. |
| 사용자의 요청을 처리하기 위해 여러번 도구를 호출할 수 있어야 합니다. |
| 도구 호출 이후 도구 실행 결과를 입력으로 받으면 해당 결과를 활용하여 답변을 생성하세요. |
| |
| 다음은 접근할 수 있는 도구들의 목록 입니다: |
| <tools> |
| ' }} |
| {%- for t in tools %} |
| {{- t | tojson }} |
| {{- '\n' }} |
| {%- endfor %} |
| {{- '</tools> |
| |
| 도구를 호출하려면 아래의 JSON으로 응답하세요. |
| 도구 호출 형식: <tool_call>{"name": 도구 이름, "arguments": dictionary 형태의 도구 인자값}</tool_call><|im_end|> |
| ' }} |
| {%- endif %} |
| |
| {%- macro render_content(content) -%} |
| |
| |
| {%- 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 -%} |
| {{- '<tool_call>\n' -}} |
| {{- '{"name": "' + tool_call.name + '"' -}} |
| {%- if tool_call.arguments is defined -%} |
| {{- ', "arguments": ' -}} |
| |
| {%- if tool_call.arguments is string -%} |
| {{- tool_call.arguments -}} |
| {%- else -%} |
| {{- tool_call.arguments | tojson -}} |
| {%- endif -%} |
| {%- endif -%} |
| {{- '}\n</tool_call>' -}} |
| {%- 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 %} |
| {{- '<think>' + reasoning_content.strip() + '</think>' }} |
| {%- 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' }} |
| |
| |
| {%- 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 '</think>' in content %} |
| {%- set reasoning_content = content.split('</think>')[0].split('<think>')[-1] %} |
| {%- set content = content.split('</think>')[-1].lstrip('\n') %} |
| {%- endif %} |
| |
| |
| {%- 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) %} |
| |
| {%- 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 %} |
| {{- '<think>' + reasoning_content.strip() + '</think>' }} |
| {%- else %} |
| {{- '</think>' }} |
| {%- 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' }} |
| {{- '<tool_response>\n' }} |
| {{- render_content(message.content) }} |
| {{- '\n' }} |
| {{- '</tool_response><|im_end|>\n' }} |
| {%- endif %} |
| {%- endfor %} |
| |
| {%- if add_generation_prompt %} |
| {{- '<|im_start|>assistant\n' }} |
| {%- if enable_thinking is defined and enable_thinking is true %} |
| {{- '<think>' }} |
| {%- else %} |
| {{- '</think>' }} |
| {%- endif %} |
| {%- endif %} |
| |