openai-php/client
Community-maintained PHP client for the OpenAI API. Install via Composer and interact with models, responses, chat, images, audio, files, and more with a clean, typed interface—ideal for Laravel and modern PHP apps.
models(), chat(), responses()), making it easy to integrate into Laravel’s service-layer pattern (e.g., repositories, services).Services/Webhooks) enable real-time interactions, critical for chatbots, assistants, or async workflows.php-http/discovery or explicit HTTP client (e.g., Guzzle). Laravel’s Http facade or GuzzleHttp\Client can replace this with minimal config.AppServiceProvider to bind the client as a singleton.assistants, threads) may require refactoring if Laravel apps rely on legacy features.gpt-4) must be handled at the application level (e.g., Laravel middleware, queue throttling).OpenAIException) should be mapped to Laravel’s exception handling (e.g., render() in App\Exceptions\Handler).createStreamed()) may require custom Laravel event listeners or broadcast channels for real-time updates.usage->totalTokens) should integrate with Laravel’s logging/monitoring (e.g., Laravel Debugbar, Sentry).chat(), embeddings(), fineTuning()) to prioritize.assistants) be used? If so, plan for migration to threads or responses.batches, vector stores) be used? Laravel’s queue workers should be sized accordingly..env + Vault or a secrets manager (e.g., AWS Secrets Manager).moderations resource).Monolog) or APM tools (e.g., New Relic).openai-php/client (core API client).guzzlehttp/guzzle (if not using Laravel’s HTTP client).php-http/discovery (optional, if not using Laravel’s built-in client).laravel/queue (for async operations).spatie/laravel-activitylog (optional, for auditing API calls).Conversation, Embedding).Phase 1: Core Integration (1–2 weeks)
AppServiceProvider:
public function register()
{
$this->app->singleton(\OpenAI\Client::class, function ($app) {
return \OpenAI::client(config('services.openai.key'));
});
}
app/Services/OpenAIService.php).chat()->create(), embeddings()->create()) in controllers or jobs.ValidatesRequests) for input parameters.Phase 2: Advanced Features (2–3 weeks)
responses()->createStreamed()).
Example:
$stream = $client->responses()->createStreamed([...]);
foreach ($stream as $chunk) {
event(new OpenAIResponseChunk($chunk));
}
return Cache::remember('openai_models', now()->addHours(1), function () {
return $client->models()->list();
});
FineTuneJob::dispatch($client, $dataset)->onQueue('openai');
POST /openai/webhook).Phase 3: Observability & Optimization (1 week)
monolog/monolog).public function handle(Request $request, Closure $next)
{
if (OpenAIRateLimiter::tooManyRequests()) {
return response()->json(['error' => 'Rate limit exceeded'], 429);
}
return $next($request);
}
withBaseUri():
$client->withBaseUri('your-resource.openai.azure.com');
chat(), embeddings(), and completions() for most use cases.assistants) unless critical.Http::fake() to simulate API responses.openai-php/client for breaking changes (e.g., OpenAI API deprecations). Use Laravel’s composer.json conflict or replace directives if forking the package.How can I help you explore Laravel packages today?