Say when a chat carries more images than the model takes

This commit is contained in:
Thorsten Sommer committed 2026-09-12 19:43:18 +02:00
1 parent 4d43164577
commit 2509fc09d1
7 files changed
+151 -6

No files matched your search

@@ -3577,6 +3577,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T1992478915"] = "approx. {
-- Code
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2036185364"] = "Code"
-- plus {0} image(s), which is more than the {1} this model accepts
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2059172343"] = "plus {0} image(s), which is more than the {1} this model accepts"
-- Italic
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2377171085"] = "Italic"
@@ -61,4 +61,23 @@ public readonly record struct ConversationTokens
/// orders of magnitude longer than what any vendor charges for the picture.
/// </remarks>
public int UncountedImages { get; init; }
/// <summary>
/// How many images the model takes, where its vendor stated a number.
/// </summary>
public ImageLimits ImageLimits { get; init; }
/// <summary>
/// Whether more images travel than the model is documented to accept.
/// </summary>
/// <remarks>
/// Counted over the whole conversation rather than over the message being written, because that
/// is what a request carries: every picture anybody attached is sent again with every further
/// message, so a chat crosses this line long after the message which added the picture -- and
/// the person who crosses it has usually forgotten that the pictures are still there.
///
/// False whenever nobody stated a limit, which is most models. An invented ceiling would refuse
/// something that works.
/// </remarks>
public bool TooManyImages => this.ImageLimits.MaxInOneMessage is { } allowed && this.UncountedImages > allowed;
}
@@ -181,7 +181,16 @@ public partial class ChatComponent : MSGComponentBase
if (this.conversationTokens.UncountedImages is 0)
return budget;
return $"{budget} {string.Format(this.T("plus {0} image(s), which cannot be counted"), this.conversationTokens.UncountedImages)}";
//
// The pictures of the whole conversation, not of the message being written: every one
// of them is sent again with every further message, so a chat runs past the model's
// limit long after anybody last thought about images.
//
var images = this.conversationTokens.TooManyImages
? string.Format(this.T("plus {0} image(s), which is more than the {1} this model accepts"), this.conversationTokens.UncountedImages, this.conversationTokens.ImageLimits.MaxInOneMessage)
: string.Format(this.T("plus {0} image(s), which cannot be counted"), this.conversationTokens.UncountedImages);
return $"{budget} {images}";
}
}
@@ -665,14 +674,25 @@ public partial class ChatComponent : MSGComponentBase
/// Two steps rather than a gradient: below four fifths there is nothing to do about it, above
/// it there is -- shorten the chat, start a new one, or pick a model which reads more -- and
/// past the window the request will be refused or trimmed by the provider.
///
/// Images share the second step and have no first one. There is no "nearly too many pictures":
/// either they fit or the request comes back as an error, and no number of them is worth a
/// warning as long as it fits.
/// </remarks>
private string TokenBudgetClass => this.TokenBudgetFill switch
private string TokenBudgetClass
{
>= 1d => "token-budget-exceeded",
>= WINDOW_NEARLY_FULL => "token-budget-nearly-full",
get
{
//
// Too many pictures is the same kind of news as a full window: the request will be
// refused, and for the same reason -- more was put in than the model takes.
//
if (this.conversationTokens.TooManyImages || this.TokenBudgetFill >= 1d)
return "token-budget-exceeded";
_ => string.Empty,
};
return this.TokenBudgetFill >= WINDOW_NEARLY_FULL ? "token-budget-nearly-full" : string.Empty;
}
}
private void ApplyStandardDataSourceOptions()
{
@@ -3579,6 +3579,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T1992478915"] = "ca. {0} v
-- Code
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2036185364"] = "Code"
-- plus {0} image(s), which is more than the {1} this model accepts
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2059172343"] = "plus {0} Bild(er), also mehr als die {1}, die dieses Modell akzeptiert"
-- Italic
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2377171085"] = "Kursiv"
@@ -3579,6 +3579,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T1992478915"] = "approx. {
-- Code
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2036185364"] = "Code"
-- plus {0} image(s), which is more than the {1} this model accepts
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2059172343"] = "plus {0} image(s), which is more than the {1} this model accepts"
-- Italic
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHATCOMPONENT::T2377171085"] = "Italic"
@@ -122,6 +122,7 @@ public sealed class ConversationTokenCounter(RustService rustService, ILogger<Co
IsEstimate = string.IsNullOrWhiteSpace(provider.TokenizerPath),
Window = profile.Context,
UncountedImages = parts.Images,
ImageLimits = profile.Images,
};
}