/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/models/ModelAI.php
234 строки
7 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\models; use api\components\ApiException; use common\helpers\DateHelper; use common\helpers\UuidHelper; use common\interfaces\TitleInterface; use common\kdf\KModelStatus; use common\traits\TitleRecordTrait; use common\validators\UidValidator; use Yii; use yii\helpers\ArrayHelper; use yii\helpers\VarDumper; /** * Таблица - модели GPT * * @property string $title Заголовок * @property int $token_limit Размер модели * @property string $guid GUID * @property string $model_id ModelID * @property int $owner_id Owner * @property string $created Время создания * @property string $description * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2024 */ class ModelAI extends \common\models\base\BaseModel implements TitleInterface { use TitleRecordTrait; /** * {@inheritdoc} */ public static function tableName() { return 'model'; } /** * {@inheritdoc} */ public function rules() { return ArrayHelper::merge(parent::rules(), [ ['guid', 'default', 'value' => UuidHelper::uuid()], ['token_limit', 'default', 'value' => 8192], [['model_id', 'title'], 'trim'], [['token_limit', 'owner_id'], 'integer'], [['model_id', 'title', 'guid', 'token_limit'], 'required'], [['model_id', 'title'], 'string', 'max' => 255], [['description'], 'string', 'max' => 2000], ['guid', UidValidator::class], [ ['created'], 'filter', 'filter' => function ($value) { return DateHelper::parse($value, true); } ], [['guid', 'model_id'], 'unique'] ]); } /** * {@inheritdoc} */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ 'title' => Yii::t('app', 'Title'), 'owner_id' => Yii::t('app', 'Owner'), 'token_limit' => Yii::t('app', 'Token limit'), 'created' => Yii::t('app', 'Created Datetime'), ]); } /** * {@inheritdoc} */ public function fields() { return [ 'guid', 'title' ]; } /** * {@inheritdoc} */ public function beforeDelete() { LinkProviderModel::deleteAll(['model_id' => $this->id]); return parent::beforeDelete(); // TODO: Change the autogenerated stub } /** * Получить идентификатор провайдера * @return false|int|string|null * @throws \yii\base\InvalidConfigException */ public function getProviderId() { return LinkProviderModel::find() ->select('provider_id') ->andWhere([ 'model_id' => $this->id, ]) ->scalar(); } /** * @inheritdoc */ public function getProvider() { return $this->hasOne(Provider::class, ['id' => 'provider_id']) ->viaTable(LinkProviderModel::tableName(), ['model_id' => 'id']); } /** * Поиск или создание модели * * @param $data * @return base\BaseModel|ModelAI|object * @throws ApiException * @throws \yii\base\InvalidConfigException * @throws \yii\db\Exception */ public static function findOrCreate($data, $options = []) { $id = ArrayHelper::getValue($data, 'id'); $title = ArrayHelper::getValue($data, 'display_name', $id); $title = ArrayHelper::getValue($data, 'name', $title); $ownedBy = ArrayHelper::getValue($data, 'owned_by'); $ownedBy = ArrayHelper::getValue($data, 'ownedBy', $ownedBy); $provider_id = ArrayHelper::getValue($options, 'provider_id'); $project_id = ArrayHelper::getValue($options, 'project_id'); $owner_id = null; $created_at = ArrayHelper::getValue($data, 'created_at'); $created_at = ArrayHelper::getValue($data, 'modified_at', $created_at); $created = ArrayHelper::getValue($data, 'created', $created_at); if ($ownedBy) { $owner = Owner::findOrNew([ 'key' => $ownedBy ]); $owner->setAttributes([ 'title' => $ownedBy ]); if (!$owner->save()) { $exception = new ApiException(422, 'Error save Owner'); $exception->errors = $owner->errors; throw $exception; } $owner_id = $owner->id; } if ($created) { $created = DateHelper::parse($created, false); } $m = ModelAI::findOrNew([ 'model_id' => $id ?: $title ]); $m->setAttributes([ 'title' => $title, 'owner_id' => $owner_id, 'created' => $created, 'rec_status' => KModelStatus::PUBLISHED ]); if ($description = ArrayHelper::getValue($data, 'description')) { $m->description = $description; } $token_limit = ArrayHelper::getValue($data, 'inputTokenLimit'); if ($token_limit = ArrayHelper::getValue($data, 'context_length', $token_limit)) { $m->token_limit = $token_limit; } if (!$m->save()) { $exception = new ApiException(422, 'Error save Model'); $exception->errors = $m->errors; Yii::debug(VarDumper::dumpAsString([ 'errors' => $m->errors ])); throw $exception; } if ($project_id) { $link = LinkProjectModel::findOrNew([ 'model_id' => $m->id ]); $link->setAttributes([ 'project_id' => $project_id ]); if (!$link->save()) { $exception = new ApiException(422, 'Error save Model Link Project'); $exception->errors = $link->errors; throw $exception; } } if ($provider_id) { $link = LinkProviderModel::findOrNew([ 'model_id' => $m->id ]); $link->setAttributes([ 'provider_id' => $provider_id ]); if (!$link->save()) { $exception = new ApiException(422, 'Error save Model Link Provider'); $exception->errors = $link->errors; throw $exception; } } return $m; } /** * Модель GPT-5 ? * @return bool */ public function isGpt5() { return str_starts_with($this->title, 'gpt-5'); } }