数値計算用のツールを作成する
いよいよツールを作りましょう。小さな建設会社を経営していて、屋根の片側の長さを計算する必要があると想像してください。直角に交わる2本の梁の長さがわかっていれば、次の斜辺の公式を使って屋根の長さを計算できます。 $$c = \sqrt{a^2 + b^2}$$

この演習はコースの一部です
LangChainで設計するエージェント型システム
演習の手順
- 必要なデコレータを使って、この関数をツールとして定義します。
- 斜辺を求めるために、
input文字列に対して.split()を使い、直角三角形の残り2辺の長さを取り出します。 - 三角形の各辺
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)