/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
backend/forms/gpt/VectorStoreForm.php
131 строка
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\Project; use common\models\VectorStore; use OpenAI\Client; use Yii; use yii\helpers\ArrayHelper; /** * Форма добавления Vector Store * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2025 */ class VectorStoreForm extends ObjectIdForm { /** * Name * @var null */ public $name = null; /** * Проект * @var null */ public $project_id = null; /** * @inheritdoc */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ 'name' => Yii::t('app', 'Name'), 'project_id' => Yii::t('app', 'Project'), ]); } /** * @inheritdoc */ public function rules() { return ArrayHelper::merge(parent::rules(), [ ['name', 'string'], ['project_id', 'number'], [['project_id', 'name'], 'required'], ]); } /** * @inheritdoc */ public function loadModel($condition) { parent::loadModel($condition); if ($project_id = Yii::$app->request->get('project_id')) { $this->project_id = $project_id; } elseif ($project_id = ArrayHelper::getValue($condition, 'project_id')) { $this->project_id = $project_id; } $a = VectorStore::getAlias(); $obj = VectorStore::find() ->alias($a) ->andWhere([ $a . '.id' => $this->oid, ]) ->one(); if ($obj) { $this->initState($obj); } } /** * @inheritdoc */ public function getPublicFields() { return ArrayHelper::merge(parent::getPublicFields(), [ 'name', 'project_id' ]); } /** * @inheritdoc */ protected function doSave() { $p = Project::findOne($this->project_id); $openAI = Yii::createObject([ 'class' => OpenAIComponent::class, 'project_id' => $p->key ]); /** @var Client $client */ $client = $openAI->getClient(); $vs = VectorStore::findOrNew($this->oid); $response = $client->vectorStores()->create([ 'name' => $this->name, ]); $vs->setAttributes([ 'project_id' => $this->project_id, 'key' => $response->id, 'name' => $this->name, ]); if (!$vs->save()) { $this->addErrors($vs->errors); return false; } $this->model = $vs; return true; } }