PHP code example of symfony / ai-hugging-face-platform

1. Go to this page and download the library: Download symfony/ai-hugging-face-platform library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

symfony / ai-hugging-face-platform example snippets


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::FEATHERLESS_AI,
);

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,
    'provider' => Provider::HF_INFERENCE,
]);

$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,
    'provider' => Provider::HF_INFERENCE,
]);

$vectors = $result->asVectors();

use Symfony\AI\Platform\Bridge\HuggingFace\Factory;
use Symfony\AI\Platform\Bridge\HuggingFace\Provider;

$platform = Factory::createPlatform(
    apiKey: 'your_api_key',
    provider: Provider::GROQ,
);

$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
]);

$result = $platform->invoke('model/id', $input, [
    'task' => Task::CHAT_COMPLETION,
    'provider' => Provider::GROQ,  // Override default provider
]);