From 10abaec00081251b5c716639484507bc037eed3a Mon Sep 17 00:00:00 2001 From: Chris <66376200+crickman@users.noreply.github.com> Date: Fri, 10 Nov 2023 00:02:37 -0800 Subject: [PATCH] Introduce "Safe" SystemDescription (#589) ### Motivation and Context Stored system prompt references `TimeSkill `plugin that has been renamed to `TimePlugin`. System prompts for each chat have been stored in cosmosdb. ### Description Dynamically intercept system-prompt and replace plugin reference. ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [Contribution Guidelines](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone :smile: --- webapi/Controllers/ChatArchiveController.cs | 2 +- webapi/Controllers/ChatHistoryController.cs | 2 +- webapi/Models/Storage/ChatSession.cs | 5 +++++ webapi/Plugins/Chat/ChatPlugin.cs | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/webapi/Controllers/ChatArchiveController.cs b/webapi/Controllers/ChatArchiveController.cs index ee2a57525..8bdd7b75b 100644 --- a/webapi/Controllers/ChatArchiveController.cs +++ b/webapi/Controllers/ChatArchiveController.cs @@ -99,7 +99,7 @@ private async Task CreateChatArchiveAsync(Guid chatId, Cancellation chatArchive.ChatTitle = chat.Title; // get the system description - chatArchive.SystemDescription = chat.SystemDescription; + chatArchive.SystemDescription = chat.SafeSystemDescription; // get the chat history chatArchive.ChatHistory = await this.GetAllChatMessagesAsync(chatIdString); diff --git a/webapi/Controllers/ChatHistoryController.cs b/webapi/Controllers/ChatHistoryController.cs index b18220976..d80e6e1ae 100644 --- a/webapi/Controllers/ChatHistoryController.cs +++ b/webapi/Controllers/ChatHistoryController.cs @@ -218,7 +218,7 @@ public async Task EditChatSessionAsync( if (await this._sessionRepository.TryFindByIdAsync(chatId.ToString(), callback: v => chat = v)) { chat!.Title = chatParameters.Title ?? chat!.Title; - chat!.SystemDescription = chatParameters.SystemDescription ?? chat!.SystemDescription; + chat!.SystemDescription = chatParameters.SystemDescription ?? chat!.SafeSystemDescription; chat!.MemoryBalance = chatParameters.MemoryBalance ?? chat!.MemoryBalance; await this._sessionRepository.UpsertAsync(chat); await messageRelayHubContext.Clients.Group(chatId.ToString()).SendAsync(ChatEditedClientCall, chat); diff --git a/webapi/Models/Storage/ChatSession.cs b/webapi/Models/Storage/ChatSession.cs index 8cdac8789..c7a1cd6a6 100644 --- a/webapi/Models/Storage/ChatSession.cs +++ b/webapi/Models/Storage/ChatSession.cs @@ -34,6 +34,11 @@ public class ChatSession : IStorageEntity /// public string SystemDescription { get; set; } + /// + /// Fixed system description with "TimeSkill" replaced by "TimePlugin" + /// + public string SafeSystemDescription => this.SystemDescription.Replace("TimeSkill", "TimePlugin", StringComparison.OrdinalIgnoreCase); + /// /// The balance between long term memory and working term memory. /// The higher this value, the more the system will rely on long term memory by lowering diff --git a/webapi/Plugins/Chat/ChatPlugin.cs b/webapi/Plugins/Chat/ChatPlugin.cs index 3e5edf283..d53760dbe 100644 --- a/webapi/Plugins/Chat/ChatPlugin.cs +++ b/webapi/Plugins/Chat/ChatPlugin.cs @@ -1024,6 +1024,6 @@ private async Task SetSystemDescriptionAsync(string chatId, CancellationToken ca throw new ArgumentException("Chat session does not exist."); } - this._promptOptions.SystemDescription = chatSession!.SystemDescription; + this._promptOptions.SystemDescription = chatSession!.SafeSystemDescription; } }