Tool Use(工具调用)
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")
tools = [
{
"name": "get_weather",
"description": "获取指定城市的天气",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
]
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "广州今天天气怎么样?"}]
)
for block in response.content:
if block.type == "tool_use":
print(f"调用工具: {block.name}, 参数: {block.input}")
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
const tools = [
{
name: "get_weather",
description: "获取指定城市的天气",
input_schema: {
type: "object",
properties: {
city: { type: "string", description: "城市名称" },
},
required: ["city"],
},
},
];
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
tools,
messages: [{ role: "user", content: "广州今天天气怎么样?" }],
});
for (const block of response.content) {
if (block.type === "tool_use") {
console.log(`调用工具: ${block.name}, 参数:`, block.input);
}
}
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"tools": [{
"name": "get_weather",
"description": "获取指定城市的天气",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string", "description": "城市名称"}},
"required": ["city"]
}
}],
"messages": [{"role": "user", "content": "广州今天天气怎么样?"}]
}'
完整工具调用循环
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://www.cheapertoken.work")
def get_weather(city: str) -> str:
return f"{city}今天晴天,25°C"
tools = [
{
"name": "get_weather",
"description": "获取指定城市的天气",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
]
messages = [{"role": "user", "content": "北京天气怎么样?"}]
while True:
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=tools,
messages=messages
)
if response.stop_reason == "end_turn":
print(response.content[0].text)
break
tool_results = []
for block in response.content:
if block.type == "tool_use":
result = get_weather(**block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result
})
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": tool_results})
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://www.cheapertoken.work" });
function getWeather(city) {
return `${city}今天晴天,25°C`;
}
const tools = [
{
name: "get_weather",
description: "获取指定城市的天气",
input_schema: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
];
const messages = [{ role: "user", content: "北京天气怎么样?" }];
while (true) {
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
tools,
messages,
});
if (response.stop_reason === "end_turn") {
console.log(response.content[0].text);
break;
}
const toolResults = [];
for (const block of response.content) {
if (block.type === "tool_use") {
const result = getWeather(block.input.city);
toolResults.push({ type: "tool_result", tool_use_id: block.id, content: result });
}
}
messages.push({ role: "assistant", content: response.content });
messages.push({ role: "user", content: toolResults });
}
# 第一步:发送请求,模型返回 tool_use
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"tools": [{"name": "get_weather", "description": "获取指定城市的天气", "input_schema": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}],
"messages": [{"role": "user", "content": "北京天气怎么样?"}]
}'
# 第二步:携带 tool_result 继续对话
curl https://www.cheapertoken.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"tools": [{"name": "get_weather", "description": "获取指定城市的天气", "input_schema": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}],
"messages": [
{"role": "user", "content": "北京天气怎么样?"},
{"role": "assistant", "content": [{"type": "tool_use", "id": "tool_123", "name": "get_weather", "input": {"city": "北京"}}]},
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "tool_123", "content": "北京今天晴天,25°C"}]}
]
}'