Create a tool for math calculations
It's time to build your tool. Let's imagine you run a small construction company and need to calculate the length of one side of a roof. If you know the lengths of two beams that support the roof at a right angle, you can use their lengths to calculate the length of the roof using the hypotenuse formula below. $$c = \sqrt{a^2 + b^2}$$

Latihan ini adalah bagian dari kursus
Designing Agentic Systems with LangChain
Petunjuk latihan
- Use the necessary decorator to define the function as a tool.
- To find the hypotenuse, use the
.split()method on theinputstring to extract the two other lengths of a right-sided triangle. - Convert each triangle side,
aandb, to floats and use.strip()to remove any extra spaces from the values. - Use Python's
mathmodule to square the lengthsaandb, sum their values, and find their square root to reveal the length of the roof.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
# 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)