- How do I install **openai-php/client** in a Laravel project?
- Run `composer require openai-php/client` in your project directory. Laravel’s autoloader will handle dependencies automatically. Ensure your project uses PHP 8.2+ for full compatibility. The package integrates seamlessly with Laravel’s service container, so no extra configuration is needed for basic usage.
- Does this package support Laravel’s service container and dependency injection?
- Yes, the package is designed for Laravel’s service container. Bind the `OpenAI` client to the container in your `AppServiceProvider` or use Laravel’s `make:provider` to manage configurations like API keys. This allows you to leverage Laravel’s DI features for testing and modularity.
- Can I use this for real-time chat applications in Laravel?
- Absolutely. The `Responses` resource includes streaming capabilities (e.g., `createStreamed()`), making it ideal for real-time chat interfaces. Pair it with Laravel’s Blade or Livewire for dynamic UI updates. For async processing, consider Laravel queues to handle long-running conversations.
- How do I handle API rate limits or cost tracking in Laravel?
- The package doesn’t include built-in rate limiting, but you can integrate OpenAI’s usage APIs or third-party tools like Stripe for billing. For rate limits, implement middleware or a decorator around the `OpenAI` client to track and enforce limits. Laravel’s caching layer can also store API responses to reduce costs.
- Is there built-in support for Laravel queues or async operations?
- No, the package doesn’t natively support Laravel queues, but you can wrap long-running operations (e.g., fine-tuning, batch processing) in a `Job` class. Dispatch the job via `dispatch()` and process it in a queue worker. For webhooks or async events, use Laravel’s queue listeners or event system.
- Which Laravel versions and PHP versions are supported?
- The package requires **PHP 8.2+** and is compatible with Laravel 9.x and 10.x. Older Laravel versions (e.g., 8.x) may work but aren’t officially tested. If you’re on PHP 8.1 or lower, upgrade to meet the package’s requirements for type safety and modern PHP features.
- How do I integrate embeddings or vector databases with Laravel?
- Use the `Embeddings` resource to generate vectors, then store them in Laravel’s database (e.g., PostgreSQL with `pgvector`) or an external vector DB like Weaviate. For retrieval, query the database directly or build a Laravel service layer to abstract the logic. The package doesn’t enforce a specific storage method.
- Are there alternatives to this package for Laravel?
- Yes, alternatives include `php-openai/php-openai` (a lower-level client) or `spatie/laravel-ai` (a Laravel-specific wrapper). However, `openai-php/client` offers a **typed, fluent API** and better Laravel integration (e.g., service container support). Choose based on your need for abstraction vs. flexibility.
- How do I handle errors or exceptions in Laravel?
- The package throws `OpenAIException` for API errors. Convert these to Laravel’s `HttpException` or `ProblemException` in a middleware or service layer. For example, catch `OpenAIException` and throw a `ProblemException` with a custom status code for consistent API responses.
- Does this package support deprecated OpenAI APIs like Assistants or Threads?
- Yes, but the package explicitly marks deprecated resources (e.g., `Assistants`, `Threads`) to help you phase them out. If you rely on these, check OpenAI’s documentation for migration paths. For new projects, focus on modern resources like `Responses` or `Conversations` for better long-term support.