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,37 +1,20 @@
# This is a generated file. Not intended for manual editing. # This is a generated file. Not intended for manual editing.
services: services:
telegrambot: nemesisBot:
build: build:
context: "/home/redcode/dev/NemesisAI" context: "I:\\NemesisAI"
dockerfile: "TelegramBot/Dockerfile" dockerfile: "TelegramBot/Dockerfile"
target: "base" target: "build"
args:
BUILD_CONFIGURATION: "Debug"
command: [] command: []
entrypoint: entrypoint:
- "/opt/JetBrains/RiderDebuggerTools/linux-x64/JetBrains.Debugger.Worker" - "dotnet"
- "--runtimeconfig" - "/app/bin/Debug/net9.0/TelegramBot.dll"
- "/opt/JetBrains/RiderDebuggerTools/JetBrains.Debugger.Worker.runtimeconfig.json"
- "--mode=server"
- "--frontend-port=57000"
- "--backend-port=57200"
- "--roslyn-worker-port=57400"
- "--timeout=60"
environment: environment:
DOTNET_ENVIRONMENT: "Development" DOTNET_ENVIRONMENT: "Development"
DOTNET_USE_POLLING_FILE_WATCHER: "true"
RIDER_DEBUGGER_LOG_DIR: "/var/opt/JetBrains/RiderDebuggerTools"
RESHARPER_LOG_CONF: "/etc/opt/JetBrains/RiderDebuggerTools/backend-log.xml"
image: "telegrambot:dev" image: "telegrambot:dev"
ports: ports: []
- "127.0.0.1:57000:57000"
- "127.0.0.1:57200:57200"
- "127.0.0.1:57400:57400"
volumes: volumes:
- "/home/redcode/dev/NemesisAI/TelegramBot:/app:rw" - "I:\\NemesisAI\\TelegramBot:/app:rw"
- "/home/redcode/dev/NemesisAI:/src:rw" - "I:\\NemesisAI:/src:rw"
- "/home/redcode/.nuget/packages:/home/app/.nuget/packages" - "C:\\Users\\airon\\.nuget\\packages:/root/.nuget/packages"
- "/home/redcode/.local/share/JetBrains/RiderRemoteDebugger/2024.3.2/Linux64:/opt/JetBrains/RiderDebuggerTools"
- "/home/redcode/.local/share/JetBrains/Toolbox/apps/rider/bin/backend-log.xml:/etc/opt/JetBrains/RiderDebuggerTools/backend-log.xml"
- "/home/redcode/.cache/JetBrains/Rider2024.3/log/DebuggerWorker/JetBrains.Debugger.Worker.2024_12_24_23_05_14:/var/opt/JetBrains/RiderDebuggerTools:rw"
working_dir: "/app" working_dir: "/app"

View File

@@ -1 +1,4 @@
TELEGRAM_BOT_TOKEN=yourTokenHere TELEGRAM_BOT_TOKEN=yourTokenHere
LMSTUDIO_BASE_URL=
LMSTUDIO_MODEL=
LMSTUDIO_API_KEY=

View File

@@ -1,8 +1,8 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base FROM mcr.microsoft.com/dotnet/dotnet:9.0 AS base
USER $APP_UID USER $APP_UID
WORKDIR /app WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release ARG BUILD_CONFIGURATION=Release
WORKDIR /src WORKDIR /src
COPY ["TelegramBot/TelegramBot.csproj", "TelegramBot/"] COPY ["TelegramBot/TelegramBot.csproj", "TelegramBot/"]

View File

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

View File

@@ -2,10 +2,11 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<LangVersion>latestmajor</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -15,6 +16,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="LMStudio" Version="1.2.0" />
<PackageReference Include="Telegram.Bot" Version="22.2.0" /> <PackageReference Include="Telegram.Bot" Version="22.2.0" />
</ItemGroup> </ItemGroup>

View File

@@ -1,5 +1,5 @@
services: services:
telegrambot: nemesisBot:
image: telegrambot image: telegrambot
build: build:
context: . context: .

7
global.json Normal file
View File

@@ -0,0 +1,7 @@
{
"sdk": {
"version": "9.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
}