Capabilities
➕ Easy to start to use GigaChat API. Only main classes and methods in root namespace. All helpers stored in child namespaces if you need set more details
➕ You can set all settings according official documentation https://developers.sber.ru/docs/ru/gigachat/api/reference
➕ The history can be saved (in file for example) and can be used in other sessions. Just load history before request prompt
Completion completion = new Completion();
completion.History = {Your deserialized history}
➕ All errors wrapped in one request field 'ErrorTextIfFailed'. It includes HttpErrors and GigaChat errors
➕ Dont worry about access token expiring. Just use method 'UpdateToken()' before sending request prompt
The code example you can find below or in application "TestGigaChatAdapter"
Step 1: //Set auth / Укажите аутентификационные данные из личного кабинета
Below after code description how to get this authData in Important! tips
string authData = "authData=="; // base64
Authorization auth = new Authorization(authData, GigaChatAdapter.Auth.RateScope.GIGACHAT_API_PERS);
var authResult = await auth.SendRequest();
Step 2: //Send Prompts to get answer from AI / отправляйте вопросы чату, чтобы получить ответ от ИИ
if (authResult.AuthorizationSuccess)
{
Completion completion = new Completion();
Console.WriteLine("EN: Type prompt or close console to end application");
Console.WriteLine("РУ: Напишите запрос к модели. В ином случае закройте окно, если дальнейшую работу с чатботом необходимо прекратить.");
while (true)
{
//read prompt / Чтение промпта с консоли
var prompt = Console.ReadLine();
//update access token if expired / Обновление токена, если он просрочился
await auth.UpdateToken();
//request / отправка промпта. Чтобы исключить историю переписки - необходимо в методе указать false для аргумента UseHistory (по умолчанию UseHistory = true)
var result = await completion.SendRequest(auth.LastResponse.GigaChatAuthorizationResponse?.AccessToken, prompt);
if (result.RequestSuccessed)
{
Console.WriteLine(result.GigaChatCompletionResponse.Choices.LastOrDefault().Message.Content);
}
else
{
Console.WriteLine(result.ErrorTextIfFailed);
}
}
}
else
{
Console.WriteLine(authResult.ErrorTextIfFailed);
}
Before using you should execute 3 steps:
-
Registred in https://developers.sber.ru/ to get authenticated key that used for authorization. Generate auth code in personal account using button "Generate new Secret Code"
-
Install certificates in OS. Information here: https://developers.sber.ru/docs/ru/gigachat/certificates
-
Access token lives only 30 mins. So use method UpdateToken() before sending request prompt
auth.UpdateToken();
UPDATES
V 1.0.5 - Now you can set reserveTime parameter when call Authorization.Update() method. It allows to update token before it expired. For example Authorization.Update() method can be called a millisecond before token ends. So just set reserveTime = 1 minute to update token if it expires less then 1 minute.
await auth.UpdateToken(reserveTime: new TimeSpan(0, 1, 0));
V 1.0.7 - Add parameter maxTokens to completions settings (thanks to Amr0x64)
CompletionSettings settings = new CompletionSettings("GigaChat:latest", 2, null, 4, 1024);
It is the first version of dll that i create for self using. Then it will be updated as needed. Welcome if you can help to upgrade this library :)