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"
}
]
# 加载模型和分词器
model_id = "MiniMaxAI/MiniMax-M1-40k"
tokenizer = AutoTokenizer.from_pretrained(model_id)
prompt = "What's the weather like in Shanghai today?"
messages = [
{"role": "system", "content": [{"type": "text", "text": "You are a helpful assistant created by MiniMax based on MiniMax-M1 model."}]},
{"role": "user", "content": [{"type": "text", "text": prompt}]},
]
with open("/vllm-workspace/examples/tool_chat_template_minimax_m1.jinja", "r") as f:
tokenizer.chat_template = f.read()
# 启用函数调用工具
tools = get_default_tools()
# 应用聊天模板,并加入工具定义
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
tools=tools
)
# 发送请求(这里使用任何推理服务)
import requests
payload = {
"model": model_id,
"prompt": text,
"max_tokens": 4000
}
response = requests.post(
"http://localhost:8000/v1/completions",
headers={"Content-Type": "application/json"},
json=payload,
stream=False,
)
# 模型输出需要手动解析
raw_json = response.json()
raw_output = raw_json["choices"][0]["text"]
print("原始输出:", raw_output)
# 使用下面的解析函数处理输出
function_calls = parse_function_calls(raw_output)