diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py index 3c426b38f..029f53d15 100644 --- a/llama_cpp/llama_chat_format.py +++ b/llama_cpp/llama_chat_format.py @@ -5371,7 +5371,7 @@ class Qwen35ChatHandler(MTMDChatHandler): "{%- if not messages -%}" " {{- raise_exception('No messages provided.') -}}" "{%- endif -%}" - "{%- if tools and tools is iterable and tools is not mapping -%}" + "{%- if tools and tools is iterable and tools is not mapping and tool_choice != 'none' -%}" " {{- '<|im_start|>system\n' -}}" " {{- '# Tools\n\nYou have access to the following functions:\n\n' -}}" " {%- for tool in tools -%}" @@ -5379,6 +5379,11 @@ class Qwen35ChatHandler(MTMDChatHandler): " {{- tool | tojson -}}" " {%- endfor -%}" " {{- '\n' -}}" + " {%- if tool_choice == 'required' -%}" + " {{- '\n\nYou MUST call at least one function in your response. Answering without a function call is not allowed. Select the most appropriate tool based on the user request and its description.\n' -}}" + " {%- else -%}" + " {{- '\n\nYou have access to tools. Use them only when necessary.\nFollow the description of each tool to determine if and when to call it.\nIf no tool is needed, respond directly.\n' -}}" + " {%- endif -%}" " {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n\n\n\nvalue_1\n\n\nThis is the value for the second parameter\nthat can span\nmultiple lines\n\n\n\n\n\nReminder:\n- Function calls MUST follow the specified format: an inner block must be nested within XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n' -}}" " {%- if messages[0].role == 'system' -%}" " {%- set content = render_content(messages[0].content, false, true) | trim -%}"