using AIStudio.Chat; namespace AIStudio.Assistants.BeyondFactsLab; /// /// Decides how much room a chat of the BeyondFacts Lab has left in the context of its model. /// /// /// Every request carries the guide of the lab, so a conversation fills the context faster than an ordinary chat. /// A model which loses the beginning of its context loses its instructions first; therefore, the lab stops before. /// public static class BflTokenBudget { /// /// From which share of the context on the lab warns. /// public const double WARNING_SHARE = 0.8; /// /// How many tokens the lab keeps free for the next message and its answer. /// public const int RESERVE_TOKENS = 4_096; /// /// Decides how much room a chat has left. /// /// What the conversation costs, with the system prompt, and the context of the model. /// The state of the budget. public static BflTokenBudgetState Of(ConversationTokens tokens) { if (!tokens.IsKnown || !tokens.Window.IsKnown) return BflTokenBudgetState.UNKNOWN; var window = tokens.Window.DefaultTokens; if (tokens.Tokens > window - RESERVE_TOKENS) return BflTokenBudgetState.FULL; return tokens.Tokens >= window * WARNING_SHARE ? BflTokenBudgetState.WARNING : BflTokenBudgetState.OK; } }