mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 08:13:49 +00:00
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
129 lines
6.9 KiB
C#
129 lines
6.9 KiB
C#
using AIStudio.Chat;
|
|
using AIStudio.Chat.Compression;
|
|
using AIStudio.Components;
|
|
using AIStudio.Tools.PluginSystem;
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace AIStudio.Dialogs;
|
|
|
|
/// <summary>
|
|
/// Tells the user before sending that the chat or the request of an assistant fills the context
|
|
/// window of the model, or no longer fits into it, and offers to compress a chat.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Only a notice: it compresses nothing by itself, and the user may send the message anyway. The
|
|
/// numbers are those of the token count under the input field, so the dialog never disagrees with
|
|
/// what the user sees there. Open it through ShowAsync, which every place that asks before sending
|
|
/// shares.
|
|
/// </remarks>
|
|
public partial class ContextWindowFullDialog : MSGComponentBase
|
|
{
|
|
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(ContextWindowFullDialog).Namespace, nameof(ContextWindowFullDialog));
|
|
|
|
[CascadingParameter]
|
|
private IMudDialogInstance MudDialog { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// How many tokens the chat takes together with the message, formatted for the reader.
|
|
/// </summary>
|
|
[Parameter]
|
|
public string Tokens { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// How many tokens the model reads at most, formatted for the reader.
|
|
/// </summary>
|
|
[Parameter]
|
|
public string WindowTokens { get; set; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Whether the chat takes more than the window, rather than nearly all of it.
|
|
/// </summary>
|
|
[Parameter]
|
|
public bool IsWindowExceeded { get; set; }
|
|
|
|
/// <summary>
|
|
/// What is about to be sent, which decides the wording.
|
|
/// </summary>
|
|
[Parameter]
|
|
public ContextWindowFullSubject Subject { get; set; } = ContextWindowFullSubject.CHAT;
|
|
|
|
/// <summary>
|
|
/// Whether compressing is offered, or why it would not help, see ChatCompressionService.OfferBeforeSendingAsync.
|
|
/// </summary>
|
|
[Parameter]
|
|
public ChatCompressionOffer Offer { get; set; } = ChatCompressionOffer.NONE;
|
|
|
|
/// <summary>
|
|
/// Whether the user may send the message anyway; not when the chat refuses a message which no longer fits.
|
|
/// </summary>
|
|
[Parameter]
|
|
public bool CanSendAnyway { get; set; } = true;
|
|
|
|
/// <summary>
|
|
/// Asks the user what to do with a message which fills the context window of the model.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The caller decides when to ask, see ConversationTokens.AsksBeforeSending, and whether
|
|
/// compressing would help, see ChatCompressionService.OfferBeforeSendingAsync. The numbers are
|
|
/// formatted with the culture of the active language.
|
|
/// </remarks>
|
|
/// <param name="dialogService">The dialog service of the caller.</param>
|
|
/// <param name="tokens">What the conversation costs together with the message, with the context window of the model.</param>
|
|
/// <param name="subject">What is about to be sent.</param>
|
|
/// <param name="offer">Whether compressing is offered, or why it would not help; NONE where nothing compresses.</param>
|
|
/// <param name="canSendAnyway">Whether the user may send the message anyway, e.g., not where the chat refuses a message which no longer fits.</param>
|
|
/// <returns>What the user decided; CANCEL when the dialog was closed.</returns>
|
|
public static async Task<ContextWindowFullDecision> ShowAsync(IDialogService dialogService, ConversationTokens tokens, ContextWindowFullSubject subject, ChatCompressionOffer offer, bool canSendAnyway = true)
|
|
{
|
|
var isWindowExceeded = tokens.FillShare >= 1d;
|
|
var culture = I18N.I.Culture;
|
|
var dialogParameters = new DialogParameters<ContextWindowFullDialog>
|
|
{
|
|
{ x => x.Tokens, TokenAmount.Format(tokens.Tokens, culture) },
|
|
{ x => x.WindowTokens, TokenAmount.Format(tokens.Window.DefaultTokens, culture) },
|
|
{ x => x.IsWindowExceeded, isWindowExceeded },
|
|
{ x => x.Subject, subject },
|
|
{ x => x.Offer, offer },
|
|
{ x => x.CanSendAnyway, canSendAnyway },
|
|
};
|
|
|
|
var title = subject is ContextWindowFullSubject.ASSISTANT_REQUEST
|
|
? isWindowExceeded ? TB("This request is too long for the model") : TB("This request nearly fills the context window of the model")
|
|
: isWindowExceeded ? TB("This chat is too long for the model") : TB("This chat nearly fills the context window of the model");
|
|
var dialogReference = await dialogService.ShowAsync<ContextWindowFullDialog>(title, dialogParameters, DialogOptions.FULLSCREEN);
|
|
var dialogResult = await dialogReference.Result;
|
|
if (dialogResult is null || dialogResult.Canceled || dialogResult.Data is not ContextWindowFullDecision decision)
|
|
return ContextWindowFullDecision.CANCEL;
|
|
|
|
return decision;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether the dialog offers to compress, see Offer.
|
|
/// </summary>
|
|
private bool CanCompress => this.Offer is ChatCompressionOffer.OFFERED;
|
|
|
|
/// <summary>
|
|
/// Why the dialog does not offer to compress, where the place compresses but it would not help; null otherwise.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The user guide recommends compressing a long chat, so a notice which leaves it out says why. In the chat of an
|
|
/// assistant, the instructions of the assistant stay as well, and they are often the larger part.
|
|
/// </remarks>
|
|
private string? CompressionIssue => this.Offer switch
|
|
{
|
|
ChatCompressionOffer.WINDOW_TOO_SMALL => string.Format(T("Compressing is not possible with the selected model, since it needs a context window of at least {0} tokens."), ChatCompressionService.MIN_CONTEXT_WINDOW.ToString("N0", I18N.I.Culture)),
|
|
ChatCompressionOffer.MESSAGE_TOO_LONG => TA("Compressing would not help, since your message alone nearly fills the context window of the model."),
|
|
ChatCompressionOffer.NOTHING_TO_SUMMARIZE => TA("Compressing would not help yet, since there is nothing to summarize before your last message and the last answer of the AI."),
|
|
ChatCompressionOffer.KEEPS_TOO_MUCH when this.Subject is ContextWindowFullSubject.EMBEDDED_CHAT => TA("Compressing would not help, since the instructions of the assistant, your last message, and the last answer of the AI stay as they are, and together with your new message, they still nearly fill the context window of the model."),
|
|
ChatCompressionOffer.KEEPS_TOO_MUCH => TA("Compressing would not help, since the new chat keeps the system prompt, your last message with its files, and the last answer of the AI word for word, and together with your new message, they still nearly fill the context window of the model."),
|
|
_ => null,
|
|
};
|
|
|
|
private void Cancel() => this.MudDialog.Cancel();
|
|
|
|
private void SendAnyway() => this.MudDialog.Close(DialogResult.Ok(ContextWindowFullDecision.SEND_ANYWAY));
|
|
|
|
private void Compress() => this.MudDialog.Close(DialogResult.Ok(ContextWindowFullDecision.COMPRESS));
|
|
} |