From c6302112b2ca2ab2b91c1c1ec1cb90dcdf8991f6 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Wed, 25 Dec 2024 21:38:26 +0100 Subject: [PATCH] Implemented also OpenAI --- TelegramBot/Program.cs | 17 ++++++++++++++--- TelegramBot/TelegramBot.csproj | 1 + compose.yaml | 6 +++++- lmstudio.env | 0 4 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 lmstudio.env diff --git a/TelegramBot/Program.cs b/TelegramBot/Program.cs index 9a0dc86..07b890f 100644 --- a/TelegramBot/Program.cs +++ b/TelegramBot/Program.cs @@ -1,16 +1,27 @@ -using System.Diagnostics; +using System.ClientModel; +using System.ClientModel.Primitives; using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using LMStudio; +using OpenAI; +using OpenAI.Chat; -string baseUrl = Environment.GetEnvironmentVariable("LMSTUDIO_BASE_URL") ?? string.Empty; + +string baseUrl = Environment.GetEnvironmentVariable("LMSTUDIO_BASE_URL") ?? "http://lmstudio:8001"; 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 lmsChats = new(); +var options = new OpenAIClientOptions() { + Endpoint = new(baseUrl), + NetworkTimeout = new TimeSpan(0, 0, 30) +}; +var openAiApiKey = new ApiKeyCredential(apiKey); +var openAiClient = new OpenAIClient(openAiApiKey, options); + string token = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN") ?? string.Empty; using var cts = new CancellationTokenSource(); var bot = new TelegramBotClient(token, cancellationToken:cts.Token); @@ -39,4 +50,4 @@ void AddChatToDictionary(long id) { var chat = new LMStudio.Chat(lmsclient); //add the entry to the dictionary lmsChats.Add(id, chat); -} \ No newline at end of file +} diff --git a/TelegramBot/TelegramBot.csproj b/TelegramBot/TelegramBot.csproj index 081076f..eaafef8 100644 --- a/TelegramBot/TelegramBot.csproj +++ b/TelegramBot/TelegramBot.csproj @@ -17,6 +17,7 @@ + diff --git a/compose.yaml b/compose.yaml index bcad2c1..a6804bf 100644 --- a/compose.yaml +++ b/compose.yaml @@ -5,4 +5,8 @@ context: . dockerfile: TelegramBot/Dockerfile env_file: - - TelegramBot/.env \ No newline at end of file + - TelegramBot/.env + + llama-cpp: + image: ghcr.io/ggerganov/llama.cpp:server + \ No newline at end of file diff --git a/lmstudio.env b/lmstudio.env new file mode 100644 index 0000000..e69de29