Get startedGet started for free

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}$$

Roof beam calculation diagram

This exercise is part of the course

Designing Agentic Systems with LangChain

View Course

Exercise instructions

  • Use the necessary decorator to define the function as a tool.
  • To find the hypotenuse, use the .split() method on the input string to extract the two other lengths of a right-sided triangle.
  • Convert each triangle side, a and b, to floats and use .strip() to remove any extra spaces from the values.
  • Use Python's math module to square the lengths a and b, sum their values, and find their square root to reveal the length of the roof.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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)
Edit and Run Code