URL: https://huggingface.co/RedHatAI/MiniMax-M2.5/raw/main/docs/tool_calling_guide.md
# MiniMax-M2.5 Tool Calling Guide
[English Version](./tool_calling_guide.md) | [Chinese Version](./tool_calling_guide_cn.md)
MiniMax-M2.5 supports the same toolcall syntax as MiniMax-M2.
## Introduction
The MiniMax-M2.5 model supports tool calling capabilities, enabling the model to identify when external tools need to be called and output tool call parameters in a structured format. This document provides detailed instructions on how to use the tool calling features of MiniMax-M2.5.
## Basic Example
The following Python script implements a weather query tool call example based on the OpenAI SDK:
```python
from openai import OpenAI
import json
client = OpenAI(base_url="http://localhost:8000/v1", api_key="dummy")
def get_weather(location: str, unit: str):
return f"Getting the weather for {location} in {unit}..."
tool_functions = {"get_weather": get_weather}
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City and state, e.g., 'San Francisco, CA'"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location", "unit"]
}
}
}]
response = client.chat.completions.create(
model=client.models.list().data[0].id,
messages=[{"role": "user", "content": "What's the weather like in San Francisco? use celsius."}],
tools=tools,
tool_choice="auto"
)
print(response)
tool_call = response.choices[0].message.tool_calls[0].function
print(f"Function called: {tool_call.name}")
print(f"Arguments: {tool_call.arguments}")
print(f"Result: {get_weather(**json.loads(tool_call.arguments))}")
```
**Output Example:**
```
Function called: get_weather
Arguments: {"location": "San Francisco, CA", "unit": "celsius"}
Result: Getting the weather for San Francisco, CA in celsius...
```
## Manually Parsing Model Output
**We strongly recommend using vLLM or SGLang for parsing tool calls.** If you cannot use the built-in parser of inference engines (e.g., vLLM and SGLang) that support MiniMax-M2.5, or need to use other inference frameworks (such as transformers, TGI, etc.), you can manually parse the model's raw output using the following method. This approach requires you to parse the XML tag format of the model output yourself.
### Example Using Transformers
Here is a complete example using the transformers library:
```python
from transformers import AutoTokenizer
def get_default_tools():
return [
{
"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"],
"type": "object"
}
]
# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_id)
prompt = "What's the weather like in Shanghai today?"
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
# Enable function calling tools
tools = get_default_tools()
# Apply chat template and include tool definitions
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
tools=tools
)
# Send request (using any inference service)
import requests
payload = {
"model": "MiniMaxAI/MiniMax-M2.5",
"prompt": text,
"max_tokens": 4096
}
response = requests.post(
"http://localhost:8000/v1/completions",
headers={"Content-Type": "application/json"},
json=payload,
stream=False,
)
# Model output needs manual parsing
raw_output = response.json()["choices"][0]["text"]
print("Raw output:", raw_output)
# Use the parsing function below to process the output
tool_calls = parse_tool_calls(raw_output, tools)
```
## 🛠️ Tool Call Definition
### Tool Structure
Tool calls need to define the `tools` field in the request body. Each tool consists of the following parts:
```json
{
"tools": [
{
"name": "search_web",
"description": "Search function.",
"parameters": {
"properties": {
"query_list": {
"description": "Keywords for search, list should contain 1 element.",
"items": { "type": "string" },
"type": "array"
},
"query_tag": {
"description": "Category of query",
"items": { "type": "string" },
"type": "array"
}
},
"required": [ "query_list", "query_tag" ],
"type": "object"
}
}
]
}
```
**Field Descriptions:**
- `name`: Function name
- `description`: Function description
- `parameters`: Function parameter definition
- `properties`: Parameter property definition, where key is the parameter name and value contains detailed parameter description
- `required`: List of required parameters
- `type`: Parameter type (usually "object")
### Internal Processing Format
When processing within the MiniMax-M2.5 model, tool definitions are converted to a special format and concatenated to the input text. Here is a complete example:
```
]~!b[]~b]system
You are a helpful assistant.
# Tools
You may call one or more tools to assist with the user query.
Here are the tools available in JSONSchema format:
When making tool calls, use XML format to invoke tools and pass parameters: