⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
<p align="center">
<a href="https://github.com/openai-php/client/actions"></a>
<a href="https://packagist.org/packages/openai-php/client"></a>
<a href="https://packagist.org/packages/openai-php/client"></a>
<a href="https://packagist.org/packages/openai-php/client"></a>
</p>
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:
Requires PHP 8.2+
First, install OpenAI via the Composer package manager:
composer require openai-php/clientEnsure 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.
composer require guzzlehttp/guzzleThen, interact with OpenAI's API:
$yourApiKey = getenv('YOUR_API_KEY');
$client = OpenAI::client($yourApiKey);
$response = $client->responses()->create([
'model' => 'gpt-4o',
'input' => 'Hello!',
]);
echo $response->outputText; // Hello! How can I assist you today?If necessary, it is possible to configure and create a separate client.
…Models ResourcelistLists the currently available models, and provides basic information about each one such as the owner and availability.
$response = $client->models()->list();
$response->object; // 'list'
foreach ($response->data as $result) {
$result->id; // 'gpt-3.5-turbo-instruct'
$result->object; // 'model'
// ...
}
$response->toArray(); // ['object' => 'list', 'data' => [...]]retrieveRetrieves a model instance, providing basic information about the model such as the owner and permissioning.
$response = $client->models()->retrieve('gpt-3.5-turbo-instruct');
$response->id; // 'gpt-3.5-turbo-instruct'
$response->object; // 'model'
$response->created; // 1642018370
$response->ownedBy; // 'openai'
$response->toArray(); // ['id' => 'gpt-3.5-turbo-instruct', ...]deleteDelete a fine-tuned model.
$response = $client->models()->delete('curie:ft-acmeco-2021-03-03-21-44-20');
$response->id; // 'curie:ft-acmeco-2021-03-03-21-44-20'
$response->object; // 'model'
$response->deleted; // true
$response->toArray(); // ['id' => 'curie:ft-acmeco-2021-03-03-21-44-20', ...]Responses ResourcecreateCreates a model response. Provide text or image inputs to generate text or JSON outputs. Have the model call your own custom code or use built-in tools like web search or file search to use your own data as input for the model's response.
…Create a model response with a function tool.
…create streamedWhen you create a Response with stream set to true, the server will emit server-sent events to the client as the Response is generated. All events and their payloads can be found in OpenAI docs.
$stream = $client->responses()->createStreamed([
'model' => 'gpt-4o-mini',
'tools' => [
[
'type' => 'web_search_preview'
]
],
'input' => "what was a positive news story from today?",
]);
foreach ($stream as $response) {
$response->event; // 'response.created'
}retrieveRetrieves a model response with the given ID.
…retrieve streamedWhen you retrieve a Response with stream set to true, the server will emit server-sent events to the client as the Response is generated. All events and their payloads can be found in OpenAI docs.
$stream = $client->responses()->retrieveStreamed('resp_67ccd2bed1ec8190b14f964abc054267', [
'starting_after' => '2',
]);
foreach ($stream as $response) {
$response->event; // 'response.created'
}cancelCancel a model response (background request) with the given ID.
$response = $client->responses()->cancel('resp_67ccd2bed1ec8190b14f964abc054267');
$response->id; // 'resp_67ccd2bed1ec8190b14f964abc054267'
$response->status; // 'canceled'
$response->toArray(); // ['id' => 'resp_67ccd2bed1ec8190b14f964abc054267', 'status' => 'canceled', ...]deleteDeletes a model response with the given ID.
$response = $client->responses()->delete('resp_67ccd2bed1ec8190b14f964abc054267');
$response->id; // 'resp_67ccd2bed1ec8190b14f964abc054267'
$response->object; // 'response'
$response->deleted; // true
$response->toArray(); // ['id' => 'resp_67ccd2bed1ec8190b14f964abc054267', 'deleted' => true, ...]listLists input items for a response with the given ID. All events and their payloads can be found in OpenAI docs.
…Conversations ResourcecreateCreate a conversation.
$response = $client->conversations()->create([
'metadata' => ['topic' => 'demo'],
'items' => [
[
'type' => 'message',
'role' => 'user',
'content' => 'Hello!'
],
],
]);
$response->id; // 'conv_123'
$response->object; // 'conversation'
$response->createdAt; // 1741900000
$response->metadata; // ['topic' => 'demo']
$response->toArray(); // ['id' => 'conv_123', 'object' => 'conversation', ...]retrieveRetrieve a conversation by ID.
$response = $client->conversations()->retrieve('conv_123');
$response->id; // 'conv_123'
$response->object; // 'conversation'
$response->createdAt; // 1741900000
$response->toArray(); // ['id' => 'conv_123', 'object' => 'conversation', ...]updateUpdate a conversation by ID.
$response = $client->conversations()->update('conv_123', [
'metadata' => ['foo' => 'bar'],
]);
$response->id; // 'conv_123'
$response->metadata; // ['foo' => 'bar']deleteDelete a conversation by ID.
$response = $client->conversations()->delete('conv_123');
$response->id; // 'conv_123'
$response->object; // 'conversation.deleted'
$response->deleted; // true
$response->toArray(); // ['id' => 'conv_123', 'object' => 'conversation.deleted', 'deleted' => true]Conversations Items ResourcecreateCreate items for a conversation.
$response = $client->conversations()->items()->create('conv_123', [
'items' => [
[
'role' => 'system',
'content' => 'Refer to me as PHPBot.',
],
],
]);
foreach ($response->data as $listItem) {
$listItem->item; // The created item (e.g., message)
}
$response->firstId; // 'msg_abc'
$response->lastId; // 'msg_abc'
$response->hasMore; // false
$response->toArray(); // ['object' => 'list', 'data' => [...], ...]listList items for a conversation.
$response = $client->conversations()->items()->list('conv_123', [
'limit' => 10,
]);
$response->object; // 'list'retrieveRetrieve a specific item from a conversation.
$response = $client->conversations()->items()->retrieve('conv_123', 'msg_abc', [
'include' => ['step_details'],
]);
$response->id; // 'msg_abc'
$response->type; // 'message'
$response->status; // 'completed'deleteDelete a specific item from a conversation. Returns the updated conversation.
$response = $client->conversations()->items()->delete('conv_123', 'msg_abc');
$response->id; // 'conv_123'
$response->object; // 'conversation'Containers ResourcecreateCreates a container for use with the Code Interpreter tool.
$response = $client->containers()->create([
'name' => 'My Container',
'expires_after' => [
'anchor' => 'last_active_at',
'minutes' => 60,
],
]);
$response->id; // 'container_abc123'
$response->object; // 'container'
$response->createdAt; // 1690000000
$response->status; // 'active'
$response->expiresAfter->anchor; // 'last_active_at'
$response->expiresAfter->minutes; // 60
$response->lastActiveAt; // 1690001000
$response->name; // 'My Container'
$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', ...]listReturns a list of containers.
…retrieveRetrieves a container with the given ID.
$response = $client->containers()->retrieve('container_abc123');
$response->id; // 'container_abc123'
$response->object; // 'container'
$response->createdAt; // 1690000000
$response->status; // 'active'
$response->expiresAfter->anchor; // 'last_active_at'
$response->expiresAfter->minutes; // 60
$response->lastActiveAt; // 1690001000
$response->name; // 'Test Container'
$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', ...]deleteDelete a container with the given ID.
$response = $client->containers()->delete('container_abc123');
$response->id; // 'container_abc123'
$response->object; // 'container'
$response->deleted; // true
$response->toArray(); // ['id' => 'container_abc123', 'object' => 'container', 'deletNo open issues yet, or sync has not completed.