PHP code example of luminovang / php-ai-models

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


composer install luminovang/php-ai-models

use Luminova\AI\Model;

// Anywhere a model string is expected — pass the constant value.
$ai->message('Hello!', ['model' => Model::GPT_4_1_MINI]);
$ai->embed('Hello world', ['model' => Model::TEXT_EMBEDDING_3_SMALL]);
$ai->vision('Describe this.', '/tmp/img.png', ['model' => Model::LLAVA]);

use Luminova\AI\Model;

Model::client(Model::GPT_4_1_MINI);      // 'openai'
Model::client(Model::CLAUDE_SONNET_4_6); // 'anthropic'
Model::client(Model::LLAVA);             // 'ollama'
Model::client('my-custom-model');        // null

$openaiModels    = Model::forClient('openai');
$anthropicModels = Model::forClient('anthropic');
$ollamaModels    = Model::forClient('ollama');

foreach ($ollamaModels as $name => $id) {
    echo "{$name} => {$id}" . PHP_EOL;
}
// LLAMA_3 => llama3
// LLAMA_3_1 => llama3.1
// ...

$visionModels    = Model::forCapability('vision');
$embeddingModels = Model::forCapability('embedding');
$reasoningModels = Model::forCapability('reasoning');

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

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

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

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

Model::isReasoning(Model::O3);            // true
Model::isReasoning(Model::DEEPSEEK_R1);  // true
Model::isReasoning(Model::GPT_4_1_MINI); // false

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

Model::exists(Model::GPT_4_1_MINI);  // true
Model::exists('my-custom-model');    // false

$all = Model::all();
// [
//   'GPT_5'          => 'gpt-5',
//   'GPT_5_MINI'     => 'gpt-5-mini',
//   'GPT_4_1_MINI'   => 'gpt-4.1-mini',
//   ...
// ]

echo count(Model::all()); // 103

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

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

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

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

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

// Vision
$output = AI::Openai($key)->vision('What is in this image?', '/tmp/photo.jpg', [
    'model' => Model::GPT_4_1,
]);

$userModel = $request->get('model', Model::GPT_4_1_MINI);

if (!Model::exists($userModel)) {
    throw new InvalidArgumentException("Unknown model: {$userModel}");
}

$reply = $ai->message('Hello!', ['model' => $userModel]);

// Build a select list for a UI
$options = [];

foreach (Model::forClient('openai') as $name => $id) {
    $options[$id] = str_replace('_', ' ', ucfirst(strtolower($name)));
}

// ['gpt-4.1-mini' => 'Gpt 4 1 mini', ...]

// Only offer vision-capable models in the UI
$visionModels = Model::forCapability('vision');

// Only offer embedding models for the vector store config
$embeddingModels = Model::forCapability('embedding');

// Show reasoning models separately
$reasoningModels = Model::forCapability('reasoning');

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

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

analyzeImage('Describe this chart.', '/tmp/q4.png', Model::GPT_4_1);   // OK
analyzeImage('Describe this chart.', '/tmp/q4.png', Model::WHISPER_1); // throws

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

function chat(string $prompt, string $model): array
{
    $client = Model::client($model);

    return match ($client) {
        'openai'    => AI::Openai($_ENV['OPENAI_KEY'])->message($prompt, ['model' => $model]),
        'anthropic' => AI::Anthropic($_ENV['ANTHROPIC_KEY'])->message($prompt, ['model' => $model]),
        'ollama'    => AI::Ollama()->message($prompt, ['model' => $model]),
        default     => throw new RuntimeException("Unsupported client: {$client}"),
    };
}

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