PHP code example of shawnveltman / laravel-openai

1. Go to this page and download the library: Download shawnveltman/laravel-openai 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/ */

    

shawnveltman / laravel-openai example snippets


use Shawnveltman\LaravelOpenai\ProviderResponseTestClass;

$ai = new ProviderResponseTestClass();

// Works with any supported model
$response = $ai->get_response_from_provider(
    prompt: 'Explain quantum computing in simple terms',
    model: 'gpt-5-nano', // or 'claude-sonnet-4', 'gemini-pro', etc.
);

use Shawnveltman\LaravelOpenai\Enums\ThinkingEffort;

// Minimal thinking for simple tasks (fast, cheaper)
$response = $ai->get_response_from_provider(
    prompt: 'Format this data as JSON',
    model: 'gpt-5-nano',
    thinking_effort: ThinkingEffort::MINIMAL
);

// High thinking for complex reasoning (slower, more thorough)
$response = $ai->get_response_from_provider(
    prompt: 'Solve this complex mathematical proof',
    model: 'claude-sonnet-4',
    thinking_effort: ThinkingEffort::HIGH
);

$response = $ai->get_response_from_provider(
    prompt: 'List the top 5 programming languages with descriptions',
    model: 'gpt-5-nano',
    json_mode: true
);

$response = $ai->get_response_from_provider(
    prompt: 'What is in this image?',
    model: 'gpt-4o',
    image_urls: ['https://example.com/image.jpg']
);

$response = $ai->get_response_from_provider(
    prompt: 'Write a haiku',
    model: 'claude-sonnet-4',
    system_prompt: 'You are a professional poet',
    messages: [
        ['role' => 'user', 'content' => 'Previous message'],
        ['role' => 'assistant', 'content' => 'Previous response'],
    ]
);

$response = $ai->get_response_from_provider(
    prompt: 'Your prompt here',
    model: 'claude-sonnet-4',
    user_id: 123,
    description: 'Marketing copy generation',
    job_uuid: 'unique-job-id',
    temperature: 0.7,
    max_tokens: 4096,
    thinking_effort: ThinkingEffort::MEDIUM
);

use Shawnveltman\LaravelOpenai\Models\CostLog;

// Get costs for a user
$costs = CostLog::where('user_id', 123)->get();

// Get costs by model
$costs = CostLog::where('model', 'gpt-5-nano')->sum('input_tokens');
bash
php artisan vendor:publish --tag="laravel-openai-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-openai-config"