Day 39 · Function Calling

Week 6 · Day 39 / 56 · 返回大纲

一、工具定义

tools = [{
    "name": "get_weather",
    "description": "查询城市天气",
    "input_schema": {
        "type": "object",
        "properties": {"city": {"type": "string"}},
        "required": ["city"]
    }
}]

resp = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024, tools=tools,
    messages=[{"role": "user", "content": "北京天气?"}]
)

if resp.stop_reason == "tool_use":
    print(f"调用工具:{resp.content[1].input}")

动手练习

定义工具
计算器工具
tools = [{name: "calculator"}]
查看答案
参考答案
tools = [{name: "calculator"}]
执行
执行工具调用
if stop_reason == "tool_use"
查看答案
参考答案
if stop_reason == "tool_use"

打卡验收

理解Function Calling核心概念
能看懂代码示例
能独立写代码
代码能跑通
完成练习
能向别人解释
Week 6 进度
继续学习
打卡方式: 全部勾选后,告诉我 "Day 39 打卡",并贴出你的代码。