mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 07:13:48 +00:00
Added context compression (#1198)
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
1 parent
28b88580a9
commit
8a4a26503b
54 files changed
+4582
-56
No files matched your search
@@ -306,6 +306,56 @@ public sealed class EmbeddedChatServiceTests
|
||||
Assert.That(await service.ResetAsync(Guid.NewGuid(), () => new ChatThread()), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AReplacedConversationIsStoredAndKeepsTheIdOfTheChat()
|
||||
{
|
||||
var store = new InMemoryEmbeddedChatStore();
|
||||
var chatId = Guid.NewGuid();
|
||||
var service = this.CreateService(Answer("An answer."));
|
||||
|
||||
var conversation = await service.GetAsync(store, COMPONENT, chatId, () => new ChatThread());
|
||||
await service.SendAsync(chatId, this.Request("A question."));
|
||||
var compressed = new ChatThread { ChatId = Guid.NewGuid(), Blocks = [new ContentBlock { Time = DateTimeOffset.Now, ContentType = ContentType.TEXT, Role = ChatRole.USER, Content = new ContentText { Text = "The summary" } }] };
|
||||
var replaced = await service.ReplaceAsync(chatId, compressed);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(replaced, Is.True);
|
||||
Assert.That(conversation.Thread, Is.SameAs(compressed), "The chat shows the new conversation.");
|
||||
Assert.That(conversation.Thread!.ChatId, Is.EqualTo(chatId), "The chat stays the one of its assistant.");
|
||||
Assert.That(store.StoredBlocks(chatId).Select(block => ((ContentText)block.Content!).Text), Is.EqualTo(new[] { "The summary" }), "What the chat shows is on the disk as well.");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConversationIsNotReplacedWhileTheModelAnswers()
|
||||
{
|
||||
var store = new InMemoryEmbeddedChatStore();
|
||||
var chatId = Guid.NewGuid();
|
||||
var answering = new TaskCompletionSource();
|
||||
var release = new TaskCompletionSource();
|
||||
var service = this.CreateService(async (_, answer, _, _) =>
|
||||
{
|
||||
answering.SetResult();
|
||||
await release.Task;
|
||||
answer.Text = "An answer.";
|
||||
});
|
||||
|
||||
var conversation = await service.GetAsync(store, COMPONENT, chatId, () => new ChatThread());
|
||||
var sending = service.SendAsync(chatId, this.Request("A question."));
|
||||
await answering.Task;
|
||||
|
||||
var replaced = await service.ReplaceAsync(chatId, new ChatThread());
|
||||
release.SetResult();
|
||||
await sending;
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(replaced, Is.False, "The answer would go into a conversation nobody sees anymore.");
|
||||
Assert.That(conversation.Thread!.Blocks, Has.Count.EqualTo(2), "The question and its answer stay.");
|
||||
}
|
||||
}
|
||||
|
||||
private EmbeddedChatService CreateService(Func<EmbeddedChatRequest, ContentText, ChatThread, CancellationToken, Task> answerAsync) => new(this.settingsManager, new AssistantSessionService(MessageBus.INSTANCE), MessageBus.INSTANCE, NullLogger<EmbeddedChatService>.Instance, answerAsync);
|
||||
|
||||
private EmbeddedChatRequest Request(string text) => new() { Provider = this.provider, Message = new ContentText { Text = text } };
|
||||
|
||||
@@ -5,7 +5,8 @@ using AIStudio.Models;
|
||||
namespace AIStudio.Tests.Chat.Embedded;
|
||||
|
||||
/// <summary>
|
||||
/// Checks when an embedded chat which shows the context window warns about a full context, and when it stops sending.
|
||||
/// Checks when an embedded chat warns about a full context, and when it stops sending, with the context window shown or
|
||||
/// hidden.
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public sealed class EmbeddedChatTokenBudgetTests
|
||||
@@ -46,4 +47,45 @@ public sealed class EmbeddedChatTokenBudgetTests
|
||||
|
||||
Assert.That(EmbeddedChatTokenBudget.Of(tokens), Is.EqualTo(EmbeddedChatTokenBudgetState.WARNING));
|
||||
}
|
||||
|
||||
[TestCase(20_000, 1_000, WINDOW, EmbeddedChatSendNotice.NONE, TestName = "A message which fits needs no notice")]
|
||||
[TestCase(27_000, 1_000, WINDOW, EmbeddedChatSendNotice.FULL, TestName = "In a small window the reserve refuses the message before the share asks")]
|
||||
[TestCase(950_000, 1_000, 1_000_000, EmbeddedChatSendNotice.NEARLY_FULL, TestName = "In a large window the share asks before the reserve refuses")]
|
||||
[TestCase(990_000, 7_000, 1_000_000, EmbeddedChatSendNotice.FULL, TestName = "In a large window the reserve still refuses the last message")]
|
||||
public void TheMessageDecidesTheNoticeBeforeSending(int historyTokens, int messageTokens, int window, EmbeddedChatSendNotice expected)
|
||||
{
|
||||
var tokens = new ConversationTokens { IsKnown = true, HistoryTokens = historyTokens, DraftTokens = messageTokens, Window = ContextWindow.Of(window) };
|
||||
|
||||
Assert.That(EmbeddedChatTokenBudget.NoticeBeforeSending(tokens, showsContextWindow: true), Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnUnknownWindowNeedsNoNotice()
|
||||
{
|
||||
var tokens = new ConversationTokens { IsKnown = true, HistoryTokens = 500_000, DraftTokens = 1_000, Window = ContextWindow.UNKNOWN };
|
||||
|
||||
Assert.That(EmbeddedChatTokenBudget.NoticeBeforeSending(tokens, showsContextWindow: true), Is.EqualTo(EmbeddedChatSendNotice.NONE), "Nobody wrote the window down, so nobody can say that it is full.");
|
||||
}
|
||||
|
||||
[TestCase(950_000, 1_000, EmbeddedChatSendNotice.NONE, TestName = "A hidden window asks nothing before it is full")]
|
||||
[TestCase(990_000, 7_000, EmbeddedChatSendNotice.FULL, TestName = "A hidden window still refuses the message which no longer fits")]
|
||||
public void AHiddenWindowOnlyRefuses(int historyTokens, int messageTokens, EmbeddedChatSendNotice expected)
|
||||
{
|
||||
var tokens = new ConversationTokens { IsKnown = true, HistoryTokens = historyTokens, DraftTokens = messageTokens, Window = ContextWindow.Of(1_000_000) };
|
||||
|
||||
Assert.That(EmbeddedChatTokenBudget.NoticeBeforeSending(tokens, showsContextWindow: false), Is.EqualTo(expected), "Asking would name the window, which tells the models apart.");
|
||||
}
|
||||
|
||||
[TestCase(10_000, true, EmbeddedChatTokenBudgetState.OK)]
|
||||
[TestCase(26_000, true, EmbeddedChatTokenBudgetState.WARNING)]
|
||||
[TestCase(30_000, true, EmbeddedChatTokenBudgetState.FULL)]
|
||||
[TestCase(10_000, false, EmbeddedChatTokenBudgetState.UNKNOWN)]
|
||||
[TestCase(26_000, false, EmbeddedChatTokenBudgetState.UNKNOWN)]
|
||||
[TestCase(30_000, false, EmbeddedChatTokenBudgetState.FULL)]
|
||||
public void AHiddenWindowShowsOnlyThatItIsFull(int historyTokens, bool showsContextWindow, EmbeddedChatTokenBudgetState expected)
|
||||
{
|
||||
var tokens = new ConversationTokens { IsKnown = true, HistoryTokens = historyTokens, Window = ContextWindow.Of(WINDOW) };
|
||||
|
||||
Assert.That(EmbeddedChatTokenBudget.Shown(tokens, showsContextWindow), Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user