/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
backend/forms/gpt/ProjectApiForm.php
129 строк
3 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace backend\forms\gpt; use common\components\OpenAIComponent; use common\forms\base\ObjectIdForm; use common\models\ModelAI; use common\models\ProjectApi; use common\models\Provider; use OpenAI\Client; use Yii; use yii\helpers\ArrayHelper; use yii\helpers\VarDumper; /** * Форма добавления ассистента * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2025 */ class ProjectApiForm extends ObjectIdForm { /** * api_key * @var null */ public $api_key = null; /** * @inheritdoc */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ 'api_key' => Yii::t('app', 'Api Key'), ]); } /** * @inheritdoc */ public function rules() { return ArrayHelper::merge(parent::rules(), [ [['api_key'], 'string'], [['api_key'], 'required'], ]); } /** * @inheritdoc */ public function loadModel($condition) { parent::loadModel($condition); $a = ProjectApi::getAlias(); $obj = ProjectApi::find() ->alias($a) ->andWhere([ $a . '.project_id' => $this->oid, ]) ->one(); if ($obj) { $this->api_key = $obj->api_key; $this->initState(null); } } /** * @inheritdoc */ public function getPublicFields() { return ArrayHelper::merge(parent::getPublicFields(), [ 'api_key', ]); } /** * @inheritdoc */ protected function doSave() { try { $openAI = new OpenAIComponent([ 'api_key' => $this->api_key ]); /** @var Client $client */ $client = $openAI->getClient(); $response = $client->models()->list(); foreach ($response->data as $result) { Yii::debug(VarDumper::dumpAsString([ 'model' => $result ])); ModelAI::findOrCreate($result, [ 'provider_id' => Provider::GPT, 'project_id' => $this->oid, ]); } $p_api = ProjectApi::findOrNew([ 'project_id' => $this->oid ]); $p_api->setAttributes([ 'api_key' => $this->api_key ]); if (!$p_api->save()) { $this->addErrors($p_api->errors); return false; } $this->model = $p_api; return true; } catch (\Exception $exception) { $this->addError('summary', $exception->getMessage()); return false; } } }