From 0f1f8710ad66ba3c6ed89cd54028d4acf6903dfe Mon Sep 17 00:00:00 2001 From: Mario Ramos Date: Mon, 15 Apr 2024 09:35:45 +0200 Subject: [PATCH 1/2] `ChatMessageHistoryRecord` change `UserId` to `IndexerId` --- .../IChatHistoryProvider.cs | 12 ++++++------ .../ChatHistoryProvider.cs | 12 ++++++------ .../Plugins/ChatMessageHistoryRecord.cs | 11 +++++++---- .../Plugins/ChatWithHistoryPlugin.cs | 18 +++++++++--------- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/Encamina.Enmarcha.SemanticKernel.Abstractions/IChatHistoryProvider.cs b/src/Encamina.Enmarcha.SemanticKernel.Abstractions/IChatHistoryProvider.cs index 6ac7e8f..ef16b74 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Abstractions/IChatHistoryProvider.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Abstractions/IChatHistoryProvider.cs @@ -10,28 +10,28 @@ public interface IChatHistoryProvider /// /// Deletes all chat history messages for a specific user. /// - /// The unique identifier of the user that is owner of the chat. + /// The unique identifier of the chat history indexer. /// A cancellation token that can be used to receive notice of cancellation. /// A that on completion indicates the asynchronous operation has executed. - Task DeleteChatMessagesHistoryAsync(string userId, CancellationToken cancellationToken); + Task DeleteChatMessagesHistoryAsync(string indexerId, CancellationToken cancellationToken); /// /// Loads chat history messages. /// /// The current chat history. - /// The unique identifier of the user that is owner of the chat. + /// The unique identifier of the chat history indexer. /// The total remaining tokens available for loading messages from the chat history. /// A cancellation token that can be used to receive notice of cancellation. /// A that on completion indicates the asynchronous operation has executed. - Task LoadChatMessagesHistoryAsync(ChatHistory chatHistory, string userId, int remainingTokens, CancellationToken cancellationToken); + Task LoadChatMessagesHistoryAsync(ChatHistory chatHistory, string indexerId, int remainingTokens, CancellationToken cancellationToken); /// /// Saves a chat message into the conversation history. /// - /// The user's unique identifier. + /// The unique identifier of the chat history indexer. /// The name of the role associated with the chat message. For example the `user`, the `assistant` or the `system`. /// The message. /// A cancellation token that can be used to receive notice of cancellation. /// A that on completion indicates the asynchronous operation has executed. - Task SaveChatMessagesHistoryAsync(string userId, string roleName, string message, CancellationToken cancellationToken); + Task SaveChatMessagesHistoryAsync(string indexerId, string roleName, string message, CancellationToken cancellationToken); } diff --git a/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/ChatHistoryProvider.cs b/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/ChatHistoryProvider.cs index d080479..8f549a7 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/ChatHistoryProvider.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/ChatHistoryProvider.cs @@ -34,16 +34,16 @@ public ChatHistoryProvider(Func tokensLengthFunction, IAsyncReposit } /// - public Task DeleteChatMessagesHistoryAsync(string userId, CancellationToken cancellationToken) + public Task DeleteChatMessagesHistoryAsync(string indexerId, CancellationToken cancellationToken) { - return chatMessagesHistoryRepository.DeleteAsync(userId, cancellationToken); + return chatMessagesHistoryRepository.DeleteAsync(indexerId, cancellationToken); } /// /// /// The maximum number of messages to load is configured in ChatHistoryProviderOptions.HistoryMaxMessages. /// - public async Task LoadChatMessagesHistoryAsync(ChatHistory chatHistory, string userId, int remainingTokens, CancellationToken cancellationToken) + public async Task LoadChatMessagesHistoryAsync(ChatHistory chatHistory, string indexerId, int remainingTokens, CancellationToken cancellationToken) { if (options.HistoryMaxMessages <= 0 || remainingTokens <= 0) { @@ -52,7 +52,7 @@ public async Task LoadChatMessagesHistoryAsync(ChatHistory chatHistory, string u // Obtain the chat history for the user, ordered by timestamps descending to get the most recent messages first, and then take 'N' messages. // This means that the first elements in the list are the most recent or newer messages, and the last elements in the list are the oldest messages. - var result = (await chatMessagesHistoryRepository.GetAllAsync(chatMessages => chatMessages.Where(chatMessage => chatMessage.UserId == userId) + var result = (await chatMessagesHistoryRepository.GetAllAsync(chatMessages => chatMessages.Where(chatMessage => chatMessage.IndexerId == indexerId) .OrderByDescending(chatMessage => chatMessage.TimestampUtc) .Take(options.HistoryMaxMessages), cancellationToken)).ToList(); @@ -102,12 +102,12 @@ public async Task LoadChatMessagesHistoryAsync(ChatHistory chatHistory, string u } /// - public async Task SaveChatMessagesHistoryAsync(string userId, string roleName, string message, CancellationToken cancellationToken) + public async Task SaveChatMessagesHistoryAsync(string indexerId, string roleName, string message, CancellationToken cancellationToken) { await chatMessagesHistoryRepository.AddAsync(new ChatMessageHistoryRecord() { Id = Guid.NewGuid().ToString(), - UserId = userId, + IndexerId = indexerId, RoleName = roleName, Message = message, TimestampUtc = DateTime.UtcNow, diff --git a/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatMessageHistoryRecord.cs b/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatMessageHistoryRecord.cs index d97af9d..61c03cc 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatMessageHistoryRecord.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatMessageHistoryRecord.cs @@ -17,11 +17,14 @@ public class ChatMessageHistoryRecord : IIdentifiable public virtual string Id { get; init; } /// - /// Gets the unique identifier of the user owner of the chat. + /// Gets the unique identifier of the chat history indexer. /// - [JsonProperty(@"userId")] - [JsonPropertyName(@"userId")] - public virtual string UserId { get; init; } + /// + /// Identifier that relates several messages. This could be, for example, a conversationId, a userId, or any other relevant identifier. + /// + [JsonProperty(@"indexerId")] + [JsonPropertyName(@"indexerId")] + public virtual string IndexerId { get; init; } /// /// Gets the name of the role associated with the chat message. diff --git a/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatWithHistoryPlugin.cs b/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatWithHistoryPlugin.cs index 70e0d44..42c92c5 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatWithHistoryPlugin.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatWithHistoryPlugin.cs @@ -54,7 +54,7 @@ public ChatWithHistoryPlugin(Kernel kernel, string chatModelName, Func /// A cancellation token that can be used to receive notice of cancellation. /// What the user says or asks when chatting. - /// A unique identifier for the user when chatting. + /// The unique identifier of the chat history indexer. /// The name of the user. /// The preferred language of the user while chatting. /// A string representing the response from the Artificial Intelligence. @@ -64,7 +64,7 @@ public ChatWithHistoryPlugin(Kernel kernel, string chatModelName, Func ChatAsync( CancellationToken cancellationToken, [Description(@"What the user says or asks when chatting")] string ask, - [Description(@"A unique identifier for the user when chatting")] string userId, + [Description(@"The unique identifier of the chat history indexer")] string chatIndexerId, [Description(@"The name of the user")] string userName = null, [Description(@"The preferred language of the user while chatting")] string locale = null) { @@ -93,15 +93,15 @@ public virtual async Task ChatAsync( return await GetErrorMessageAsync(chatHistory, locale, systemPromptTokens, askTokens, chatModelMaxTokens, cancellationToken); } - await chatHistoryProvider.LoadChatMessagesHistoryAsync(chatHistory, userId, remainingTokens, cancellationToken); + await chatHistoryProvider.LoadChatMessagesHistoryAsync(chatHistory, chatIndexerId, remainingTokens, cancellationToken); chatHistory.AddUserMessage(ask); var chatMessage = await kernel.GetRequiredService().GetChatMessageContentAsync(chatHistory, options.ChatRequestSettings, kernel, cancellationToken); var response = chatMessage.Content; - await chatHistoryProvider.SaveChatMessagesHistoryAsync(userId, AuthorRole.User.ToString(), ask, cancellationToken); // Save in chat history the user message (a.k.a. ask). - await chatHistoryProvider.SaveChatMessagesHistoryAsync(userId, AuthorRole.Assistant.ToString(), response, cancellationToken); // Save in chat history the assistant message (a.k.a. response). + await chatHistoryProvider.SaveChatMessagesHistoryAsync(chatIndexerId, AuthorRole.User.ToString(), ask, cancellationToken); // Save in chat history the user message (a.k.a. ask). + await chatHistoryProvider.SaveChatMessagesHistoryAsync(chatIndexerId, AuthorRole.Assistant.ToString(), response, cancellationToken); // Save in chat history the assistant message (a.k.a. response). return response; } @@ -109,18 +109,18 @@ public virtual async Task ChatAsync( /// /// Deletes the chat message history when a user asks to forget previous conversations or to start over again. /// - /// The unique identifier of the user owner of the chat history. + /// The unique identifier of the chat history indexer. /// A cancellation token that can be used to cancel the operation. /// A Task representing the asynchronous operation. [KernelFunction] [Description(@"Deletes the chat message history when a user asks to forget previous conversations or to start all over again.")] public async Task DeleteChatMessagesHistoryAsync( - [Description(@"A unique identifier of the user")] string userId, + [Description(@"The unique identifier of the chat history indexer")] string chatIndexerId, CancellationToken cancellationToken) { - ArgumentException.ThrowIfNullOrWhiteSpace(userId); + ArgumentException.ThrowIfNullOrWhiteSpace(chatIndexerId); - await chatHistoryProvider.DeleteChatMessagesHistoryAsync(userId, cancellationToken); + await chatHistoryProvider.DeleteChatMessagesHistoryAsync(chatIndexerId, cancellationToken); } /// From 433aff80d5679ce804c1e49cc820b5de59a4ec1f Mon Sep 17 00:00:00 2001 From: Mario Ramos Date: Mon, 15 Apr 2024 12:35:19 +0200 Subject: [PATCH 2/2] Update changelog and version --- CHANGELOG.md | 5 +++++ Directory.Build.props | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aca3f4d..f499a97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,12 @@ Also, any bug fix must start with the prefix �Bug fix:� followed by the desc Previous classification is not required if changes are simple or all belong to the same category. +## [8.1.6] +### Breaking Changes + + - Renamed `UserId` to `IndexerId` in `ChatMessageHistoryRecord`. This change requires consumers to update their database to match the new property name. + - In case of using Cosmos DB, `IndexerId` should be the new partition key of the collection. You can learn how to change the partition key and do the data migration [here](https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/change-partition-key). ## [8.1.5] diff --git a/Directory.Build.props b/Directory.Build.props index 6d8540c..713c2f0 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -16,8 +16,8 @@ - 8.1.5 - + 8.1.6 + preview-01