Agents are the foundation of LarAgent. They define how your AI assistant behaves, what tools it can use, and how it processes information.

Creating an Agent

You can create a new agent manually by extending the LarAgent\Agent class. This is the parent class for building your custom AI agent with specific capabilities and behaviors.

Using make:agent command is a recommended way of creating agents.

namespace App\AiAgents;

use LarAgent\Agent;

class MyAgent extends Agent
{
    // Your agent implementation
}

For rapid development, you can use the artisan command to generate a new agent with a basic structure:

php artisan make:agent MyAgent

This will create a new agent class in the App\AiAgents directory with all the necessary boilerplate code:

YourAgentName.php
<?php

namespace App\AiAgents;

use LarAgent\Agent;

class YourAgentName extends Agent
{
    protected $model = 'gpt-4o-mini';

    protected $history = 'in_memory';

    protected $provider = 'default';

    protected $tools = [];

    public function instructions()
    {
        return "Define your agent's instructions here.";
    }

    public function prompt($message)
    {
        return $message;
    }
}

Configuring an Agent

Agents can be configured through various properties and methods to customize their behavior. Here are the core configuration options:

/** @var string - Define the agent's behavior and role */
protected $instructions;

This property sets the system instructions for your agent, defining its behavior, personality, and capabilities.

// Example
protected $instructions = "You are a helpful assistant specialized in weather forecasting.";

Core methods

The agent also provides three core methods that you can override to customize its behavior:

/**
 * Define the agent's system instructions
 * This sets the behavior, role, and capabilities of your agent
 * For simple textual instructions, use the `instructions` property
 * For more complex instructions or dynamic behavior, use the `instructions` method
 */
public function instructions()
{
    return "Define your agent's instructions here.";
}

Using an Agent

There are two ways to interact with your agent: direct response or chainable methods.

Direct Response

The simplest way is to use the for() method to specify a chat history name and get an immediate response:

// Using a specific chat history name
echo WeatherAgent::for('test_chat')->respond('What is the weather like?');

Chainable Methods

For more control over the interaction, you can use the chainable syntax:

$response = WeatherAgent::for('test_chat')
    ->message('What is the weather like?')  // Set the message
    ->temperature(0.7)                      // Optional: Override temperature
    ->respond();                            // Get the response

Chainable Methods Reference

Here are some chainable methods to modify the agent’s behavior on the fly:

Agent Accessors

You can access the agent’s properties using these methods on an instance of the agent:

Example Configuration

class WeatherAgent extends Agent
{
    protected $model = 'gpt-4o';
    protected $history = 'session';
    protected $temperature = 0.7;
    
    public function instructions()
    {
        return "You are a weather expert assistant. Provide accurate weather information.";
    }
    
    public function prompt(string $message)
    {
        return "Weather query: " . $message;
    }
}