/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/components/AnthropicComponent.php
111 строк
3 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; /** * Компонент для работы с Anthropic * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2019-2025 */ class AnthropicComponent extends Component { /** * Клиент для работы с OpenAI * @var \Anthropic\Client */ protected $client = 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, 'Anthropic'); Yii::debug(VarDumper::dumpAsString([ 'config' => $config ])); $options = []; if ($proxy = ArrayHelper::getValue($config, 'proxy')) { $options[CURLOPT_PROXY] = $proxy; } $http_client = new Client(null, null, $options); if ($this->api_key) { $apiKey = $this->api_key; } else { $apiKey = ArrayHelper::getValue($config, 'api_key'); } $factory = \Anthropic::factory() ->withApiKey($apiKey) ->withHttpClient($http_client) // default: HTTP client found using PSR-18 HTTP Client Discovery ->withHttpHeader('anthropic-version', '2023-06-01'); $this->client = $factory->make(); } /** * Получить экземпляр клиента для работы * @return \Anthropic\Client|null */ public function getClient() { return $this->client; } /** * Получить список моделей * * @return mixed * @throws \yii\base\InvalidConfigException * @throws \yii\httpclient\Exception */ public function models() { $client = new \yii\httpclient\Client([ 'transport' => 'yii\httpclient\CurlTransport', ]); $url = 'https://api.anthropic.com/v1/models'; $config = ArrayHelper::getValue(Yii::$app->params, 'Anthropic'); $options = []; if ($proxy = ArrayHelper::getValue($config, 'proxy')) { $options[CURLOPT_PROXY] = $proxy; } $apiKey = ArrayHelper::getValue($config, 'api_key'); $request = $client->createRequest(); $request->getHeaders() ->add('x-api-key', $apiKey) ->add('anthropic-version', '2023-06-01'); $response = $request ->setMethod('GET') ->setUrl($url) ->setOptions($options) ->send(); return ArrayHelper::getValue($response->data, 'data'); } }