完整的curl命令来测试Knox项目的tool calling功能,包括流式和非流式响应。
📋 前置条件
- Knox:
https://knox.chat
- 使用测试API密钥:
KNOXCHAT_API_KEY
- 可选:安装
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包含
id
、type
、function
字段
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}]}
🐛 故障排除
常见问题:
-
缺少tool_calls字段
- 检查Knox服务器是否使用了修复后的代码
- 验证模型是否支持tool calling
-
流式响应中断
- 确保使用
-N
参数(不缓冲输出) - 检查网络连接稳定性
- 确保使用
-
认证失败
- 验证API密钥是否正确
- 检查Authorization header格式
-
模型不支持
- 确保使用支持tool calling的模型
- 如
anthropic/claude-3.5-haiku
📝 测试检查清单
- 非流式tool call正常工作
- 流式tool call正常工作
- 多工具选择正常工作
- 强制工具调用正常工作
- 响应格式与OpenRouter一致
- finish_reason正确设置为"tool_calls"
- tool_calls数组包含完整信息
🎯 修复验证
通过以上测试,验证了Knox项目的以下修复:
- ✅ 非流式响应:正确包含tool_calls字段
- ✅ 流式响应:正确处理tool_calls增量更新
- ✅ 兼容性:与OpenRouter API完全兼容
- ✅ 多种场景:支持auto、强制、多工具等各种tool_choice模式
Knox目前完全支持OpenAI兼容的Tool Calling功能!