Skip to content

Providers ​

LabsLLM lets you easily switch between different LLM providers without having to rewrite your code, following the philosophy write once, run anywhere.

Supported Providers ​

Currently, LabsLLM supports the following providers:

FeatureOpenAIGoogleAnthropic
Text Prompts✅✅📅
System Instructions✅✅📅
Chat✅✅📅
Tools/Functions✅✅❌
Structure Output✅✅❌
Streaming✅✅❌
Embeddings📅❌❌
Voice📅❌❌
Image Generation📅❌📅

Configuring a Provider ​

Each provider requires specific configuration, but the interface is consistent. You just need to specify which provider you want to use when creating your instance.

OpenAI ​

php
// Using OpenAI
$execute = LabsLLM::text()
    ->using(new OpenAI('your-api-key', 'gpt-4o'))
    ->executePrompt('Hello world');

Google ​

php
// Using Google
$execute = LabsLLM::text()
    ->using(new Google('your-api-key', 'gemini-pro'))
    ->executePrompt('Hello world');

Anthropic (Planned) ​

php
// Using Anthropic (future development)
$execute = LabsLLM::text()
    ->using(new Anthropic('your-api-key', 'claude-3-opus'))
    ->executePrompt('Hello world');

Switching Between Providers ​

The beauty of LabsLLM is that you can easily switch between providers without changing any other code in your application. This means you can:

  1. Develop with one provider and deploy with another
  2. Conduct A/B testing between different providers
  3. Build redundancy into your application in case of provider unavailability

Practical Example ​

This code works with any supported provider, allowing you to easily switch:

php
// Initially using OpenAI
$provider = new OpenAI('your-api-key', 'gpt-4o');

// To switch to Google, just replace the line above with:
// $provider = new Google('your-api-key', 'gemini-pro');

// The rest of the code remains identical
$execute = LabsLLM::text()
    ->using($provider)
    ->executePrompt('What is the capital of Brazil?');

$response = $execute->getResponseData();
echo $response->response; // "The capital of Brazil is Brasília."

Provider-Specific Configurations ​

Each provider may have its own specific configurations, such as available models, request parameters, etc.

Available Models by Provider ​

ProviderPopular Models
OpenAIgpt-4o, gpt-4-turbo, gpt-3.5-turbo
Googlegemini-pro, gemini-ultra
Anthropicclaude-3-opus, claude-3-sonnet, claude-3-haiku

Model and Parameter Configuration ​

php
// Using OpenAI with specific model and temperature
$execute = LabsLLM::text()
    ->using(new OpenAI('your-api-key', 'gpt-4o'))
    ->withTemperature(0.7) // Adjusts model creativity
    ->executePrompt('Tell me a story');

// Using Google with different settings
$execute = LabsLLM::text()
    ->using(new Google('your-api-key', 'gemini-pro'))
    ->withTopP(0.9) // Controls response diversity
    ->executePrompt('Tell me a story');

Dependency Injection ​

In real applications, consider using dependency injection to facilitate provider switching:

php
class AIService
{
    private $llm;
    
    public function __construct(ProviderInterface $provider)
    {
        $this->llm = LabsLLM::text()->using($provider);
    }
    
    public function ask($question)
    {
        $execute = $this->llm->executePrompt($question);
        return $execute->getResponseData()->response;
    }
}

// In your configuration file or service provider
$service = new AIService(new OpenAI('your-api-key', 'gpt-4o'));

// To switch to another provider, just change this line
// $service = new AIService(new Google('your-api-key', 'gemini-pro'));

Recommendations ​

  • Start with OpenAI: The OpenAI implementation is the most complete and tested.
  • Centralize Configuration: Keep your provider configuration in a centralized location for easy switching.
  • Test with Multiple Providers: If possible, test your application with different providers to ensure compatibility.

Next Steps ​

Now that you understand how to configure and switch between providers, you can explore the different features of LabsLLM, such as Prompts, Chat, and Tools.

Released under the MIT License.