https://github.com/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
https://github.com/sourceability/openai-client
Last synced: 12 months ago
JSON representation
PHP 8.0+ OpenAI API client with fully typed/documented requests+responses models, guzzle and symfony/http-client support and async/parallel requests
- Host: GitHub
- URL: https://github.com/sourceability/openai-client
- Owner: sourceability
- License: mit
- Created: 2023-02-16T23:12:59.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-09T15:14:43.000Z (about 2 years ago)
- Last Synced: 2025-03-25T06:10:00.852Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 416 KB
- Stars: 18
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sourceability/openai-client
PHP 8.0+ [OpenAI API][openai_api] client with fully typed/documented requests+responses models,
`guzzlehttp/guzzle` + `symfony/http-client` support through [HTTPPug][httplug], and async/parallel requests.
The client is generated using [openai's OpenAPI][openai_openapi] with [jane-php][janephp].
Features:
- The requests models are typed and include descriptions from the OpenAPI documentation.
- Uses [HTTPPug][httplug] as the HTTP Abstraction
- [Many supported http clients like `guzzlehttp/guzzle` or `symfony/http-client`][httplug_adapters]
- [A lot of useful plugins like Cache or Retry][httplug_plugins]
- [Symfony Bundle][httplug_sf_bundle]
- Async/parallel requests.
This is a community-maintained/unofficial library.
## Installation
```
composer require sourceability/openai-client
```
## Getting started
```php
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`:
```php
createChatCompletions($requests);
var_dump($completionResponses);
```
## Cost calculator
You can use `ResponseCostCalculator`, which relies on [brick/money][brick/money], to calculate the cost of a response:
```php
use Sourceability\OpenAIClient\Pricing\ResponseCostCalculator;
$responseCostCalculator = new ResponseCostCalculator();
$responseCost = $responseCostCalculator->calculate($myCompletionResponse);
var_dump([
'total' => $responseCost->getTotal()->formatTo('en_US'),
'prompt' => $responseCost->getPrompt()->formatTo('en_US'),
'completion' => $responseCost->getCompletion()->formatTo('en_US'),
]);
array(3) {
["total"]=>
string(10) "$0.0001280"
["prompt"]=>
string(10) "$0.0000980"
["completion"]=>
string(10) "$0.0000300"
}
```
[janephp]: https://github.com/janephp/janephp
[openai_api]: https://platform.openai.com/docs/
[openai_openapi]: https://github.com/openai/openai-openapi
[httplug]: http://httplug.io
[httplug_adapters]: https://docs.php-http.org/en/latest/clients.html
[httplug_plugins]: https://docs.php-http.org/en/latest/plugins/index.html
[httplug_sf_bundle]: https://docs.php-http.org/en/latest/integrations/symfony-bundle.html
[brick/money]: https://github.com/brick/money