Download the PHP package sanjarani/openai without Composer
On this page you can find all versions of the php package sanjarani/openai. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sanjarani/openai
More information about sanjarani/openai
Files in sanjarani/openai
Package openai
Short Description A comprehensive Laravel 12 package for interacting with the OpenAI API, including advanced features like Assistants, Vector Stores, and optimization techniques.
License MIT
Informations about the package openai
Sanjarani OpenAI Laravel Package
A comprehensive Laravel 12 package for interacting with the OpenAI API. This package provides an easy-to-use interface for all major OpenAI features, including Chat Completions, Embeddings, Image Generation, Audio Transcription/Translation, File Management, Fine-tuning, Moderations, Models, and the powerful Assistants API with Vector Store support. It also includes advanced features like configurable caching, batching, and flexible API endpoint configuration for optimized performance and cost-efficiency.
Features
- Full coverage of the OpenAI REST API.
- Intuitive, fluent API design leveraging Laravel conventions.
- Support for all major OpenAI models.
- Flexible API Endpoint Configuration: Globally configurable
baseUrland per-requestbaseUrloverride for maximum flexibility. - Chat Completions: Including streaming responses.
- Embeddings: With batch creation support.
- Image Generation: Create, edit, and create variations of images.
- Audio: Transcribe audio to text and translate audio to English using Whisper models. Generate speech from text using TTS models.
- Files: Upload, list, retrieve, and delete files.
- Fine-tuning: Manage fine-tuning jobs and events.
- Moderations: Classify text against OpenAI's content policy.
- Models: List and retrieve available models.
- Assistants API: Full support for Assistants, Threads, Messages, and Runs, including tool calls and streaming. Compatible with Assistants API v2 via header configuration.
- Vector Stores: Manage vector stores and files within them, including batch operations.
- Configurable Caching: Cache API responses to reduce latency and costs, with support for various Laravel cache stores.
- Retry Mechanism: Automatic retries for transient server errors and rate limit issues.
- Detailed Configuration: Extensive configuration options for API keys, default models, HTTP client settings, caching, logging, and API base URL.
- Laravel 12 Ready: Built and tested for Laravel 12.
- Composer Compatible: Easy installation and autoloading.
Requirements
- PHP 8.1+
- Laravel 11.x or 12.x
- An OpenAI API Key
Installation
You can install the package via Composer:
The package will automatically register its service provider and facade.
Next, publish the configuration file:
This will create a config/openai.php file in your application. You should add your OpenAI API Key and (optionally) Organization ID and Base URL to your .env file:
Configuration
The config/openai.php file allows you to configure various aspects of the package:
api_key: Your OpenAI API key (preferably set via.env).organization_id: Your OpenAI Organization ID (optional, via.env).base_url: The base URL for the OpenAI API. Defaults tohttps://api.openai.com/v1/. Can be overridden per request.openai_beta_headers: An array to specify anyOpenAI-Betaheaders, e.g.,["OpenAI-Beta" => "assistants=v2"].defaults: Default models for chat, embeddings, audio, etc.http: HTTP client settings (timeout, retry attempts, retry delay).caching: Enable/disable caching, select cache store, set TTL, and configure endpoint-specific caching.logging: Enable/disable logging of API requests/responses.assistants: Default polling intervals and timeouts for Assistant runs.
Configuring OpenAI-Beta Headers
To use features like Assistants API v2, you might need to send specific OpenAI-Beta headers. You can configure these in config/openai.php:
This setup allows you to enable beta features like Assistants API v2 by setting OPENAI_BETA_ASSISTANTS_VERSION=v2 in your .env file. The Client.php has been updated to automatically include these configured headers in all API requests.
Usage
The package provides a Facade for easy access to its functionalities.
Per-Request Base URL Override
All resource methods (e.g., OpenAI::chat()->create(...), OpenAI::embeddings()->create(...)) now accept an optional final string parameter to override the baseUrl for that specific request. This is useful if you need to target a different API endpoint for a particular call (e.g., a beta endpoint or a custom proxy).
If no baseUrlOverride is provided, the base_url from your config/openai.php (or its default https://api.openai.com/v1/) will be used.
Basic Example: Chat Completion
Streaming Chat Completion
Embeddings
Image Generation
Audio Transcription (Whisper)
Audio Speech (TTS)
Assistants API Example
(Ensure openai_beta_headers in config/openai.php is set correctly if using Assistants API v2, e.g., by setting OPENAI_BETA_ASSISTANTS_VERSION=v2 in your .env file.)
Vector Stores (with Assistants API)
(Ensure openai_beta_headers in config/openai.php is set correctly if using Assistants API v2.)
Caching
If caching is enabled in config/openai.php, responses for configured endpoints will be cached automatically.
Error Handling
The package throws specific exceptions for different types of API errors (as previously documented).
Testing
(As previously documented)
Contributing
(As previously documented)
License
(As previously documented)
This README provides a comprehensive overview. For specific API parameters and more detailed information, please refer to the official OpenAI API documentation.
All versions of openai with dependencies
guzzlehttp/guzzle Version ^7.0
illuminate/support Version ^11.0|^12.0
illuminate/contracts Version ^11.0|^12.0
illuminate/http Version ^11.0|^12.0