/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
backend/forms/gpt/AssistantForm.php
258 строк
7 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\helpers\NumHelper; use common\models\Assistant; use common\models\AssistantVectorStore; use common\models\ModelAI; use common\models\Project; use common\models\VectorStore; use OpenAI\Client; use OpenAI\Responses\Assistants\AssistantResponseToolFileSearch; use OpenAI\Responses\Assistants\AssistantResponseToolResourceFileSearch; use OpenAI\Responses\Assistants\AssistantResponseToolResources; use Yii; use yii\helpers\ArrayHelper; use yii\helpers\VarDumper; /** * Форма добавления ассистента * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2025 */ class AssistantForm extends ObjectIdForm { /** * title * @var null */ public $title = null; /** * Проект * @var null */ public $project_id = null; /** * Модель * @var null */ public $model_id = null; /** * ТопП * @var array */ public $top_p = null; /** * промт * @var null */ public $instructions = null; /** * Температура * @var null */ public $temperature = null; /** * Векторная база данных * @var null */ public $vector_store_id = null; /** * @inheritdoc */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ 'title' => Yii::t('app', 'Assistant name'), 'model_id' => Yii::t('app', 'Model AI'), 'top_p' => Yii::t('app', 'topP'), 'vector_store_id' => Yii::t('app', 'Vector Store'), 'instructions' => Yii::t('app', 'Instructions'), 'temperature' => Yii::t('app', 'Temperature'), ]); } /** * @inheritdoc */ public function rules() { return ArrayHelper::merge(parent::rules(), [ [['title', 'project_id'], 'required'], [['title'], 'string'], [['model_id', 'instructions'], 'required'], [['instructions'], 'string'], [['model_id', 'project_id', 'vector_store_id'], 'integer'], [['temperature', 'top_p'], 'number'] ]); } /** * @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 = Assistant::getAlias(); $obj = Assistant::find() ->alias($a) ->andWhere([ $a . '.id' => $this->oid, ]) ->one(); if ($obj) { $vector_store_id = AssistantVectorStore::find() ->select('vector_store_id') ->andWhere(['assistant_id' => $obj->id]) ->scalar(); $this->vector_store_id = $vector_store_id; $this->initState($obj); } } /** * @inheritdoc */ public function getPublicFields() { return ArrayHelper::merge(parent::getPublicFields(), [ 'title', 'top_p', 'instructions', 'project_id', 'temperature', 'model_id' => function () { return $this->prepareSelectField(ModelAI::find(), 'model_id', 'title'); }, 'vector_store_id' => function () { return $this->prepareSelectField(VectorStore::find(), 'vector_store_id', 'title'); }, ]); } /** * @inheritdoc */ protected function doSave() { $p = Project::findOne($this->project_id); if (!$p) { $this->addError('summary', Yii::t('app', 'Project not found')); return false; } $m = ModelAI::findOne($this->model_id); if (!$m) { $this->addError('summary', Yii::t('app', 'Model not found')); return false; } $openAI = Yii::createObject([ 'class' => OpenAIComponent::class, 'project_id' => $p->key ]); /** @var Client $client */ $client = $openAI->getClient(); $data = [ 'temperature' => (float)NumHelper::toPoint($this->temperature), 'top_p' => (float)NumHelper::toPoint($this->top_p), 'model' => $m->title, 'name' => $this->title, 'instructions' => $this->instructions, ]; if ($this->vector_store_id) { $vs = VectorStore::findOne($this->vector_store_id); $data['tools'][] = AssistantResponseToolFileSearch::from(['type' => 'file_search'])->toArray(); $re = AssistantResponseToolResources::from([ 'file_search' => [ 'vector_store_ids' => [ $vs->key ] ] ]); $data['tool_resources'] = $re->toArray(); } Yii::debug(VarDumper::dumpAsString([ 'data' => $data ])); $a = Assistant::findOrNew($this->oid); try { if ($a->isNewRecord) { $response = $client->assistants()->create($data); $a->setAttributes([ 'key' => $response->id ]); } else { $response = $client->assistants()->modify($a->key, $data); } } catch (\Exception $e) { $this->addError('summary', $e->getMessage()); return false; } $a->setAttributes([ 'project_id' => $this->project_id, 'temperature' => $this->temperature, 'top_p' => $this->top_p, 'name' => $this->title, 'instructions' => $this->instructions, 'model_id' => $this->model_id ]); if (!$a->save()) { $this->addErrors($a->errors); return false; } AssistantVectorStore::deleteAll(['assistant_id' => $a->id]); if ($this->vector_store_id) { $link = new AssistantVectorStore([ 'vector_store_id' => $this->vector_store_id, 'assistant_id' => $a->id, ]); if (!$link->save()) { $this->addErrors($link->errors); return false; } } $this->model = $a; return true; } }