/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/components/OpenAIComponent.php
102 строки
2 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\components; use common\helpers\ArrayHelper; use Http\Client\Curl\Client; use yii\base\Component; use Yii; use yii\helpers\VarDumper; /** * Компонент для работы с OpenAI * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2019-2024 */ class OpenAIComponent extends Component { /** * Клиент для работы с OpenAI * @var \OpenAI\Client */ protected $client = null; /** * Идентификатор ассистента * @var string */ protected $assistant = null; /** * Идентификатор проекта * @var null */ public $project_id = null; /** * Внешний API KEY * @var null */ public $api_key = null; /** * @inheritdoc */ public function init() { parent::init(); // TODO: Change the autogenerated stub $config = ArrayHelper::getValue(Yii::$app->params, 'OpenAI'); Yii::debug(VarDumper::dumpAsString([ 'config' => $config ])); $options = []; if ($proxy = ArrayHelper::getValue($config, 'proxy')) { $options[CURLOPT_PROXY] = $proxy; } $this->assistant = ArrayHelper::getValue($config, 'assistant'); $http_client = new Client(null, null, $options); if ($this->api_key) { $apiKey = $this->api_key; } else { $apiKey = ArrayHelper::getValue($config, 'api_key'); } $factory = \OpenAI::factory() ->withApiKey($apiKey) ->withHttpClient($http_client) // default: HTTP client found using PSR-18 HTTP Client Discovery ->withHttpHeader('OpenAI-Beta', 'assistants=v2'); if ($this->project_id) { $factory->withHttpHeader('OpenAI-Project', $this->project_id); } $this->client = $factory->make(); } /** * Получить экземпляр клиента для работы * @return \OpenAI\Client|null */ public function getClient() { return $this->client; } /** * Получить идентификатор ассистента * @return string|null */ public function getAssistant() { return $this->assistant; } }