PHPackages                             sourceability/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. [HTTP &amp; Networking](/categories/http)
4. /
5. sourceability/openai-client

ActiveLibrary[HTTP &amp; Networking](/categories/http)

sourceability/openai-client
===========================

PHP 8.0+ OpenAI API client with fully typed/documented requests+responses models, guzzle and symfony/http-client support and async/parallel requests.

0.3.6(2y ago)189.3k↓29.2%1[1 PRs](https://github.com/sourceability/openai-client/pulls)1MITPHPPHP &gt;8.0

Since Feb 17Pushed 2y agoCompare

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

READMEChangelogDependencies (14)Versions (17)Used By (1)

sourceability/openai-client
===========================

[](#sourceabilityopenai-client)

PHP 8.0+ [OpenAI API](https://platform.openai.com/docs/) client with fully typed/documented requests+responses models, `guzzlehttp/guzzle` + `symfony/http-client` support through [HTTPPug](http://httplug.io), and async/parallel requests.

The client is generated using [openai's OpenAPI](https://github.com/openai/openai-openapi) with [jane-php](https://github.com/janephp/janephp).

Features:

- The requests models are typed and include descriptions from the OpenAPI documentation.
- Uses [HTTPPug](http://httplug.io) as the HTTP Abstraction
    - [Many supported http clients like `guzzlehttp/guzzle` or `symfony/http-client`](https://docs.php-http.org/en/latest/clients.html)
    - [A lot of useful plugins like Cache or Retry](https://docs.php-http.org/en/latest/plugins/index.html)
    - [Symfony Bundle](https://docs.php-http.org/en/latest/integrations/symfony-bundle.html)
- Async/parallel requests.

This is a community-maintained/unofficial library.

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

[](#installation)

```
composer require sourceability/openai-client

```

Getting started
---------------

[](#getting-started)

```
require __DIR__ . '/vendor/autoload.php';

use Sourceability\OpenAIClient\Client;
use Sourceability\OpenAIClient\Generated\Model\CreateCompletionRequest;

$apiClient = Client::create(
    apiKey: getenv('OPENAI_API_KEY')
);

$requests = [
    (new CreateCompletionRequest())
        ->setModel('text-davinci-003')
        ->setTemperature(0)
        ->setMaxTokens(512)
        ->setPrompt('The jane php library is very useful because'),
    new CreateCompletionRequest(
        model: 'text-davinci-003',
        temperature: 0,
        maxTokens: 512,
        prompt: 'Symfony symfony symfony is like sourceability on a'
    ),
];
$completionResponses = $apiClient->createCompletions($requests);

var_dump($completionResponses);
```

ChatGPT with `/v1/chat/completions`:

```
