tokenizer.apply_chat_template is right? Manually Parsing Tool Calls Failed! but openai api Success, Why?
I'm test the tool call following https://huggingface.co/moonshotai/Kimi-K2-Thinking/blob/main/docs/tool_call_guidance.md
Here I print out the text (result of tokenizer.apply_chat_template)
'<|im_system|>tool_declare<|im_middle|>[{"function":{"description":"Get weather information. Call this tool when the user needs to get weather information","name":"get_weather","parameters":{"properties":{"city":{"description":"City name","type":"string"}},"required":["city"],"type":"object"}},"type":"function"}]<|im_end|><|im_system|>system<|im_middle|>You are Kimi, an AI assistant created by Moonshot AI.<|im_end|><|im_user|>user<|im_middle|>What's the weather like in Beijing today? Let's check using the tool.<|im_end|><|im_assistant|>assistant<|im_middle|>'
and the raw_output (raw_output = raw_out["choices"][0]["text"])
' The user is asking about the weather in Beijing today and wants to check using a tool. I have access to a weather tool that can get weather information. However, I need the user's request—the user hasn't specified a location yet. The user mentioned "What's the weather like in Beijing today" a couple of times, so it seems they want the weather for Beijing.\n\nLet me respond to let the user know I'm going to call the tool to get the Beijing weather forecast. But looking at the information given, it seems like the available action is the functionality to get_weather, which enables me to fetch weather data. I'll now combine this with the function parameters for a call with the requested included. By the way, thank you for the details about the tool! Bring cloud and rain.\n\nI will call the function to get the weather information for Beijing. In preparation, I will get the arguments. The tool call references work normally and returns the result immediately after:\n\n{\n "name": "get_weself",\n "arguments": "北京"\n}'
So what's wrong? I doubt tokenizer.apply_chat_template is not right.
My code:
model_repo = 'moonshotai/Kimi-K2-Thinking'
tokenizer = AutoTokenizer.from_pretrained(model_repo, trust_remote_code=True)
while True:
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
tools=tools,
add_generation_prompt=True,
)
.....
