From edbb35f94dc0c59156c11cf94e75062ae06d3d87 Mon Sep 17 00:00:00 2001 From: Leonid Pershin Date: Thu, 16 Oct 2025 08:18:11 +0300 Subject: [PATCH] fix role --- ChatBot/Models/ChatSession.cs | 11 ++++++---- ChatBot/Models/Dto/ChatMessage.cs | 4 +++- ChatBot/Services/AIService.cs | 22 +++---------------- .../Telegram/Commands/CommandRegistry.cs | 2 +- ChatBot/appsettings.json | 2 +- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/ChatBot/Models/ChatSession.cs b/ChatBot/Models/ChatSession.cs index 8f9b5b0..737cfe6 100644 --- a/ChatBot/Models/ChatSession.cs +++ b/ChatBot/Models/ChatSession.cs @@ -1,4 +1,5 @@ using ChatBot.Models.Dto; +using OllamaSharp.Models.Chat; namespace ChatBot.Models { @@ -64,9 +65,11 @@ namespace ChatBot.Models if (_messageHistory.Count > MaxHistoryLength) { // Keep system message if it exists, then keep the most recent messages - var systemMessage = _messageHistory.FirstOrDefault(m => m.Role == "system"); + var systemMessage = _messageHistory.FirstOrDefault(m => + m.Role == ChatRole.System + ); var recentMessages = _messageHistory - .Where(m => m.Role != "system") + .Where(m => m.Role != ChatRole.System) .TakeLast(MaxHistoryLength - (systemMessage != null ? 1 : 0)) .ToList(); @@ -87,7 +90,7 @@ namespace ChatBot.Models { var message = new ChatMessage { - Role = "user", + Role = ChatRole.User, Content = ChatType == "private" ? content : $"{username}: {content}", }; AddMessage(message); @@ -98,7 +101,7 @@ namespace ChatBot.Models /// public void AddAssistantMessage(string content) { - var message = new ChatMessage { Role = "assistant", Content = content }; + var message = new ChatMessage { Role = ChatRole.Assistant, Content = content }; AddMessage(message); } diff --git a/ChatBot/Models/Dto/ChatMessage.cs b/ChatBot/Models/Dto/ChatMessage.cs index 5a5015e..e25863f 100644 --- a/ChatBot/Models/Dto/ChatMessage.cs +++ b/ChatBot/Models/Dto/ChatMessage.cs @@ -1,3 +1,5 @@ +using OllamaSharp.Models.Chat; + namespace ChatBot.Models.Dto { /// @@ -13,6 +15,6 @@ namespace ChatBot.Models.Dto /// /// The role of the message author (system, user, assistant) /// - public required string Role { get; set; } + public required ChatRole Role { get; set; } } } diff --git a/ChatBot/Services/AIService.cs b/ChatBot/Services/AIService.cs index 3780b6d..d240aaa 100644 --- a/ChatBot/Services/AIService.cs +++ b/ChatBot/Services/AIService.cs @@ -1,8 +1,8 @@ -using ChatBot.Common.Constants; +using System.Text; +using ChatBot.Common.Constants; using ChatBot.Models.Dto; using ChatBot.Services.Interfaces; using OllamaSharp.Models.Chat; -using System.Text; namespace ChatBot.Services { @@ -65,9 +65,7 @@ namespace ChatBot.Services { _client.SelectedModel = model; - var chatMessages = messages - .Select(m => new Message(ConvertRole(m.Role), m.Content)) - .ToList(); + var chatMessages = messages.Select(m => new Message(m.Role, m.Content)).ToList(); var chatRequest = new ChatRequest { Messages = chatMessages, Stream = true }; var response = new StringBuilder(); @@ -86,19 +84,5 @@ namespace ChatBot.Services return response.ToString(); } - - /// - /// Convert string role to OllamaSharp ChatRole - /// - private static ChatRole ConvertRole(string role) - { - return role.ToLower() switch - { - ChatRoles.System => ChatRole.System, - ChatRoles.User => ChatRole.User, - ChatRoles.Assistant => ChatRole.Assistant, - _ => ChatRole.User, - }; - } } } diff --git a/ChatBot/Services/Telegram/Commands/CommandRegistry.cs b/ChatBot/Services/Telegram/Commands/CommandRegistry.cs index df4e8c2..e1dbd8c 100644 --- a/ChatBot/Services/Telegram/Commands/CommandRegistry.cs +++ b/ChatBot/Services/Telegram/Commands/CommandRegistry.cs @@ -1,5 +1,5 @@ -using ChatBot.Services.Telegram.Interfaces; using System.Reflection; +using ChatBot.Services.Telegram.Interfaces; namespace ChatBot.Services.Telegram.Commands { diff --git a/ChatBot/appsettings.json b/ChatBot/appsettings.json index 96f0bd8..b6c1103 100644 --- a/ChatBot/appsettings.json +++ b/ChatBot/appsettings.json @@ -33,6 +33,6 @@ }, "Ollama": { "Url": "http://10.10.1.202:11434", - "DefaultModel": "llama3chat" + "DefaultModel": "llama3" } }