openai-php/client
Community-maintained PHP client for the OpenAI API. Send requests for models, responses and chat, conversations, containers and files, with streaming support and a clean, typed interface. Install via Composer and use in any PHP app (Laravel-friendly).
OpenAI facade, Client class, and dedicated service classes for models like Chat, Embeddings, etc.), making it easy to adopt incrementally (e.g., start with chat completions before expanding to embeddings or fine-tuning)..env. This minimizes invasive changes to existing codebases.config/openai.php) and per-request configurations, accommodating use cases like multi-tenant deployments or dynamic API key management.file-* operations, Assistants API). Monitor updates or consider a wrapper layer for critical features.VCR or Mockery to streamline testing.spatie/fractal) or local fallbacks (e.g., cached responses).OpenAI facade and binds the Client interface to its implementation.config/openai.php) for API key, base URL, and timeout settings.@OpenAI annotations for AI-powered endpoints).Chat::create()->collect()).OpenAI::completion()) for simplicity, then refactor to dependency-injected services for scalability.php artisan vendor:publish --tag=openai-config
OPENAI_API_KEY) in .env.Mockery to stub OpenAI responses).composer.json dependencies.curl, json, and mbstring. Verify these are enabled in your php.ini.Cache::remember() for embeddings).FormRequest or Validator to sanitize prompts.composer require openai-php/client
use OpenAI\Laravel\Facades\OpenAI;
Route::get('/generate', function () {
return OpenAI::completion()->create([
'model' => 'gpt-3.5-turbo',
'prompt' => 'Explain Laravel middleware',
]);
});
OpenAI\Exceptions\OpenAIException).headers['x-ratelimit-remaining']).OpenAI::completion()->create([
'model' => 'gpt-3.5-turbo',
'stream' => true,
'prompt' => 'Count to 10',
]);
composer why-not to audit dependency conflicts.config/openai.php) to avoid hardcoded values.OPENAI_API_KEY).text-davinci-003 to gpt-3.5-turbo).config/openai.php: 'debug' => true) to log API requests/responses.dd() or dump() to inspect OpenAI responses:
$response = OpenAI::chat()->create([...]);
dd($response->choices[0]->message->content);
How can I help you explore Laravel packages today?