使用工具调用的推理
工具调用功能允许你将模型连接到外部工具和系统。这在许多场景下非常有用,例如为AI助手赋能增强能力,或在你的应用程序与模型之间构建深度集成。
在本教程中,您将学习如何在GPUStack中设置和使用工具调用功能,以扩展AI的能力。
先决条件
在继续之前,请确保以下事项:
- GPUStack 已安装并正在运行。
- 有一个带GPU的Linux工作节点可用。本教程将使用Qwen2.5-7B-Instruct作为模型。该模型需要至少18GB显存的GPU。
- 访问Hugging Face以下载模型文件。
步骤1:部署模型
从目录部署
支持工具调用的LLMs在目录中被标记为tools能力。当你从目录中选择这类模型时,工具调用功能默认启用。
使用llama-box进行自定义部署的示例
当你使用llama-box部署GGUF模型时,默认会为支持工具调用的模型启用该功能。
- 在GPUStack用户界面中导航至
Models页面,点击Deploy Model按钮。在下拉菜单中选择Hugging Face作为模型来源。 - 启用
GGUF复选框可按GGUF格式筛选模型。 - 使用搜索栏查找
Qwen/Qwen2.5-7B-Instruct-GGUF模型。 - 点击
Save按钮部署模型。
使用vLLM进行自定义部署的示例
使用vLLM部署模型时,需要通过额外参数启用工具调用功能。
- 在GPUStack用户界面中导航至
Models页面,点击Deploy Model按钮。在下拉菜单中选择Hugging Face作为模型来源。 - 使用搜索栏查找
Qwen/Qwen2.5-7B-Instruct模型。 - 在配置中展开
Advanced(高级)部分,向下滚动至Backend Parameters(后端参数)部分。 - 点击
Add Parameter按钮并添加以下参数:
--enable-auto-tool-choice--tool-call-parser=hermes
- 点击
Save按钮部署模型。
部署完成后,您可以在Models页面监控模型状态。
步骤2:生成API密钥
我们将使用GPUStack API与模型进行交互。为此,您需要生成一个API密钥:
- 在GPUStack用户界面中导航至
API Keys页面。 - 点击
New API Key按钮。 - 为API密钥输入一个名称,然后点击
Save按钮。 - 复制生成的API密钥以备后续使用。
步骤3:执行推理
模型部署完成并获得API密钥后,您可以通过GPUStack API调用该模型。以下是一个使用curl的示例脚本(请将替换为您的GPUStack服务器URL,并将替换为上一步生成的API密钥):
export GPUSTACK_SERVER_URL=<your-server-url>
export GPUSTACK_API_KEY=<your-api-key>
curl $GPUSTACK_SERVER_URL/v1-openai/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $GPUSTACK_API_KEY" \
-d '{
"model": "qwen2.5-7b-instruct",
"messages": [
{
"role": "user",
"content": "What'\''s the weather like in Boston today?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}
],
"tool_choice": "auto"
}'
示例响应:
{
"model": "qwen2.5-7b-instruct",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "chatcmpl-tool-b99d32848b324eaea4bac5a5830d00b8",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{\"location\": \"Boston, MA\", \"unit\": \"fahrenheit\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 212,
"total_tokens": 242,
"completion_tokens": 30
}
}

