Skip to main content
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.
For rapid development, you can use the artisan command to generate a new agent with a basic structure:
This will create a new agent class in the App\AiAgents directory with all the necessary boilerplate code:
YourAgentName.php

Configuring an Agent

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

Core methods

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

Properties

Instructions

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

History

Choose from built-in chat history implementations: “in_memory”, “session”, “cache”, “file”, or “json”.

Driver

The driver class that handles communication with the AI provider.

Provider

References a provider configuration from your config file.

Model

The specific model to use from your chosen provider.

Other properties

All config properties have relevant setter (chainable methods) you can use to set them at runtime. For example, $maxCompletionTokens -> maxCompletionTokens(int $tokens), $topP -> topP(float $topP) and etc.
All config properties can be defined by config file in provider settings. For example, $maxCompletionTokens -> max_completion_tokens => 2000 and etc.
Tokens
Limits the length of the AI’s response.
Temperature
Controls randomness: 0.0 for focused responses, 2.0 for creative ones.
Message
n
How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.
In case of $n > 1, the agent will return an array of responses.
top_p
An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering $topP or $temperature but not both.
frequency_penalty
Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
presence_penalty
Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.

Arbitrary Configuration

You can set custom configuration values on agents that will be passed through to the driver. This is useful for provider-specific settings or experimental features that aren’t part of the standard agent configuration.

Setting Configuration

Usage Example

Custom configurations are passed directly to the driver, allowing you to leverage provider-specific features without modifying the agent’s core properties.

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:
If you don’t need to save / retrive chat history, because you are using “in_memory” type or for any other reason, you can skip ‘for’ method:

Using Chainable Methods

For more control over the interaction, you can use the chainable syntax:
If you don’t need to maintain conversation histories, you can just replace for with make method:

Ready made user message

Instead of passing a string as a message, you can build your own UserMessage instance. It allows you to add metadata to the message, such as the user ID or request ID. Also, using different methods associated with UserMessage instance.
If you use UserMessage instance instead string, it will bypass the prompt method.

Return message

By default, respond method returns:
  • string with regular request
  • array of string if $n is greater than 1.
  • array Associative array of defined structured output schema.
You can change the return type by using returnMessage method or setting $returnMessage property to true, which will enforce respond method to return MessageInterface instance.
If returnMessage is true and structured output is defined, beforeStructuredOutput hook will not happen, because structured output is not processed to array.

Image input

You can pass publicly accessible image URLs to the agent as an array of strings.
Alternative way to pass images is to use base64 encoded data.
Base64 image input Contributed by havspect in issue #74.

Audio input

You can pass base64 encoded audio data to the agent as an array of arrays containing the format and data. Supported formats by OpenAI: “wav”, “mp3”, “ogg”, “flac”, “m4a”, “webm”

Agent Mutators Reference

Here are some chainable methods to modify the agent’s behavior on the fly:
Sets the message that will be sent to the AI model.
Adds images to the message for multimodal models.
Adds audios to the message, use for multimodal models.
Overrides the default model for this specific call.
Adds a custom message to the chat history. MessageInterface: Easily created with Message class: Message::system('Your message here') Use with caution, as it is added directly to the chat history and keep in mind that history needs to keep certain structure defined by openai.
Clear the chat history. This removes all messages from the chat history
Replaces the current chat history with a different instance.
Adds a tool for this specific call.
Removes a tool for this specific call.
Controls the randomness of the response (0.0 for focused, 2.0 for creative).

Agent Accessors Reference

You can access the agent’s properties using these methods on an instance of the agent:
Returns the unique identifier for the current chat session.
Returns the name of the AI provider being used.
Returns all tools registered with the agent.
Returns the current chat history instance.
Returns the message currently being processed.
Returns the last message in the chat history.
Returns all chat keys associated with this agent class.
public
Returns all audio associated with this agent class.

Example Configuration