创建一个用于数学计算的工具
现在开始构建您的工具。假设您经营一家小型施工公司,需要计算屋顶一条边的长度。如果您已知在直角处支撑屋顶的两根梁的长度,就可以使用下方的斜边公式来计算屋顶的长度。 $$c = \sqrt{a^2 + b^2}$$

本练习是课程的一部分
使用 LangChain 设计 Agentic 系统
练习说明
- 使用所需的装饰器将该函数定义为一个工具。
- 要求斜边长度,请对字符串
input使用.split()方法,以提取直角三角形的另外两条边长。 - 将三角形的每条边
a和b转换为浮点数,并用.strip()去除值中的多余空格。 - 使用 Python 的
math模块对a和b的长度进行平方、求和,并取平方根,从而得到屋顶的长度。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define this math function as a tool
____
def hypotenuse_length(input: str) -> float:
"""Calculates the length of the hypotenuse of a right-angled triangle given the lengths of the other two sides."""
# Split the input string to get the lengths of the triangle
sides = ____.____(',')
# Convert the input values to floats, removing extra spaces
a = ____(____[0].____())
b = ____(____[1].____())
# Square each of the values, add them together, and find the square root
return ____.sqrt(a____2 + b____2)