Introduction to LLM Agents PHP SDK

What is LLM Agents PHP SDK?

The LLM Agents PHP SDK is a powerful library designed to help PHP developers create and manage Language Model (LLM)-based agents. These agents can perform complex tasks, interact with external tools, and respond intelligently to user input. The library provides an efficient framework to integrate AI capabilities into PHP applications, allowing for the development of autonomous, AI-driven systems.

Official documentation: https://github.com/llm-agents-php/agents

Introduction to LLM Agents PHP SDK

This SDK is designed to work with any LLM service or API, making it highly flexible and adaptable to various AI use cases.

Key Features

Key Features of LLM Agents PHP SDK:

  • Agent Creation: Developers can create and configure LLM-based agents with customizable behaviors, tailoring them to specific application needs.​

  • Tool Integration: The SDK allows seamless integration of various tools and APIs, enhancing the agent's capabilities within PHP applications.​

  • Memory Management: Agents can retain and recall information across interactions, facilitating more coherent and contextually relevant responses.

  • Prompt Management: Efficient handling of prompts and instructions guides agent behavior, ensuring responses align with user expectations.​

  • Extensible Architecture: The framework supports the addition of new agent types, tools, and capabilities, promoting scalability and adaptability.​

  • Multi-Agent Support: Developers can build systems with multiple interacting agents, enabling complex problem-solving scenarios.​

Installation

To install the SDK, use Composer:

composer require llm-agents/agents

Creating an Agent

Here’s a basic example of defining an agent:

use LLM\Agents\Agent\Agent;
use LLM\Agents\Agent\AgentAggregate;

class MyAgent extends AgentAggregate {
    public static function create(): self {
        $agent = new Agent(
            key: 'my_agent',
            name: 'My Custom Agent',
            description: 'An AI-powered agent that performs specific tasks.',
            instruction: 'Follow the provided guidelines strictly.'
        );

        return new self($agent);
    }
}

Implementing a Custom Tool

Agents can use tools to perform specific actions. Here’s an example of a site status checker tool:

use LLM\Agents\Tool\PhpTool;

class CheckSiteAvailabilityTool extends PhpTool {
    public const NAME = 'check_site_availability';

    public function execute(object $input): string {
        $ch = curl_init($input->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);

        $startTime = microtime(true);
        curl_exec($ch);
        $endTime = microtime(true);

        $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return json_encode([
            'status_code' => $statusCode,
            'response_time_ms' => round(($endTime - $startTime) * 1000, 2),
            'is_online' => $statusCode >= 200 && $statusCode < 400,
        ]);
    }
}

Executing an Agent

To run an agent, use the AgentExecutor:

use LLM\Agents\AgentExecutor\ExecutorInterface;

class AgentRunner {
    public function __construct(private ExecutorInterface $executor) {}

    public function run(string $input): string {
        return (string) $this->executor->execute('my_agent', $input)->result->content;
    }
}

Conclusion

The LLM Agents PHP SDK simplifies the process of integrating AI into PHP applications. With its flexible architecture, developers can build intelligent agents that interact with tools, retain memory, and process complex tasks efficiently. Whether you're automating workflows or enhancing user experiences, this SDK provides a robust foundation for AI-driven applications in PHP.

Last updated