Download the PHP package luckthemall/opanai-php-client without Composer
On this page you can find all versions of the php package luckthemall/opanai-php-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package opanai-php-client
OpenAI PHP is a community-maintained PHP API client that allows you to interact with the Open AI API. If you or your business relies on this package, it's important to support the developers who have contributed their time and effort to create and maintain this valuable tool:
- Nuno Maduro: github.com/sponsors/nunomaduro
- Sandro Gehri: github.com/sponsors/gehrisandro
Table of Contents
- Get Started
- Usage
- Models Resource
- Completions Resource
- Chat Resource
- Audio Resource
- Embeddings Resource
- Files Resource
- FineTuning Resource
- Moderations Resource
- Images Resource
- Assistants Resource
- Threads Resource
- Threads Messages Resource
- Threads Runs Resource
- Threads Runs Steps Resource
- Vector Stores Resource
- Vector Stores Files Resource
- Vector Stores File Batches Resource
- Batches Resource
- FineTunes Resource (deprecated)
- Edits Resource (deprecated)
- Meta Information
- Troubleshooting
- Testing
- Services
- Azure
Get Started
Requires PHP 8.1+
First, install OpenAI via the Composer package manager:
Ensure that the php-http/discovery
composer plugin is allowed to run or install a client manually if your project does not already have a PSR-18 client integrated.
Then, interact with OpenAI's API:
If necessary, it is possible to configure and create a separate client.
Usage
Models
Resource
list
Lists the currently available models, and provides basic information about each one such as the owner and availability.
retrieve
Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
delete
Delete a fine-tuned model.
Completions
Resource
create
Creates a completion for the provided prompt and parameters.
create streamed
Creates a streamed completion for the provided prompt and parameters.
Chat
Resource
create
Creates a completion for the chat message.
Creates a completion for the chat message with a tool call.
Creates a completion for the chat message with a function call.
create streamed
Creates a streamed completion for the chat message.
Audio
Resource
speech
Generates audio from the input text.
speechStreamed
Generates streamed audio from the input text.
transcribe
Transcribes audio into the input language.
translate
Translates audio into English.
Embeddings
Resource
create
Creates an embedding vector representing the input text.
Files
Resource
list
Returns a list of files that belong to the user's organization.
delete
Delete a file.
retrieve
Returns information about a specific file.
upload
Upload a file that contains document(s) to be used across various endpoints/features.
download
Returns the contents of the specified file.
FineTuning
Resource
create job
Creates a job that fine-tunes a specified model from a given dataset.
list jobs
List your organization's fine-tuning jobs.
You can pass additional parameters to the listJobs
method to narrow down the results.
retrieve job
Get info about a fine-tuning job.
cancel job
Immediately cancel a fine-tune job.
list job events
Get status updates for a fine-tuning job.
You can pass additional parameters to the listJobEvents
method to narrow down the results.
FineTunes
Resource (deprecated)
create
Creates a job that fine-tunes a specified model from a given dataset.
list
List your organization's fine-tuning jobs.
retrieve
Gets info about the fine-tune job.
cancel
Immediately cancel a fine-tune job.
list events
Get fine-grained status updates for a fine-tune job.
list events streamed
Get streamed fine-grained status updates for a fine-tune job.
Moderations
Resource
create
Classifies if text violates OpenAI's Content Policy.
Images
Resource
create
Creates an image given a prompt.
edit
Creates an edited or extended image given an original image and a prompt.
variation
Creates a variation of a given image.
Assistants
Resource
Note: If you are creating the client manually from the factory. Make sure you provide the necessary header:
create
Create an assistant with a model and instructions.
retrieve
Retrieves an assistant.
modify
Modifies an assistant.
delete
Delete an assistant.
list
Returns a list of assistants.
Threads
Resource
create
Create a thread.
createAndRun
Create a thread and run it in one request.
retrieve
Retrieves a thread.
modify
Modifies a thread.
delete
Delete a thread.
Threads Messages
Resource
create
Create a message.
retrieve
Retrieve a message.
modify
Modifies a message.
delete
Deletes a message.
list
Returns a list of messages for a given thread.
Threads Runs
Resource
create
Create a run.
create streamed
Creates a streamed run.
create streamed with function calls
Creates a streamed run with function calls
retrieve
Retrieves a run.
modify
Modifies a run.
cancel
Cancels a run that is in_progress
.
submitToolOutputs
When a run has the status: requires_action
and required_action.type
is submit_tool_outputs
, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.
list
Returns a list of runs belonging to a thread.
Threads Runs Steps
Resource
retrieve
Retrieves a run step.
list
Returns a list of run steps belonging to a run.
Batches
Resource
create
Creates a batch.
retrieve
Retrieves a batch.
cancel
Cancels a batch.
list
Returns a list of batches.
Vector Stores
Resource
create
Create a vector store.
retrieve
Retrieves a vector store.
modify
Modifies a vector store.
delete
Delete a vector store.
list
Returns a list of vector stores.
Vector Store Files
Resource
create
Create a vector store file by attaching a File to a vector store.
retrieve
Retrieves a vector store file.
delete
Delete a vector store file. This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the delete file endpoint.
list
Returns a list of vector store files.
Vector Store File Batches
Resource
create
Create a vector store file batch.
retrieve
Retrieves a vector store file batch.
cancel
Cancel a vector store file batch. This attempts to cancel the processing of files in this batch as soon as possible.
list
Returns a list of vector store files.
Edits
Resource (deprecated)
OpenAI has deprecated the Edits API and will stop working by January 4, 2024. https://openai.com/blog/gpt-4-api-general-availability#deprecation-of-the-edits-api
create
Creates a new edit for the provided input, instruction, and parameters.
Meta Information
On all response objects you can access the meta information returned by the API via the meta()
method.
The toArray()
method returns the meta information in the form originally returned by the API.
On streaming responses you can access the meta information on the reponse stream object.
For further details about the rates limits and what to do if you hit them visit the OpenAI documentation.
Troubleshooting
Timeout
You may run into a timeout when sending requests to the API. The default timeout depends on the HTTP client used.
You can increase the timeout by configuring the HTTP client and passing in to the factory.
This example illustrates how to increase the timeout using Guzzle.
Testing
The package provides a fake implementation of the OpenAI\Client
class that allows you to fake the API responses.
To test your code ensure you swap the OpenAI\Client
class with the OpenAI\Testing\ClientFake
class in your test case.
The fake responses are returned in the order they are provided while creating the fake client.
All responses are having a fake()
method that allows you to easily create a response object by only providing the parameters relevant for your test case.
In case of a streamed response you can optionally provide a resource holding the fake response data.
After the requests have been sent there are various methods to ensure that the expected requests were sent:
To write tests expecting the API request to fail you can provide a Throwable
object as the response.
Services
Azure
In order to use the Azure OpenAI Service, it is necessary to construct the client manually using the factory.
To use Azure, you must deploy a model, identified by the {deployment-id}, which is already incorporated into the API calls. As a result, you do not have to provide the model during the calls since it is included in the BaseUri
.
Therefore, a basic sample completion call would be:
OpenAI PHP is an open-sourced software licensed under the MIT license.
All versions of opanai-php-client with dependencies
php-http/discovery Version ^1.19.4
php-http/multipart-stream-builder Version ^1.3.0
psr/http-client Version ^1.0.3
psr/http-client-implementation Version ^1.0.1
psr/http-factory-implementation Version *
psr/http-message Version ^1.1.0|^2.0.0