Say when a chat carries more images than the model takes

This commit is contained in:
Thorsten Sommer 2026-09-12 19:41:28 +02:00
parent 4d43164577
commit 2509fc09d1
Signed by untrusted user who does not match committer: tsommer
GPG Key ID: 371BBA77A02C0108
7 changed files with 151 additions and 6 deletions

View File

@ -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"

View File

@ -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;
}

View File

@ -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()
{

View File

@ -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"

View File

@ -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"

View File

@ -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,
};
}

View File

@ -0,0 +1,96 @@
using AIStudio.Chat;
using AIStudio.Models;
namespace AIStudio.Tests.Chat;
/// <summary>
/// Checks what the chat says about the images a conversation carries.
/// </summary>
/// <remarks>
/// The visual briefing refuses a build with too many pictures, because that build is expensive and
/// fails late. A chat cannot refuse anything: the pictures are already in the conversation, and
/// taking them back out means deleting messages. So the chat says so instead, and what is checked
/// here is that it says so at the right moment -- and, more importantly, that it stays quiet when
/// nobody wrote a limit down.
/// </remarks>
[TestFixture]
public sealed class ConversationTokensTests
{
[TestCase(1, 100, false)]
[TestCase(100, 100, false, Description = "Exactly the limit still fits. It is a maximum, not a threshold.")]
[TestCase(101, 100, true)]
[TestCase(3_601, 3_600, true)]
public void TooManyPicturesIsAQuestionOfTheNumberTheVendorStated(int images, int allowed, bool tooMany)
{
var counted = new ConversationTokens
{
IsKnown = true,
UncountedImages = images,
ImageLimits = new ImageLimits(null, allowed),
};
Assert.That(counted.TooManyImages, Is.EqualTo(tooMany));
}
[Test]
public void WithoutAStatedLimitThereIsNoSuchThingAsTooMany()
{
//
// The common case. Most models are served at whatever their operator configured, and an app
// which warned about the seventh picture would be inventing a ceiling nobody wrote.
//
var counted = new ConversationTokens
{
IsKnown = true,
UncountedImages = 500,
ImageLimits = ImageLimits.UNKNOWN,
};
Assert.That(counted.TooManyImages, Is.False);
}
[Test]
public void TheSmallerOfTwoStatedLimitsIsTheOneWhichDecides()
{
//
// A message is part of a request, so a conversation which fits the request limit can still
// be too much for one message. Both are compared against the same number of pictures,
// because a chat sends all of them in one message.
//
var counted = new ConversationTokens
{
IsKnown = true,
UncountedImages = 20,
ImageLimits = new ImageLimits(8, 100),
};
Assert.Multiple(() =>
{
Assert.That(counted.TooManyImages, Is.True);
Assert.That(counted.ImageLimits.MaxInOneMessage, Is.EqualTo(8));
});
}
[Test]
public void AConversationWithoutPicturesNeverComplainsAboutThem()
{
var counted = new ConversationTokens
{
IsKnown = true,
UncountedImages = 0,
ImageLimits = new ImageLimits(null, 0),
};
Assert.That(counted.TooManyImages, Is.False, "Not even against a model which takes none at all.");
}
[Test]
public void AnUnavailableCountClaimsNothingAboutPictures()
{
//
// Nothing could be counted, so nothing is known -- including how many pictures travel. A
// warning built on that would be made up.
//
Assert.That(ConversationTokens.UNAVAILABLE.TooManyImages, Is.False);
}
}