Skip to content

Commit

Permalink
added gigachat dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Gaynutdinov committed Feb 3, 2024
1 parent 144cd03 commit 3dc7290
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 16 deletions.
54 changes: 38 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ PHP >= 7.4
require 'vendor/autoload.php';

use Edvardpotter\GigaChat\GigaChat;
use Edvardpotter\GigaChat\GigaChatDialog;
use Edvardpotter\GigaChat\GigaChatOAuth;
use Edvardpotter\GigaChat\Type\Message;
use Edvardpotter\GigaChat\Type\Model;

// https://gu-st.ru/content/Other/doc/russiantrustedca.pem
$cert = __DIR__ . '/russiantrustedca.pem';
Expand All @@ -37,40 +39,60 @@ $oauthClient = new GigaChatOAuth(
$cert // false для отключения проверки сертификата
);

// Получить токена
$accessToken = $oauthClient->getAccessToken();
echo $accessToken->getAccessToken();
echo $accessToken->isExpired();

$client = new GigaChat(
$gigaChat = new GigaChat(
$accessToken->getAccessToken(),
$cert
);

$models = $client->getModels();
// Пример отправки сообщения
$messages = [
new Message(
'Когда уже ИИ захватит этот мир?'
),
];
$completion = $gigaChat->chatCompletions($messages);

foreach ($completion->getChoices() as $choice) {
echo $choice->getMessage()->getContent();
echo $choice->getMessage()->getRole();
}

// Пример для работы с GigaChat в режиме диалога
$dialog = new GigaChatDialog($gigaChat);
$questionMessage = new Message('Когда уже ИИ захватит этот мир?');
$answerMessage = $dialog->getAnswer($questionMessage);

$questionMessage = new Message('Как ИИ изменятся в будущем?');
$answerMessage = $dialog->getAnswer($questionMessage);

// Сброс истории диалога
$dialog->reset();


// Получить список доступных моделей
$models = $gigaChat->getModels();
foreach ($models as $model) {
echo $model->getId();
echo $model->getObject();
echo $model->getOwnedBy();
}

$tokensCount = $client->tokensCount('GigaChat:latest', 'Напиши интересный факт о сбербанке');
// Посчитать кол-во токенов для строки
$tokensCount = $gigaChat->tokensCount(Model::ID_GIGACHAT_LATEST, 'Когда уже ИИ захватит этот мир?');
echo $tokensCount->getObject();
echo $tokensCount->getTokens();
echo $tokensCount->getCharacters();

$completion = $client->chatCompletions([
new Message(
'Напиши интересный факт о сбербанке'
)
]);

foreach ($completion->getChoices() as $choice) {
echo $choice->getMessage()->getContent();
echo $choice->getMessage()->getRole();
}
// Скачивание файла
$stream = $gigaChat->getFile('file_id');
file_put_contents('file_name.jpg', $stream);

$embeddings = $client->getEmbeddings(['1234']);
// Создать векторные представления
$embeddings = $gigaChat->getEmbeddings(['1234']);

$stream = $client->getFile('file_id');
file_put_contents('file_name.jpg', $stream);
```
66 changes: 66 additions & 0 deletions src/GigaChatDialog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

namespace Edvardpotter\GigaChat;

use Edvardpotter\GigaChat\Type\Message;
use Edvardpotter\GigaChat\Type\Model;

class GigaChatDialog
{
private GigaChat $gigaChat;
/**
* @var Message[]
*/
private array $messages;

public function __construct(GigaChat $gigaChat)
{
$this->gigaChat = $gigaChat;
}

public function getAnswer(
Message $message,
string $model = Model::ID_GIGACHAT_LATEST,
float $temperature = 1.0,
float $topP = 0.1,
int $n = 1,
bool $stream = false,
int $maxTokens = 1024,
float $repetitionPenalty = 1,
int $updateInterval = 0
): Message
{
$this->messages[] = $message;

$completion = $this->gigaChat->chatCompletions(
$this->messages,
$model,
$temperature,
$topP,
$n,
$stream,
$maxTokens,
$repetitionPenalty,
$updateInterval
);

$message = $completion->getChoices()[0]->getMessage();
$this->messages[] = $message;

return $message;
}

public function reset(): void
{
$this->messages = [];
}

/**
* @return Message[]
*/
public function getHistory(): array
{
return $this->messages;
}
}

0 comments on commit 3dc7290

Please sign in to comment.