grnsv/neuron-gigachat

GigaChat provider for Neuron AI.

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/grnsv/neuron-gigachat

v0.0.1 2025-11-07 19:12 UTC

This package is auto-updated.

Last update: 2025-11-07 19:15:55 UTC


README

Packagist License PHP NeuronAI

Неофициальный провайдер GigaChat (Сбер) для фреймворка NeuronAI.
Позволяет подключить LLM GigaChat к вашему агенту на базе NeuronAI.

⚙️ Установка

composer require grnsv/neuron-gigachat

🔧 Настройка (на примере Laravel)

В config/services.php добавьте:

'gigachat' => [
    'client_id' => env('GIGACHAT_CLIENT_ID'),
    'client_secret' => env('GIGACHAT_CLIENT_SECRET'),
    'model' => env('GIGACHAT_MODEL', 'GigaChat'),
    'scope' => env('GIGACHAT_SCOPE', 'GIGACHAT_API_PERS'),
],

🧩 Пример агента

Создаём агента:

php vendor/bin/neuron make:agent App\\Neuron\\Agents\\MyAgent

Пример класса:

<?php declare(strict_types=1);

namespace App\Neuron\Agents;

use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use NeuronAI\Agent;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\GigaChat\Config;
use NeuronAI\Providers\GigaChat\GigaChat;
use NeuronAI\SystemPrompt;

final class MyAgent extends Agent
{
    public function __construct(
        private readonly ConfigRepository $config,
        private readonly CacheRepository $cache,
    ) {}

    protected function provider(): AIProviderInterface
    {
        return new GigaChat(
            config: new Config(...$this->config->get('services.gigachat')),
            cache: $this->cache,
        );
    }

    public function instructions(): string
    {
        return (string) new SystemPrompt(
            background: ['Ты дружелюбный ИИ-агент.'],
        );
    }
}

🧪 Отключение TLS (для разработки)

Если сертификат Минцифры раздражает:

protected function provider(): AIProviderInterface
{
    return new GigaChat(
        config: new Config(...$this->config->get('services.gigachat')),
        cache: $this->cache,
        // отключаем проверку сертификата
        verifyTLS: false,
    );
}

🧠 Контекстная память (сессии)

protected function provider(): AIProviderInterface
{
    return new GigaChat(
        config: new Config(...$this->config->get('services.gigachat')),
        cache: $this->cache,
        // сессия передается в заголовке `X-Session-ID`
        httpOptions: new HttpClientOptions(headers: ['X-Session-ID' => $this->getSessionId()]),
    );
}

// здесь ваш механизм хранения сессий
private function getSessionId(): string
{
    return $this->cache->remember(
        'my_agent:session_id',
        now()->endOfWeek(),
        fn (): string => (string) Str::uuid(),
    );
}

🧰 Тестовая команда

php artisan make:command TestAgent
<?php

namespace App\Console\Commands;

use App\Neuron\Agents\MyAgent;
use Illuminate\Console\Command;
use NeuronAI\Chat\Messages\UserMessage;

final class TestAgent extends Command
{
    protected $signature = 'app:test-agent';
    protected $description = 'Test NeuronAI + GigaChat agent';

    public function handle(MyAgent $agent)
    {
        $response = $agent->chat(
            new UserMessage('Когда уже ИИ захватит этот мир?'),
        );

        $this->info($response->getContent());
    }
}

📊 Структурированный вывод (Structured Output)

Пример DTO:

<?php

namespace App\Neuron\DTO;

use NeuronAI\StructuredOutput\SchemaProperty;

class Output
{
    #[SchemaProperty(description: 'Значение вероятности в процентах.', required: true)]
    public float $percent;

    #[SchemaProperty(description: 'Причина выбора такого значения.', required: false)]
    public string $reason;
}

Использование в агенте:

final class MyAgent extends Agent
{
    public function instructions(): string
    {
        return (string) new SystemPrompt(
            background: ['Ты специалист по правдоподобным предсказаниям. Даешь оценку вероятности события в процентах.'],
        );
    }

    protected function getOutputClass(): string
    {
        return Output::class;
    }
}

Пример вызова:

final class TestAgent extends Command
{
    public function handle(MyAgent $agent)
    {
        $response = $agent->structured(
            new UserMessage('Какова вероятность в процентах, что ИИ в этом году захватит мир?'),
        );

        $this->info(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
    }
}

🤝 Contributing

Если будут вопросы или идеи — PR и issue приветствуются 👋

🔗 Links