/
sersar
/
GigaChat.NET.SendSMS
Обзор
Документация
Войти
/
sersar
/
GigaChat.NET.SendSMS
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
ConsoleApp/Program.cs
107 строк
5 KB
sersar
upload files
15 янв 2026, 19:09
15 янв 2026, 19:09
1c64741
Код
Авторство
О чём код?
using LD.Sber.GigaChatSDK; using LD.Sber.GigaChatSDK.Interfaces; using LD.Sber.GigaChatSDK.Models; using System.ComponentModel.Design; public class Program { private static readonly IHttpService httpService = new HttpService(true); private static readonly ITokenService tokenService = new TokenService(httpService, "NGNhM2U4NzEtYzJjMi00ZDU3LWI0ZjQtNWQwYmY4ZDE4YzQzOjAyNzE1ZDI0LTdjMjUtNDQ3Yy04ZTMyLWM0Y2I1ZmE3NjA5OA==", false); private static readonly IGigaChat gigaChatClient = new GigaChat(tokenService, httpService, true); private static readonly MessageQuery? query = new MessageQuery(); async static Task Main(string[] args) { await gigaChatClient.CreateTokenAsync(); query?.Functions?.Add(sendSmsFunction); // Добавляем нашу функцию while (true) { Console.WriteLine("Введите сообщение:"); var input = Console.ReadLine(); if (string.IsNullOrWhiteSpace(input)) continue; // Отправляем текст query?.Messages.Add(new MessageContent("user", input)); var response = await gigaChatClient.CompletionsAsync(query); if (response != null && response.Choices.Any()) { var choice = response.Choices.LastOrDefault(); query.Messages.Add(choice.Message); // Проверяем, была ли вызвана пользовательская функция if (choice.Message.FunctionCall != null) { var functionCall = choice.Message.FunctionCall; if (functionCall.Name == "send_sms") { var arguments = functionCall.Arguments; if (arguments.ContainsKey("recipient") && arguments.ContainsKey("message")) { string phoneNumber = arguments["recipient"].ToString(); string messageToSend = arguments["message"].ToString(); // Вызываем функцию отправки SMS await SendSmsAsync(phoneNumber, messageToSend); Console.WriteLine("SMS отправлено!"); } } choice.Message.FunctionCall = null; } if (!string.IsNullOrEmpty(choice.Message.Content)) { Console.WriteLine($"Ответ от GigaChat: {choice.Message.Content}"); } } } } private static async Task SendSmsAsync(string phoneNumber, string message) { Console.WriteLine($"Отправка SMS на номер {phoneNumber}: {message}"); await Task.Delay(1000); // Эмуляция асинхронной операции } static FunctionDescription sendSmsFunction = new FunctionDescription { Name = "send_sms", Description = "Отправить SMS-сообщение", Parameters = new FunctionParameters { Type = "object", Properties = new Dictionary<string, ParameterProperty> { { "recipient", new ParameterProperty { Type = "string", Description = "Номер телефона получателя" } }, { "message", new ParameterProperty { Type = "string", Description = "Содержимое сообщения" } } }, Required = new List<string> { "recipient", "message" } }, ReturnParameters = new ReturnParameters { Type = "object", Properties = new Dictionary<string, ReturnProperty> { { "status", new ReturnProperty { Type = "string", Description = "Статус отправки сообщения" } }, { "message", new ReturnProperty { Type = "string", Description = "Сообщение о результате отправки SMS" } } }, Required = new List<string> { "status", "message" } }, FewShotExamples = new List<FewShotExample> { new FewShotExample { Request = "Можешь ли ты отправить SMS-сообщение на номер 123456789 с содержимым 'Привет, как дела?'", Params = new Dictionary<string, object> { { "recipient", "123456789" }, { "message", "Привет, как дела?" } } } } }; }