symfony/ai-meta-platform
Symfony AI bridge for Meta’s Llama platform. Connect to Llama models and use official prompt formats for Llama 3, 3.2, and 3.3. Part of the Symfony AI ecosystem; issues and PRs are handled in the main symfony/ai repository.
Install the Package
Add to composer.json:
composer require symfony/ai-meta-platform
For Laravel, ensure compatibility with Symfony’s HttpClient via a facade or service provider.
Configure API Credentials
Set Meta’s API key in .env (or config/services.php):
META_LLAMA_API_KEY=your_api_key_here
Basic Prompt Usage Create a service to wrap the Meta client:
use Symfony\Component\Ai\MetaPlatform\Client\MetaClient;
use Symfony\Component\Ai\MetaPlatform\Prompt\Prompt;
$client = new MetaClient('http://meta-api-endpoint', $apiKey);
$prompt = new Prompt('Llama 3', 'Summarize this: {text}');
$response = $client->complete($prompt->withVariables(['text' => 'Your input here']));
Laravel Integration (Quick Start)
Create a facade or service provider to bridge Symfony’s MetaClient:
// app/Providers/MetaServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Ai\MetaPlatform\Client\MetaClient;
class MetaServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('meta.client', function ($app) {
return new MetaClient(
config('services.meta.endpoint'),
config('services.meta.api_key')
);
});
}
}
Configure in config/services.php:
'meta' => [
'endpoint' => env('META_LLAMA_ENDPOINT', 'https://api.meta.com/llama/v1'),
'api_key' => env('META_LLAMA_API_KEY'),
],
First Use Case: Dynamic Content Generation Use in a Laravel controller or command:
use Illuminate\Support\Facades\Meta;
public function generateSummary(Request $request)
{
$text = $request->input('text');
$prompt = new \Symfony\Component\Ai\MetaPlatform\Prompt\Prompt('Llama 3', 'Summarize: {text}');
$response = Meta::client()->complete($prompt->withVariables(['text' => $text]));
return response()->json(['summary' => $response->getContent()]);
}
Prompt Standardization
[INST] tokens).$prompt = new Prompt('Llama 3.2', '[INST] {instruction} [/INST]');
$prompt->withVariables(['instruction' => 'Translate to French: Hello']);
Hybrid AI Pipelines
$llamaResponse = $metaClient->complete($llamaPrompt);
$openaiResponse = $openaiClient->complete($openaiPrompt);
Laravel Service Integration
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
class GenerateContentJob implements ShouldQueue
{
use Queueable;
public function handle()
{
$response = Meta::client()->complete($this->prompt);
// Store or process response
}
}
event(new ContentGenerated($response));
Dynamic Prompt Templates
$template = view('prompts.summary')->with(['text' => $input])->render();
$prompt = new Prompt('Llama 3', $template);
Error Handling
try-catch helpers:
try {
$response = Meta::client()->complete($prompt);
} catch (\Symfony\Component\Ai\Exception\AiException $e) {
Log::error('Meta API failed', ['error' => $e->getMessage()]);
throw new \Exception('AI service unavailable');
}
'meta' => [
'endpoints' => [
'local' => 'http://localhost:11434',
'cloud' => 'https://api.meta.com/llama/v1',
],
'default' => env('META_LLAMA_ENDPOINT', 'local'),
],
throttle middleware for API calls:
Route::middleware(['throttle:10,1'])->group(function () {
Route::post('/generate', [AIController::class, 'generate']);
});
$cacheKey = 'ai_summary_' . md5($text);
$response = Cache::remember($cacheKey, now()->addHours(1), function () use ($prompt) {
return Meta::client()->complete($prompt);
});
Symfony Dependency Overhead
HttpClient, Messenger, or EventDispatcher.Http facade or GuzzleHttp for HTTP calls.Messenger with Laravel’s Bus or Queue.EventDispatcher unless critical; use Laravel’s Events.Prompt Format Rigidity
[INST] tokens). Deviations may break responses.if (!str_contains($prompt->getContent(), '[INST]')) {
throw new \InvalidArgumentException('Prompt must include [INST] for Llama 3');
}
API Key Management
Vault or encrypted .env:
php artisan vault:make META_LLAMA_API_KEY
Token Limits
$chunkedText = array_chunk($longText, 2000);
foreach ($chunkedText as $chunk) {
$response = Meta::client()->complete($prompt->withVariables(['text' => $chunk]));
}
Local vs. Cloud Deployment
$client = new MetaClient(config('services.meta.endpoints.' . env('META_ENV', 'cloud')));
Enable Verbose Logging
Configure Symfony’s HttpClient to log requests/responses:
$client = new MetaClient($endpoint, $apiKey, [
'headers' => ['Accept' => 'application/json'],
'debug' => true, // Enable debug mode
]);
Validate Prompt Structure Use a regex to check for required tokens:
$pattern = '/\[INST\].*?\[\/INST\]/s';
if (!preg_match($pattern, $prompt->getContent())) {
throw new \RuntimeException('Invalid Llama 3 prompt format');
}
Handle Rate Limits Gracefully Implement exponential backoff for retries:
use Symfony\Component\Ai\Exception\RateLimitException;
try {
$response = $client->complete($prompt);
} catch (RateLimitException $e) {
sleep(2 ** $attempt); // Exponential backoff
retry();
}
Test with Minimal Prompts Start with simple prompts to isolate issues:
$simplePrompt = new Prompt('Llama 3', '[INST] Hello [/INST]');
$response = $client->complete($simplePrompt);
Prompt class to add validation:
class CustomPrompt extends \Symfony\Component\Ai\MetaPlatform\Prompt\Prompt
{
public function
How can I help you explore Laravel packages today?