eseperio / yii2-openai-responses
Yii2 component wrapper around openai-php client responses API.
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:yii2-extension
pkg:composer/eseperio/yii2-openai-responses
Requires
- php: >=8.1
- openai-php/client: ^0.15
- yiisoft/yii2: ^2.0.49
Requires (Dev)
- codeception/codeception: ^5.0
- codeception/module-asserts: ^3.0
- codeception/module-yii2: ^2.0
- yiisoft/yii2-codeception: ^2.0
This package is auto-updated.
Last update: 2025-10-02 11:36:17 UTC
README
Easily integrate AI features into any Yii2 application. This library provides a drop‑in component to interact with OpenAI's Responses API.
Wrapper for the openai-php/client focused on the Responses API.
Installation
composer require eseperio/yii2-openai-responses
Configuration
Register the component in the application configuration:
return [ 'components' => [ 'openai' => [ 'class' => eseperio\openai\responses\OpenAiComponent::class, 'apiKey' => 'YOUR_API_KEY', 'model' => eseperio\openai\responses\enums\OpenAiModel::GPT_4_1_MINI, // optional defaults 'instructions' => 'Always answer politely.', 'instructionsPolicy' => eseperio\openai\responses\OpenAiComponent::INSTRUCTIONS_COMPLEMENTARY, ], ], ];
apiKey and instructions are write-only properties to avoid exposing sensitive values.
Available OpenAI models are exposed through the OpenAiModel enum for convenient access.
Usage
Basic request
$content = Yii::$app->openai->ask('Explain gravity.');
With instructions and metadata
$content = Yii::$app->openai->ask( 'Explain gravity.', 'Provide examples', ['topic' => 'physics'] );
Handling instructions
OpenAiComponent supports a policy for default instructions:
- required – user instructions are ignored. If the caller provides instructions, an exception is thrown.
- complementary – user instructions are appended to the default ones.
- optional – user instructions override the defaults.
Creating custom requests
To override default configuration for a single call, create an AskRequest model:
$request = Yii::$app->openai->createAskRequest(); $request->model = eseperio\\openai\\responses\\enums\\OpenAiModel::GPT_4_1; $request->input = 'Explain gravity.'; $request->instructions = 'Use simple terms.'; $content = Yii::$app->openai->ask($request);
The model is validated before sending the request. If validation fails an exception is thrown.
Retrieve last response
$response = Yii::$app->openai->getLastResponse();
The component returns only the text content of the first item in the response. The full response object can be obtained via getLastResponse().
Tests
Functional tests are included. Run them with:
vendor/bin/codecept run functional
License
MIT