mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 08:53: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
@@ -1,4 +1,6 @@
|
||||
using AIStudio.Chat;
|
||||
using AIStudio.Chat.Compression;
|
||||
using AIStudio.Dialogs;
|
||||
using AIStudio.Provider;
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Settings.DataModel;
|
||||
@@ -52,6 +54,9 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase wher
|
||||
[Inject]
|
||||
private AssistantLaunchService AssistantLaunchService { get; init; } = null!;
|
||||
|
||||
[Inject]
|
||||
private ConversationTokenCounter TokenCounter { get; init; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the job service used to run assistant-created chats independently from the assistant UI.
|
||||
/// </summary>
|
||||
@@ -264,6 +269,12 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase wher
|
||||
private MediaImportOwner CurrentMediaImportOwner => MediaImportOwner.ForAssistant(this.assistantSessionKey);
|
||||
private Guid? assistantSessionId;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the user decided already in this run to send a request which nearly fills the context window, see
|
||||
/// ConfirmContextWindowAsync; reset whenever a run starts.
|
||||
/// </summary>
|
||||
private bool askedAboutContextWindow;
|
||||
|
||||
/// <summary>
|
||||
/// When the running session started, which the time below the progress bar counts from; null while
|
||||
/// no session runs. It comes from the session, so an instance which returns to a running session
|
||||
@@ -413,6 +424,7 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase wher
|
||||
|
||||
this.CancellationTokenSource = new();
|
||||
this.IsProcessing = true;
|
||||
this.askedAboutContextWindow = false;
|
||||
var startedSession = await this.AssistantSessionService.TryBeginAsync(this.assistantSessionKey, this.Title, this.CancellationTokenSource, this.ChatThread, this.CaptureAssistantSessionState(), this);
|
||||
if (startedSession.IsActive is not true || startedSession.Key != this.assistantSessionKey)
|
||||
{
|
||||
@@ -690,9 +702,13 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase wher
|
||||
/// <param name="useSelectedTools">Whether the model may use the tools the user selected. False for a request
|
||||
/// which must rely on what the conversation holds already, e.g., one which reconciles results the tools
|
||||
/// gathered before.</param>
|
||||
/// <returns>The answer, or an empty string when the provider failed.</returns>
|
||||
/// <returns>The answer, or an empty string when the provider failed or the user kept the request from being
|
||||
/// sent, see ConfirmContextWindowAsync.</returns>
|
||||
protected async Task<string> AddAIResponseAsync(DateTimeOffset time, bool hideContentFromUser = false, bool useSelectedTools = true)
|
||||
{
|
||||
if (!await this.ConfirmContextWindowAsync(time))
|
||||
return string.Empty;
|
||||
|
||||
var manageCancellationLocally = this.CancellationTokenSource is null;
|
||||
this.CancellationTokenSource ??= new CancellationTokenSource();
|
||||
|
||||
@@ -817,6 +833,52 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase wher
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asks the user what to do when the request about to be sent nearly fills the context window of the model, or no
|
||||
/// longer fits into it, e.g., because of its files.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Without it, the user hears only afterward that the request was too long, from the error of the provider, or not
|
||||
/// at all, when the provider leaves out a part of it. It asks from the share on from which the chat asks as well,
|
||||
/// see ConversationTokens.AsksBeforeSending, and only where the model states its window. The count takes the
|
||||
/// conversation of the assistant with its files and the system prompt it stores, but neither what the request adds
|
||||
/// to the system prompt nor the tools; it is an estimate, which rather asks too late than too early. A file which
|
||||
/// the counter has not read yet is read for it once, so the first request with a large file waits a little longer.
|
||||
/// <br/><br/>
|
||||
///
|
||||
/// It asks at most once per run: a run with several requests which goes on after the user chose to send anyway
|
||||
/// would otherwise ask again with every request. When the user does not send, the request leaves the conversation,
|
||||
/// and the run stops as if the user had stopped it, so that no later request of the run goes out instead.
|
||||
/// <br/><br/>
|
||||
///
|
||||
/// Requests which do not go through AddAIResponseAsync are not covered: the SWOT analysis, the stages of the
|
||||
/// Visual Briefing, the hidden requests of the BeyondFacts Lab, the batch processing, and the generation of
|
||||
/// assistant plugins send on their own. The chats of assistants decide for themselves, see
|
||||
/// EmbeddedChat.ShowsContextWindow.
|
||||
/// </remarks>
|
||||
/// <param name="time">When the request was added, see AddUserRequest.</param>
|
||||
/// <returns>True when the request is to be sent.</returns>
|
||||
private async Task<bool> ConfirmContextWindowAsync(DateTimeOffset time)
|
||||
{
|
||||
if (this.askedAboutContextWindow || this.ChatThread is not { } thread)
|
||||
return true;
|
||||
|
||||
var parts = ConversationParts.Of(thread, thread.SystemPrompt, string.Empty, null, this.ProviderSettings.SupportsImageInput(), null);
|
||||
var tokens = await this.TokenCounter.CountAsync(this.ProviderSettings, parts, thread.ReportedHistoryFor(this.ProviderSettings.Model), this.CancellationTokenSource?.Token ?? CancellationToken.None);
|
||||
if (!tokens.AsksBeforeSending)
|
||||
return true;
|
||||
|
||||
this.askedAboutContextWindow = true;
|
||||
if (await ContextWindowFullDialog.ShowAsync(this.DialogService, tokens, ContextWindowFullSubject.ASSISTANT_REQUEST, ChatCompressionOffer.NONE) is ContextWindowFullDecision.SEND_ANYWAY)
|
||||
return true;
|
||||
|
||||
thread.Blocks.RemoveAll(block => block.Role is ChatRole.USER && block.Time == time);
|
||||
if (this.CancellationTokenSource is { } cancellation)
|
||||
await cancellation.CancelAsync();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the current assistant chat thread as a regular background-capable chat generation job.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user