mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-04 01:14:20 +00:00
Added settings for chat compression, with a model of its own (#1241)
This commit is contained in:
1 parent
899139c88b
commit
1fbc1f4796
71 files changed
+3023
-797
No files matched your search
@@ -1,6 +1,10 @@
|
||||
using AIStudio.AIOperations;
|
||||
using AIStudio.AIOperations.ConversationSummary;
|
||||
using AIStudio.Chat;
|
||||
using AIStudio.Chat.Compression;
|
||||
using AIStudio.Components;
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.PluginSystem;
|
||||
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
@@ -20,9 +24,15 @@ public partial class ChatCompressionDialog : MSGComponentBase
|
||||
[CascadingParameter]
|
||||
private IMudDialogInstance MudDialog { get; set; } = null!;
|
||||
|
||||
[Inject]
|
||||
private ConversationSummaryOperation ConversationSummary { get; init; } = null!;
|
||||
|
||||
[Inject]
|
||||
private ChatCompressionService ChatCompressionService { get; init; } = null!;
|
||||
|
||||
[Inject]
|
||||
private AIOperationProviderResolver ProviderResolver { get; init; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// The chat to compress.
|
||||
/// </summary>
|
||||
@@ -36,10 +46,14 @@ public partial class ChatCompressionDialog : MSGComponentBase
|
||||
public ChatCompressionPlan Plan { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// The provider which writes the summary: the one the chat would send its next message to.
|
||||
/// The provider of the chat: the one it would send its next message to, and which continues with the summary.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// It writes the summary as well, unless the settings pick another model for compressing which may see the
|
||||
/// chat, see ChatCompressionService.GetProviderOptions.
|
||||
/// </remarks>
|
||||
[Parameter]
|
||||
public AIStudio.Settings.Provider ProviderSettings { get; set; } = AIStudio.Settings.Provider.NONE;
|
||||
public AIStudio.Settings.Provider ChatProvider { get; set; } = AIStudio.Settings.Provider.NONE;
|
||||
|
||||
/// <summary>
|
||||
/// Where the conversation goes on: in a new chat, which the user names here, or in the same chat.
|
||||
@@ -59,6 +73,17 @@ public partial class ChatCompressionDialog : MSGComponentBase
|
||||
[Parameter]
|
||||
public bool StoresChatFirst { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the summary starts right away, with the suggested name and the preselected focus, since the settings say
|
||||
/// to compress without asking, see ContextWindowFullHandling.Decide.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The dialog stays, so the user sees the progress and may cancel. When the summary fails, the dialog shows why and
|
||||
/// waits for the user, as it does otherwise.
|
||||
/// </remarks>
|
||||
[Parameter]
|
||||
public bool StartsAutomatically { get; set; }
|
||||
|
||||
private static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new();
|
||||
|
||||
private readonly CancellationTokenSource cancellationTokenSource = new();
|
||||
@@ -68,7 +93,9 @@ public partial class ChatCompressionDialog : MSGComponentBase
|
||||
private string focus = string.Empty;
|
||||
private string issue = string.Empty;
|
||||
private bool isSummarizing;
|
||||
private ChatCompressionProgress progress;
|
||||
private ConversationSummaryProgress progress;
|
||||
private ChatCompressionOptions options = ChatCompressionOptions.DEFAULT;
|
||||
private AIOperationProviderChoice writer = new(AIStudio.Settings.Provider.NONE, AIOperationProviderIssue.NOT_SET, AIOperationProviderIssue.NOT_SET);
|
||||
|
||||
#region Overrides of ComponentBase
|
||||
|
||||
@@ -77,9 +104,23 @@ public partial class ChatCompressionDialog : MSGComponentBase
|
||||
// Configure the spellchecking for the user input:
|
||||
this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES);
|
||||
this.chatName = this.SuggestedName;
|
||||
|
||||
// Read once, and again only when the user changes them through the gear, see ApplyChangedSettings:
|
||||
this.options = ChatCompressionOptions.Of(this.SettingsManager.ConfigurationData.ChatCompression);
|
||||
this.focus = this.options.PreselectedFocus;
|
||||
this.writer = this.ChatCompressionService.ChooseWriter(this.SourceChat, this.ChatProvider);
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
|
||||
// The form exists only after the first render, and CompressAsync validates it:
|
||||
if (firstRender && this.StartsAutomatically)
|
||||
await this.CompressAsync();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private bool HasDocumentsToSummarize => this.Plan.ToSummarize.Any(block => block.Content?.FileAttachments.MayContainDocuments() is true);
|
||||
@@ -87,20 +128,57 @@ public partial class ChatCompressionDialog : MSGComponentBase
|
||||
private bool HasImagesToSummarize => this.Plan.ToSummarize.Any(block => block.Content?.FileAttachments.ContainsImages() is true);
|
||||
|
||||
private string SummaryScope => this.Plan.ToSummarize.Count is 1
|
||||
? string.Format(T("'{0}' summarizes 1 message."), this.ProviderSettings.InstanceName)
|
||||
: string.Format(T("'{0}' summarizes {1} messages."), this.ProviderSettings.InstanceName, this.Plan.ToSummarize.Count);
|
||||
? string.Format(T("'{0}' summarizes 1 message."), this.writer.Provider.InstanceName)
|
||||
: string.Format(T("'{0}' summarizes {1} messages."), this.writer.Provider.InstanceName, this.Plan.ToSummarize.Count);
|
||||
|
||||
/// <summary>
|
||||
/// Why the model of the chat writes the summary instead of the model picked for compressing, or an empty string when it does not.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Told from the reason the choice reports, never guessed afterward, see AIOperationProviderIssue. Without a model
|
||||
/// picked for compressing, the model of the chat writes the summary as intended, and there is nothing to tell.
|
||||
/// </remarks>
|
||||
private string FallbackNotice => !this.writer.UsesFallback ? string.Empty : this.writer.PreferredIssue switch
|
||||
{
|
||||
AIOperationProviderIssue.NOT_SET => string.Empty,
|
||||
AIOperationProviderIssue.MISSING => T("The model picked for compressing chats no longer exists, so the model of this chat writes the summary."),
|
||||
AIOperationProviderIssue.PROHIBITED => TA("Your organization does not allow the model picked for compressing chats, so the model of this chat writes the summary."),
|
||||
AIOperationProviderIssue.BELOW_MINIMUM_CONFIDENCE => T("The model picked for compressing chats does not reach the minimum confidence level, so the model of this chat writes the summary."),
|
||||
AIOperationProviderIssue.NOT_ALLOWED_FOR_CONTENT => T("This chat holds data which the model picked for compressing chats must not see, so the model of this chat writes the summary."),
|
||||
AIOperationProviderIssue.CONTEXT_WINDOW_TOO_SMALL => T("The context window of the model picked for compressing chats is too small, so the model of this chat writes the summary."),
|
||||
|
||||
_ => T("The model picked for compressing chats cannot write this summary, so the model of this chat writes it."),
|
||||
};
|
||||
|
||||
private string ProgressMessage => this.progress.Step switch
|
||||
{
|
||||
ChatCompressionStep.SUMMARIZING when this.progress.Total > 1 => string.Format(T("The AI is summarizing part {0} of {1}..."), this.progress.Current, this.progress.Total),
|
||||
ChatCompressionStep.SUMMARIZING => T("The AI is summarizing the chat..."),
|
||||
ChatCompressionStep.CONDENSING => T("The summary grew too long. The AI is shortening it..."),
|
||||
ConversationSummaryStep.SUMMARIZING when this.progress.Total > 1 => string.Format(T("The AI is summarizing part {0} of {1}..."), this.progress.Current, this.progress.Total),
|
||||
ConversationSummaryStep.SUMMARIZING => T("The AI is summarizing the chat..."),
|
||||
ConversationSummaryStep.CONDENSING => T("The summary grew too long. The AI is shortening it..."),
|
||||
|
||||
_ => T("AI Studio is reading the messages and their files..."),
|
||||
};
|
||||
|
||||
private string? ValidateChatName(string? value) => string.IsNullOrWhiteSpace(value) ? TA("Please enter a chat name.") : null;
|
||||
|
||||
/// <summary>
|
||||
/// Reads the settings again once the user closed them through the gear.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The model which writes the summary may have changed, and so may the preselected focus. A focus the user
|
||||
/// wrote stays; one which is still the preselected focus follows the settings.
|
||||
/// </remarks>
|
||||
private void ApplyChangedSettings()
|
||||
{
|
||||
var previousFocus = this.options.PreselectedFocus;
|
||||
this.options = ChatCompressionOptions.Of(this.SettingsManager.ConfigurationData.ChatCompression);
|
||||
if (string.IsNullOrWhiteSpace(this.focus) || this.focus == previousFocus)
|
||||
this.focus = this.options.PreselectedFocus;
|
||||
|
||||
this.writer = this.ChatCompressionService.ChooseWriter(this.SourceChat, this.ChatProvider);
|
||||
this.StateHasChanged();
|
||||
}
|
||||
|
||||
private async Task HandleChatNameKeyDown(KeyboardEventArgs keyEvent)
|
||||
{
|
||||
if (keyEvent.IsPlainEnter)
|
||||
@@ -118,13 +196,25 @@ public partial class ChatCompressionDialog : MSGComponentBase
|
||||
this.progress = default;
|
||||
await this.InvokeAsync(this.StateHasChanged);
|
||||
|
||||
var progressReporter = new Progress<ChatCompressionProgress>(reported =>
|
||||
var progressReporter = new Progress<ConversationSummaryProgress>(reported =>
|
||||
{
|
||||
this.progress = reported;
|
||||
this.InvokeAsync(this.StateHasChanged).Observe($"{nameof(ChatCompressionDialog)}: showing the progress of the summary");
|
||||
});
|
||||
|
||||
var result = await this.ChatCompressionService.SummarizeAsync(this.SourceChat, this.Plan, this.ProviderSettings, this.focus, progressReporter, this.cancellationTokenSource.Token);
|
||||
var request = new ConversationSummaryRequest(this.SourceChat, this.Plan, this.focus, this.options.MaxSummaryTokens, this.ChatProvider.GetModelProfile().Context, progressReporter);
|
||||
|
||||
// Chosen again right before running, since the settings may have changed while the dialog was open:
|
||||
this.writer = this.ProviderResolver.Resolve(this.ConversationSummary, request, ConversationSummaryOperation.GetProviderSettings(this.SettingsManager), ChatCompressionService.GetProviderOptions(this.SourceChat, this.ChatProvider));
|
||||
if (!this.writer.HasProvider)
|
||||
{
|
||||
this.issue = string.Format(TA("No model may write the summary of this chat. Please select a model with a context window of at least {0} tokens."), ConversationSummaryBudget.MIN_CONTEXT_WINDOW.ToString("N0", I18N.I.Culture));
|
||||
this.isSummarizing = false;
|
||||
await this.InvokeAsync(this.StateHasChanged);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = await this.ConversationSummary.RunAsync(request, new AIOperationContext(this.writer.Provider), this.cancellationTokenSource.Token);
|
||||
|
||||
// The user canceled, and the dialog is gone already:
|
||||
if (this.cancellationTokenSource.IsCancellationRequested)
|
||||
|
||||
Reference in new issue
Block a user