symfony/ai-hugging-face-platform
Symfony AI HuggingFace bridge for the HuggingFace Inference API and multiple providers (Cerebras, Cohere, Groq, Together, etc.). Invoke thousands of pretrained models across 40+ tasks—chat, text generation, vision, audio, embeddings—with model discovery, flexible I/O, and typed results.
composer require symfony/ai-hugging-face-platform
.env:
HUGGINGFACE_API_KEY=hf_your_api_key_here
use Symfony\AI\Platform\Bridge\HuggingFace\Factory;
use Symfony\AI\Platform\Bridge\HuggingFace\Provider;
$platform = Factory::createPlatform(
apiKey: env('HUGGINGFACE_API_KEY'),
provider: Provider::HUGGINGFACE_INFERENCE, // Default provider
);
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Bridge\HuggingFace\Task;
$messages = new MessageBag([
Message::ofUser('Explain Laravel dependency injection in simple terms.'),
]);
$result = $platform->invoke('HuggingFaceH4/zephyr-7b-beta', $messages, [
'task' => Task::CHAT_COMPLETION,
'temperature' => 0.7,
]);
echo $result->asText();
php artisan ai:huggingface:model-list --task=chat-completion --search=zephyr
google/flan-t5-small) before scaling.Task::TEXT_GENERATION, Task::IMAGE_CLASSIFICATION).MessageBag or raw strings.Image::fromFile() or Image::fromUri().$result = $platform->invoke(
modelId: 'model-name',
input: $input,
options: [
'task' => Task::TASK_NAME,
'temperature' => 0.5,
'max_new_tokens' => 100,
// Provider-specific options (e.g., 'provider' => Provider::GROQ)
]
);
asText(), asVectors(), or asObject().$vectors = $result->asVectors();
$firstVector = $vectors->first()->getValues();
AppServiceProvider:
public function register()
{
$this->app->singleton('huggingface.platform', function ($app) {
return Factory::createPlatform(
apiKey: env('HUGGINGFACE_API_KEY'),
provider: Provider::HUGGINGFACE_INFERENCE,
httpClient: $app->make(\Symfony\Contracts\HttpClient\HttpClientInterface::class)
);
});
}
use Illuminate\Support\Facades\App;
public function generateText(Request $request)
{
$platform = App::make('huggingface.platform');
$result = $platform->invoke('model-id', $request->input, ['task' => Task::TEXT_GENERATION]);
return response()->json(['response' => $result->asText()]);
}
$result = $platform->invoke('model-id', $input, [
'task' => Task::CHAT_COMPLETION,
'provider' => Provider::GROQ, // Force Groq for low-latency
]);
$platform = Factory::createPlatform(
apiKey: env('HUGGINGFACE_API_KEY'),
provider: Provider::HUGGINGFACE_INFERENCE,
// Optional: Configure fallback logic in a custom factory
);
MessageBag for multi-turn conversations.$messages = new MessageBag([
Message::ofSystem('You are a helpful assistant.'),
Message::ofUser('What is Laravel?'),
]);
$platform->invoke('model-id', ['text1', 'text2'], ['task' => Task::FEATURE_EXTRACTION]);
vector extension) for semantic search.$image = Image::fromUri('https://example.com/large-image.jpg');
try {
$result = $platform->invoke(...);
} catch (\Symfony\AI\Platform\Exception\RateLimitException $e) {
sleep(2 ** $attempt); // Exponential backoff
retry();
}
$primaryModel = 'model-a';
$fallbackModel = 'model-b';
try {
$result = $platform->invoke($primaryModel, $input, ['task' => Task::TASK_NAME]);
} catch (\Exception $e) {
$result = $platform->invoke($fallbackModel, $input, ['task' => Task::TASK_NAME]);
}
$mockPlatform = Mockery::mock(\Symfony\AI\Platform\PlatformInterface::class);
$mockPlatform->shouldReceive('invoke')
->once()
->andReturn(new \Symfony\AI\Platform\Response\TextResponse('Mocked response'));
$this->app->instance('huggingface.platform', $mockPlatform);
Cold Start Latency:
--warm in CLI).API Key Management:
.env or a secrets manager (e.g., AWS Secrets Manager).Rate Limits:
ai:huggingface:model-info model-id --show-usage
Input/Output Size Limits:
text-generation).ai:huggingface:model-info model-id --show-limits
Provider-Specific Quirks:
Type Safety:
asText() vs. asVectors()).instanceof checks:
if ($result instanceof \Symfony\AI\Platform\Response\TextResponse) {
echo $result->getText();
}
$platform = Factory::createPlatform(
apiKey: env('HUGGINGFACE_API_KEY'),
httpClient: \Symfony\Contracts\HttpClient\HttpClient::create([
'debug' => true,
]),
);
How can I help you explore Laravel packages today?