Skip to content
Pavel Buchnev edited this page Sep 19, 2024 · 2 revisions

Feature Explanation

The core functionality implemented in this code is a flexible and extensible tool system for AI agents. The Tool class serves as an abstract base class for creating specific tools that can be used by AI agents to perform various tasks. Here are the key aspects of this feature:

  • Abstract Base Class: The Tool class extends ToolLink and implements the ToolInterface, providing a foundation for creating specific tools.
  • Input Schema: Each tool has an associated input schema, which defines the structure of the input data the tool expects.
  • Language Support: Tools can be implemented in different programming languages, with the ToolLanguage enum defining supported languages.
  • Execution: The execute method is abstract, allowing each specific tool implementation to define its own execution logic.

Use Cases (5 Examples)

a) Web Scraping Tool

class WebScraperTool extends Tool
{
    public function __construct(
        private HttpClientInterface $httpClient,
        string $name = 'web_scraper',
        string $description = 'Scrapes content from a given URL'
    ) {
        parent::__construct(name: $name, inputSchema: WebScraperInput::class, description: $description);
    }

    public function execute(object $input): string|\Stringable
    {
        $response = $this->httpClient->request('GET', $input->url);
        return $response->getContent();
    }

    public function getLanguage(): ToolLanguage
    {
        return ToolLanguage::PHP;
    }
}

// Usage
$webScraperTool = new WebScraperTool(httpClient: new HttpClient());
$result = $webScraperTool->execute(new WebScraperInput(url: 'https://example.com'));

b) Weather Information Tool

class WeatherTool extends Tool
{
    public function __construct(
        private WeatherApiClient $weatherApi,
        string $name = 'weather_info',
        string $description = 'Retrieves weather information for a given location'
    ) {
        parent::__construct(name: $name, inputSchema: WeatherInput::class, description: $description);
    }

    public function execute(object $input): string|\Stringable
    {
        return $this->weatherApi->getWeather(location: $input->location);
    }

    public function getLanguage(): ToolLanguage
    {
        return ToolLanguage::PHP;
    }
}

// Usage
$weatherTool = new WeatherTool(weatherApi: new WeatherApiClient());
$result = $weatherTool->execute(new WeatherInput(location: 'New York'));

c) Text Translation Tool

class TranslationTool extends Tool
{
    public function __construct(
        private TranslationService $translationService,
        string $name = 'translator',
        string $description = 'Translates text from one language to another'
    ) {
        parent::__construct(name: $name, inputSchema: TranslationInput::class, description: $description);
    }

    public function execute(object $input): string|\Stringable
    {
        return $this->translationService->translate(
            text: $input->text,
            fromLanguage: $input->fromLang,
            toLanguage: $input->toLang
        );
    }

    public function getLanguage(): ToolLanguage
    {
        return ToolLanguage::PHP;
    }
}

// Usage
$translationTool = new TranslationTool(translationService: new GoogleTranslateService());
$result = $translationTool->execute(new TranslationInput(
    text: 'Hello, world!',
    fromLang: 'en',
    toLang: 'es'
));

d) Image Generation Tool

class ImageGenerationTool extends Tool
{
    public function __construct(
        private ImageGenerationAPI $imageApi,
        string $name = 'image_generator',
        string $description = 'Generates images based on text prompts'
    ) {
        parent::__construct(name: $name, inputSchema: ImageGenerationInput::class, description: $description);
    }

    public function execute(object $input): string|\Stringable
    {
        $imageUrl = $this->imageApi->generateImage(prompt: $input->prompt, size: $input->size);
        return json_encode(['image_url' => $imageUrl]);
    }

    public function getLanguage(): ToolLanguage
    {
        return ToolLanguage::PHP;
    }
}

// Usage
$imageGenerationTool = new ImageGenerationTool(imageApi: new DallEAPI());
$result = $imageGenerationTool->execute(new ImageGenerationInput(
    prompt: 'A futuristic city skyline',
    size: '1024x1024'
));

e) Database Query Tool

class DatabaseQueryTool extends Tool
{
    public function __construct(
        private DatabaseConnection $db,
        string $name = 'db_query',
        string $description = 'Executes SQL queries on the database'
    ) {
        parent::__construct(name: $name, inputSchema: DatabaseQueryInput::class, description: $description);
    }

    public function execute(object $input): string|\Stringable
    {
        $result = $this->db->query(sql: $input->query, parameters: $input->parameters);
        return json_encode($result->fetchAll());
    }

    public function getLanguage(): ToolLanguage
    {
        return ToolLanguage::PHP;
    }
}

// Usage
$dbQueryTool = new DatabaseQueryTool(db: new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'));
$result = $dbQueryTool->execute(new DatabaseQueryInput(
    query: 'SELECT * FROM users WHERE age > :age',
    parameters: ['age' => 18]
));

Configuration Options

The Tool class itself doesn't have many configuration options, as it's designed to be extended. However, there are a few key options when creating a tool:

a) Name

  • Default: No default, must be provided
  • Configuration: Set in the constructor
$tool = new CustomTool(name: 'my_custom_tool', /* other parameters */);

b) Input Schema

  • Default: No default, must be provided
  • Configuration: Set in the constructor, should be a class name
$tool = new CustomTool(inputSchema: MyCustomInput::class, /* other parameters */);

c) Description

  • Default: Empty string
  • Configuration: Set in the constructor
$tool = new CustomTool(description: 'This tool does something amazing', /* other parameters */);

d) Language

  • Default: Must be implemented in the child class
  • Configuration: Override the getLanguage() method
public function getLanguage(): ToolLanguage
{
    return ToolLanguage::PHP;
}

Related Classes

a) ToolInterface

  • Purpose: Defines the contract for all tools
  • Interaction: The Tool class implements this interface, ensuring all tools have the required methods

b) ToolLink

  • Purpose: Represents a link to a tool that can be used to solve a problem
  • Interaction: The Tool class extends ToolLink, inheriting its properties and methods

c) ToolLanguage

  • Purpose: Enum defining supported programming languages for tools
  • Interaction: Used by the getLanguage() method to specify the tool's implementation language

d) Solution

  • Purpose: Base class for various components in the system
  • Interaction: ToolLink extends Solution, making Tool a part of the broader solution hierarchy

e) SolutionType

  • Purpose: Enum defining types of solutions
  • Interaction: Used by Solution to categorize different components, including tools

Mermaid Class Diagram

Here's a Mermaid class diagram illustrating the relationships between the main feature and related classes:

classDiagram
    class Solution {
        +name: string
        +type: SolutionType
        +description: string
        +addMetadata(SolutionMetadata)
        +getMetadata(): array
    }
    class ToolLink {
        +getName(): string
    }
    class Tool {
        #inputSchema: string
        +getName(): string
        +getDescription(): string
        +getInputSchema(): string
        +getLanguage(): ToolLanguage
        +execute(object): string|Stringable
    }
    class ToolInterface {
        <<interface>>
        +getName(): string
        +getDescription(): string
        +getInputSchema(): string
        +getLanguage(): ToolLanguage
    }
    class ToolLanguage {
        <<enumeration>>
        PHP
        Lua
        Python
        Ruby
        JavaScript
        TypeScript
        Shell
    }
    Solution <|-- ToolLink
    ToolLink <|-- Tool
    Tool ..|> ToolInterface
    Tool ..> ToolLanguage
Loading

This diagram shows the inheritance hierarchy and relationships between the Tool class and its related classes, illustrating how they fit together in the overall system architecture.