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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# MiniMax-Text-01 Function Call Guide
|
2 |
+
|
3 |
+
## 📖 Introduction
|
4 |
+
|
5 |
+
MiniMax-Text-01 model supports function calling capability, allowing the model to identify when an external function needs to be called and output function call parameters in a structured format. This document provides detailed instructions on how to use the function calling feature of MiniMax-Text-01.
|
6 |
+
|
7 |
+
## 🛠️ Defining Function Calls
|
8 |
+
|
9 |
+
### Function Structure
|
10 |
+
|
11 |
+
Function calls need to be defined in the `tools` field of the request body. Each function consists of:
|
12 |
+
|
13 |
+
```json
|
14 |
+
{
|
15 |
+
"tools": [
|
16 |
+
{
|
17 |
+
"type": "function",
|
18 |
+
"function": {
|
19 |
+
"name": "function_name", // Function name, required
|
20 |
+
"description": "function_description", // Brief description of the function's purpose
|
21 |
+
"parameters": { // Parameter definition in JSON Schema format
|
22 |
+
"type": "object", // Overall type, fixed as "object"
|
23 |
+
"properties": { // Parameter property object
|
24 |
+
"param_name": { // Parameter name
|
25 |
+
"description": "Parameter description", // Description
|
26 |
+
"type": "string|number|boolean|array|object" // Type
|
27 |
+
}
|
28 |
+
},
|
29 |
+
"required": ["param1", "param2"] // List of required parameters
|
30 |
+
}
|
31 |
+
}
|
32 |
+
}
|
33 |
+
]
|
34 |
+
}
|
35 |
+
```
|
36 |
+
|
37 |
+
### Example
|
38 |
+
|
39 |
+
Below is a simple example of a weather query function definition:
|
40 |
+
|
41 |
+
```json
|
42 |
+
"tools": [
|
43 |
+
{
|
44 |
+
"type": "function",
|
45 |
+
"function": {
|
46 |
+
"name": "get_current_weather",
|
47 |
+
"description": "Get the latest weather for a location",
|
48 |
+
"parameters": {
|
49 |
+
"type": "object",
|
50 |
+
"properties": {
|
51 |
+
"location": {
|
52 |
+
"type": "string",
|
53 |
+
"description": "A certain city, such as Beijing, Shanghai"
|
54 |
+
}
|
55 |
+
},
|
56 |
+
"required": ["location"]
|
57 |
+
}
|
58 |
+
}
|
59 |
+
}
|
60 |
+
]
|
61 |
+
```
|
62 |
+
|
63 |
+
### Complete Request Example
|
64 |
+
|
65 |
+
Below is a complete Python code example that includes function definitions:
|
66 |
+
|
67 |
+
```python
|
68 |
+
payload = json.dumps({
|
69 |
+
"model": "MiniMax-Text-01",
|
70 |
+
"messages": [
|
71 |
+
{
|
72 |
+
"role": "system",
|
73 |
+
"content": "MM Intelligent Assistant is a large-scale language model developed by MiniMax and has no interfaces to call other products. MiniMax is a China technology company that has been committed to conducting research related to large models."
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"role": "user",
|
77 |
+
"content": "What's the weather like in Shanghai today?"
|
78 |
+
}
|
79 |
+
],
|
80 |
+
"tools": [
|
81 |
+
{
|
82 |
+
"type": "function",
|
83 |
+
"function": {
|
84 |
+
"name": "get_current_weather",
|
85 |
+
"description": "Get the latest weather for a location",
|
86 |
+
"parameters": {
|
87 |
+
"type": "object",
|
88 |
+
"properties": {
|
89 |
+
"location": {
|
90 |
+
"type": "string",
|
91 |
+
"description": "A certain city, such as Beijing, Shanghai"
|
92 |
+
}
|
93 |
+
},
|
94 |
+
"required": ["location"]
|
95 |
+
}
|
96 |
+
}
|
97 |
+
}
|
98 |
+
],
|
99 |
+
"tool_choice": "auto",
|
100 |
+
"stream": True,
|
101 |
+
"max_tokens": 10000,
|
102 |
+
"temperature": 0.9,
|
103 |
+
"top_p": 1
|
104 |
+
})
|
105 |
+
```
|
106 |
+
|
107 |
+
## 🔄 Function Call Input Format
|
108 |
+
|
109 |
+
When processed internally by the model, function definitions are converted to a special format and concatenated to the input text:
|
110 |
+
|
111 |
+
```
|
112 |
+
<beginning_of_sentence>system function_setting=functions
|
113 |
+
{"name": "get_current_weather", "description": "Get the latest weather for a location", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "A certain city, such as Beijing, Shanghai"}}, "required": ["location"]}}<end_of_sentence>
|
114 |
+
```
|
115 |
+
|
116 |
+
Important notes:
|
117 |
+
1. Function definitions are placed after the system settings and before the conversation data
|
118 |
+
2. Function definitions are marked with `function_setting=functions`
|
119 |
+
3. Each function is defined as a JSON string
|
120 |
+
4. The area ends with `<end_of_sentence>`
|
121 |
+
|
122 |
+
## 📤 Model Function Call Output
|
123 |
+
|
124 |
+
When the model decides to call a function, it outputs the function call information in a special format:
|
125 |
+
|
126 |
+
````
|
127 |
+
<function_call>```typescript
|
128 |
+
functions.get_current_weather({"location": "Shanghai"})
|
129 |
+
```
|
130 |
+
````
|
131 |
+
|
132 |
+
"<function_call>" is a special token, followed by "functions.function_name(parameter json structure)". The parameters need to be string-matched and executed externally.
|
133 |
+
|
134 |
+
## 📥 Handling Function Results
|
135 |
+
|
136 |
+
After a function is successfully executed, the model will return output in the following format:
|
137 |
+
|
138 |
+
````typescript
|
139 |
+
```typescript
|
140 |
+
functions.get_current_weather({"location": "Shanghai"})
|
141 |
+
```
|
142 |
+
````
|
143 |
+
|
144 |
+
You can use the following regular expression method to extract the function name and parameters for subsequent processing:
|
145 |
+
|
146 |
+
````python
|
147 |
+
def parse_function_calls(content: str):
|
148 |
+
"""
|
149 |
+
Parse the function call content returned by the model, extract function name and parameters
|
150 |
+
|
151 |
+
Parameters:
|
152 |
+
content: The original content string returned by the model
|
153 |
+
|
154 |
+
Returns:
|
155 |
+
A dictionary of parsed function call information, including function name and parameters
|
156 |
+
"""
|
157 |
+
# Match typescript code block
|
158 |
+
pattern = r"```typescript\n(.+?)?\n```"
|
159 |
+
matches = re.finditer(pattern, content, re.DOTALL)
|
160 |
+
|
161 |
+
for match in matches:
|
162 |
+
function_code = match.group(1)
|
163 |
+
# Extract function name and parameters
|
164 |
+
function_match = re.search(r'functions\.(\w+)\((.+)\)', function_code)
|
165 |
+
|
166 |
+
if not function_match:
|
167 |
+
continue
|
168 |
+
|
169 |
+
function_name = function_match.group(1)
|
170 |
+
arguments_str = function_match.group(2)
|
171 |
+
|
172 |
+
try:
|
173 |
+
# Parse parameter JSON
|
174 |
+
arguments = json.loads(arguments_str)
|
175 |
+
print(f"Function call: {function_name}, Parameters: {arguments}")
|
176 |
+
|
177 |
+
# Example: Handle weather query function
|
178 |
+
if function_name == "get_current_weather":
|
179 |
+
location = arguments.get("location", "Unknown location")
|
180 |
+
# Build function execution result
|
181 |
+
return {
|
182 |
+
"role": "function",
|
183 |
+
"name": function_name,
|
184 |
+
"text": json.dumps({
|
185 |
+
"location": location,
|
186 |
+
"temperature": "25",
|
187 |
+
"unit": "celsius",
|
188 |
+
"weather": "Sunny"
|
189 |
+
}, ensure_ascii=False)
|
190 |
+
}
|
191 |
+
except json.JSONDecodeError as e:
|
192 |
+
print(f"Parameter parsing failed: {arguments_str}, Error: {e}")
|
193 |
+
|
194 |
+
return {}
|
195 |
+
````
|
196 |
+
|
197 |
+
After successfully parsing the function call, you should add the function execution result to the conversation history so that the model can access and utilize this information in subsequent interactions.
|
198 |
+
|
199 |
+
## 💻 Function Call Example with Transformers Library
|
200 |
+
|
201 |
+
The official MiniMax-Text-01 repository provides a complete example of function calling using the Transformers library. You can view the source code in the [MiniMaxAI/MiniMax-Text-01 huggingface repository](https://huggingface.co/MiniMaxAI/MiniMax-Text-01/blob/main/main.py).
|
202 |
+
|
203 |
+
The following is the key part of implementing function calls using the Transformers library:
|
204 |
+
|
205 |
+
```python
|
206 |
+
def get_default_tools():
|
207 |
+
return [
|
208 |
+
{
|
209 |
+
"type": "function",
|
210 |
+
"function": {
|
211 |
+
"name": "get_current_weather",
|
212 |
+
"description": "Get the latest weather for a location",
|
213 |
+
"parameters": {
|
214 |
+
"type": "object",
|
215 |
+
"properties": {
|
216 |
+
"location": {
|
217 |
+
"type": "string",
|
218 |
+
"description": "A certain city, such as Beijing, Shanghai"
|
219 |
+
}
|
220 |
+
},
|
221 |
+
"required": ["location"]
|
222 |
+
}
|
223 |
+
}
|
224 |
+
}
|
225 |
+
]
|
226 |
+
|
227 |
+
# Load model and tokenizer
|
228 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
229 |
+
prompt = "What's the weather like in Shanghai today?"
|
230 |
+
messages = [
|
231 |
+
{"role": "system", "content": [{"type": "text", "text": "You are a helpful assistant created by Minimax based on MiniMax-Text-01 model."}]},
|
232 |
+
{"role": "user", "content": [{"type": "text", "text": prompt}]},
|
233 |
+
]
|
234 |
+
|
235 |
+
# Enable function call tools
|
236 |
+
tools = get_default_tools()
|
237 |
+
|
238 |
+
# Apply chat template and add tool definitions
|
239 |
+
text = tokenizer.apply_chat_template(
|
240 |
+
messages,
|
241 |
+
tokenize=False,
|
242 |
+
add_generation_prompt=True,
|
243 |
+
tools=tools
|
244 |
+
)
|
245 |
+
|
246 |
+
# Generate response
|
247 |
+
model_inputs = tokenizer(text, return_tensors="pt").to("cuda")
|
248 |
+
quantized_model = AutoModelForCausalLM.from_pretrained(
|
249 |
+
model_id,
|
250 |
+
torch_dtype="bfloat16",
|
251 |
+
device_map=device_map,
|
252 |
+
quantization_config=quantization_config,
|
253 |
+
trust_remote_code=True,
|
254 |
+
offload_buffers=True,
|
255 |
+
)
|
256 |
+
generation_config = GenerationConfig(
|
257 |
+
max_new_tokens=20,
|
258 |
+
eos_token_id=200020,
|
259 |
+
use_cache=True,
|
260 |
+
)
|
261 |
+
|
262 |
+
# Execute generation
|
263 |
+
generated_ids = quantized_model.generate(**model_inputs, generation_config=generation_config)
|
264 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
265 |
+
```
|
266 |
+
|
267 |
+
### Running the Example
|
268 |
+
|
269 |
+
You can run the example code using the following command:
|
270 |
+
|
271 |
+
```bash
|
272 |
+
export SAFETENSORS_FAST_GPU=1
|
273 |
+
python main.py --quant_type int8 --world_size 8 --model_id <model_path> --enable_tools
|
274 |
+
```
|
275 |
+
|
276 |
+
Parameter description:
|
277 |
+
- `--quant_type`: Quantization type, options are "default" or "int8"
|
278 |
+
- `--world_size`: Number of GPUs, int8 quantization requires at least 8 GPUs
|
279 |
+
- `--model_id`: Model path
|
280 |
+
- `--enable_tools`: Enable function call feature
|
281 |
+
|
282 |
+
### Result Processing
|
283 |
+
As expected, you will get the following output:
|
284 |
+
|
285 |
+
````base
|
286 |
+
```typescript
|
287 |
+
functions.get_current_weather({"location": "Shanghai"})
|
288 |
+
```
|
289 |
+
````
|
290 |
+
|
291 |
+
You can use regular expressions to extract the function to call and its corresponding parameters:
|
292 |
+
|
293 |
+
````python
|
294 |
+
def try_parse_tool_calls(content: str):
|
295 |
+
pattern = r"```typescript\n(.+?)?\n```"
|
296 |
+
matches = re.finditer(pattern, content, re.DOTALL)
|
297 |
+
|
298 |
+
for match in matches:
|
299 |
+
function_code = match.group(1)
|
300 |
+
function_match = re.search(r'functions\.(\w+)\((.+)\)', function_code)
|
301 |
+
|
302 |
+
if not function_match:
|
303 |
+
continue
|
304 |
+
|
305 |
+
function_name = function_match.group(1)
|
306 |
+
arguments_str = function_match.group(2)
|
307 |
+
|
308 |
+
try:
|
309 |
+
arguments = json.loads(arguments_str)
|
310 |
+
print(f"tool_calls: [{{'type': 'function', 'function': {{'name': '{function_name}', 'arguments': {arguments}}}}}]")
|
311 |
+
|
312 |
+
if function_name == "get_current_weather":
|
313 |
+
location = arguments.get("location", "Unknown")
|
314 |
+
return {"role": "function", "name": function_name, "text": f'{{"location": "{location}", "temperature": "25", "unit": "celsius", "weather": "Sun"}}'}
|
315 |
+
except json.JSONDecodeError as e:
|
316 |
+
print(f"Failed parse tools: {arguments_str}, Error: {e}")
|
317 |
+
|
318 |
+
return {}
|
319 |
+
````
|
320 |
+
|
321 |
+
### Chat Template
|
322 |
+
|
323 |
+
MiniMax-Text-01 uses a specific chat template format to process function calls. The chat template is defined in `tokenizer_config.json`:
|
324 |
+
|
325 |
+
```json
|
326 |
+
"{% for message in messages %}{% if message['role'] == 'system' %}{{ '<beginning_of_sentence>system ai_setting=assistant\\n' + message['content'][0]['text'] + '<end_of_sentence>\\n'}}{% elif message['role'] == 'user' %}{{ '<beginning_of_sentence>user name=user\\n' + message['content'][0]['text'] + '<end_of_sentence>\\n'}}{% elif message['role'] == 'assistant' %}{{ '<beginning_of_sentence>ai name=assistant\\n' }}{% for content in message['content'] | selectattr('type', 'equalto', 'text') %}{% generation %}{{ content['text'] }}{% endgeneration %}{% endfor %}{{ '<end_of_sentence>\\n' }}{% elif message['role'] == 'function' %}{{ '<beginning_of_sentence>system function_response=functions\\n' + '{\"name\": \"' + message['name'] + '\", \"response\": ' + message['content'][0]['text'] + '}' + '<end_of_sentence>\\n'}}{% endif %}{% endfor %}{% if tools %}{% for function in tools %}{{ '<beginning_of_sentence>system function_setting=functions\\n' + function | tojson + '<end_of_sentence>\\n'}}{% endfor %}{% endif %}{% if add_generation_prompt %}{{ '<beginning_of_sentence>ai name=assistant\\n' }}{% endif %}"
|
327 |
+
```
|
328 |
+
|
329 |
+
## 📝 Important Notes
|
330 |
+
|
331 |
+
1. Function names should follow programming language naming conventions and avoid special characters
|
332 |
+
2. Parameter descriptions should be concise and help the model understand the parameter's purpose and constraints
|
333 |
+
3. The model does not guarantee that it will call a function; this depends on the user's input and the model's judgment
|
334 |
+
4. Function results should be returned in a structured format for easy processing by the model
|
335 |
+
5. The model might not call a function even if one is provided, depending on whether it determines a function call is appropriate for the given user query
|
MiniMax-Text-01_Function_Call_Guide_CN.md
ADDED
@@ -0,0 +1,335 @@
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# MiniMax-Text-01 函数调用(Function Call)功能指南
|
2 |
+
|
3 |
+
## 📖 简介
|
4 |
+
|
5 |
+
MiniMax-Text-01 模型支持函数调用功能,使模型能够识别何时需要调用外部函数,并以结构化格式输出函数调用参数。本文档详细介绍了如何使用 MiniMax-Text-01 的函数调用功能。
|
6 |
+
|
7 |
+
## 🛠️ 函数调用的定义
|
8 |
+
|
9 |
+
### 函数结构体
|
10 |
+
|
11 |
+
函数调用需要在请求体中定义 `tools` 字段,每个函数由以下部分组成:
|
12 |
+
|
13 |
+
```json
|
14 |
+
{
|
15 |
+
"tools": [
|
16 |
+
{
|
17 |
+
"type": "function",
|
18 |
+
"function": {
|
19 |
+
"name": "function_name", // 函数名称,必填
|
20 |
+
"description": "function_description", // 函数描述,应简明扼要说明函数功能
|
21 |
+
"parameters": { // 函数参数定义,符合 JSON Schema 格式
|
22 |
+
"type": "object", // 参数整体类型,固定为object
|
23 |
+
"properties": { // 参数属性对象
|
24 |
+
"param_name": { // 参数名称
|
25 |
+
"description": "参数描述", // 参数说明
|
26 |
+
"type": "string|number|boolean|array|object" // 参数类型
|
27 |
+
}
|
28 |
+
},
|
29 |
+
"required": ["param1", "param2"] // 必填参数列表
|
30 |
+
}
|
31 |
+
}
|
32 |
+
}
|
33 |
+
]
|
34 |
+
}
|
35 |
+
```
|
36 |
+
|
37 |
+
### 示例
|
38 |
+
|
39 |
+
以下是一个简单的天气查询函数定义示例:
|
40 |
+
|
41 |
+
```json
|
42 |
+
"tools": [
|
43 |
+
{
|
44 |
+
"type": "function",
|
45 |
+
"function": {
|
46 |
+
"name": "get_current_weather",
|
47 |
+
"description": "Get the latest weather for a location",
|
48 |
+
"parameters": {
|
49 |
+
"type": "object",
|
50 |
+
"properties": {
|
51 |
+
"location": {
|
52 |
+
"type": "string",
|
53 |
+
"description": "A certain city, such as Beijing, Shanghai"
|
54 |
+
}
|
55 |
+
},
|
56 |
+
"required": ["location"]
|
57 |
+
}
|
58 |
+
}
|
59 |
+
}
|
60 |
+
]
|
61 |
+
```
|
62 |
+
|
63 |
+
### 完整请求示例
|
64 |
+
|
65 |
+
下面是一个包含函数定义的完整Python代码示例:
|
66 |
+
|
67 |
+
```python
|
68 |
+
payload = json.dumps({
|
69 |
+
"model": "MiniMax-Text-01",
|
70 |
+
"messages": [
|
71 |
+
{
|
72 |
+
"role": "system",
|
73 |
+
"content": "MM Intelligent Assistant is a large-scale language model developed by MiniMax and has no interfaces to call other products. MiniMax is a China technology company that has been committed to conducting research related to large models."
|
74 |
+
},
|
75 |
+
{
|
76 |
+
"role": "user",
|
77 |
+
"content": "上海今天天气怎么样?"
|
78 |
+
}
|
79 |
+
],
|
80 |
+
"tools": [
|
81 |
+
{
|
82 |
+
"type": "function",
|
83 |
+
"function": {
|
84 |
+
"name": "get_current_weather",
|
85 |
+
"description": "Get the latest weather for a location",
|
86 |
+
"parameters": {
|
87 |
+
"type": "object",
|
88 |
+
"properties": {
|
89 |
+
"location": {
|
90 |
+
"type": "string",
|
91 |
+
"description": "A certain city, such as Beijing, Shanghai"
|
92 |
+
}
|
93 |
+
},
|
94 |
+
"required": ["location"]
|
95 |
+
}
|
96 |
+
}
|
97 |
+
}
|
98 |
+
],
|
99 |
+
"tool_choice": "auto",
|
100 |
+
"stream": True,
|
101 |
+
"max_tokens": 10000,
|
102 |
+
"temperature": 0.9,
|
103 |
+
"top_p": 1
|
104 |
+
})
|
105 |
+
```
|
106 |
+
|
107 |
+
## 🔄 函数调用的输入格式
|
108 |
+
|
109 |
+
在模型内部处理时,函数定义会被转换为特殊格式并拼接到输入文本中:
|
110 |
+
|
111 |
+
```
|
112 |
+
<beginning_of_sentence>system function_setting=functions
|
113 |
+
{"name": "get_current_weather", "description": "Get the latest weather for a location", "parameters": {"type": "object", "properties": {"location": {"type": "string", "description": "A certain city, such as Beijing, Shanghai"}}, "required": ["location"]}}<end_of_sentence>
|
114 |
+
```
|
115 |
+
|
116 |
+
注意事项:
|
117 |
+
1. 函数定义位于系统设置之后、对话数据之前
|
118 |
+
2. 使用 `function_setting=functions` 标记函数定义区域
|
119 |
+
3. 每个函数定义使用JSON字符串表示
|
120 |
+
4. 区域以 `<end_of_sentence>` 结束
|
121 |
+
|
122 |
+
## 📤 模型的函数调用输出
|
123 |
+
|
124 |
+
当模型决定调用函数时,它会在响应中使用特殊格式输出函数调用信息:
|
125 |
+
|
126 |
+
````
|
127 |
+
<function_call>```typescript
|
128 |
+
functions.get_current_weather({"location": "上海"})
|
129 |
+
```
|
130 |
+
````
|
131 |
+
|
132 |
+
"<function_call>" 是 special token, 后面的 "functions.函数名(参数 json 结构体)", 需要字符串匹配出参数, 交外部执行.
|
133 |
+
|
134 |
+
## 📥 函数执行结果的处理
|
135 |
+
|
136 |
+
当函数调用成功执行后,模型将返回以下格式的输出:
|
137 |
+
|
138 |
+
````typescript
|
139 |
+
```typescript
|
140 |
+
functions.get_current_weather({"location": "Shanghai"})
|
141 |
+
```
|
142 |
+
````
|
143 |
+
|
144 |
+
您可以使用以下正则表达式方法提取函数名称和参数,便于后续处理:
|
145 |
+
|
146 |
+
````python
|
147 |
+
def parse_function_calls(content: str):
|
148 |
+
"""
|
149 |
+
解析模型返回的函数调用内容,提取函数名和参数
|
150 |
+
|
151 |
+
参数:
|
152 |
+
content: 模型返回的原始内容字符串
|
153 |
+
|
154 |
+
返回:
|
155 |
+
解析后的函数调用信息字典,包含函数名和参数
|
156 |
+
"""
|
157 |
+
# 匹配 typescript 代码块
|
158 |
+
pattern = r"```typescript\n(.+?)?\n```"
|
159 |
+
matches = re.finditer(pattern, content, re.DOTALL)
|
160 |
+
|
161 |
+
for match in matches:
|
162 |
+
function_code = match.group(1)
|
163 |
+
# 提取函数名和参数
|
164 |
+
function_match = re.search(r'functions\.(\w+)\((.+)\)', function_code)
|
165 |
+
|
166 |
+
if not function_match:
|
167 |
+
continue
|
168 |
+
|
169 |
+
function_name = function_match.group(1)
|
170 |
+
arguments_str = function_match.group(2)
|
171 |
+
|
172 |
+
try:
|
173 |
+
# 解析参数JSON
|
174 |
+
arguments = json.loads(arguments_str)
|
175 |
+
print(f"调用函数: {function_name}, 参数: {arguments}")
|
176 |
+
|
177 |
+
# 示例: 处理天气查询函数
|
178 |
+
if function_name == "get_current_weather":
|
179 |
+
location = arguments.get("location", "未知位置")
|
180 |
+
# 构建函数执行结果
|
181 |
+
return {
|
182 |
+
"role": "function",
|
183 |
+
"name": function_name,
|
184 |
+
"text": json.dumps({
|
185 |
+
"location": location,
|
186 |
+
"temperature": "25",
|
187 |
+
"unit": "celsius",
|
188 |
+
"weather": "晴朗"
|
189 |
+
}, ensure_ascii=False)
|
190 |
+
}
|
191 |
+
except json.JSONDecodeError as e:
|
192 |
+
print(f"参数解析失败: {arguments_str}, 错误: {e}")
|
193 |
+
|
194 |
+
return {}
|
195 |
+
````
|
196 |
+
|
197 |
+
成功解析函数调用后,您应将函数执行结果添加到对话历史中,以便模型在后续交互中能够访问和利用这些信息。
|
198 |
+
|
199 |
+
## 💻 使用 Transformers 库的函数调用示例
|
200 |
+
|
201 |
+
MiniMax-Text-01 官方仓库提供了使用 Transformers 库进行函数调用的完整示例。您可以在 [MiniMaxAI/MiniMax-Text-01 huggingface 仓库](https://huggingface.co/MiniMaxAI/MiniMax-Text-01/blob/main/main.py) 中查看源代码。
|
202 |
+
|
203 |
+
以下是使用 Transformers 库实现函数调用的关键部分:
|
204 |
+
|
205 |
+
```python
|
206 |
+
def get_default_tools():
|
207 |
+
return [
|
208 |
+
{
|
209 |
+
"type": "function",
|
210 |
+
"function": {
|
211 |
+
"name": "get_current_weather",
|
212 |
+
"description": "Get the latest weather for a location",
|
213 |
+
"parameters": {
|
214 |
+
"type": "object",
|
215 |
+
"properties": {
|
216 |
+
"location": {
|
217 |
+
"type": "string",
|
218 |
+
"description": "A certain city, such as Beijing, Shanghai"
|
219 |
+
}
|
220 |
+
},
|
221 |
+
"required": ["location"]
|
222 |
+
}
|
223 |
+
}
|
224 |
+
}
|
225 |
+
]
|
226 |
+
|
227 |
+
# 加载模型和分词器
|
228 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
229 |
+
prompt = "What's the weather like in Shanghai today?"
|
230 |
+
messages = [
|
231 |
+
{"role": "system", "content": [{"type": "text", "text": "You are a helpful assistant created by Minimax based on MiniMax-Text-01 model."}]},
|
232 |
+
{"role": "user", "content": [{"type": "text", "text": prompt}]},
|
233 |
+
]
|
234 |
+
|
235 |
+
# 启用函数调用工具
|
236 |
+
tools = get_default_tools()
|
237 |
+
|
238 |
+
# 应用聊天模板,并加入工具定义
|
239 |
+
text = tokenizer.apply_chat_template(
|
240 |
+
messages,
|
241 |
+
tokenize=False,
|
242 |
+
add_generation_prompt=True,
|
243 |
+
tools=tools
|
244 |
+
)
|
245 |
+
|
246 |
+
# 生成回复
|
247 |
+
model_inputs = tokenizer(text, return_tensors="pt").to("cuda")
|
248 |
+
quantized_model = AutoModelForCausalLM.from_pretrained(
|
249 |
+
model_id,
|
250 |
+
torch_dtype="bfloat16",
|
251 |
+
device_map=device_map,
|
252 |
+
quantization_config=quantization_config,
|
253 |
+
trust_remote_code=True,
|
254 |
+
offload_buffers=True,
|
255 |
+
)
|
256 |
+
generation_config = GenerationConfig(
|
257 |
+
max_new_tokens=20,
|
258 |
+
eos_token_id=200020,
|
259 |
+
use_cache=True,
|
260 |
+
)
|
261 |
+
|
262 |
+
# 执行生成
|
263 |
+
generated_ids = quantized_model.generate(**model_inputs, generation_config=generation_config)
|
264 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
265 |
+
```
|
266 |
+
|
267 |
+
### 运行方式
|
268 |
+
|
269 |
+
您可以通过以下命令运行示例代码:
|
270 |
+
|
271 |
+
```bash
|
272 |
+
export SAFETENSORS_FAST_GPU=1
|
273 |
+
python main.py --quant_type int8 --world_size 8 --model_id <model_path> --enable_tools
|
274 |
+
```
|
275 |
+
|
276 |
+
参数说明:
|
277 |
+
- `--quant_type`: 量化类型,可选 "default" 或 "int8"
|
278 |
+
- `--world_size`: GPU 数量,int8 量化至少需要 8 个 GPU
|
279 |
+
- `--model_id`: 模型路径
|
280 |
+
- `--enable_tools`: 启用函数调用功能
|
281 |
+
|
282 |
+
### 结果处理
|
283 |
+
符合预期的情况下,你将得到以下输出
|
284 |
+
|
285 |
+
````base
|
286 |
+
```typescript
|
287 |
+
functions.get_current_weather({"location": "Shanghai"})
|
288 |
+
```
|
289 |
+
````
|
290 |
+
|
291 |
+
你可以使用正则表达式提取出需要调用的 function 和 对应的参数
|
292 |
+
|
293 |
+
````python
|
294 |
+
def try_parse_tool_calls(content: str):
|
295 |
+
pattern = r"```typescript\n(.+?)?\n```"
|
296 |
+
matches = re.finditer(pattern, content, re.DOTALL)
|
297 |
+
|
298 |
+
for match in matches:
|
299 |
+
function_code = match.group(1)
|
300 |
+
function_match = re.search(r'functions\.(\w+)\((.+)\)', function_code)
|
301 |
+
|
302 |
+
if not function_match:
|
303 |
+
continue
|
304 |
+
|
305 |
+
function_name = function_match.group(1)
|
306 |
+
arguments_str = function_match.group(2)
|
307 |
+
|
308 |
+
try:
|
309 |
+
arguments = json.loads(arguments_str)
|
310 |
+
print(f"tool_calls: [{{'type': 'function', 'function': {{'name': '{function_name}', 'arguments': {arguments}}}}}]")
|
311 |
+
|
312 |
+
if function_name == "get_current_weather":
|
313 |
+
location = arguments.get("location", "Unknown")
|
314 |
+
return {"role": "function", "name": function_name, "text": f'{{"location": "{location}", "temperature": "25", "unit": "celsius", "weather": "Sun"}}'}
|
315 |
+
except json.JSONDecodeError as e:
|
316 |
+
print(f"Failed parse tools: {arguments_str}, Error: {e}")
|
317 |
+
|
318 |
+
return {}
|
319 |
+
````
|
320 |
+
|
321 |
+
### 聊天模板
|
322 |
+
|
323 |
+
MiniMax-Text-01 使用特定的聊天模板格式处理函数调用。聊天模板定义在 `tokenizer_config.json` 中:
|
324 |
+
|
325 |
+
```json
|
326 |
+
"{% for message in messages %}{% if message['role'] == 'system' %}{{ '<beginning_of_sentence>system ai_setting=assistant\\n' + message['content'][0]['text'] + '<end_of_sentence>\\n'}}{% elif message['role'] == 'user' %}{{ '<beginning_of_sentence>user name=user\\n' + message['content'][0]['text'] + '<end_of_sentence>\\n'}}{% elif message['role'] == 'assistant' %}{{ '<beginning_of_sentence>ai name=assistant\\n' }}{% for content in message['content'] | selectattr('type', 'equalto', 'text') %}{% generation %}{{ content['text'] }}{% endgeneration %}{% endfor %}{{ '<end_of_sentence>\\n' }}{% elif message['role'] == 'function' %}{{ '<beginning_of_sentence>system function_response=functions\\n' + '{\"name\": \"' + message['name'] + '\", \"response\": ' + message['content'][0]['text'] + '}' + '<end_of_sentence>\\n'}}{% endif %}{% endfor %}{% if tools %}{% for function in tools %}{{ '<beginning_of_sentence>system function_setting=functions\\n' + function | tojson + '<end_of_sentence>\\n'}}{% endfor %}{% endif %}{% if add_generation_prompt %}{{ '<beginning_of_sentence>ai name=assistant\\n' }}{% endif %}"
|
327 |
+
|
328 |
+
```
|
329 |
+
|
330 |
+
## 📝 注意事项
|
331 |
+
|
332 |
+
1. 函数名称应当遵循编程语言的命名规范,避免使用特殊字符
|
333 |
+
2. 参数描述应当简洁明了,帮助模型理解参数的用途和约束
|
334 |
+
3. 模型并不保证每次都会调用函数,这取决于用户的输入和模型的判断
|
335 |
+
4. 函数调用结果应当以结构化方式返回,便于模型理解和处理
|