/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
backend/forms/gpt/SyncAssistantForm.php
133 строки
4 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\BaseModel; use common\helpers\ArrayHelper; use common\kdf\KModelStatus; 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\Exceptions\ErrorException; use Yii; use yii\helpers\VarDumper; /** * Форма синхронизации Assistant * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2024 */ class SyncAssistantForm extends BaseModel { /** * @var string */ public $type = 'assistant'; /** * @inheritdoc */ public function rules() { return ArrayHelper::merge(parent::rules(), [ [['type'], 'string'], ]); } /** * @inheritdoc */ protected function doSave() { $pl = Project::find() ->published() ->all(); /** @var Project $p */ foreach ($pl as $p) { try { $openAI = Yii::createObject([ 'class' => OpenAIComponent::class, 'project_id' => $p->key ]); /** @var Client $client */ $client = $openAI->getClient(); $response = $client->assistants()->list([ 'limit' => 50, ]); foreach ($response->data as $result) { Yii::debug(VarDumper::dumpAsString([ 'assistant' => $result ])); $m = ModelAI::findOne(['title' => $result->model]); $a = Assistant::findOrNew([ 'key' => $result->id, ]); $a->setAttributes([ 'project_id' => $p->id, 'temperature' => $result->temperature, 'top_p' => $result->topP, 'name' => $result->name ?: $result->id, 'instructions' => $result->instructions, 'model_id' => $m->id ]); if (!$a->save()) { $this->addErrors($a->errors); return false; } if ($result->toolResources) { if ($result->toolResources->fileSearch) { $vs_id = $result->toolResources->fileSearch->vectorStoreIds; $vs_list = VectorStore::find() ->andWhere(['key' => $vs_id]) ->all(); AssistantVectorStore::deleteAll(['assistant_id' => $a->id]); /** @var VectorStore $vs */ foreach ($vs_list as $vs) { $link = AssistantVectorStore::findOrNew([ 'vector_store_id' => $vs->id, 'assistant_id' => $a->id, ]); if (!$link->save()) { $this->addErrors($link->errors); return false; } } } } } } catch (ErrorException $e) { Yii::debug(VarDumper::dumpAsString([ 'error' => $e->getMessage() ])); $p->updateAttributes([ 'rec_status' => KModelStatus::ARCHIVE ]); } } return true; } }