回顾:天气工作台
本课属于 Coddy AI Engineering 学习旅程的“工具与 Agent”单元。第 5 课,共 22 课。
挑战
中等构建一个使用两个工具进行回答的天气服务台。起始代码定义了 get_weather 和 get_air_quality,并读取了问题。
- 定义两个工具,每个工具都有一个必填的字符串属性
city:get_weather的描述为Get the current weather for a city,get_air_quality的描述为Get the air quality index for a city。 - 将问题与这两个工具一起发送,使用
model="claude-opus-5-5"和max_tokens=300。 - 如果停止原因为
"tool_use":打印Tool:、工具名称、空格及其输入;运行对应的函数;发送 assistant 回复和tool_result;并再次调用模型。否则打印Tool: none。 - 打印
Answer:以及最后一次回复文本块中的文本。
例如,对于输入 What is the weather in Lisbon?,程序会打印:
Tool: get_weather {'city': 'Lisbon'}
Answer: Here is what I found: Lisbon: 19 C and sunny亲手试试
import coddy_ai as anthropic # 在生产环境中:import anthropic
client = anthropic.Anthropic()
WEATHER = {"Lisbon": "19 C and sunny", "Delhi": "31 C and hazy", "Montreal": "-3 C and snowing"}
AIR = {"Lisbon": "index 22, good", "Delhi": "index 168, unhealthy", "Montreal": "index 35, good"}
def get_weather(city):
return f"{city}: {WEATHER.get(city, 'no weather data')}"
def get_air_quality(city):
return f"{city}: {AIR.get(city, 'no air quality data')}"
question = input()
# 在下方编写你的代码
工具与 Agent中的所有课
自己练习: Python 在线编译器