From 674c754bb10df8ce1541c40b9fa689d9390c289c Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Sun, 24 Mar 2024 18:39:08 +0100 Subject: [PATCH] Fix deletion. --- Directory.Build.props | 2 +- ai/Squidex.AI.Tests/OpenAIChatAgentTests.cs | 38 +++++++++++++++---- ai/Squidex.AI/SemanticKernel/IChatStore.cs | 2 +- .../SemanticKernel/InMemoryChatStore.cs | 2 +- .../SemanticKernel/Mongo/MongoChatStore.cs | 2 +- .../SemanticKernel/OpenAIChatAgent.cs | 2 +- 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 68711e1..a65f8d6 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -11,7 +11,7 @@ https://github.com/squidex/squidex true snupkg - 6.6.3 + 6.6.4 diff --git a/ai/Squidex.AI.Tests/OpenAIChatAgentTests.cs b/ai/Squidex.AI.Tests/OpenAIChatAgentTests.cs index 33eadbc..258ac57 100644 --- a/ai/Squidex.AI.Tests/OpenAIChatAgentTests.cs +++ b/ai/Squidex.AI.Tests/OpenAIChatAgentTests.cs @@ -8,6 +8,7 @@ using System.ComponentModel; using Microsoft.Extensions.DependencyInjection; using Microsoft.SemanticKernel; +using Squidex.AI.SemanticKernel; using Xunit; namespace Squidex.AI; @@ -82,17 +83,38 @@ public void Should_be_configured_if_open_ai_is_added() [Trait("Category", "Dependencies")] public async Task Should_ask_questions_without_conversation() { - var sut = CreateSut(); + var (sut, _) = CreateSut(); var message1 = await sut.PromptAsync("Write an interesting article about Paris in 5 words."); AssertMessage("Paris: City of Love and Lights", message1); } + [Fact] + [Trait("Category", "Dependencies")] + public async Task Should_delete_conversation() + { + var (sut, services) = CreateSut(); + + var conversationId = Guid.NewGuid().ToString(); + try + { + await sut.PromptAsync(string.Empty, conversationId); + } + finally + { + await sut.StopConversationAsync(conversationId); + } + + var store = services.GetRequiredService(); + + Assert.Null(await store.GetAsync(conversationId, default)); + } + [Fact] [Trait("Category", "Dependencies")] public async Task Should_ask_questions() { - var sut = CreateSut(); + var (sut, _) = CreateSut(); var conversationId = Guid.NewGuid().ToString(); try @@ -113,7 +135,7 @@ public async Task Should_ask_questions() [Trait("Category", "Dependencies")] public async Task Should_ask_question_with_tool() { - var sut = CreateSut(); + var (sut, _) = CreateSut(); var conversationId = Guid.NewGuid().ToString(); try @@ -134,7 +156,7 @@ public async Task Should_ask_question_with_tool() [Trait("Category", "Dependencies")] public async Task Should_ask_question_with_tool2() { - var sut = CreateSut(); + var (sut, _) = CreateSut(); var conversationId = Guid.NewGuid().ToString(); try @@ -155,7 +177,7 @@ public async Task Should_ask_question_with_tool2() [Trait("Category", "Dependencies")] public async Task Should_ask_multiple_question_with_tools() { - var sut = CreateSut(); + var (sut, _) = CreateSut(); var conversationId = Guid.NewGuid().ToString(); try @@ -176,7 +198,7 @@ public async Task Should_ask_multiple_question_with_tools() [Trait("Category", "Dependencies")] public async Task Should_ask_multiple_question_with_tools2() { - var sut = CreateSut(); + var (sut, _) = CreateSut(); var conversationId = Guid.NewGuid().ToString(); try @@ -193,7 +215,7 @@ public async Task Should_ask_multiple_question_with_tools2() } } - private static IChatAgent CreateSut() + private static (IChatAgent, IServiceProvider) CreateSut() { var services = new ServiceCollection() @@ -212,7 +234,7 @@ private static IChatAgent CreateSut() }) .BuildServiceProvider(); - return services.GetRequiredService(); + return (services.GetRequiredService(), services); } private static void AssertMessage(string text, ChatBotResponse message) diff --git a/ai/Squidex.AI/SemanticKernel/IChatStore.cs b/ai/Squidex.AI/SemanticKernel/IChatStore.cs index 40fa100..59fa048 100644 --- a/ai/Squidex.AI/SemanticKernel/IChatStore.cs +++ b/ai/Squidex.AI/SemanticKernel/IChatStore.cs @@ -9,7 +9,7 @@ namespace Squidex.AI.SemanticKernel; public interface IChatStore { - Task ClearAsync(string conversationId, + Task RemoveAsync(string conversationId, CancellationToken ct); Task StoreAsync(string conversationId, string value, DateTime expires, diff --git a/ai/Squidex.AI/SemanticKernel/InMemoryChatStore.cs b/ai/Squidex.AI/SemanticKernel/InMemoryChatStore.cs index 3079973..5492608 100644 --- a/ai/Squidex.AI/SemanticKernel/InMemoryChatStore.cs +++ b/ai/Squidex.AI/SemanticKernel/InMemoryChatStore.cs @@ -13,7 +13,7 @@ public sealed class InMemoryChatStore : IChatStore { private readonly ConcurrentDictionary values = new ConcurrentDictionary(); - public Task ClearAsync(string conversationId, + public Task RemoveAsync(string conversationId, CancellationToken ct) { values.Remove(conversationId, out _); diff --git a/ai/Squidex.AI/SemanticKernel/Mongo/MongoChatStore.cs b/ai/Squidex.AI/SemanticKernel/Mongo/MongoChatStore.cs index e1fbd31..90dd159 100644 --- a/ai/Squidex.AI/SemanticKernel/Mongo/MongoChatStore.cs +++ b/ai/Squidex.AI/SemanticKernel/Mongo/MongoChatStore.cs @@ -33,7 +33,7 @@ public Task InitializeAsync( cancellationToken: ct); } - public Task ClearAsync(string conversationId, + public Task RemoveAsync(string conversationId, CancellationToken ct) { return collection.DeleteOneAsync(x => x.Id == conversationId, ct); diff --git a/ai/Squidex.AI/SemanticKernel/OpenAIChatAgent.cs b/ai/Squidex.AI/SemanticKernel/OpenAIChatAgent.cs index 180dfec..9af1cf6 100644 --- a/ai/Squidex.AI/SemanticKernel/OpenAIChatAgent.cs +++ b/ai/Squidex.AI/SemanticKernel/OpenAIChatAgent.cs @@ -134,6 +134,6 @@ private Task StoreHistoryAsync(ChatHistory history, string conversationId, public Task StopConversationAsync(string conversationId, CancellationToken ct = default) { - return store.GetAsync(conversationId, ct); + return store.RemoveAsync(conversationId, ct); } }