LLM Drivers provide a flexible interface to connect with different AI providers while maintaining a consistent API for your application.
LLM Drivers allow you to switch between different AI providers (like OpenAI, Ollama, or OpenRouter) without changing your application code, providing flexibility and vendor independence.
LLM Drivers provide a standardized interface for interacting with different language model providers, allowing you to easily switch between providers without changing your application code.
The built-in drivers implement this interface and provide a simple way to use various AI APIs with a consistent interface.
If you need to integrate with an AI provider that doesn’t have a built-in driver, you can create your own by implementing the LlmDriver interface:
Copy
namespace App\LlmDrivers;use LarAgent\Core\Abstractions\LlmDriver;use LarAgent\Core\Contracts\LlmDriver as LlmDriverInterface;use LarAgent\Core\Contracts\ToolCall as ToolCallInterface;use LarAgent\Messages\AssistantMessage;use LarAgent\Messages\StreamedAssistantMessage;use LarAgent\Messages\ToolCallMessage;use LarAgent\ToolCall;class CustomProviderDriver extends LlmDriver implements LlmDriverInterface{ public function sendMessage(array $messages, array $options = []): AssistantMessage { // Implement the API call to your provider } public function sendMessageStreamed(array $messages, array $options = [], ?callable $callback = null): \Generator { // Implement streaming for your custom provider } public function toolCallsToMessage(array $toolCalls): array { // Implement tool calls to message conversion } public function toolResultToMessage(ToolCallInterface $toolCall, mixed $result): array { // Implement tool result to message conversion } // Implement other helper methods...}
Then register your custom driver in the configuration:
Copy
// config/laragent.php'providers' => [ 'custom' => [ 'label' => 'my-custom-provider', 'driver' => \App\LlmDrivers\CustomProviderDriver::class, 'api_key' => env('CUSTOM_PROVIDER_API_KEY'), 'api_url' => env('CUSTOM_PROVIDER_API_URL'), 'model' => 'model-name', // Any other configuration your driver needs ],],