using System.Linq.Expressions; using AIStudio.Provider; namespace AIStudio.Settings.DataModel; /// /// The settings of compressing a chat, and of what happens when a chat fills the context window of its model. /// /// /// The summary is written by an AI task. Unlike the other AI tasks, it has no switch for its preselection: /// without a provider of its own, the model of the chat writes the summary, and that model saw everything /// the summary reads anyway. /// public sealed class DataChatCompression(Expression>? configSelection = null) { /// /// The lowest share of the context window, in percent, from which AI Studio acts before sending. /// /// /// A summary takes up to a quarter of the window. Below half of it, a compressed chat could reach the /// share again right away, and compressing would never be offered. /// public const int MIN_CONTEXT_WINDOW_THRESHOLD_PERCENT = 50; /// /// The highest share of the context window, in percent, from which AI Studio acts before sending. /// /// /// The rest of the window holds the answer of the model. /// public const int MAX_CONTEXT_WINDOW_THRESHOLD_PERCENT = 95; /// /// The fewest tokens which a summary may be limited to. /// public const int MIN_SUMMARY_TOKENS = 1_024; /// /// The most tokens which a summary may be limited to. /// public const int MAX_SUMMARY_TOKENS = 32_768; /// /// The default constructor for the JSON deserializer. /// public DataChatCompression() : this(null) { } /// /// The provider which writes the summary; empty for the model of the chat. /// public string PreselectedProvider { get; set; } = ManagedConfiguration.Register(configSelection, n => n.PreselectedProvider, string.Empty); /// /// The minimum confidence level of the provider which writes the summary. /// /// /// It applies to the preselected provider only. When the model of the chat writes the summary instead, /// the minimum does not apply: that model saw everything the summary reads anyway. /// public ConfidenceLevel MinimumProviderConfidence { get; set; } = ManagedConfiguration.Register(configSelection, n => n.MinimumProviderConfidence, ConfidenceLevel.NONE); /// /// What the summary should pay particular attention to, filled into the dialog of compressing; empty for nothing. /// public string PreselectedFocus { get; set; } = ManagedConfiguration.Register(configSelection, n => n.PreselectedFocus, string.Empty); /// /// The share of the context window, in percent, from which AI Studio acts before sending a message. /// /// /// Between MIN_CONTEXT_WINDOW_THRESHOLD_PERCENT and MAX_CONTEXT_WINDOW_THRESHOLD_PERCENT. What AI Studio /// does then decides ContextWindowFullBehavior. /// public int ContextWindowThresholdPercent { get; set; } = ManagedConfiguration.Register(configSelection, n => n.ContextWindowThresholdPercent, MAX_CONTEXT_WINDOW_THRESHOLD_PERCENT); /// /// The most tokens a summary should take. /// /// /// Between MIN_SUMMARY_TOKENS and MAX_SUMMARY_TOKENS. A summary never takes more than a quarter of the /// context window of the model which writes it, nor of the model of the chat.

/// /// Context windows of 250,000 tokens and more are common by now. A summary of 8,192 tokens takes a small /// share of them, yet keeps room for the details of a long conversation. A summary meant to save tokens has /// no business being much longer anyway. ///
public int MaxSummaryTokens { get; set; } = ManagedConfiguration.Register(configSelection, n => n.MaxSummaryTokens, 8_192); /// /// What AI Studio does when a message would fill the context window of the model. /// public ContextWindowFullBehavior ContextWindowFullBehavior { get; set; } = ManagedConfiguration.Register(configSelection, n => n.ContextWindowFullBehavior, ContextWindowFullBehavior.ASK); /// /// Whether a share of the context window is one AI Studio may act from. /// /// The share in percent. /// True between MIN_CONTEXT_WINDOW_THRESHOLD_PERCENT and MAX_CONTEXT_WINDOW_THRESHOLD_PERCENT. public static bool IsValidContextWindowThresholdPercent(int percent) => percent is >= MIN_CONTEXT_WINDOW_THRESHOLD_PERCENT and <= MAX_CONTEXT_WINDOW_THRESHOLD_PERCENT; /// /// Whether a number of tokens is one a summary may be limited to. /// /// The tokens. /// True between MIN_SUMMARY_TOKENS and MAX_SUMMARY_TOKENS. public static bool IsValidMaxSummaryTokens(int tokens) => tokens is >= MIN_SUMMARY_TOKENS and <= MAX_SUMMARY_TOKENS; }