From ae678194f8d05cf864a62cc3fd62a22db9ca91eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peer=20Sch=C3=BCtt?= <20603780+peerschuett@users.noreply.github.com> Date: Mon, 19 May 2025 16:16:52 +0200 Subject: [PATCH] Additional messages from the chat templates are now used in the chats --- app/MindWork AI Studio/Chat/ChatRole.cs | 14 ++++++++++++++ app/MindWork AI Studio/Chat/ChatThread.cs | 7 +++++-- app/MindWork AI Studio/Chat/ContentBlock.cs | 12 ++++++++++++ app/MindWork AI Studio/Chat/ContentImage.cs | 12 ++++++++++++ app/MindWork AI Studio/Chat/ContentText.cs | 11 +++++++++++ app/MindWork AI Studio/Chat/IContent.cs | 6 ++++++ .../Components/ChatComponent.razor.cs | 5 +++-- .../Dialogs/ChatTemplateDialog.razor | 2 +- 8 files changed, 64 insertions(+), 5 deletions(-) diff --git a/app/MindWork AI Studio/Chat/ChatRole.cs b/app/MindWork AI Studio/Chat/ChatRole.cs index 339be971..3b6db234 100644 --- a/app/MindWork AI Studio/Chat/ChatRole.cs +++ b/app/MindWork AI Studio/Chat/ChatRole.cs @@ -60,4 +60,18 @@ public static class ExtensionsChatRole _ => Icons.Material.Filled.Help, }; + + /// + /// Returns the specific name of the role for the chat template. + /// + /// The role. + /// The name of the role. + public static string ToChatTemplateName(this ChatRole role) => role switch + { + ChatRole.SYSTEM => "System", + ChatRole.USER => "User", + ChatRole.AI => "Assistant", + + _ => "Unknown", + }; } \ No newline at end of file diff --git a/app/MindWork AI Studio/Chat/ChatThread.cs b/app/MindWork AI Studio/Chat/ChatThread.cs index 5b02397f..a028e8db 100644 --- a/app/MindWork AI Studio/Chat/ChatThread.cs +++ b/app/MindWork AI Studio/Chat/ChatThread.cs @@ -93,7 +93,7 @@ public sealed record ChatThread { // - // Prepare the prompts using the chatTemplate: + // Use the information from the chat template, if provided. Otherwise, use the default system prompt // string systemPromptTextWithChatTemplate; var logMessage = $"Using no chat template for chat thread '{chatThread.Name}'."; @@ -125,6 +125,9 @@ public sealed record ChatThread } logger.LogInformation(logMessage); + // + // Add augmented data, if available: + // var isAugmentedDataAvailable = !string.IsNullOrWhiteSpace(chatThread.AugmentedData); var systemPromptWithAugmentedData = isAugmentedDataAvailable switch { @@ -144,7 +147,7 @@ public sealed record ChatThread // - // Prepare the system prompt: + // Add information from profile if available and allowed: // string systemPromptText; logMessage = $"Using no profile for chat thread '{chatThread.Name}'."; diff --git a/app/MindWork AI Studio/Chat/ContentBlock.cs b/app/MindWork AI Studio/Chat/ContentBlock.cs index 4ac347e9..0568d597 100644 --- a/app/MindWork AI Studio/Chat/ContentBlock.cs +++ b/app/MindWork AI Studio/Chat/ContentBlock.cs @@ -29,4 +29,16 @@ public class ContentBlock /// Should the content block be hidden from the user? /// public bool HideFromUser { get; set; } + + public ContentBlock DeepClone() + { + return new() + { + Time = this.Time, + ContentType = this.ContentType, + Content = this.Content?.DeepClone(), + Role = this.Role, + HideFromUser = this.HideFromUser, + }; + } } \ No newline at end of file diff --git a/app/MindWork AI Studio/Chat/ContentImage.cs b/app/MindWork AI Studio/Chat/ContentImage.cs index c7e785eb..22eff248 100644 --- a/app/MindWork AI Studio/Chat/ContentImage.cs +++ b/app/MindWork AI Studio/Chat/ContentImage.cs @@ -32,6 +32,18 @@ public sealed class ContentImage : IContent, IImageSource { throw new NotImplementedException(); } + + /// + public IContent DeepClone() + { + return new ContentImage + { + Source = this.Source, + InitialRemoteWait = this.InitialRemoteWait, + IsStreaming = this.IsStreaming, + SourceType = this.SourceType, + }; + } #endregion diff --git a/app/MindWork AI Studio/Chat/ContentText.cs b/app/MindWork AI Studio/Chat/ContentText.cs index 38872edf..66166d7d 100644 --- a/app/MindWork AI Studio/Chat/ContentText.cs +++ b/app/MindWork AI Studio/Chat/ContentText.cs @@ -125,6 +125,17 @@ public sealed class ContentText : IContent return chatThread; } + /// + public IContent DeepClone() + { + return new ContentText + { + Text = this.Text, + InitialRemoteWait = this.InitialRemoteWait, + IsStreaming = this.IsStreaming, + }; + } + #endregion /// diff --git a/app/MindWork AI Studio/Chat/IContent.cs b/app/MindWork AI Studio/Chat/IContent.cs index be3bf097..c03f6574 100644 --- a/app/MindWork AI Studio/Chat/IContent.cs +++ b/app/MindWork AI Studio/Chat/IContent.cs @@ -43,6 +43,12 @@ public interface IContent /// public Task CreateFromProviderAsync(IProvider provider, Model chatModel, IContent? lastPrompt, ChatThread? chatChatThread, CancellationToken token = default); + /// + /// Creates a deep copy + /// + /// The copy + public IContent DeepClone(); + /// /// Returns the corresponding ERI content type. /// diff --git a/app/MindWork AI Studio/Components/ChatComponent.razor.cs b/app/MindWork AI Studio/Components/ChatComponent.razor.cs index 5fce1335..31c37c28 100644 --- a/app/MindWork AI Studio/Components/ChatComponent.razor.cs +++ b/app/MindWork AI Studio/Components/ChatComponent.razor.cs @@ -334,6 +334,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable this.ChatThread = this.ChatThread with { SelectedChatTemplate = this.currentChatTemplate.Id, + Blocks = this.currentChatTemplate.AdditionalMessages.Select(x => x.DeepClone()).ToList(), }; await this.ChatThreadChanged.InvokeAsync(this.ChatThread); @@ -440,7 +441,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable DataSourceOptions = this.earlyDataSourceOptions, Name = this.ExtractThreadName(this.userInput), Seed = this.RNG.Next(), - Blocks = [], + Blocks = this.currentChatTemplate == default ? [] : this.currentChatTemplate.AdditionalMessages.Select(x => x.DeepClone()).ToList(), }; await this.ChatThreadChanged.InvokeAsync(this.ChatThread); @@ -671,7 +672,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable ChatId = Guid.NewGuid(), Name = string.Empty, Seed = this.RNG.Next(), - Blocks = [], + Blocks = this.currentChatTemplate == default ? [] : this.currentChatTemplate.AdditionalMessages.Select(x => x.DeepClone()).ToList(), }; } diff --git a/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor b/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor index fbfaa710..21d83bc1 100644 --- a/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor +++ b/app/MindWork AI Studio/Dialogs/ChatTemplateDialog.razor @@ -92,7 +92,7 @@ @foreach (var role in availableRoles) { - @role.ToName() + @role.ToChatTemplateName() }