Update docs/function_call_guide_cn.md (#7)
Browse files- Update docs/function_call_guide_cn.md (5c218ff08e68db962142794f3d9b30a192de72fa)
Co-authored-by: Boji Shan <[email protected]>
- docs/function_call_guide_cn.md +86 -53
docs/function_call_guide_cn.md
CHANGED
|
@@ -16,21 +16,19 @@ from transformers import AutoTokenizer
|
|
| 16 |
def get_default_tools():
|
| 17 |
return [
|
| 18 |
{
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
},
|
| 30 |
-
}
|
| 31 |
-
"required": ["location"],
|
| 32 |
-
"type": "object"
|
| 33 |
}
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
]
|
| 36 |
|
|
@@ -52,6 +50,22 @@ text = tokenizer.apply_chat_template(
|
|
| 52 |
add_generation_prompt=True,
|
| 53 |
tools=tools
|
| 54 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
```
|
| 56 |
|
| 57 |
## 🛠️ 函数调用的定义
|
|
@@ -100,9 +114,9 @@ text = tokenizer.apply_chat_template(
|
|
| 100 |
在模型内部处理时,函数定义会被转换为特殊格式并拼接到输入文本中:
|
| 101 |
|
| 102 |
```
|
| 103 |
-
|
| 104 |
-
MiniMax AI是由上海稀宇科技有限公司(MiniMax)自主研发的AI
|
| 105 |
-
|
| 106 |
You are provided with these tools:
|
| 107 |
<tools>
|
| 108 |
{"name": "search_web", "description": "搜索函数。", "parameters": {"properties": {"query_list": {"description": "进行搜索的关键词,列表元素个数为1。", "items": {"type": "string"}, "type": "array"}, "query_tag": {"description": "query的分类", "items": {"type": "string"}, "type": "array"}}, "required": ["query_list", "query_tag"], "type": "object"}}
|
|
@@ -112,10 +126,10 @@ If you need to call tools, please respond with <tool_calls></tool_calls> XML tag
|
|
| 112 |
<tool_calls>
|
| 113 |
{"name": <tool-name>, "arguments": <args-json-object>}
|
| 114 |
...
|
| 115 |
-
</tool_calls>
|
| 116 |
-
|
| 117 |
-
OpenAI 和 Gemini
|
| 118 |
-
|
| 119 |
```
|
| 120 |
|
| 121 |
### 模型输出格式
|
|
@@ -191,23 +205,33 @@ def execute_function_call(function_name: str, arguments: dict):
|
|
| 191 |
# 构建函数执行结果
|
| 192 |
return {
|
| 193 |
"role": "tool",
|
| 194 |
-
"
|
| 195 |
-
|
| 196 |
-
"
|
| 197 |
-
"
|
| 198 |
-
"
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
elif function_name == "search_web":
|
| 203 |
query_list = arguments.get("query_list", [])
|
| 204 |
query_tag = arguments.get("query_tag", [])
|
| 205 |
# 模拟搜索结果
|
| 206 |
return {
|
| 207 |
"role": "tool",
|
| 208 |
-
"
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
return None
|
| 213 |
```
|
|
@@ -222,46 +246,55 @@ def execute_function_call(function_name: str, arguments: dict):
|
|
| 222 |
|
| 223 |
```json
|
| 224 |
{
|
| 225 |
-
"
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
| 231 |
]
|
| 232 |
}
|
| 233 |
```
|
| 234 |
|
| 235 |
对应如下的模型输入格式:
|
| 236 |
```
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
| 239 |
```
|
| 240 |
|
| 241 |
|
| 242 |
#### 多个结果
|
| 243 |
-
假如模型同时调用了 `search_web` 和 `get_current_weather` 函数,您可以参考如下格式添加执行结果,`
|
| 244 |
|
| 245 |
```json
|
| 246 |
{
|
| 247 |
-
"
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
]
|
| 254 |
}
|
| 255 |
```
|
| 256 |
|
| 257 |
对应如下的模型输入格式:
|
| 258 |
```
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
```
|
| 266 |
|
| 267 |
-
虽然我们建议您参考以上格式,但只要返回给模型的输入易于理解,`name` 和 `
|
|
|
|
| 16 |
def get_default_tools():
|
| 17 |
return [
|
| 18 |
{
|
| 19 |
+
"name": "get_current_weather",
|
| 20 |
+
"description": "Get the latest weather for a location",
|
| 21 |
+
"parameters": {
|
| 22 |
+
"type": "object",
|
| 23 |
+
"properties": {
|
| 24 |
+
"location": {
|
| 25 |
+
"type": "string",
|
| 26 |
+
"description": "A certain city, such as Beijing, Shanghai"
|
| 27 |
+
}
|
| 28 |
+
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
+
"required": ["location"],
|
| 31 |
+
"type": "object"
|
| 32 |
}
|
| 33 |
]
|
| 34 |
|
|
|
|
| 50 |
add_generation_prompt=True,
|
| 51 |
tools=tools
|
| 52 |
)
|
| 53 |
+
|
| 54 |
+
# 发送请求
|
| 55 |
+
import requests
|
| 56 |
+
payload = {
|
| 57 |
+
"model": "MiniMaxAI/MiniMax-M1-40k",
|
| 58 |
+
"prompt": text,
|
| 59 |
+
"max_tokens": 4000
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
response = requests.post(
|
| 63 |
+
"http://localhost:8000/v1/completions",
|
| 64 |
+
headers={"Content-Type": "application/json"},
|
| 65 |
+
json=payload,
|
| 66 |
+
stream=False,
|
| 67 |
+
)
|
| 68 |
+
print(response.json()["choices"][0]["text"])
|
| 69 |
```
|
| 70 |
|
| 71 |
## 🛠️ 函数调用的定义
|
|
|
|
| 114 |
在模型内部处理时,函数定义会被转换为特殊格式并拼接到输入文本中:
|
| 115 |
|
| 116 |
```
|
| 117 |
+
<begin_of_document><beginning_of_sentence>system ai_setting=MiniMax AI
|
| 118 |
+
MiniMax AI是由上海稀宇科技有限公司(MiniMax)自主研发的AI助理。<end_of_sentence>
|
| 119 |
+
<beginning_of_sentence>system tool_setting=tools
|
| 120 |
You are provided with these tools:
|
| 121 |
<tools>
|
| 122 |
{"name": "search_web", "description": "搜索函数。", "parameters": {"properties": {"query_list": {"description": "进行搜索的关键词,列表元素个数为1。", "items": {"type": "string"}, "type": "array"}, "query_tag": {"description": "query的分类", "items": {"type": "string"}, "type": "array"}}, "required": ["query_list", "query_tag"], "type": "object"}}
|
|
|
|
| 126 |
<tool_calls>
|
| 127 |
{"name": <tool-name>, "arguments": <args-json-object>}
|
| 128 |
...
|
| 129 |
+
</tool_calls><end_of_sentence>
|
| 130 |
+
<beginning_of_sentence>user name=用户
|
| 131 |
+
OpenAI 和 Gemini 的最近一次发布会都是什么时候?<end_of_sentence>
|
| 132 |
+
<beginning_of_sentence>ai name=MiniMax AI
|
| 133 |
```
|
| 134 |
|
| 135 |
### 模型输出格式
|
|
|
|
| 205 |
# 构建函数执行结果
|
| 206 |
return {
|
| 207 |
"role": "tool",
|
| 208 |
+
"content": [
|
| 209 |
+
{
|
| 210 |
+
"name": function_name,
|
| 211 |
+
"type": "text",
|
| 212 |
+
"text": json.dumps({
|
| 213 |
+
"location": location,
|
| 214 |
+
"temperature": "25",
|
| 215 |
+
"unit": "celsius",
|
| 216 |
+
"weather": "晴朗"
|
| 217 |
+
}, ensure_ascii=False)
|
| 218 |
+
}
|
| 219 |
+
]
|
| 220 |
+
}
|
| 221 |
elif function_name == "search_web":
|
| 222 |
query_list = arguments.get("query_list", [])
|
| 223 |
query_tag = arguments.get("query_tag", [])
|
| 224 |
# 模拟搜索结果
|
| 225 |
return {
|
| 226 |
"role": "tool",
|
| 227 |
+
"content": [
|
| 228 |
+
{
|
| 229 |
+
"name": function_name,
|
| 230 |
+
"type": "text",
|
| 231 |
+
"text": f"搜索关键词: {query_list}, 分类: {query_tag}\n搜索结果: 相关信息已找到"
|
| 232 |
+
}
|
| 233 |
+
]
|
| 234 |
+
}
|
| 235 |
|
| 236 |
return None
|
| 237 |
```
|
|
|
|
| 246 |
|
| 247 |
```json
|
| 248 |
{
|
| 249 |
+
"role": "tool",
|
| 250 |
+
"content": [
|
| 251 |
+
{
|
| 252 |
+
"name": "search_web",
|
| 253 |
+
"type": "text",
|
| 254 |
+
"text": "test_result"
|
| 255 |
+
}
|
| 256 |
]
|
| 257 |
}
|
| 258 |
```
|
| 259 |
|
| 260 |
对应如下的模型输入格式:
|
| 261 |
```
|
| 262 |
+
<beginning_of_sentence>tool name=tools
|
| 263 |
+
tool name: search_web
|
| 264 |
+
tool result: test_result
|
| 265 |
+
<end_of_sentence>
|
| 266 |
```
|
| 267 |
|
| 268 |
|
| 269 |
#### 多个结果
|
| 270 |
+
假如模型同时调用了 `search_web` 和 `get_current_weather` 函数,您可以参考如下格式添加执行结果,`content`包含多个结果。
|
| 271 |
|
| 272 |
```json
|
| 273 |
{
|
| 274 |
+
"role": "tool",
|
| 275 |
+
"content": [
|
| 276 |
+
{
|
| 277 |
+
"name": "search_web",
|
| 278 |
+
"type": "text",
|
| 279 |
+
"text": "test_result1"
|
| 280 |
+
},
|
| 281 |
+
{
|
| 282 |
+
"name": "get_current_weather",
|
| 283 |
+
"type": "text",
|
| 284 |
+
"text": "test_result2"
|
| 285 |
+
}
|
| 286 |
]
|
| 287 |
}
|
| 288 |
```
|
| 289 |
|
| 290 |
对应如下的模型输入格式:
|
| 291 |
```
|
| 292 |
+
<beginning_of_sentence>tool name=tools
|
| 293 |
+
tool name: search_web
|
| 294 |
+
tool result: test_result1
|
| 295 |
|
| 296 |
+
tool name: get_current_weather
|
| 297 |
+
tool result: test_result2<end_of_sentence>
|
| 298 |
```
|
| 299 |
|
| 300 |
+
虽然我们建议您参考以上格式,但只要返回给模型的输入易于理解,`name` 和 `text` 的具体内容完全由您自主决定。
|