diff --git a/README.md b/README.md index 4400bac..eecf6fa 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ ChatGPT is aimed to support conversational scenarios: user can talk to ChatGPT w * *MessageLimit*: specifies how many messages for each conversation must be saved. When this limit is reached, oldest messages are automatically removed. * *MessageExpiration*: specifies the time interval used to maintain messages in cache, regardless their count. +We can also set ChatGPT parameters for chat completion at startup. Check the [official documentation](https://platform.openai.com/docs/api-reference/chat/create) for the list of available parameters and their meaning. + The configuration can be automatically read from [IConfiguration](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfiguration), using for example a _ChatGPT_ section in the _appsettings.json_ file: "ChatGPT": { @@ -141,7 +143,7 @@ Response streaming works by returning an [IAsyncEnumerable](https://learn.micros **Changing the assistant's behavior** -ChatGPT supports messages with the *system* role to influence how the assistant should behave. For example, we can tell to ChatGPT something like that: +ChatGPT supports messages with the _system_ role to influence how the assistant should behave. For example, we can tell to ChatGPT something like that: - You are an helpful assistant - Answer like Shakespeare @@ -161,7 +163,9 @@ The *system* message does not count for messages limit number. Conversation history is automatically deleted when expiration time (specified by *MessageExpiration* property) is reached. However, if necessary it is possible to immediately clear the history: - await chatGptClient.DeleteConversationAsync(conversationId); + await chatGptClient.DeleteConversationAsync(conversationId, preserveSetup: false); + +The _preserveSetup_ argument allows to decide whether mantain also the _system_ message that has been set with the **SetupAsync** method (default: _false_). **Contribute** diff --git a/samples/ChatGptBlazor.Wasm/ChatGptBlazor.Wasm.sln b/samples/ChatGptBlazor.Wasm/ChatGptBlazor.Wasm.sln deleted file mode 100644 index ac19c6e..0000000 --- a/samples/ChatGptBlazor.Wasm/ChatGptBlazor.Wasm.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.5.33516.290 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatGptBlazor.Wasm", "ChatGptBlazor.Wasm.csproj", "{E082D7E8-EB1C-49D1-9B62-32D998B05C4B}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {E082D7E8-EB1C-49D1-9B62-32D998B05C4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E082D7E8-EB1C-49D1-9B62-32D998B05C4B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E082D7E8-EB1C-49D1-9B62-32D998B05C4B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E082D7E8-EB1C-49D1-9B62-32D998B05C4B}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {D10FF537-B8BB-4DA9-9DE4-572CAAED260C} - EndGlobalSection -EndGlobal diff --git a/src/ChatGptNet/ChatGptClient.cs b/src/ChatGptNet/ChatGptClient.cs index ea3e135..a1efa07 100644 --- a/src/ChatGptNet/ChatGptClient.cs +++ b/src/ChatGptNet/ChatGptClient.cs @@ -180,9 +180,20 @@ public Task> GetConversationAsync(Guid conversationI return Task.FromResult(messages); } - public Task DeleteConversationAsync(Guid conversationId) + public Task DeleteConversationAsync(Guid conversationId, bool preserveSetup = false) { - cache.Remove(conversationId); + if (!preserveSetup) + { + // We don't want to preserve setup message, so just deletes all the cache history. + cache.Remove(conversationId); + } + else if (cache.TryGetValue>(conversationId, out var messages)) + { + // Removes all the messages, except system ones. + messages!.RemoveAll(m => m.Role != ChatGptRoles.System); + cache.Set(conversationId, messages, options.MessageExpiration); + } + return Task.CompletedTask; } diff --git a/src/ChatGptNet/IChatGptClient.cs b/src/ChatGptNet/IChatGptClient.cs index 4130e24..5d9b755 100644 --- a/src/ChatGptNet/IChatGptClient.cs +++ b/src/ChatGptNet/IChatGptClient.cs @@ -110,6 +110,7 @@ Task SetupAsync(string message) /// Deletes a chat conversation, clearing all the history. /// /// The unique identifier of the conversation. + /// to maintain the system message that has been specified with the method. /// The corresponding to the asynchronous operation. - Task DeleteConversationAsync(Guid conversationId); + Task DeleteConversationAsync(Guid conversationId, bool preserveSetup = false); }