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.
The HuggingFace bridge provides integration with the HuggingFace Inference API, enabling access to thousands of pre-trained models for various AI tasks including natural language processing, computer vision, audio processing, and more.
Install the symfony/ai-hugging-face-platform package:
composer require symfony/ai-hugging-face-platform
use Symfony\AI\Platform\Bridge\HuggingFace\Factory;
use Symfony\AI\Platform\Bridge\HuggingFace\Provider;
$platform = Factory::createPlatform(
apiKey: 'hf_your_api_key_here',
provider: Provider::CEREBRAS, // or other providers
httpClient: $httpClient // optional, uses default if not provided
);
use Symfony\AI\Platform\Bridge\HuggingFace\Task;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
$messages = new MessageBag(
Message::ofUser('Hello, how are you doing today?')
);
$result = $platform->invoke('HuggingFaceH4/zephyr-7b-beta', $messages, [
'task' => Task::CHAT_COMPLETION,
]);
echo $result->asText();
use Symfony\AI\Platform\Bridge\HuggingFace\Task;
use Symfony\AI\Platform\Message\Content\Image;
$image = Image::fromFile('/path/to/image.jpg');
$result = $platform->invoke('google/vit-base-patch16-224', $image, [
'task' => Task::IMAGE_CLASSIFICATION,
]);
$classifications = $result->asObject()->getClassifications();
foreach ($classifications as $classification) {
echo $classification->getLabel() . ': ' . $classification->getScore() . PHP_EOL;
}
$result = $platform->invoke('sentence-transformers/all-MiniLM-L6-v2', 'Hello world', [
'task' => Task::FEATURE_EXTRACTION,
]);
$vectors = $result->asVectors();
The bridge supports multiple inference providers. For a complete list of available providers and their constants, see
the Provider interface.
Specify a provider when creating the platform:
use Symfony\AI\Platform\Bridge\HuggingFace\Factory;
use Symfony\AI\Platform\Bridge\HuggingFace\Provider;
$platform = Factory::createPlatform(
apiKey: 'your_api_key',
provider: Provider::GROQ,
);
The bridge supports 40+ AI tasks across multiple categories including:
For the complete list of supported tasks and their constants, see the Task interface.
The bridge includes commands to discover available models without using inference credits. They help to research and use models, and can be registered in a Symfony Console application.
The HuggingFace bridge provides two console commands for model discovery:
List available models with optional filtering:
ai:huggingface:model-list [options]
Options:
--provider, -p: Filter by inference provider (e.g., inference)--task, -t: Filter by task type (e.g., text-generation)--search, -s: Search term to filter models--warm, -w: Only list models with warm inference (ready without cold start)Examples:
# List all text generation models
ai:huggingface:model-list --task=text-generation
# List warm models for a specific provider
ai:huggingface:model-list --provider=hf-inference --warm
# Search for specific models
ai:huggingface:model-list --search=llama
Retrieve detailed information about a specific model:
ai:huggingface:model-info <model-name>
Examples:
# Get information about a specific model
ai:huggingface:model-info meta-llama/Llama-2-7b-chat
ai:huggingface:model-info gpt2
Output includes:
When invoking the platform, you can pass task-specific options:
$result = $platform->invoke('model/id', $input, [
'task' => Task::TEXT_GENERATION,
'temperature' => 0.7,
'top_p' => 0.9,
'top_k' => 50,
'max_new_tokens' => 256,
'repetition_penalty' => 1.2,
// ... other provider-specific parameters
]);
You can also override the provider per request:
$result = $platform->invoke('model/id', $input, [
'task' => Task::CHAT_COMPLETION,
'provider' => Provider::GROQ, // Override default provider
]);
How can I help you explore Laravel packages today?