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功能!

相关推荐
冬奇Lab8 分钟前
Agent 系列(21):Harness 测试工程——45 个测试怎么设计,以及它发现了什么 bug
人工智能·llm·agent
冬奇Lab17 分钟前
每日一个开源项目(第133篇):EchoBird - 把 AI 工具的安装和部署做成傻瓜操作
人工智能·开源·资讯
星星在线34 分钟前
MusicFree:一个「All in One」的个人音乐服务器,让听歌回归简单
前端·后端
IT_陈寒2 小时前
Redis的SETNX并发问题让我加了三天班
前端·人工智能·后端
demo007x2 小时前
Docling 文档转换以及技术架构分析
前端·后端·程序员
保持当下3 小时前
分享一些程序员很棘手但是却又简单的工具
程序员·免费·js·工具
用户5191495848453 小时前
Windows 渗透测试载荷加载器 POC 工具集
人工智能·aigc
袋鱼不重3 小时前
我的神奇同事,AI 用多了居然写了个 Open In Codex
前端·后端·ai编程
大树883 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
用户8356290780513 小时前
使用 Python 操作 Word 内容控件
后端·python