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

相关推荐
用户3521802454752 小时前
🕸️ GraphRAG 图数据质量评估:让你的知识图谱不再“翻车”!
人工智能·python·ai编程
知其然亦知其所以然2 小时前
SpringAI 玩转 OCI GenAI:这次我们聊聊 Cohere 聊天模型
java·后端·spring
种子q_q2 小时前
Redis的三种典型的 “缓存失效” 问题
后端·面试
金銀銅鐵2 小时前
[Java] 观察 CompactStrings 选项的影响
java·后端
程序猿二饭2 小时前
Spring Boot 项目启动报错:MongoSocketOpenException 连接被拒绝排查日记
后端
UP2 小时前
【C++基础】内存管理——malloc/free和new/delete之间的盘根错节
后端
齐穗穗2 小时前
springboot集成websocket
spring boot·后端·websocket
玉衡子2 小时前
四、索引优化实战
java·后端
大千AI助手2 小时前
残差:从统计学到深度学习的核心概念
人工智能·深度学习·resnet·统计学·方差分析·残差·残差分析