mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-13 14:13:48 +00:00
Fixed the exact token count outliving the conversation it was reported for
This commit is contained in:
6 files changed
+239
-52
No files matched your search
@@ -392,6 +392,48 @@ public sealed record ChatThread
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds what a provider reported for this conversation, as long as the report still describes it.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A report describes one request: the conversation up to the answer which carries it. It is
|
||||
/// worth something only while the thread still is that conversation, so only the last block is
|
||||
/// asked, and no earlier answer ever stands in for it. Whatever came after an older report -- a
|
||||
/// message whose request was turned down, an answer which is still being written, an answer
|
||||
/// without a report of its own -- is missing from that report's number, and the estimate is
|
||||
/// closer to the truth than a figure which leaves it out. Answers written while tools were
|
||||
/// offered carry no report yet, so for them the estimate always takes over.<br/><br/>
|
||||
///
|
||||
/// A report also stops counting when the thread holds a different number of blocks than the
|
||||
/// request did, which means an earlier message was deleted, and when the next request goes to
|
||||
/// another model, which counts the same conversation with another tokenizer. Editing the last
|
||||
/// message or rolling the chat back makes an earlier answer the last block again, with exactly
|
||||
/// the blocks it was reported for, so its report counts once more.<br/><br/>
|
||||
///
|
||||
/// What goes unnoticed is a change beside the messages: another system prompt, profile, or
|
||||
/// selection of tools. That shows only with the next answer. Noticing it would take a
|
||||
/// fingerprint of the system prompt as it was sent, after the data sources added to it, which
|
||||
/// is a lot of machinery for a number which corrects itself one answer later.
|
||||
/// </remarks>
|
||||
/// <param name="model">The model the next request would go to.</param>
|
||||
/// <returns>What the provider reported, or unknown when no report describes this conversation.</returns>
|
||||
public TokenUsage ReportedUsageFor(Model model)
|
||||
{
|
||||
if (this.Blocks.Count is 0)
|
||||
return TokenUsage.UNKNOWN;
|
||||
|
||||
if (this.Blocks[^1].Content is not ContentText { IsStreaming: false, ReportedUsage: { } reported })
|
||||
return TokenUsage.UNKNOWN;
|
||||
|
||||
if (reported.BlockCount != this.Blocks.Count)
|
||||
return TokenUsage.UNKNOWN;
|
||||
|
||||
if (!string.Equals(reported.ModelId, model.Id, StringComparison.Ordinal))
|
||||
return TokenUsage.UNKNOWN;
|
||||
|
||||
return reported.ToTokenUsage();
|
||||
}
|
||||
|
||||
private static void DeleteManagedAttachments(ContentBlock block)
|
||||
{
|
||||
if (block.Content is not ContentText textContent)
|
||||
|
||||
@@ -58,10 +58,12 @@ public sealed class ContentText : IContent
|
||||
/// <remarks>
|
||||
/// Kept on the answer rather than beside the chat, so that it is stored, loaded, and exported
|
||||
/// with the message it belongs to -- and so that it goes away when the message does. An edited
|
||||
/// or regenerated answer removes its block, which removes these numbers with it, and the chat
|
||||
/// falls back to the estimate instead of carrying a figure for a conversation which no longer
|
||||
/// exists. Null for every answer written before this was recorded, and at every provider which
|
||||
/// reports nothing.
|
||||
/// or regenerated answer removes its block, which removes these numbers with it. Null for every
|
||||
/// answer written before this was recorded, and at every provider which reports nothing.
|
||||
///
|
||||
/// Having a report is not the same as the report still being true. Whether it still describes
|
||||
/// the conversation is decided by ChatThread.ReportedUsageFor, which looks at the thread around
|
||||
/// the answer, not just at the answer.
|
||||
/// </remarks>
|
||||
public ReportedTokenUsage? ReportedUsage { get; set; }
|
||||
|
||||
@@ -121,7 +123,8 @@ public sealed class ContentText : IContent
|
||||
/// </remarks>
|
||||
/// <param name="usage">What the current chunk of the stream says, which is mostly nothing.</param>
|
||||
/// <param name="modelId">The model the request went to.</param>
|
||||
public void RecordReportedUsage(TokenUsage usage, string modelId)
|
||||
/// <param name="blockCount">How many blocks the conversation had when the request went out, this answer included.</param>
|
||||
public void RecordReportedUsage(TokenUsage usage, string modelId, int blockCount)
|
||||
{
|
||||
if (!usage.IsKnown)
|
||||
return;
|
||||
@@ -131,6 +134,7 @@ public sealed class ContentText : IContent
|
||||
PromptTokens = usage.PromptTokens,
|
||||
CompletionTokens = usage.CompletionTokens,
|
||||
ModelId = modelId,
|
||||
BlockCount = blockCount,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -194,7 +198,10 @@ public sealed class ContentText : IContent
|
||||
|
||||
// Get the settings manager:
|
||||
var settings = Program.SERVICE_PROVIDER.GetService<SettingsManager>()!;
|
||||
|
||||
|
||||
// What the request carries, for telling later whether a report still describes this chat:
|
||||
var blocksSent = chatThread.Blocks.Count;
|
||||
|
||||
// Start another thread by using a task to uncouple
|
||||
// the UI thread from the AI processing:
|
||||
try
|
||||
@@ -224,7 +231,7 @@ public sealed class ContentText : IContent
|
||||
this.Sources.MergeSources(contentStreamChunk.Sources);
|
||||
|
||||
// Keep what the provider says the request cost:
|
||||
this.RecordReportedUsage(contentStreamChunk.Usage, chatModel.Id);
|
||||
this.RecordReportedUsage(contentStreamChunk.Usage, chatModel.Id, blocksSent);
|
||||
|
||||
// Notify the UI that the content has changed,
|
||||
// depending on the energy saving mode:
|
||||
@@ -349,6 +356,12 @@ public sealed class ContentText : IContent
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <remarks>
|
||||
/// The reported usage stays behind on purpose. A clone continues somewhere else -- as the
|
||||
/// example conversation of a chat template, or as an assistant's conversation carried over into
|
||||
/// a chat -- with another system prompt around it, and what the provider stated was for the
|
||||
/// request this answer came out of.
|
||||
/// </remarks>
|
||||
public IContent DeepClone() => new ContentText
|
||||
{
|
||||
Text = this.Text,
|
||||
|
||||
@@ -37,6 +37,20 @@ public sealed record ReportedTokenUsage
|
||||
/// </remarks>
|
||||
public string ModelId { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// How many blocks the conversation had when the request went out, this answer included.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// What tells an outdated report apart without anybody having to remember anything about it.
|
||||
/// Blocks are only ever added at the end, so while this answer is the last block, a thread with
|
||||
/// the same count is the thread the provider saw. A lower count means an earlier message was
|
||||
/// deleted, and that message is still inside the reported number.
|
||||
///
|
||||
/// Taken when the request is sent rather than when the report arrives: whatever is deleted
|
||||
/// while the answer streams in was still part of what the provider counted.
|
||||
/// </remarks>
|
||||
public int BlockCount { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// States the stored numbers as a usage again.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user