Knox工具调用功能测试

完整的curl命令来测试Knox项目的tool calling功能,包括流式和非流式响应。

📋 前置条件

  1. Knox: https://knox.chat
  2. 使用测试API密钥:KNOXCHAT_API_KEY
  3. 可选:安装 jq 用于格式化JSON输出 (brew install jq)

🔧 测试命令

1. 基础非流式Tool Call测试

bash 复制代码
curl -X POST https://knox.chat/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer KNOXCHAT_API_KEY" \
  -d '{
    "model": "anthropic/claude-3.5-haiku",
    "messages": [
      {
        "role": "system",
        "content": "你是一个有用的助手。当用户询问时间时,请使用get_current_time工具来获取准确的时间信息。"
      },
      {
        "role": "user",
        "content": "现在几点了?请告诉我当前时间。"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_current_time",
          "description": "获取当前时间",
          "parameters": {
            "type": "object",
            "properties": {
              "timezone": {
                "type": "string",
                "description": "时区,例如:Asia/Shanghai",
                "default": "UTC"
              }
            },
            "required": []
          }
        }
      }
    ],
    "tool_choice": "auto",
    "temperature": 0.7,
    "max_tokens": 1000
  }'

期望结果:

  • HTTP 200 状态码
  • choices[0].message.tool_calls 数组存在
  • finish_reason"tool_calls"
  • tool_call包含 idtypefunction 字段

2. 流式Tool Call测试

bash 复制代码
curl -X POST https://knox.chat/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer KNOXCHAT_API_KEY" \
  -N \
  -d '{
    "model": "anthropic/claude-3.5-haiku",
    "messages": [
      {
        "role": "system",
        "content": "你是一个有用的助手。当用户询问时间时,请使用get_current_time工具来获取准确的时间信息。"
      },
      {
        "role": "user",
        "content": "现在几点了?请告诉我当前时间。"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_current_time",
          "description": "获取当前时间",
          "parameters": {
            "type": "object",
            "properties": {
              "timezone": {
                "type": "string",
                "description": "时区,例如:Asia/Shanghai",
                "default": "UTC"
              }
            },
            "required": []
          }
        }
      }
    ],
    "tool_choice": "auto",
    "temperature": 0.7,
    "max_tokens": 1000,
    "stream": true
  }'

期望结果:

  • 服务器发送事件流 (SSE)
  • 包含 data: 前缀的消息
  • 某些chunk中包含 choices[0].delta.tool_calls
  • 最后以 data: [DONE] 结束

3. 多工具测试

bash 复制代码
curl -X POST https://knox.chat/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer KNOXCHAT_API_KEY" \
  -d '{
    "model": "anthropic/claude-3.5-haiku",
    "messages": [
      {
        "role": "system",
        "content": "你是一个助手,可以获取时间和天气信息。"
      },
      {
        "role": "user",
        "content": "请告诉我现在北京的时间和天气情况。"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_current_time",
          "description": "获取当前时间",
          "parameters": {
            "type": "object",
            "properties": {
              "timezone": {
                "type": "string",
                "description": "时区,例如:Asia/Shanghai"
              }
            },
            "required": ["timezone"]
          }
        }
      },
      {
        "type": "function",
        "function": {
          "name": "get_weather",
          "description": "获取天气信息",
          "parameters": {
            "type": "object",
            "properties": {
              "city": {
                "type": "string",
                "description": "城市名称"
              },
              "unit": {
                "type": "string",
                "description": "温度单位",
                "enum": ["celsius", "fahrenheit"],
                "default": "celsius"
              }
            },
            "required": ["city"]
          }
        }
      }
    ],
    "tool_choice": "auto",
    "temperature": 0.7,
    "max_tokens": 1000
  }'

4. 强制工具调用测试

bash 复制代码
curl -X POST https://knox.chat/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer KNOXCHAT_API_KEY" \
  -d '{
    "model": "anthropic/claude-3.5-haiku",
    "messages": [
      {
        "role": "user",
        "content": "你好"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_current_time",
          "description": "获取当前时间",
          "parameters": {
            "type": "object",
            "properties": {
              "timezone": {
                "type": "string",
                "description": "时区",
                "default": "UTC"
              }
            }
          }
        }
      }
    ],
    "tool_choice": {
      "type": "function",
      "function": {
        "name": "get_current_time"
      }
    },
    "temperature": 0.7,
    "max_tokens": 1000
  }'

5. 与OpenRouter对比测试

bash 复制代码
# OpenRouter API对比测试
curl -X POST https://openrouter.ai/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer OPENROUTER_API_KEY" \
  -d '{
    "model": "anthropic/claude-3.5-haiku",
    "messages": [
      {
        "role": "system",
        "content": "你是一个有用的助手。当用户询问时间时,请使用get_current_time工具来获取准确的时间信息。"
      },
      {
        "role": "user",
        "content": "现在几点了?请告诉我当前时间。"
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_current_time",
          "description": "获取当前时间",
          "parameters": {
            "type": "object",
            "properties": {
              "timezone": {
                "type": "string",
                "description": "时区,例如:Asia/Shanghai",
                "default": "UTC"
              }
            },
            "required": []
          }
        }
      }
    ],
    "tool_choice": "auto",
    "temperature": 0.7,
    "max_tokens": 1000
  }'

📊 响应格式验证

✅ 成功的Tool Call响应应包含:

json 复制代码
{
  "choices": [
    {
      "finish_reason": "tool_calls",
      "index": 0,
      "message": {
        "content": "我将使用get_current_time工具来获取当前时间...",
        "role": "assistant",
        "tool_calls": [
          {
            "id": "toolu_01SR862k3e4m1rZYzrMwEX35",
            "type": "function",
            "function": {
              "name": "get_current_time",
              "arguments": "{\"timezone\": \"Asia/Shanghai\"}"
            }
          }
        ]
      }
    }
  ],
  "created": 1757746953,
  "id": "gen-1757746953-wTky1fn6VRFseHnLsxsv",
  "model": "anthropic/claude-3.5-haiku",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 101,
    "prompt_tokens": 448,
    "total_tokens": 549
  }
}

🌊 流式响应中的Tool Call片段:

kotlin 复制代码
data: {"id":"gen-xxx","choices":[{"index":0,"delta":{"tool_calls":[{"id":"toolu_xxx","type":"function","function":{"name":"get_current_time","arguments":""}}]},"finish_reason":null}]}

data: {"id":"gen-xxx","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\"timezone\": \"Asia/Shanghai\"}"}}]},"finish_reason":null}]}

🐛 故障排除

常见问题:

  1. 缺少tool_calls字段

    • 检查Knox服务器是否使用了修复后的代码
    • 验证模型是否支持tool calling
  2. 流式响应中断

    • 确保使用 -N 参数(不缓冲输出)
    • 检查网络连接稳定性
  3. 认证失败

    • 验证API密钥是否正确
    • 检查Authorization header格式
  4. 模型不支持

    • 确保使用支持tool calling的模型
    • anthropic/claude-3.5-haiku

📝 测试检查清单

  • 非流式tool call正常工作
  • 流式tool call正常工作
  • 多工具选择正常工作
  • 强制工具调用正常工作
  • 响应格式与OpenRouter一致
  • finish_reason正确设置为"tool_calls"
  • tool_calls数组包含完整信息

🎯 修复验证

通过以上测试,验证了Knox项目的以下修复:

  1. 非流式响应:正确包含tool_calls字段
  2. 流式响应:正确处理tool_calls增量更新
  3. 兼容性:与OpenRouter API完全兼容
  4. 多种场景:支持auto、强制、多工具等各种tool_choice模式

Knox目前完全支持OpenAI兼容的Tool Calling功能!

相关推荐
LucianaiB12 分钟前
【征文计划】用 Rokid AR 眼镜做“厨房小助手”:让做饭不再手忙脚乱
后端
通往曙光的路上24 分钟前
国庆回来的css
人工智能·python·tensorflow
数据知道26 分钟前
Go基础:Go语言ORM框架GORM详解
开发语言·jvm·后端·golang·go语言
算家计算1 小时前
国产大模型问鼎全球:混元图像3.0登顶文生图榜单的启示
人工智能·开源·资讯
程序员鱼皮1 小时前
Claude 封杀中国后,我终于找到了平替!
计算机·ai·程序员·大模型·互联网
Rock_yzh2 小时前
AI学习日记——神经网络参数的更新
人工智能·python·深度学习·神经网络·学习
Pr Young2 小时前
go build命令
后端·golang
wa的一声哭了2 小时前
Stanford CS336 assignment1 | Transformer Language Model Architecture
人工智能·pytorch·python·深度学习·神经网络·语言模型·transformer