Tools Management

Overview

The framework's tools system is designed for simplicity and extensibility. Each tool is defined by a JSON configuration file and a corresponding Python implementation. The configuration file specifies details such as the tool's name, description, function path, and expected parameters. The core agent dynamically loads these tool configurations at startup, making them available during query processing.

Key Components

ToolManager (Module: tool.py)

The ToolManager class provides several static methods for managing tools:

  • create_tool_config
    Creates a tool configuration file in the tools/ directory. It defines the tool's metadata, the function path to its implementation, and a JSON schema for its parameters.
  • register_basic_tools
    Registers a set of pre-defined tools (e.g., a calculator) by creating the required configuration files and directory structure.
  • setup_basic_config
    Sets up the initial agent configuration file (e.g., agent_config.json) and registers basic tools, providing a ready-to-use environment for the agent.

Creating a New Tool

1. Define the Tool Configuration

Use the ToolManager.create_tool_config method to define a new tool. For example, here's how you might create a currency converter tool:

from tool import ToolManager

ToolManager.create_tool_config(
    tool_name="currency_converter",
    description="Converts an amount from one currency to another.",
    function_path="tools.currency_converter.convert",  # The module and function to call
    parameters={
        "amount": {
            "type": "number",
            "description": "The amount of money to convert."
        },
        "from_currency": {
            "type": "string",
            "description": "The currency code to convert from (e.g., 'USD')."
        },
        "to_currency": {
            "type": "string",
            "description": "The currency code to convert to (e.g., 'EUR')."
        }
    },
    required_params=["amount", "from_currency", "to_currency"]
)

This code creates a JSON configuration file (e.g., tools/currency_converter.json) that describes the tool.

2. Implement the Tool Function

Create a corresponding Python module for the tool's functionality. For the currency converter example, you might implement it as follows:

# File: tools/currency_converter/convert.py

def convert(amount, from_currency, to_currency):
    # Dummy conversion logic for demonstration purposes
    conversion_rate = 0.85  # Assume a fixed conversion rate
    result = amount * conversion_rate
    return f"{amount} {from_currency} is approximately {result:.2f} {to_currency}"

Ensure the module and function name match the function_path specified in the tool configuration.

3. Register the Tool

Once the configuration and implementation are in place, add the tool's name to your agent configuration file (e.g., agent_config.json) under the "tools" list:

{
  "agent_name": "SmartAssistant",
  "config": {
    "backstory": "I help users with various tasks.",
    "task": "assist with tasks using integrated tools",
    "tools": ["currency_converter", "calculator"],
    "memory": true
  }
}

When the agent starts, it loads tool configurations from the tools/ directory, making the new tool available for use.

Tool Execution

During query processing, if the LLM suggests a tool call (formatted in JSON), the agent will:

  • Extract the tool name and parameters.
  • Dynamically import the module specified in the tool configuration.
  • Execute the tool function with the provided parameters.
  • Integrate the tool's result into the conversation context.

The _execute_tool method in the Agent class handles these steps, ensuring a smooth workflow from the LLM's suggestion to the tool's execution.

Example of Tool Usage in an Agent

Below is an example that demonstrates how the agent uses the tool system to process a query requiring tool execution:

from agent import create_agent, run_agent

# Create an agent using the configuration file that includes tool definitions
agent = create_agent("agent_config.json", llm_provider="openai")

# Run the agent with a query that triggers a tool call (e.g., a calculation or conversion)
response = run_agent(agent, "Convert 100 USD to EUR")
print("Agent Response:", response)

If the LLM determines that the query should be handled by the currency converter tool, the agent will call the convert function from tools/currency_converter/convert.py and incorporate the result into its final response.

Summary

  • Configuration Driven:
    Tools are defined via JSON configuration files, making it easy to add, remove, or update tools without modifying core code.
  • Dynamic Execution:
    The agent dynamically loads and executes tool functions based on the configuration, allowing flexible extension of capabilities.
  • Easy Integration:
    New tools can be seamlessly integrated by following the steps of defining a configuration, implementing the functionality, and registering the tool in your agent configuration.

This modular approach empowers developers to expand the framework's functionality and tailor the agent to a wide variety of use cases.