PHP code example of luminovang / php-ai-models-enum

1. Go to this page and download the library: Download luminovang/php-ai-models-enum 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/ */

    

luminovang / php-ai-models-enum example snippets


composer install luminovang/php-ai-models-enum

use Luminova\AI\Model;

// Correct — pass ->value to the client
$ai->message('Hello!', ['model' => Model::GPT_4_1_MINI->value]);

// Also correct when your method accepts Model directly and extracts ->value internally
function chat(Model $model, string $prompt): array {
    return $ai->message($prompt, ['model' => $model->value]);
}

$model = Model::from('gpt-4.1-mini');  // Model::GPT_4_1_MINI
$model = Model::from('unknown');        // throws \ValueError

$model = Model::tryFrom('gpt-4.1-mini'); // Model::GPT_4_1_MINI
$model = Model::tryFrom('unknown');       // null

foreach (Model::cases() as $model) {
    echo $model->name . ' => ' . $model->value . PHP_EOL;
}
// GPT_5 => gpt-5
// GPT_5_MINI => gpt-5-mini
// ...
// ALL_MINILM => all-minilm

Model::GPT_4_1_MINI->client();      // 'openai'
Model::CLAUDE_SONNET_4_6->client(); // 'anthropic'
Model::LLAVA->client();             // 'ollama'
Model::DEEPSEEK_R1->client();       // 'ollama'

Model::O3->capabilities();
// ['chat', 'vision', 'reasoning', 'coding']

Model::GPT_4_1_MINI->capabilities();
// ['chat', 'vision', 'coding', 'fine-tuning']

Model::NOMIC_EMBED_TEXT->capabilities();
// ['embedding']

Model::DALL_E_3->capabilities();
// ['image']

Model::WHISPER_1->capabilities();
// ['transcription']

Model::GPT_4_1->isVision();          // true
Model::LLAVA->isVision();            // true
Model::NOMIC_EMBED_TEXT->isVision(); // false
Model::WHISPER_1->isVision();        // false

Model::O3->isReasoning();            // true
Model::O3_PRO->isReasoning();        // true
Model::DEEPSEEK_R1->isReasoning();   // true
Model::CLAUDE_SONNET_3_7->isReasoning(); // true
Model::GPT_4_1_MINI->isReasoning();  // false

Model::TEXT_EMBEDDING_3_SMALL->isEmbedding(); // true
Model::NOMIC_EMBED_TEXT->isEmbedding();       // true
Model::MXBAI_EMBED_LARGE->isEmbedding();      // true
Model::GPT_4_1->isEmbedding();               // false

Model::GPT_4_1_MINI->isChat(); // true
Model::LLAMA_3_2->isChat();    // true
Model::DALL_E_3->isChat();     // false
Model::WHISPER_1->isChat();    // false

Model::DEEPSEEK_CODER->isCoding();   // true
Model::QWEN_2_5_CODER->isCoding();   // true
Model::CODE_LLAMA->isCoding();       // true
Model::GPT_4_1->isCoding();          // true
Model::LLAVA->isCoding();            // false

Model::GPT_4_1->isFineTunable();      // true
Model::GPT_4_1_MINI->isFineTunable(); // true
Model::GPT_4_1_NANO->isFineTunable(); // true
Model::O3->isFineTunable();           // false
Model::CLAUDE_SONNET_4_6->isFineTunable(); // false

$cases = Model::forClient('openai');
// [Model::GPT_5, Model::GPT_5_MINI, ..., Model::TEXT_MODERATION]

$cases = Model::forClient('anthropic');
// [Model::CLAUDE_OPUS_4_6, Model::CLAUDE_SONNET_4_6, ...]

$cases = Model::forClient('ollama');
// [Model::LLAMA_3, Model::LLAMA_3_1, ..., Model::ALL_MINILM]

foreach (Model::forClient('ollama') as $model) {
    echo $model->name . ' = ' . $model->value . PHP_EOL;
}

$visionModels    = Model::forCapability('vision');
$embeddingModels = Model::forCapability('embedding');
$reasoningModels = Model::forCapability('reasoning');
$codingModels    = Model::forCapability('coding');
$imageModels     = Model::forCapability('image');

foreach (Model::forCapability('reasoning') as $model) {
    echo $model->value . ' (' . $model->client() . ')' . PHP_EOL;
}
// o3 (openai)
// o3-pro (openai)
// deepseek-r1 (ollama)
// claude-sonnet-3-7 (anthropic)
// ...

$model = Model::resolve('gpt-4.1-mini');  // Model::GPT_4_1_MINI
$model = Model::resolve('bad-string');     // null

// Safe fallback pattern
$model = Model::resolve($config['model']) ?? Model::GPT_4_1_MINI;

use Luminova\AI\Model;
use Luminova\AI\AI;

// Chat with OpenAI
$reply = AI::Openai($key)->message('Hello!', [
    'model' => Model::GPT_4_1_MINI->value,
]);

// Chat with Claude
$reply = AI::Anthropic($key)->message('Summarise this.', [
    'model' => Model::CLAUDE_SONNET_4_6->value,
]);

// Local inference with Ollama
$reply = AI::Ollama()->message('Explain closures.', [
    'model' => Model::LLAMA_3_2->value,
]);

// Embeddings
$vector = AI::Openai($key)->embed('Hello world', [
    'model' => Model::TEXT_EMBEDDING_3_SMALL->value,
]);

// Ollama vision
$reply = AI::Ollama()->vision('What is in this image?', '/tmp/photo.jpg', [
    'model' => Model::LLAVA->value,
]);

use Luminova\AI\Model;

function chat(string $prompt, Model $model = Model::GPT_4_1_MINI): array
{
    return AI::getInstance()->message($prompt, ['model' => $model->value]);
}

// Valid calls
chat('Hello!');
chat('Hello!', Model::O3);
chat('Hello!', Model::CLAUDE_OPUS_4_6);
chat('Hello!', Model::LLAMA_3_3);

// Invalid — PHP type error at call time, not a runtime client error
chat('Hello!', 'gpt-4.1-mini');  // TypeError: Argument 2 must be of type Model

// From a config file
$configured = $config->get('ai.model', 'gpt-4.1-mini');
$model = Model::resolve($configured) ?? Model::GPT_4_1_MINI;

echo "Using: {$model->value} ({$model->client()})";

// From a web request — reject unknown values
$userModel = $_POST['model'] ?? '';
$model = Model::tryFrom($userModel);

if ($model === null) {
    http_response_code(400);
    exit("Unknown model: {$userModel}");
}

$reply = $ai->message($prompt, ['model' => $model->value]);

$model = Model::CLAUDE_SONNET_4_6;

$tier = match ($model) {
    Model::GPT_5, Model::CLAUDE_OPUS_4_6, Model::O3_PRO     => 'flagship',
    Model::GPT_4_1, Model::CLAUDE_SONNET_4_6, Model::O3     => 'standard',
    Model::GPT_4_1_MINI, Model::CLAUDE_HAIKU_4_5, Model::O4_MINI => 'efficient',
    default => 'other',
};

use Luminova\AI\AI;
use Luminova\AI\Model;

function chat(string $prompt, Model $model): array
{
    return match ($model->client()) {
        'openai'    => AI::Openai($_ENV['OPENAI_KEY'])->message($prompt, ['model' => $model->value]),
        'anthropic' => AI::Anthropic($_ENV['ANTHROPIC_KEY'])->message($prompt, ['model' => $model->value]),
        'ollama'    => AI::Ollama()->message($prompt, ['model' => $model->value]),
    };
}

chat('Tell me a joke.', Model::GPT_4_1_MINI);      // OpenAI
chat('Tell me a joke.', Model::CLAUDE_SONNET_4_6); // Anthropic
chat('Tell me a joke.', Model::LLAMA_3_2);         // Ollama

use Luminova\AI\Model;

function analyzeImage(string $prompt, string $imagePath, Model $model): array
{
    if (!$model->isVision()) {
        throw new RuntimeException(
            "Model '{$model->value}' does not support vision. " .
            'Try Model::GPT_4_1, Model::LLAVA, or Model::LLAMA_3_2_VISION.'
        );
    }

    return AI::getInstance()->vision($prompt, $imagePath, ['model' => $model->value]);
}

analyzeImage('What breed is this?', '/tmp/dog.jpg', Model::GPT_4_1);   // OK
analyzeImage('What breed is this?', '/tmp/dog.jpg', Model::WHISPER_1); // throws

function embed(string $text, Model $model = Model::TEXT_EMBEDDING_3_SMALL): array
{
    if (!$model->isEmbedding()) {
        throw new RuntimeException("'{$model->value}' is not an embedding model.");
    }

    return AI::getInstance()->embed($text, ['model' => $model->value]);
}

// All available models grouped by client for a settings page
$grouped = [];

foreach (Model::cases() as $model) {
    $grouped[$model->client()][] = [
        'value' => $model->value,
        'label' => str_replace('_', ' ', ucfirst(strtolower($model->name))),
        'tags'  => $model->capabilities(),
    ];
}

// Only offer vision-capable models in a vision task dropdown
$visionOptions = array_map(
    fn(Model $m): array => ['value' => $m->value, 'label' => $m->name],
    Model::forCapability('vision')
);

// Always-latest alias — may quietly change behavior when Anthropic updates it
$model = Model::CLAUDE_OPUS_4_5;        // 'claude-opus-4-5'

// Pinned snapshot — behavior is frozen to the exact release
$model = Model::CLAUDE_OPUS_4_5_SNAP;   // 'claude-opus-4-5-20251101'