Skip to content

Functions as Tools

In Railtracks, you can turn any Python function into a tool that agents can call, no special boilerplate needed. The key is to provide a Google-style docstring which acts as the tool's description and schema.

Function Nodes

rt.function_node is a convenience function that wraps a function into a Railtrack node. Read more about this DynamicFunctionNode.

Creating a Function Tool

1. Using an RT Function

Let's start with a simple function that takes two arguments and returns their sum:

def add(a: int, b: int) -> int:
    """
    Adds two numbers together.
    Args:
        a (int): The first number.
        b (int): The second number.

    Returns:
        int: The sum of the two numbers.
    """
    return a + b

To turn this function into a tool, we need to provide a docstring that describes the function's parameters. Then we can pass the function to rt.function_node to create a tool:

AddNode = rt.function_node(add)

2. Using a decorator

Let's make another tool that we can use in our agent, this time using the @rt.function_node decorator:

@rt.function_node
def solve_expression(equation: str, solving_for: str):
    """
    Solves the given equation (assumed to be equal to 0) for the specified variable.

    Args:
        equation (str): The equation to solve, written in valid Python syntax.
        solving_for (str): The variable to solve for.
    """
    # Convert the string into a symbolic expression
    eq = sympify(equation, evaluate=True)

    # Solve the equation for the given variable
    return solve(eq, solving_for)

Using the tools

Now that we have our tool, we can use it in our agent:

MathAgent = rt.agent_node(
                name="MathAgent",
                tool_nodes=[
                  solve_expression, 
                  AddNode,
                ],    # the agent has access to these tools
                llm = rt.llm.OpenAILLM("gpt-4o"),
            )

# run the agent
result = asyncio.run(rt.call(MathAgent, "What is 3 + 4?"))

Want to go further with tools in Railtracks?