{"id":20284850,"url":"https://github.com/sourceability/openai-client","last_synced_at":"2025-04-11T08:38:22.535Z","repository":{"id":65933629,"uuid":"602776423","full_name":"sourceability/openai-client","owner":"sourceability","description":"PHP 8.0+ OpenAI API client with fully typed/documented requests+responses models, guzzle and symfony/http-client support and async/parallel requests","archived":false,"fork":false,"pushed_at":"2024-01-09T15:14:43.000Z","size":426,"stargazers_count":18,"open_issues_count":1,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-25T06:10:00.852Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sourceability.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-16T23:12:59.000Z","updated_at":"2024-10-26T22:09:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"17d51587-1a3c-461a-b222-3b3062fd8eb0","html_url":"https://github.com/sourceability/openai-client","commit_stats":{"total_commits":27,"total_committers":1,"mean_commits":27.0,"dds":0.0,"last_synced_commit":"4e20a0ff1ebbe860084ce9120718f2599bcc49d6"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceability%2Fopenai-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceability%2Fopenai-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceability%2Fopenai-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceability%2Fopenai-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sourceability","download_url":"https://codeload.github.com/sourceability/openai-client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248362116,"owners_count":21091054,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T14:22:24.742Z","updated_at":"2025-04-11T08:38:22.526Z","avatar_url":"https://github.com/sourceability.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sourceability/openai-client\n\nPHP 8.0+ [OpenAI API][openai_api] client with fully typed/documented requests+responses models,\n`guzzlehttp/guzzle` + `symfony/http-client` support through [HTTPPug][httplug], and async/parallel requests.\n\nThe client is generated using [openai's OpenAPI][openai_openapi] with [jane-php][janephp].\n\nFeatures:\n- The requests models are typed and include descriptions from the OpenAPI documentation.\n- Uses [HTTPPug][httplug] as the HTTP Abstraction\n  - [Many supported http clients like `guzzlehttp/guzzle` or `symfony/http-client`][httplug_adapters]\n  - [A lot of useful plugins like Cache or Retry][httplug_plugins]\n  - [Symfony Bundle][httplug_sf_bundle]\n- Async/parallel requests.\n\nThis is a community-maintained/unofficial library.\n\n## Installation\n\n```\ncomposer require sourceability/openai-client\n```\n\n## Getting started\n\n```php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Sourceability\\OpenAIClient\\Client;\nuse Sourceability\\OpenAIClient\\Generated\\Model\\CreateCompletionRequest;\n\n$apiClient = Client::create(\n    apiKey: getenv('OPENAI_API_KEY')\n);\n\n$requests = [\n    (new CreateCompletionRequest())\n        -\u003esetModel('text-davinci-003')\n        -\u003esetTemperature(0)\n        -\u003esetMaxTokens(512)\n        -\u003esetPrompt('The jane php library is very useful because'),\n    new CreateCompletionRequest(\n        model: 'text-davinci-003',\n        temperature: 0,\n        maxTokens: 512,\n        prompt: 'Symfony symfony symfony is like sourceability on a'\n    ),\n];\n$completionResponses = $apiClient-\u003ecreateCompletions($requests);\n\nvar_dump($completionResponses);\n```\n\nChatGPT with `/v1/chat/completions`:\n```php\n\u003c?php\n\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Sourceability\\OpenAIClient\\Client;\nuse Sourceability\\OpenAIClient\\Generated\\Model\\ChatCompletionRequestMessage;\nuse Sourceability\\OpenAIClient\\Generated\\Model\\CreateChatCompletionRequest;\n\n$apiClient = Client::create(\n    apiKey: getenv('OPENAI_API_KEY')\n);\n\n$requests = [\n    new CreateChatCompletionRequest(\n        model: 'gpt-3.5-turbo',\n        temperature: 0,\n        messages: [\n            new ChatCompletionRequestMessage(\n                role: 'user',\n                content: 'The jane php library is very useful because'\n            )\n        ],\n    ),\n    new CreateChatCompletionRequest(\n        model: 'gpt-3.5-turbo',\n        temperature: 0,\n        messages: [\n            new ChatCompletionRequestMessage(\n                role: 'user',\n                content: 'Symfony symfony symfony is like sourceability on a'\n            )\n        ],\n    ),\n];\n$completionResponses = $apiClient-\u003ecreateChatCompletions($requests);\n\nvar_dump($completionResponses);\n```\n\n## Cost calculator\n\nYou can use `ResponseCostCalculator`, which relies on [brick/money][brick/money], to calculate the cost of a response:\n\n```php\nuse Sourceability\\OpenAIClient\\Pricing\\ResponseCostCalculator;\n\n$responseCostCalculator = new ResponseCostCalculator();\n$responseCost = $responseCostCalculator-\u003ecalculate($myCompletionResponse);\n\nvar_dump([\n    'total' =\u003e $responseCost-\u003egetTotal()-\u003eformatTo('en_US'),\n    'prompt' =\u003e $responseCost-\u003egetPrompt()-\u003eformatTo('en_US'),\n    'completion' =\u003e $responseCost-\u003egetCompletion()-\u003eformatTo('en_US'),\n]);\n\narray(3) {\n  [\"total\"]=\u003e\n  string(10) \"$0.0001280\"\n  [\"prompt\"]=\u003e\n  string(10) \"$0.0000980\"\n  [\"completion\"]=\u003e\n  string(10) \"$0.0000300\"\n}\n```\n\n[janephp]: https://github.com/janephp/janephp\n[openai_api]: https://platform.openai.com/docs/\n[openai_openapi]: https://github.com/openai/openai-openapi\n[httplug]: http://httplug.io\n[httplug_adapters]: https://docs.php-http.org/en/latest/clients.html\n[httplug_plugins]: https://docs.php-http.org/en/latest/plugins/index.html\n[httplug_sf_bundle]: https://docs.php-http.org/en/latest/integrations/symfony-bundle.html\n[brick/money]: https://github.com/brick/money\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsourceability%2Fopenai-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsourceability%2Fopenai-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsourceability%2Fopenai-client/lists"}