Fixed code, enabled to also always answer in a private chat

This commit is contained in:
Samuele Lorefice
2024-12-26 01:34:10 +01:00
parent 2357c7570c
commit b74e5d75e1
4 changed files with 48 additions and 13 deletions

2
.env
View File

@@ -1,2 +1,2 @@
MODEL_PATH=./model MODEL_PATH=./model
MODEL_NAME=Qwen2.5-7B-Instruct-Q8_0 MODEL_NAME=Qwen2.5-7B-Instruct-Q8_0.gguf

View File

@@ -5,16 +5,34 @@ services:
context: "I:\\NemesisAI" context: "I:\\NemesisAI"
dockerfile: "TelegramBot/Dockerfile" dockerfile: "TelegramBot/Dockerfile"
target: "build" target: "build"
args:
BUILD_CONFIGURATION: "Debug"
command: [] command: []
entrypoint: entrypoint:
- "dotnet" - "/opt/JetBrains/RiderDebuggerTools/linux-x64/JetBrains.Debugger.Worker"
- "/app/bin/Debug/net9.0/TelegramBot.dll" - "--runtimeconfig"
- "/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"
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:57017:57000"
- "127.0.0.1:57217:57200"
- "127.0.0.1:57417:57400"
volumes: volumes:
- "I:\\NemesisAI\\TelegramBot:/app:rw" - "I:\\NemesisAI\\TelegramBot:/app:rw"
- "I:\\NemesisAI:/src:rw" - "I:\\NemesisAI:/src:rw"
- "C:\\Users\\airon\\.nuget\\packages:/root/.nuget/packages" - "C:\\Users\\airon\\.nuget\\packages:/root/.nuget/packages"
- "C:\\Users\\airon\\AppData\\Local\\JetBrains\\RiderRemoteDebugger\\2024.3.3\\\
Linux64:/opt/JetBrains/RiderDebuggerTools"
- "C:\\Users\\airon\\AppData\\Local\\Programs\\Rider\\bin\\backend-log.xml:/etc/opt/JetBrains/RiderDebuggerTools/backend-log.xml"
- "C:\\Users\\airon\\AppData\\Local\\JetBrains\\Rider2024.3\\log\\DebuggerWorker\\\
JetBrains.Debugger.Worker.2024_12_26_01_25_50:/var/opt/JetBrains/RiderDebuggerTools:rw"
working_dir: "/app" working_dir: "/app"

View File

@@ -1,4 +1,4 @@
TELEGRAM_BOT_TOKEN=yourTokenHere TELEGRAM_BOT_TOKEN=yourTokenHere
OPENAI_BASE_URL=llm-server OPENAI_BASE_URL=http://llm-server/
OPENAI_MODEL=Qwen2.5-7B-Instruct-Q8_0 OPENAI_MODEL=Qwen2.5-7B-Instruct-Q8_0.gguf
OPENAI_API_KEY= OPENAI_API_KEY=MyApiKey

View File

@@ -1,6 +1,7 @@
using OpenAI; using OpenAI;
using OpenAI.Chat; using OpenAI.Chat;
using System.ClientModel; using System.ClientModel;
using System.Threading.Channels;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
@@ -8,6 +9,13 @@ using Telegram.Bot.Types.Enums;
string baseUrl = Environment.GetEnvironmentVariable("OPENAI_BASE_URL") ?? "https://api.openai.com"; string baseUrl = Environment.GetEnvironmentVariable("OPENAI_BASE_URL") ?? "https://api.openai.com";
string model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? string.Empty; string model = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? string.Empty;
string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? string.Empty; string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? string.Empty;
Console.WriteLine("Starting the bot...");
Console.WriteLine(
$"""
Base URL: {baseUrl}
Model: {model}
API Key: {apiKey}
""");
string nemesisPrompt = string nemesisPrompt =
""" """
@@ -91,20 +99,29 @@ var openAiClient = new OpenAIClient(openAiApiKey, options);
var chatClient = openAiClient.GetChatClient(model); var chatClient = openAiClient.GetChatClient(model);
string token = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN") ?? string.Empty; string token = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN") ?? string.Empty;
Console.WriteLine("OpenAI Chat Client created");
using var cts = new CancellationTokenSource(); using var cts = new CancellationTokenSource();
var bot = new TelegramBotClient(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.WriteLine("Bot running");
cts.Cancel(); Thread.Sleep(Timeout.Infinite);
cts.Cancel(); // stop the bot
async Task OnMessage(Message msg, UpdateType type) 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 or a reply to a message sent by the bot //Check if the message contains the bot's username or a reply to a message sent by the bot or a private chat
if (msg.Text!.Contains(me.Result.Username!, StringComparison.OrdinalIgnoreCase) || if (msg.Text!.Contains(me.Result.FirstName!, StringComparison.OrdinalIgnoreCase) ||
msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id) { msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id ||
msg.Chat.Type == ChatType.Private) {
Console.WriteLine(
$"""
Received message from {msg.Chat.Id} Type: {type}
Message: {msg.Text}
""");
var chatid = msg.Chat.Id; var chatid = msg.Chat.Id;
//Check if the chat is already in the dictionary //Check if the chat is already in the dictionary
if (!oaiChats.ContainsKey(chatid)) if (!oaiChats.ContainsKey(chatid))
@@ -114,7 +131,7 @@ async Task OnMessage(Message msg, UpdateType type)
//fetch existing messages history //fetch existing messages history
var messages = oaiChats[chatid]; var messages = oaiChats[chatid];
//Fetch the response from the model //Fetch the response from the model
var result = chatClient.CompleteChat(messages).Value.Content.ToString()!; var result = chatClient.CompleteChat(messages).Value.Content[0].Text;
//Add the response to the chat //Add the response to the chat
oaiChats[chatid].Add(new AssistantChatMessage(result)); oaiChats[chatid].Add(new AssistantChatMessage(result));
//Send the response to the user //Send the response to the user