Creating an Agent
You can create a new agent manually by extending theLarAgent\Agent class. This is the parent class for building your custom AI agent with specific capabilities and behaviors.
App\AiAgents directory with all the necessary boilerplate code:
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
History
Driver
Provider
Model
Other properties
Tokens
Temperature
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
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 thefor() method to specify a chat history name and get an immediate response:
Using Chainable Methods
For more control over the interaction, you can use the chainable syntax: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.Return message
By default,respond method returns:
stringwith regular requestarrayofstringif$nis greater than 1.arrayAssociative array of defined structured output schema.
returnMessage method or setting $returnMessage property to true,
which will enforce respond method to return MessageInterface instance.
Image input
You can pass publicly accessible image URLs to the agent as an array of strings.Audio input
You can pass base64 encoded audio data to the agent as an array of arrays containing theformat 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:message() - Set the message
message() - Set the message
withImages() - Add images
withImages() - Add images
withAudios() - Add audios
withAudios() - Add audios
withModel() - Change model
withModel() - Change model
addMessage() - Add custom message
addMessage() - Add custom message
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() - Clear history
clear() - Clear history
setChatHistory() - Change history
setChatHistory() - Change history
withTool() - Add tool
withTool() - Add tool
removeTool() - Remove tool
removeTool() - Remove tool
temperature() - Adjust creativity
temperature() - Adjust creativity
Agent Accessors Reference
You can access the agent’s properties using these methods on an instance of the agent:getChatSessionId() - Get session ID
getChatSessionId() - Get session ID
getProviderName() - Get provider
getProviderName() - Get provider
getTools() - Get tools
getTools() - Get tools
chatHistory() - Get history
chatHistory() - Get history
currentMessage() - Get current message
currentMessage() - Get current message
lastMessage() - Get last message
lastMessage() - Get last message
getChatKeys() - Get all chat keys
getChatKeys() - Get all chat keys
getModalities() - Get modalities
getModalities() - Get modalities

