base on ⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API <p align="center"> <p align="center"> <a href="https://github.com/openai-php/laravel/actions"><img alt="GitHub Workflow Status (master)" src="https://img.shields.io/github/actions/workflow/status/openai-php/laravel/tests.yml?branch=main&label=tests&style=round-square"></a> <a href="https://packagist.org/packages/openai-php/laravel"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/openai-php/laravel"></a> <a href="https://packagist.org/packages/openai-php/laravel"><img alt="Latest Version" src="https://img.shields.io/packagist/v/openai-php/laravel"></a> <a href="https://packagist.org/packages/openai-php/laravel"><img alt="License" src="https://img.shields.io/github/license/openai-php/laravel"></a> </p> </p> ------ **OpenAI PHP** for Laravel is a community-maintained PHP API client that allows you to interact with the [Open AI API](https://platform.openai.com/docs/api-reference/introduction). 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: - Nuno Maduro: **[github.com/sponsors/nunomaduro](https://github.com/sponsors/nunomaduro)** - Sandro Gehri: **[github.com/sponsors/gehrisandro](https://github.com/sponsors/gehrisandro)** > **Note:** This repository contains the integration code of the **OpenAI PHP** for Laravel. If you want to use the **OpenAI PHP** client in a framework-agnostic way, take a look at the [openai-php/client](https://github.com/openai-php/client) repository. ## Get Started > **Requires [PHP 8.2+](https://www.php.net/releases/)** First, install OpenAI via the [Composer](https://getcomposer.org/) package manager: ```bash composer require openai-php/laravel ``` Next, execute the install command: ```bash php artisan openai:install ``` This will create a `config/openai.php` configuration file in your project, which you can modify to your needs using environment variables. Blank environment variables for the OpenAI API key and organization id are already appended to your `.env` file. ```env OPENAI_API_KEY=sk-... OPENAI_ORGANIZATION=org-... ``` Finally, you may use the `OpenAI` facade to access the OpenAI API: ```php use OpenAI\Laravel\Facades\OpenAI; $response = OpenAI::responses()->create([ 'model' => 'gpt-5', 'input' => 'Hello!', ]); echo $response->outputText; // Hello! How can I assist you today? ``` ## Configuration Configuration is done via environment variables or directly in the configuration file (`config/openai.php`). ### OpenAI API Key and Organization Specify your OpenAI API Key and organization. This will be used to authenticate with the OpenAI API - you can find your API key and organization on your OpenAI dashboard, at https://openai.com. ```env OPENAI_API_KEY= OPENAI_ORGANIZATION= ``` ### OpenAI Project For implementations that require a project ID, you can specify the OpenAI project ID in your environment variables. ```env OPENAI_PROJECT=proj_... ``` ### OpenAI API Base URL The base URL for the OpenAI API. By default, this is set to `api.openai.com/v1`. ```env OPENAI_BASE_URL= ``` ### Request Timeout The timeout may be used to specify the maximum number of seconds to wait for a response. By default, the client will time out after 30 seconds. ```env OPENAI_REQUEST_TIMEOUT= ``` ## Usage For usage examples, take a look at the [openai-php/client](https://github.com/openai-php/client) repository. ## Testing The `OpenAI` facade comes with a `fake()` method that allows you to fake the API responses. The fake responses are returned in the order they are provided to the `fake()` method. All responses are having a `fake()` method that allows you to easily create a response object by only providing the parameters relevant for your test case. ```php use OpenAI\Laravel\Facades\OpenAI; use OpenAI\Responses\Responses\CreateResponse; OpenAI::fake([ CreateResponse::fake([ 'choices' => [ [ 'text' => 'awesome!', ], ], ]), ]); $response = OpenAI::responses()->create([ 'model' => 'gpt-5', 'input' => 'PHP is ', ]); expect($response->outputText)->toBe('awesome!'); ``` After the requests have been sent there are various methods to ensure that the expected requests were sent: ```php use OpenAI\Resources\Responses; // assert completion create request was sent OpenAI::assertSent(Responses::class, function (string $method, array $parameters): bool { return $method === 'create' && $parameters['model'] === 'gpt-5' && $parameters['prompt'] === 'PHP is '; }); ``` For more testing examples, take a look at the [openai-php/client](https://github.com/openai-php/client#testing) repository. --- OpenAI PHP for Laravel is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**. ", Assign "at most 3 tags" to the expected json: {"id":"3898","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"