Learn how to use LarAgent in non-Laravel PHP projects by leveraging the standalone engine.
Important Notice: LarAgent will soon drop support for direct usage outside of Laravel. However, the core engine will be released as a separate package with the same API (methods/functions), ensuring continued support for non-Laravel projects.
LarAgent’s core functionality is powered by the LarAgent\LarAgent class, often referred to as the “LarAgent engine.” This engine is a standalone component that contains all the abstractions and doesn’t depend on Laravel. It manages agents, tools, chat histories, structured output, and other core features.
To use LarAgent outside of Laravel, you’ll need to handle some configuration and initialization manually:
Copy
<?phprequire_once __DIR__.'/vendor/autoload.php';use LarAgent\Drivers\OpenAi\OpenAiDriver;use LarAgent\History\InMemoryChatHistory;use LarAgent\LarAgent;use LarAgent\Message;// Setup$yourApiKey = 'your-openai-api-key'; // Replace with your actual API key$driver = new OpenAiDriver(['api_key' => $yourApiKey]);$chatKey = 'test-chat-history';$chatHistory = new InMemoryChatHistory($chatKey);$agent = LarAgent::setup($driver, $chatHistory, [ 'model' => 'gpt-4o-mini',]);// Add a message and get a response$userMessage = Message::user('Hello, how can you help me?');$agent->withMessage($userMessage);$response = $agent->run();echo $response;
You can add tools to the agent using the Tool class:
Copy
use LarAgent\Tool;// Create a tool$toolName = 'get_current_weather';$tool = Tool::create($toolName, 'Get the current weather in a given location');$tool->addProperty('location', 'string', 'The city and state, e.g. San Francisco, CA') ->addProperty('unit', 'string', 'The unit of temperature', ['celsius', 'fahrenheit']) ->setRequired('location') ->setMetaData(['sent_at' => '2024-01-01']) ->setCallback(function ($location, $unit = 'fahrenheit') { // "Call the weather API" return 'The weather in '.$location.' is 72 degrees '.$unit; });// Add the tool to the agent$agent->setTools([$tool]);
Outside of Laravel, you’ll need to create and pass chat history instance into setup method:
Copy
use LarAgent\History\JsonChatHistory;// Setup$yourApiKey = 'your-openai-api-key'; // Replace with your actual API key$driver = new OpenAiDriver(['api_key' => $yourApiKey]);$chatHistory = new JsonChatHistory('test-chat-history');$agent = LarAgent::setup($driver, $chatHistory, [ 'model' => 'gpt-4o-mini',]);
There is 2 availble chat history classes for usage outside of Laravel:
JsonChatHistory
InMemoryChatHistory
You can easily implement your own, check custom-chat-history for more information.
As mentioned at the top of this page, LarAgent will soon separate its core engine into a standalone package. This will provide a cleaner, more focused API for non-Laravel projects while maintaining full compatibility with the current methods and functions.
The standalone engine will offer:
Improved documentation specifically for non-Laravel usage
Simplified installation for PHP projects
Reduced dependencies
The same powerful features you’re used to in LarAgent
Stay tuned to the official repository for announcements about this upcoming change.
Here’s a complete example combining structured output, tools, and custom instructions:
Copy
use LarAgent\Drivers\OpenAi\OpenAiDriver;use LarAgent\History\InMemoryChatHistory;use LarAgent\LarAgent;use LarAgent\Message;use LarAgent\Messages\ToolCallMessage;use LarAgent\Tool;// Setup$driver = new OpenAiDriver(['api_key' => $yourApiKey]);$chatHistory = new InMemoryChatHistory('weather-chat');$agent = LarAgent::setup($driver, $chatHistory, [ 'model' => 'gpt-4o-mini',]);// Create a weather tool$weatherTool = Tool::create('get_current_weather', 'Get the current weather in a given location') ->addProperty('location', 'string', 'The city and state, e.g. San Francisco, CA') ->addProperty('unit', 'string', 'The unit of temperature', ['celsius', 'fahrenheit']) ->setRequired('location') ->setCallback(function ($location, $unit = 'fahrenheit') { return 'The weather in '.$location.' is 72 degrees '.$unit; });// Define structured output schema$weatherInfoSchema = [ 'name' => 'weather_info', 'schema' => [ 'type' => 'object', 'properties' => [ 'locations' => [ 'type' => 'array', 'items' => [ 'type' => 'object', 'properties' => [ 'city' => ['type' => 'string'], 'weather' => ['type' => 'string'], ], 'required' => ['city', 'weather'], ], ], ], 'required' => ['locations'], ], 'strict' => true,];// Set up the agent with all components$userMessage = Message::user('What\'s the weather like in Boston and Los Angeles? I prefer celsius');$instructions = 'You are weather assistant and always respond using celsius. If it provided as fahrenheit, convert it to celsius.';$agent->setTools([$weatherTool]) ->structured($weatherInfoSchema) ->withInstructions($instructions) ->withMessage($userMessage);// Get the structured response$response = $agent->run();