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.
Copy
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:
Copy
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
Copy
<?phpnamespace 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; }}
The agent also provides several core methods that you can override to customize its behavior:
Copy
/** * 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.";}
For more control over the interaction, you can use the chainable syntax:
Copy
$response = WeatherAgent::for('test_chat') ->message('What is the weather like?') // Set the message ->temperature(0.7) // Optional: Override temperature ->respond(); // Get the response
Here are some chainable methods to modify the agent’s behavior on the fly:
Copy
/** * Set the message for the agent to process */public function message(string $message);
Sets the message that will be sent to the AI model.
Copy
// Example$agent = WeatherAgent::for('test_chat');$agent->message('What is the weather like today?')->respond();
Copy
/** * Add images to the agent's input (message) * @param array $imageUrls Array of image URLs */public function withImages(array $imageUrls);
Adds images to the message for multimodal models.
Copy
// Example$agent->message('What's in this image?') ->withImages(['https://example.com/image.jpg']) ->respond();
Copy
/** * Decide model dynamically in your controller * @param string $model Model identifier (e.g., 'gpt-4o', 'gpt-3.5-turbo') */public function withModel(string $model);
Overrides the default model for this specific call.
You can access the agent’s properties using these methods on an instance of the agent:
Copy
/** * Get the current chat session ID * String like "[AGENT_NAME]_[MODEL_NAME]_[CHAT_NAME]" * CHAT_NAME is defined by "for" method * Example: WeatherAgent_gtp-4o-mini_test-chat */public function getChatSessionId(): string;
Returns the unique identifier for the current chat session.
/** * Get all chat keys associated with this agent class */public function getChatKeys(): array;
Returns all chat keys associated with this agent class.
Copy
// Example$chatKeys = $agent->getChatKeys();// Returns: ["user_1_chat", "user_2_chat", ...]// You can use this to list all active conversationsforeach ($chatKeys as $key) { echo "Active chat: " . $key . "\n";}
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; }}