MCPcopy Create free account
hub / github.com/PaddlePaddle/FastDeploy / test_chat_with_tools

Method test_chat_with_tools

tests/entrypoints/test_chat.py:66–127  ·  view source on GitHub ↗

Test chat with tools: 1. spliced_message (after chat_template) contains tool-related content 2. Model output contains tool_call

(self)

Source from the content-addressed store, hash-verified

64 self.assertEqual(outputs[-1].num_cached_tokens, 64)
65
66 def test_chat_with_tools(self):
67 """Test chat with tools:
68 1. spliced_message (after chat_template) contains tool-related content
69 2. Model output contains tool_call
70 """
71 prompts = [{"role": "user", "content": "北京海淀区今天天气怎么样?用摄氏度表示温度。"}]
72 tools = [
73 {
74 "type": "function",
75 "function": {
76 "name": "get_weather",
77 "description": "Determine weather in my location",
78 "parameters": {
79 "type": "object",
80 "properties": {
81 "location": {"type": "string", "description": "The city and state e.g. San Francisco, CA"},
82 "unit": {"type": "string", "enum": ["c", "f"]},
83 },
84 "additionalProperties": False,
85 "required": ["location", "unit"],
86 },
87 "strict": True,
88 },
89 }
90 ]
91 chat_template = "{%- if tools %}\n {{- '<|im_start|>system\\n' }}\n {%- if messages[0].role == 'system' %}\n {{- messages[0].content + '\\n\\n' }}\n {%- endif %}\n {{- \"# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within <tools></tools> XML tags:\\n<tools>\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n {{- \"\\n</tools>\\n\\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\\n<tool_call>\\n{\\\"name\\\": <function-name>, \\\"arguments\\\": <args-json-object>}\\n</tool_call><|im_end|>\\n\" }}\n{%- else %}\n {%- if messages[0].role == 'system' %}\n {{- '<|im_start|>system\\n' + messages[0].content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}\n{%- for message in messages[::-1] %}\n {%- set index = (messages|length - 1) - loop.index0 %}\n {%- if ns.multi_step_tool and message.role == \"user\" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}\n {%- set ns.multi_step_tool = false %}\n {%- set ns.last_query_index = index %}\n {%- endif %}\n{%- endfor %}\n{%- for message in messages %}\n {%- if message.content is string %}\n {%- set content = message.content %}\n {%- else %}\n {%- set content = '' %}\n {%- endif %}\n {%- if (message.role == \"user\") or (message.role == \"system\" and not loop.first) %}\n {{- '<|im_start|>' + message.role + '\\n' + content + '<|im_end|>' + '\\n' }}\n {%- elif message.role == \"assistant\" %}\n {%- set reasoning_content = '' %}\n {%- if message.reasoning_content is string %}\n {%- set reasoning_content = message.reasoning_content %}\n {%- else %}\n {%- if '' in content %}\n {%- set reasoning_content = content.split('')[0].rstrip('\\n').split('')[-1].lstrip('\\n') %}\n {%- set content = content.split('')[-1].lstrip('\\n') %}\n {%- endif %}\n {%- endif %}\n {%- if loop.index0 > ns.last_query_index %}\n {%- if loop.last or (not loop.last and reasoning_content) %}\n {{- '<|im_start|>' + message.role + '\\n\\n' + reasoning_content.strip('\\n') + '\\n\\n\\n' + content.lstrip('\\n') }}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + content }}\n {%- endif %}\n {%- if message.tool_calls %}\n {%- for tool_call in message.tool_calls %}\n {%- if (loop.first and content) or (not loop.first) %}\n {{- '\\n' }}\n {%- endif %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {%- if tool_call.arguments is string %}\n {{- tool_call.arguments }}\n {%- else %}\n {{- tool_call.arguments | tojson }}\n {%- endif %}\n {{- '}\\n</tool_call>' }}\n {%- endfor %}\n {%- endif %}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.first or (messages[loop.index0 - 1].role != \"tool\") %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n<tool_response>\\n' }}\n {{- content }}\n {{- '\\n</tool_response>' }}\n {%- if loop.last or (messages[loop.index0 + 1].role != \"tool\") %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n{%- endfor %}\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '\\n\\n\\n\\n' }}\n {%- endif %}\n{%- endif %}"
92
93 data_processor = self.llm.llm_engine.data_processor
94 captured_spliced_message = None
95
96 def capture_spliced_message(request_or_messages, **kwargs):
97 """Wrap original messages2ids to capture spliced_message"""
98 token_ids = data_processor.original_messages2ids(request_or_messages, **kwargs)
99 nonlocal captured_spliced_message
100 captured_spliced_message = request_or_messages.get("prompt_tokens")
101 return token_ids
102
103 data_processor.original_messages2ids = data_processor.messages2ids
104 data_processor.messages2ids = capture_spliced_message
105
106 try:
107 outputs = self.llm.chat(
108 messages=prompts,
109 tools=tools,
110 chat_template=chat_template,
111 chat_template_kwargs={"enable_thinking": False},
112 stream=False,
113 )
114
115 self.assertIsNotNone(captured_spliced_message, "Failed to capture spliced_message from messages2ids")
116 self.assertIn(
117 "<tools>",
118 captured_spliced_message,
119 f"spliced_message '{captured_spliced_message}' missing <tools> tag (chat_template not applied)",
120 )
121
122 output = outputs[0]
123 self.assertEqual(len(prompts), len(outputs))

Callers

nothing calls this directly

Calls 1

chatMethod · 0.80

Tested by

no test coverage detected