Add files using upload-large-folder tool
Browse files- .mdl +0 -0
- .msc +0 -0
- .mv +1 -0
- configuration.json +1 -0
- docs/function_call_guide_cn.md +482 -0
- docs/vllm_deploy_guide_cn.md +85 -0
- generation_config.json +7 -0
- merges.txt +0 -0
- model-00018-of-00041.safetensors +3 -0
- model-00019-of-00041.safetensors +3 -0
- model-00021-of-00041.safetensors +3 -0
- model-00023-of-00041.safetensors +3 -0
- model-00025-of-00041.safetensors +3 -0
- model-00030-of-00041.safetensors +3 -0
- model-00035-of-00041.safetensors +3 -0
.mdl
ADDED
|
Binary file (44 Bytes). View file
|
|
|
.msc
ADDED
|
Binary file (4.67 kB). View file
|
|
|
.mv
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Revision:master,CreatedAt:1761613818
|
configuration.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"framework": "pytorch", "task": "text-generation", "allow_remote": true}
|
docs/function_call_guide_cn.md
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MiniMax-M2 函数调用(Function Call)功能指南
|
| 2 |
+
|
| 3 |
+
## 简介
|
| 4 |
+
|
| 5 |
+
MiniMax-M2 模型支持函数调用功能,使模型能够识别何时需要调用外部函数,并以结构化格式输出函数调用参数。本文档详细介绍了如何使用 MiniMax-M2 的函数调用功能。
|
| 6 |
+
|
| 7 |
+
## 基础示例
|
| 8 |
+
|
| 9 |
+
以下 Python 脚本基于 OpenAI SDK 实现了一个天气查询函数的调用示例:
|
| 10 |
+
|
| 11 |
+
```python
|
| 12 |
+
from openai import OpenAI
|
| 13 |
+
import json
|
| 14 |
+
|
| 15 |
+
client = OpenAI(base_url="http://localhost:8000/v1", api_key="dummy")
|
| 16 |
+
|
| 17 |
+
def get_weather(location: str, unit: str):
|
| 18 |
+
return f"Getting the weather for {location} in {unit}..."
|
| 19 |
+
|
| 20 |
+
tool_functions = {"get_weather": get_weather}
|
| 21 |
+
|
| 22 |
+
tools = [{
|
| 23 |
+
"type": "function",
|
| 24 |
+
"function": {
|
| 25 |
+
"name": "get_weather",
|
| 26 |
+
"description": "Get the current weather in a given location",
|
| 27 |
+
"parameters": {
|
| 28 |
+
"type": "object",
|
| 29 |
+
"properties": {
|
| 30 |
+
"location": {"type": "string", "description": "City and state, e.g., 'San Francisco, CA'"},
|
| 31 |
+
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
|
| 32 |
+
},
|
| 33 |
+
"required": ["location", "unit"]
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
}]
|
| 37 |
+
|
| 38 |
+
response = client.chat.completions.create(
|
| 39 |
+
model=client.models.list().data[0].id,
|
| 40 |
+
messages=[{"role": "user", "content": "What's the weather like in San Francisco? use celsius."}],
|
| 41 |
+
tools=tools,
|
| 42 |
+
tool_choice="auto"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
print(response)
|
| 46 |
+
|
| 47 |
+
tool_call = response.choices[0].message.tool_calls[0].function
|
| 48 |
+
print(f"Function called: {tool_call.name}")
|
| 49 |
+
print(f"Arguments: {tool_call.arguments}")
|
| 50 |
+
print(f"Result: {get_weather(**json.loads(tool_call.arguments))}")
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
**输出示例:**
|
| 54 |
+
```
|
| 55 |
+
Function called: get_weather
|
| 56 |
+
Arguments: {"location": "San Francisco, CA", "unit": "celsius"}
|
| 57 |
+
Result: Getting the weather for San Francisco, CA in celsius...
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
## 手动解析模型输出
|
| 61 |
+
|
| 62 |
+
如果您无法使用已支持 MiniMax-M2 的推理引擎的内置解析器,或者需要使用其他推理框架(如 transformers、TGI 等),可以使用以下方法手动解析模型的原始输出。这种方法需要您自己解析模型输出的 XML 标签格式。
|
| 63 |
+
|
| 64 |
+
### 使用 Transformers 的示例
|
| 65 |
+
|
| 66 |
+
以下是使用 transformers 库的完整示例:
|
| 67 |
+
|
| 68 |
+
```python
|
| 69 |
+
from transformers import AutoTokenizer
|
| 70 |
+
|
| 71 |
+
def get_default_tools():
|
| 72 |
+
return [
|
| 73 |
+
{
|
| 74 |
+
"name": "get_current_weather",
|
| 75 |
+
"description": "Get the latest weather for a location",
|
| 76 |
+
"parameters": {
|
| 77 |
+
"type": "object",
|
| 78 |
+
"properties": {
|
| 79 |
+
"location": {
|
| 80 |
+
"type": "string",
|
| 81 |
+
"description": "A certain city, such as Beijing, Shanghai"
|
| 82 |
+
}
|
| 83 |
+
},
|
| 84 |
+
}
|
| 85 |
+
"required": ["location"],
|
| 86 |
+
"type": "object"
|
| 87 |
+
}
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
# 加载模型和分词器
|
| 91 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 92 |
+
prompt = "What's the weather like in Shanghai today?"
|
| 93 |
+
messages = [
|
| 94 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 95 |
+
{"role": "user", "content": prompt},
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
# 启用函数调用工具
|
| 99 |
+
tools = get_default_tools()
|
| 100 |
+
|
| 101 |
+
# 应用聊天模板,并加入工具定义
|
| 102 |
+
text = tokenizer.apply_chat_template(
|
| 103 |
+
messages,
|
| 104 |
+
tokenize=False,
|
| 105 |
+
add_generation_prompt=True,
|
| 106 |
+
tools=tools
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# 发送请求(这里使用任何推理服务)
|
| 110 |
+
import requests
|
| 111 |
+
payload = {
|
| 112 |
+
"model": "MiniMaxAI/MiniMax-M2",
|
| 113 |
+
"prompt": text,
|
| 114 |
+
"max_tokens": 4096
|
| 115 |
+
}
|
| 116 |
+
response = requests.post(
|
| 117 |
+
"http://localhost:8000/v1/completions",
|
| 118 |
+
headers={"Content-Type": "application/json"},
|
| 119 |
+
json=payload,
|
| 120 |
+
stream=False,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# 模型输出需要手动解析
|
| 124 |
+
raw_output = response.json()["choices"][0]["text"]
|
| 125 |
+
print("原始输出:", raw_output)
|
| 126 |
+
|
| 127 |
+
# 使用下面的解析函数处理输出
|
| 128 |
+
function_calls = parse_tool_calls(raw_output, tools)
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
## 🛠️ 函数调用的定义
|
| 132 |
+
|
| 133 |
+
### 函数结构体
|
| 134 |
+
|
| 135 |
+
函数调用需要在请求体中定义 `tools` 字段,每个函数由以下部分组成:
|
| 136 |
+
|
| 137 |
+
```json
|
| 138 |
+
{
|
| 139 |
+
"tools": [
|
| 140 |
+
{
|
| 141 |
+
"name": "search_web",
|
| 142 |
+
"description": "搜索函数。",
|
| 143 |
+
"parameters": {
|
| 144 |
+
"properties": {
|
| 145 |
+
"query_list": {
|
| 146 |
+
"description": "进行搜索的关键词,列表元素个数为1。",
|
| 147 |
+
"items": { "type": "string" },
|
| 148 |
+
"type": "array"
|
| 149 |
+
},
|
| 150 |
+
"query_tag": {
|
| 151 |
+
"description": "query的分类",
|
| 152 |
+
"items": { "type": "string" },
|
| 153 |
+
"type": "array"
|
| 154 |
+
}
|
| 155 |
+
},
|
| 156 |
+
"required": [ "query_list", "query_tag" ],
|
| 157 |
+
"type": "object"
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
]
|
| 161 |
+
}
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
**字段说明:**
|
| 165 |
+
- `name`: 函数名称
|
| 166 |
+
- `description`: 函数功能描述
|
| 167 |
+
- `parameters`: 函数参数定义
|
| 168 |
+
- `properties`: 参数属性定义,key 是参数名,value 包含参数的详细描述
|
| 169 |
+
- `required`: 必填参数列表
|
| 170 |
+
- `type`: 参数类型(通常为 "object")
|
| 171 |
+
|
| 172 |
+
### 模型内部处理格式
|
| 173 |
+
|
| 174 |
+
在 MiniMax-M2 模型内部处理���,函数定义会被转换为特殊格式并拼接到输入文本中。以下是一个完整的示例:
|
| 175 |
+
|
| 176 |
+
```
|
| 177 |
+
]~!b[]~b]system
|
| 178 |
+
You are a helpful assistant.
|
| 179 |
+
|
| 180 |
+
# Tools
|
| 181 |
+
You may call one or more tools to assist with the user query.
|
| 182 |
+
Here are the tools available in JSONSchema format:
|
| 183 |
+
|
| 184 |
+
<tools>
|
| 185 |
+
<tool>{"name": "search_web", "description": "搜索函数。", "parameters": {"type": "object", "properties": {"query_list": {"type": "array", "items": {"type": "string"}, "description": "进行搜索的关键词,列表元素个数为1。"}, "query_tag": {"type": "array", "items": {"type": "string"}, "description": "query的分类"}}, "required": ["query_list", "query_tag"]}}</tool>
|
| 186 |
+
</tools>
|
| 187 |
+
|
| 188 |
+
When making tool calls, use XML format to invoke tools and pass parameters:
|
| 189 |
+
|
| 190 |
+
<minimax:tool_call>
|
| 191 |
+
<invoke name="tool-name-1">
|
| 192 |
+
<parameter name="param-key-1">param-value-1</parameter>
|
| 193 |
+
<parameter name="param-key-2">param-value-2</parameter>
|
| 194 |
+
...
|
| 195 |
+
</invoke>
|
| 196 |
+
[e~[
|
| 197 |
+
]~b]user
|
| 198 |
+
OpenAI 和 Gemini 的最近一次发布会都是什么时候?[e~[
|
| 199 |
+
]~b]ai
|
| 200 |
+
<think>
|
| 201 |
+
```
|
| 202 |
+
|
| 203 |
+
**格式说明:**
|
| 204 |
+
|
| 205 |
+
- `]~!b[]~b]system`: System 消息开始标记
|
| 206 |
+
- `[e~[`: 消息结束标记
|
| 207 |
+
- `]~b]user`: User 消息开始标记
|
| 208 |
+
- `]~b]ai`: Assistant 消息开始标记
|
| 209 |
+
- `]~b]tool`: Tool 结果消息开始标记
|
| 210 |
+
- `<tools>...</tools>`: 工具定义区域,每个工具用 `<tool>` 标签包裹,内容为 JSON Schema
|
| 211 |
+
- `<minimax:tool_call>...</minimax:tool_call>`: 工具调用区域
|
| 212 |
+
- `<think>`: 生成时的思考过程标记(可选)
|
| 213 |
+
|
| 214 |
+
### 模型输出格式
|
| 215 |
+
|
| 216 |
+
MiniMax-M2使用结构化的 XML 标签格式:
|
| 217 |
+
|
| 218 |
+
```xml
|
| 219 |
+
<minimax:tool_call>
|
| 220 |
+
<invoke name="search_web">
|
| 221 |
+
<parameter name="query_tag">["technology", "events"]</parameter>
|
| 222 |
+
<parameter name="query_list">["\"OpenAI\" \"latest\" \"release\""]</parameter>
|
| 223 |
+
</invoke>
|
| 224 |
+
<invoke name="search_web">
|
| 225 |
+
<parameter name="query_tag">["technology", "events"]</parameter>
|
| 226 |
+
<parameter name="query_list">["\"Gemini\" \"latest\" \"release\""]</parameter>
|
| 227 |
+
</invoke>
|
| 228 |
+
</minimax:tool_call>
|
| 229 |
+
```
|
| 230 |
+
|
| 231 |
+
每个函数调用使用 `<invoke name="函数名">` 标签,参数使用 `<parameter name="参数名">` 标签包裹。
|
| 232 |
+
|
| 233 |
+
## 手动解析函数调用结果
|
| 234 |
+
|
| 235 |
+
### 解析函数调用
|
| 236 |
+
|
| 237 |
+
MiniMax-M2使用结构化的 XML 标签,需要不同的解析方式。核心函数如下:
|
| 238 |
+
|
| 239 |
+
```python
|
| 240 |
+
import re
|
| 241 |
+
import json
|
| 242 |
+
from typing import Any, Optional, List, Dict
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def extract_name(name_str: str) -> str:
|
| 246 |
+
"""从引号包裹的字符串中提取名称"""
|
| 247 |
+
name_str = name_str.strip()
|
| 248 |
+
if name_str.startswith('"') and name_str.endswith('"'):
|
| 249 |
+
return name_str[1:-1]
|
| 250 |
+
elif name_str.startswith("'") and name_str.endswith("'"):
|
| 251 |
+
return name_str[1:-1]
|
| 252 |
+
return name_str
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def convert_param_value(value: str, param_type: str) -> Any:
|
| 256 |
+
"""根据参数类型转换参数值"""
|
| 257 |
+
if value.lower() == "null":
|
| 258 |
+
return None
|
| 259 |
+
|
| 260 |
+
param_type = param_type.lower()
|
| 261 |
+
|
| 262 |
+
if param_type in ["string", "str", "text"]:
|
| 263 |
+
return value
|
| 264 |
+
elif param_type in ["integer", "int"]:
|
| 265 |
+
try:
|
| 266 |
+
return int(value)
|
| 267 |
+
except (ValueError, TypeError):
|
| 268 |
+
return value
|
| 269 |
+
elif param_type in ["number", "float"]:
|
| 270 |
+
try:
|
| 271 |
+
val = float(value)
|
| 272 |
+
return val if val != int(val) else int(val)
|
| 273 |
+
except (ValueError, TypeError):
|
| 274 |
+
return value
|
| 275 |
+
elif param_type in ["boolean", "bool"]:
|
| 276 |
+
return value.lower() in ["true", "1"]
|
| 277 |
+
elif param_type in ["object", "array"]:
|
| 278 |
+
try:
|
| 279 |
+
return json.loads(value)
|
| 280 |
+
except json.JSONDecodeError:
|
| 281 |
+
return value
|
| 282 |
+
else:
|
| 283 |
+
# 尝试 JSON 解析,失败则返回字符串
|
| 284 |
+
try:
|
| 285 |
+
return json.loads(value)
|
| 286 |
+
except json.JSONDecodeError:
|
| 287 |
+
return value
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
def parse_tool_calls(model_output: str, tools: Optional[List[Dict]] = None) -> List[Dict]:
|
| 291 |
+
"""
|
| 292 |
+
从模型输出中提取所有工具调用
|
| 293 |
+
|
| 294 |
+
Args:
|
| 295 |
+
model_output: 模型的完整输出文本
|
| 296 |
+
tools: 工具定义列表,用于获取参数类型信息,格式可以是:
|
| 297 |
+
- [{"name": "...", "parameters": {...}}]
|
| 298 |
+
- [{"type": "function", "function": {"name": "...", "parameters": {...}}}]
|
| 299 |
+
|
| 300 |
+
Returns:
|
| 301 |
+
解析后的工具调用列表,每个元素包含 name 和 arguments 字段
|
| 302 |
+
|
| 303 |
+
Example:
|
| 304 |
+
>>> tools = [{
|
| 305 |
+
... "name": "get_weather",
|
| 306 |
+
... "parameters": {
|
| 307 |
+
... "type": "object",
|
| 308 |
+
... "properties": {
|
| 309 |
+
... "location": {"type": "string"},
|
| 310 |
+
... "unit": {"type": "string"}
|
| 311 |
+
... }
|
| 312 |
+
... }
|
| 313 |
+
... }]
|
| 314 |
+
>>> output = '''<minimax:tool_call>
|
| 315 |
+
... <invoke name="get_weather">
|
| 316 |
+
... <parameter name="location">San Francisco</parameter>
|
| 317 |
+
... <parameter name="unit">celsius</parameter>
|
| 318 |
+
... </invoke>
|
| 319 |
+
... </minimax:tool_call>'''
|
| 320 |
+
>>> result = parse_tool_calls(output, tools)
|
| 321 |
+
>>> print(result)
|
| 322 |
+
[{'name': 'get_weather', 'arguments': {'location': 'San Francisco', 'unit': 'celsius'}}]
|
| 323 |
+
"""
|
| 324 |
+
# 快速检查是否包含工具调用标记
|
| 325 |
+
if "<minimax:tool_call>" not in model_output:
|
| 326 |
+
return []
|
| 327 |
+
|
| 328 |
+
tool_calls = []
|
| 329 |
+
|
| 330 |
+
try:
|
| 331 |
+
# 匹配所有 <minimax:tool_call> 块
|
| 332 |
+
tool_call_regex = re.compile(r"<minimax:tool_call>(.*?)</minimax:tool_call>", re.DOTALL)
|
| 333 |
+
invoke_regex = re.compile(r"<invoke name=(.*?)</invoke>", re.DOTALL)
|
| 334 |
+
parameter_regex = re.compile(r"<parameter name=(.*?)</parameter>", re.DOTALL)
|
| 335 |
+
|
| 336 |
+
# 遍历所有 tool_call 块
|
| 337 |
+
for tool_call_match in tool_call_regex.findall(model_output):
|
| 338 |
+
# 遍历该块中的所有 invoke
|
| 339 |
+
for invoke_match in invoke_regex.findall(tool_call_match):
|
| 340 |
+
# 提取函数名
|
| 341 |
+
name_match = re.search(r'^([^>]+)', invoke_match)
|
| 342 |
+
if not name_match:
|
| 343 |
+
continue
|
| 344 |
+
|
| 345 |
+
function_name = extract_name(name_match.group(1))
|
| 346 |
+
|
| 347 |
+
# 获取参数配置
|
| 348 |
+
param_config = {}
|
| 349 |
+
if tools:
|
| 350 |
+
for tool in tools:
|
| 351 |
+
tool_name = tool.get("name") or tool.get("function", {}).get("name")
|
| 352 |
+
if tool_name == function_name:
|
| 353 |
+
params = tool.get("parameters") or tool.get("function", {}).get("parameters")
|
| 354 |
+
if isinstance(params, dict) and "properties" in params:
|
| 355 |
+
param_config = params["properties"]
|
| 356 |
+
break
|
| 357 |
+
|
| 358 |
+
# 提取参数
|
| 359 |
+
param_dict = {}
|
| 360 |
+
for match in parameter_regex.findall(invoke_match):
|
| 361 |
+
param_match = re.search(r'^([^>]+)>(.*)', match, re.DOTALL)
|
| 362 |
+
if param_match:
|
| 363 |
+
param_name = extract_name(param_match.group(1))
|
| 364 |
+
param_value = param_match.group(2).strip()
|
| 365 |
+
|
| 366 |
+
# 去除首尾的换行符
|
| 367 |
+
if param_value.startswith('\n'):
|
| 368 |
+
param_value = param_value[1:]
|
| 369 |
+
if param_value.endswith('\n'):
|
| 370 |
+
param_value = param_value[:-1]
|
| 371 |
+
|
| 372 |
+
# 获取参数类型并转换
|
| 373 |
+
param_type = "string"
|
| 374 |
+
if param_name in param_config:
|
| 375 |
+
if isinstance(param_config[param_name], dict) and "type" in param_config[param_name]:
|
| 376 |
+
param_type = param_config[param_name]["type"]
|
| 377 |
+
|
| 378 |
+
param_dict[param_name] = convert_param_value(param_value, param_type)
|
| 379 |
+
|
| 380 |
+
tool_calls.append({
|
| 381 |
+
"name": function_name,
|
| 382 |
+
"arguments": param_dict
|
| 383 |
+
})
|
| 384 |
+
|
| 385 |
+
except Exception as e:
|
| 386 |
+
print(f"解析工具调用失败: {e}")
|
| 387 |
+
return []
|
| 388 |
+
|
| 389 |
+
return tool_calls
|
| 390 |
+
```
|
| 391 |
+
|
| 392 |
+
**使用示例:**
|
| 393 |
+
|
| 394 |
+
```python
|
| 395 |
+
# 定义工具
|
| 396 |
+
tools = [
|
| 397 |
+
{
|
| 398 |
+
"name": "get_weather",
|
| 399 |
+
"parameters": {
|
| 400 |
+
"type": "object",
|
| 401 |
+
"properties": {
|
| 402 |
+
"location": {"type": "string"},
|
| 403 |
+
"unit": {"type": "string"}
|
| 404 |
+
},
|
| 405 |
+
"required": ["location", "unit"]
|
| 406 |
+
}
|
| 407 |
+
}
|
| 408 |
+
]
|
| 409 |
+
|
| 410 |
+
# 模型输出
|
| 411 |
+
model_output = """我来帮你查询天气。
|
| 412 |
+
<minimax:tool_call>
|
| 413 |
+
<invoke name="get_weather">
|
| 414 |
+
<parameter name="location">San Francisco</parameter>
|
| 415 |
+
<parameter name="unit">celsius</parameter>
|
| 416 |
+
</invoke>
|
| 417 |
+
</minimax:tool_call>"""
|
| 418 |
+
|
| 419 |
+
# 解析工具调用
|
| 420 |
+
tool_calls = parse_tool_calls(model_output, tools)
|
| 421 |
+
|
| 422 |
+
# 输出结果
|
| 423 |
+
for call in tool_calls:
|
| 424 |
+
print(f"调用函数: {call['name']}")
|
| 425 |
+
print(f"参数: {call['arguments']}")
|
| 426 |
+
# 输出: 调用函数: get_weather
|
| 427 |
+
# 参数: {'location': 'San Francisco', 'unit': 'celsius'}
|
| 428 |
+
```
|
| 429 |
+
|
| 430 |
+
### 执行函数调用
|
| 431 |
+
|
| 432 |
+
解析完成后,您可以执行对应的函数并构建返回结果:
|
| 433 |
+
|
| 434 |
+
```python
|
| 435 |
+
def execute_function_call(function_name: str, arguments: dict):
|
| 436 |
+
"""执行函数调用并返回结果"""
|
| 437 |
+
if function_name == "get_weather":
|
| 438 |
+
location = arguments.get("location", "未知位置")
|
| 439 |
+
unit = arguments.get("unit", "celsius")
|
| 440 |
+
# 构建函数执行结果
|
| 441 |
+
return {
|
| 442 |
+
"role": "tool",
|
| 443 |
+
"content": [
|
| 444 |
+
{
|
| 445 |
+
"name": function_name,
|
| 446 |
+
"type": "text",
|
| 447 |
+
"text": json.dumps({
|
| 448 |
+
"location": location,
|
| 449 |
+
"temperature": "25",
|
| 450 |
+
"unit": unit,
|
| 451 |
+
"weather": "晴朗"
|
| 452 |
+
}, ensure_ascii=False)
|
| 453 |
+
}
|
| 454 |
+
]
|
| 455 |
+
}
|
| 456 |
+
elif function_name == "search_web":
|
| 457 |
+
query_list = arguments.get("query_list", [])
|
| 458 |
+
query_tag = arguments.get("query_tag", [])
|
| 459 |
+
# 模拟搜索结果
|
| 460 |
+
return {
|
| 461 |
+
"role": "tool",
|
| 462 |
+
"content": [
|
| 463 |
+
{
|
| 464 |
+
"name": function_name,
|
| 465 |
+
"type": "text",
|
| 466 |
+
"text": f"搜索关键词: {query_list}, 分类: {query_tag}\n搜索结果: 相关信息已找到"
|
| 467 |
+
}
|
| 468 |
+
]
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
return None
|
| 472 |
+
```
|
| 473 |
+
|
| 474 |
+
### 将函数执行结果返回给模型
|
| 475 |
+
|
| 476 |
+
成功解析函数调用后,您应将函数执行结果添加到对话历史中,以便模型在后续交互中能够访问和利用这些信息,拼接格式参考chat_template.jinja
|
| 477 |
+
|
| 478 |
+
## 参考资料
|
| 479 |
+
|
| 480 |
+
- [MiniMax-M2 模型仓库](https://github.com/MiniMax-AI/MiniMax-M2)
|
| 481 |
+
- [vLLM 项目主页](https://github.com/vllm-project/vllm)
|
| 482 |
+
- [OpenAI Python SDK](https://github.com/openai/openai-python)
|
docs/vllm_deploy_guide_cn.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# MiniMax M2 模型 vLLM 部署指南
|
| 2 |
+
|
| 3 |
+
我们推荐使用 [vLLM](https://docs.vllm.ai/en/stable/) 来部署 [MiniMax-M2](https://huggingface.co/MiniMaxAI/MiniMax-M2) 模型。vLLM 是一个高性能的推理引擎,其具有卓越的服务吞吐、高效智能的内存管理机制、强大的批量请求处理能力、深度优化的底层性能等特性。我们建议在部署之前查看 vLLM 的官方文档以检查硬件兼容性。
|
| 4 |
+
|
| 5 |
+
## 环境要求
|
| 6 |
+
|
| 7 |
+
- OS:Linux
|
| 8 |
+
|
| 9 |
+
- Python:3.9 - 3.12
|
| 10 |
+
|
| 11 |
+
- GPU:
|
| 12 |
+
|
| 13 |
+
- compute capability 7.0 or higher
|
| 14 |
+
|
| 15 |
+
- 显存需求:权重需要 220 GB,每 1M 上下文 token 需要 60 GB
|
| 16 |
+
|
| 17 |
+
以下为推荐配置,实际需求请根据业务场景调整:
|
| 18 |
+
|
| 19 |
+
- 96G x4 GPU:支持 40 万 token 的上下文输入。
|
| 20 |
+
|
| 21 |
+
- 144G x8 GPU:支持长达 300 万 token 的上下文输入。
|
| 22 |
+
|
| 23 |
+
## 使用 Python 部署
|
| 24 |
+
|
| 25 |
+
建议使用虚拟环境(如 venv、conda、uv)以避免依赖冲突。建议在全新的 Python 环境中安装 vLLM:
|
| 26 |
+
```bash
|
| 27 |
+
# 尚未 release,请安装 nightly 构建
|
| 28 |
+
uv pip install -U vllm \
|
| 29 |
+
--torch-backend=auto \
|
| 30 |
+
--extra-index-url https://wheels.vllm.ai/nightly
|
| 31 |
+
# 如果 release,使用 uv 安装
|
| 32 |
+
uv pip install "vllm" --torch-backend=auto
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
运行如下命令启动 vLLM 服务器,vLLM 会自动从 Huggingface 下载并缓存 MiniMax-M2 模型。
|
| 36 |
+
|
| 37 |
+
4 卡部署命令:
|
| 38 |
+
|
| 39 |
+
```bash
|
| 40 |
+
SAFETENSORS_FAST_GPU=1 VLLM_USE_V1=0 vllm serve \
|
| 41 |
+
--model MiniMaxAI/MiniMax-M2 \
|
| 42 |
+
--trust-remote-code \
|
| 43 |
+
--enable-expert-parallel --tensor-parallel-size 4 \
|
| 44 |
+
--enable-auto-tool-choice --tool-call-parser minimax_m2 \
|
| 45 |
+
--reasoning-parser minimax_m2
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## 测试部署
|
| 49 |
+
|
| 50 |
+
启动后,可以通过如下命令测试 vLLM OpenAI 兼容接口:
|
| 51 |
+
|
| 52 |
+
```bash
|
| 53 |
+
curl http://localhost:8000/v1/chat/completions \
|
| 54 |
+
-H "Content-Type: application/json" \
|
| 55 |
+
-d '{
|
| 56 |
+
"model": "MiniMaxAI/MiniMax-M2",
|
| 57 |
+
"messages": [
|
| 58 |
+
{"role": "system", "content": [{"type": "text", "text": "You are a helpful assistant."}]},
|
| 59 |
+
{"role": "user", "content": [{"type": "text", "text": "Who won the world series in 2020?"}]}
|
| 60 |
+
]
|
| 61 |
+
}'
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
## 常见问题
|
| 65 |
+
|
| 66 |
+
### Huggingface 网络问题
|
| 67 |
+
|
| 68 |
+
如果遇到网络问题,可以设置代理后再进行拉取。
|
| 69 |
+
|
| 70 |
+
```bash
|
| 71 |
+
export HF_ENDPOINT=https://hf-mirror.com
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### MiniMax-M2 model is not currently supported
|
| 75 |
+
|
| 76 |
+
该 vLLM 版本过旧,请升级到最新版本。
|
| 77 |
+
|
| 78 |
+
## 获取支持
|
| 79 |
+
|
| 80 |
+
如果在部署 MiniMax 模型过程中遇到任何问题:
|
| 81 |
+
|
| 82 |
+
- 通过邮箱 api@minimaxi.com 等官方渠道联系我们的技术支持团队
|
| 83 |
+
|
| 84 |
+
- 在我们的 [GitHub](https://github.com/MiniMax-AI) 仓库提交 Issue
|
| 85 |
+
我们会持续优化模型的部署体验,欢迎反馈!
|
generation_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"do_sample": true,
|
| 3 |
+
"temperature": 1.0,
|
| 4 |
+
"top_p": 0.95,
|
| 5 |
+
"top_k": 40,
|
| 6 |
+
"transformers_version": "4.46.1"
|
| 7 |
+
}
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model-00018-of-00041.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0c484c513b19332876b8a9c60d9c048d35bd7f901dee85626b0e4df3026b2b3b
|
| 3 |
+
size 3000142744
|
model-00019-of-00041.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5547279b84b99df76481050b1fa52a620746c6eb37ef7a57fd19a8234ef31588
|
| 3 |
+
size 2999228296
|
model-00021-of-00041.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6f5049087df124a4a8ce46f4f353d9b8f13d1b4246c92839af2802094ccd3fc2
|
| 3 |
+
size 3000144048
|
model-00023-of-00041.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:52bca9a623326262d11f48db4f5bc8a831a22afe1a3f66951d1886693bc997cf
|
| 3 |
+
size 3000144016
|
model-00025-of-00041.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6d85eae43d95143d3ac1670620b2d9000969feaa3e2d3ae71e1e2cc4f56488f7
|
| 3 |
+
size 3000142752
|
model-00030-of-00041.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cd88be612cafe8676970c19dc1fb4108257f4f6275b4a9bdf62b66ce39e347de
|
| 3 |
+
size 3000144016
|
model-00035-of-00041.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:adedc007dc2f50cb0f19da74182367e2b5188a7daae2ce213d454689e04ff7b1
|
| 3 |
+
size 3000144048
|