/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/forms/ai/BaseRequestForm.php
201 строка
4 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\forms\ai; use common\components\Connection; use common\forms\base\BaseModel; use common\helpers\ArrayHelper; use common\interfaces\BatchInterface; use common\models\Request; use common\models\ResponseBatch; use common\models\ResponseBatchError; use Yii; use yii\helpers\VarDumper; use yii\web\ServerErrorHttpException; /** * Запрос к чату GPT * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2024 */ abstract class BaseRequestForm extends BaseModel { /** * @var bool */ protected $use_transaction = false; /** * Запрос * @var array */ public $request = []; /** * attempt * @var int */ public $attempt = 1; /** * Режим отладки * @var int */ public $is_debug = 0; /** * Пользователь * @var null */ public $user_id = null; /** * * @var null */ private $start_at = null; /** * @inheritdoc */ public function rules() { return [ [['attempt', 'is_debug', 'user_id'], 'number'], ['attempt', 'required'], ['request', 'safe'], ]; } /** * @inheritdoc */ protected function beforeSave() { set_time_limit(0); $this->start_at = microtime(true); return parent::beforeSave(); // TODO: Change the autogenerated stub } /** * @inheritdoc */ protected function afterSave($result) { parent::afterSave($result); // TODO: Change the autogenerated stub if ($result) { $done = microtime(true) - $this->start_at; $this->model['direct_time_spent'] = $done; } } /** * Делаем запрос к AI * @return mixed */ abstract protected function doAiRequest(); /** * @inheritdoc */ protected function doSave() { /** @var Connection $db */ $db = Yii::$app->db; $this->attempt = 1; while (true) { $db->pingRepeat(); try { if ($this->doAiRequest()) { break; } } catch (\Exception $exception) { Yii::debug(VarDumper::dumpAsString([ 'error' => $exception->getMessage() ])); } if ($this->attempt > 3) { break; } $this->attempt++; } return true; } /** * Название формы * @return string */ public function getAction() { return 'request'; } /** * Сформировать набор данных для отправки в AI * * @param Request $r * @param $options * @return array */ public function initOptions($r, $options) { return ArrayHelper::merge($options, [ 'request' => $r->getBatchList() ]); } /** * Дополнительные манипуляции с данными * @param $index * @param $result * @param $error * @return boolean */ public function proccessBatch($index, $done, $attempt, $error) { $rb = ResponseBatch::findOrNew([ 'request_batch_id' => $index, ]); $rb->setAttributes([ 'time_spent' => $done, 'attempt' => $attempt ]); if (!$rb->save()) { Yii::debug(VarDumper::dumpAsString([ 'errors' => $rb->errors ])); throw new ServerErrorHttpException(Yii::t('app', 'Error save response body')); } if ($error) { $rbe = ResponseBatchError::findOrNew([ 'response_batch_id' => $rb->id, ]); $rbe->setAttributes([ 'error' => $error ]); if (!$rbe->save()) { Yii::debug(VarDumper::dumpAsString([ 'errors' => $rbe->errors ])); throw new ServerErrorHttpException(Yii::t('app', 'Error save response body')); } return false; } return true; } }