@@ -302,6 +302,67 @@ def test_small_chat_model_with_response_template_prefix(self):
302302 self .assertIsInstance (parsed_message ["reasoning_content" ], str )
303303 self .assertGreater (len (parsed_message ["reasoning_content" ]), 0 )
304304
305+ @require_torch
306+ def test_small_chat_model_forwards_tool_schema (self ):
307+ text_generator = pipeline (
308+ "text-generation" ,
309+ model = "hf-internal-testing/tiny-gpt2-with-chatml-template" ,
310+ )
311+ text_generator .tokenizer .chat_template = (
312+ "{% for m in messages %}"
313+ "{{ '<|im_start|>' + m['role'] + '\n ' + m['content'] + '<|im_end|>\n ' }}"
314+ "{% endfor %}"
315+ "{% if add_generation_prompt %}"
316+ "{{ '<|im_start|>assistant\n <tool_call>\n <function=set_alarm>\n '"
317+ " + '<parameter=hour>7</parameter>\n '"
318+ " + '<parameter=enabled>false</parameter>\n '"
319+ " + '</function>\n </tool_call>' }}"
320+ "{% endif %}"
321+ )
322+ text_generator .tokenizer .response_template = {
323+ "defaults" : {"role" : "assistant" },
324+ "start_anchor" : "<|im_start|>assistant\n " ,
325+ "fields" : {
326+ "tool_calls" : {
327+ "open_pattern" : r"<tool_call>\s*<function=(?P<name>\w+)>" ,
328+ "close" : "</tool_call>" ,
329+ "repeats" : True ,
330+ "content" : "xml-inline" ,
331+ "content_args" : {"tag_pattern" : r"<parameter=(?P<key>\w+)>\s*(?P<value>.*?)\s*</parameter>" },
332+ "transform" : {
333+ "type" : "function" ,
334+ "function" : {
335+ "name" : "{name}" ,
336+ "arguments" : "{content}" ,
337+ },
338+ },
339+ }
340+ },
341+ }
342+ tools = [
343+ {
344+ "type" : "function" ,
345+ "function" : {
346+ "name" : "set_alarm" ,
347+ "parameters" : {
348+ "type" : "object" ,
349+ "properties" : {
350+ "hour" : {"type" : "integer" },
351+ "enabled" : {"type" : "boolean" },
352+ },
353+ },
354+ },
355+ }
356+ ]
357+ result = text_generator (
358+ [{"role" : "user" , "content" : "Set alarm" }],
359+ tools = tools ,
360+ do_sample = False ,
361+ max_new_tokens = 1 ,
362+ )
363+ arguments = result [0 ]["generated_text" ][- 1 ]["tool_calls" ][0 ]["function" ]["arguments" ]
364+ self .assertEqual (arguments , {"hour" : 7 , "enabled" : False })
365+
305366 @require_torch
306367 def test_return_full_text_false_with_chat_template (self ):
307368 """Regression test for #45854: return_full_text=False must not include prompt when using chat template."""
0 commit comments