Added LMStudio client, upgraded to .net 9.0

This commit is contained in:
Samuele Lorefice
2024-12-25 19:37:14 +01:00
parent 0ba298b955
commit 4b308b762a
7 changed files with 51 additions and 45 deletions

View File

@@ -1,9 +1,19 @@
using Telegram.Bot;
using System.Diagnostics;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using LMStudio;
string baseUrl = Environment.GetEnvironmentVariable("LMSTUDIO_BASE_URL") ?? string.Empty;
string model = Environment.GetEnvironmentVariable("LMSTUDIO_MODEL") ?? string.Empty;
string apiKey = Environment.GetEnvironmentVariable("LMSTUDIO_API_KEY") ?? string.Empty;
var lmsclient = new ModelConnection(baseUrl, model, apiKey);
Dictionary<long, LMStudio.Chat> lmsChats = new();
string token = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN") ?? string.Empty;
using var cts = new CancellationTokenSource();
var bot = new TelegramBotClient(Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN"), cancellationToken:cts.Token);
var bot = new TelegramBotClient(token, cancellationToken:cts.Token);
var me = bot.GetMe();
bot.OnMessage += OnMessage;
Console.ReadLine();
@@ -13,19 +23,20 @@ async Task OnMessage(Message msg, UpdateType type)
{
//Discard any message that is not a text message
if (msg.Type != MessageType.Text) return;
//Check if the message contains the bot's username
if (msg.Text!.Contains(me.Result.Username!, StringComparison.OrdinalIgnoreCase))
await bot.SendMessage(msg.Chat, "You mentioned me!");
//Check if the message is a reply to a message sent by the bot
else if(msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id){
await bot.SendMessage(msg.Chat, "You replied to a message!");
//Check if the message contains the bot's username or a reply to a message sent by the bot
if (msg.Text!.Contains(me.Result.Username!, StringComparison.OrdinalIgnoreCase) ||
msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id) {
//Check if the chat is already in the dictionary
if (!lmsChats.ContainsKey(msg.Chat.Id))
AddChatToDictionary(msg.Chat.Id);
lmsChats[msg.Chat.Id].CreateChatCompletion(msg.Text);
}
//We can safely return otherwise
}
HttpClient AiClient = new HttpClient();
string getNemesisMsg() {
//AiClient.GetAsync()
return "";
void AddChatToDictionary(long id) {
//Create a new chat object
var chat = new LMStudio.Chat(lmsclient);
//add the entry to the dictionary
lmsChats.Add(id, chat);
}