PHPackages                             mathsgod/openai-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mathsgod/openai-client

ActiveLibrary[API Development](/categories/api)

mathsgod/openai-client
======================

A PHP client for OpenAI's API

2.3.0(9mo ago)0773MITPHP

Since Jun 13Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/mathsgod/openai-client)[ Packagist](https://packagist.org/packages/mathsgod/openai-client)[ RSS](/packages/mathsgod-openai-client/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (24)Used By (3)

openai-client
=============

[](#openai-client)

A very simple client for the OpenAI API.

Installation
------------

[](#installation)

```
composer require mathsgod/openai-client
```

Usage
-----

[](#usage)

Create a client instance with your API key.

```
use OpenAI\Client;
$client=new Client("OPEN_API_KEY");
```

### Responses

[](#responses)

```
$tools = [
    [
        "type" => "function",
        "name" => "get_iphone_price",
        "description" => "Get the current price of an iPhone model.",
        "parameters" => [
            "type" => "object",
            "properties" => [
                "model" => [
                    "type" => "string",
                    "description" => "The iPhone model, e.g. iPhone 14 Pro Max"
                ]
            ],
            "required" => ["model"],
            "additional_properties" => false
        ]
    ],
    [
        "type" => "function",
        "name" => "get_iphone_release_date",
        "description" => "Get the release date of an iPhone model.",
        "parameters" => [
            "type" => "object",
            "properties" => [
                "model" => [
                    "type" => "string",
                    "description" => "The iPhone model, e.g. iPhone 14 Pro Max"
                ]
            ],
            "required" => ["model"],
            "additional_properties" => false
        ]
    ]
];

$data = $client->responses->create([
    "model" => "gpt-4o-mini",
    "input" => "What is the price and release date of iPhone 14 Pro Max?",
    "tools" => $tools,
]);

$input = [];

foreach ($data["output"] as $output) {
    if ($output["name"] == "get_iphone_price") {
        $input[] = [
            "type" => "function_call_output",
            "call_id" => $output["call_id"],
            "output" => json_encode([
                "model" => "iPhone 14 Pro Max",
                "price" => "$1099"
            ]),
        ];
    }

    if ($output["name"] == "get_iphone_release_date") {
        $input[] = [
            "type" => "function_call_output",
            "call_id" => $output["call_id"],
            "output" => json_encode([
                "model" => "iPhone 14 Pro Max",
                "release_date" => "September 16, 2022"
            ]),
        ];
    }
}

$data = $client->responses->create([
    "model" => "gpt-4o-mini",
    "input" => $input,
    "tools" => $tools,
    "previous_response_id" => $data["id"],
]);

print_r($data);
```

### Responses Streaming

[](#responses-streaming)

```
$tool = [
    "type" => "function",
    "name" => "get_weather",
    "description" => "Get current temperature for a given location.",
    "parameters" => [
        "type" => "object",
        "properties" => [
            "location" => [
                "type" => "string",
                "description" => "The city and state, e.g. San Francisco, CA"
            ]
        ],
        "required" => ["location"],
        "additional_properties" => false
    ]
];

$stream = $client->responses->createStream([
    "model" => "gpt-4o-mini",
    "input" => "What is the weather like in Paris today?",
    "tools" => [$tool],
]);

$stream->onCompleted(function ($data) use ($client, $tool) {
    $input = [];
    $input[] = [
        "type" => "function_call_output",
        "call_id" => $data["response"]["output"][0]["call_id"],
        "output" => json_encode("Paris is currently 20 degrees Celsius with clear skies."),
    ];

    $s2 = $client->responses->createStream([
        "model" => "gpt-4o-mini",
        "input" => $input,
        "tools" => [$tool],
        "previous_response_id" => $data["response"]["id"],
    ]);

    $s2->onOutputTextDelta(function ($data) {
        echo "output_text.delta: " . $data["delta"] . "\n";
    });

    $s2->onCompleted(function ($data) {
        echo "completed\n";
        print_r($data);
    });

});
```

### Chat completion

[](#chat-completion)

```
$data = $client->chatCompletions->create([
    "model" => "gpt-4o-mini",
    "messages" => [
        ["role" => "user", "content" => "Hi"]
    ]
]);

print_r($data);
```

```
Array
(
    [id] => chatcmpl-1234
    [object] => chat.completion
    [created] => 1722324090
    [model] => gpt-4o-mini-2024-07-18
    [choices] => Array
        (
            [0] => Array
                (
                    [index] => 0
                    [message] => Array
                        (
                            [role] => assistant
                            [content] => Hello! How can I assist you today?
                        )

                    [logprobs] =>
                    [finish_reason] => stop
                )

        )

    [usage] => Array
        (
            [prompt_tokens] => 8
            [completion_tokens] => 9
            [total_tokens] => 17
        )

    [system_fingerprint] => fp_1234
)

```

#### Function call

[](#function-call)

```
$data=$client->chatCompletions->create([
   "model" => "gpt-4o-mini",
    "messages" => [
      ["role" => "user", "content" => "What is the price of iphone14?"]
    ],
    "functions" =>[
        [
            "name" => "get_iphone_price",
            "description" => "Get the price of iphone",
            "parameters" => [
                "type" => "object",
                "properties" => [
                    "model" => [
                        "type" => "string",
                        "description" => "The model of the iphone"
                    ]
                ],
                "required" => ["model"]
            ],
        ]
    ]
]);

print_r($data);
```

```
Array
(
    [id] => chatcmpl-1234
    [object] => chat.completion
    [created] => 1722324296
    [model] => gpt-4o-mini-2024-07-18
    [choices] => Array
        (
            [0] => Array
                (
                    [index] => 0
                    [message] => Array
                        (
                            [role] => assistant
                            [content] =>
                            [function_call] => Array
                                (
                                    [name] => get_iphone_price
                                    [arguments] => {"model":"iPhone 14"}
                                )

                        )

                    [logprobs] =>
                    [finish_reason] => function_call
                )

        )

    [usage] => Array
        (
            [prompt_tokens] => 60
            [completion_tokens] => 19
            [total_tokens] => 79
        )

    [system_fingerprint] => fp_1234
)

```

### Images

[](#images)

#### Create image

[](#create-image)

```
print_r($client->images()->generations([
    "model" => "dall-e-3",
    "prompt" => "a white siamese cat",
    "n" => 1,
    "size" => "1024x1024"
]));
```

### Embeddings

[](#embeddings)

```
print_r($client->embeddings->create([
    "model" => "text-embedding-3-small",
    "input"=>"I feel great",
]));
```

### Audio

[](#audio)

#### Speech

[](#speech)

```
print_r($client->audio->speech([
    "model"=>"tts-1",
    "input"=>"Hello, how are you?",
    "voice"=>"alloy"
]));
```

#### Transcriptions

[](#transcriptions)

```
print_r($client->audio->transcriptions([
    "model"=>"whisper-1",
    "file"=>fopen('/path/to/audio.mp3', 'r')
]));
```

#### Translation

[](#translation)

```
print_r($client->audio->translation([
    "model"=>"whisper-1",
    "file"=>fopen('/path/to/audio.mp3', 'r')
]));
```

Assistants
----------

[](#assistants)

### Create

[](#create)

```
$client->assistants->create([
    "model" => "gpt-4o-mini",
]);
```

### List

[](#list)

```
$client->assistants->list();
```

### Retrieve

[](#retrieve)

```
$client->assistants->retrieve("asst_1234");
```

### Delete

[](#delete)

```
$client->assistants->delete("asst_1234");
```

Threads
-------

[](#threads)

### Create

[](#create-1)

```
$client->threads->create(); //return Thread object
```

Messages
--------

[](#messages)

### Create

[](#create-2)

```
$client->thread("thread_1234")->messages()->create([
    "role" => "user",
    "content" => "Hello"
]);
```

#### Create with stream

[](#create-with-stream)

```
$stream = $thread->runs()->createStream([
    "assistant_id" => "asst_1234",
]);

$stream->on("thread.message.delta", function ($data) {
    echo $data;
    echo "\n";
});

$stream->on("thread.message.completed", function ($data) {
    echo $data;
    echo "\n";
});

$stream->on("done", function () use (&$thread) {
    echo "End\n";
});
```

Example
-------

[](#example)

```
$thread = $client->threads->create([
    "messages" => [
        [
            "role" => "user",
            "content" => "Hi"
        ]
    ]
]);

$data=$thread->runs->create([
    "assistant_id" => "asst_1234", //assistant_id
]);

print_r($data);
```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance57

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~35 days

Recently: every ~7 days

Total

23

Last Release

283d ago

Major Versions

1.3.0 → 2.0.02024-07-30

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/18732337?v=4)[Raymond](/maintainers/mathsgod)[@mathsgod](https://github.com/mathsgod)

---

Top Contributors

[![mathsgod](https://avatars.githubusercontent.com/u/18732337?v=4)](https://github.com/mathsgod "mathsgod (81 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mathsgod-openai-client/health.svg)

```
[![Health](https://phpackages.com/badges/mathsgod-openai-client/health.svg)](https://phpackages.com/packages/mathsgod-openai-client)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[theodo-group/llphant

LLPhant is a library to help you build Generative AI applications.

1.5k311.5k5](/packages/theodo-group-llphant)[wheelpros/fitment-platform-api

Magento 2 (Open Source)

12.1k1.2k](/packages/wheelpros-fitment-platform-api)[avalara/avataxclient

Client library for Avalara's AvaTax suite of business tax calculation and processing services. Uses the REST v2 API.

517.9M7](/packages/avalara-avataxclient)[alexacrm/dynamics-webapi-toolkit

Web API toolkit for Microsoft Dynamics 365 and Dynamics CRM

81324.1k1](/packages/alexacrm-dynamics-webapi-toolkit)[keboola/storage-api-client

Keboola Storage API PHP Client

10387.5k25](/packages/keboola-storage-api-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
