Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(chat): a prompt longer than the context window says so, not "send it again"

purple8c91b440e68f
diff --git a/src/lib/__tests__/ein-zu-langer-prompt-sagt-das.test.ts b/src/lib/__tests__/ein-zu-langer-prompt-sagt-das.test.ts
new file mode 100644
index 00000000..2612626b
--- /dev/null
+++ b/src/lib/__tests__/ein-zu-langer-prompt-sagt-das.test.ts
@@ -0,0 +1,34 @@
+// T1 Nacht-Durchtest 3.0.9 (Mac, Ollama, 16K-Fenster): ein Prompt von
+// 204.062 Zeichen kam als HTTP 400 "request (42120 tokens) exceeds the
+// available context size (16384 tokens)" zurueck, und die Blase sagte den
+// allgemeinen 400-Satz: "Send the message again ... nothing was charged".
+// Nochmal senden hilft nicht, und bei einem lokalen Modell wird nichts
+// berechnet. Die Ursache stand nur im Detail darunter.
+import { describe, expect, it } from 'vitest'
+import { BAD_REQUEST_SENTENCE, CONTEXT_OVERFLOW_REFUSAL_SENTENCE, explainSendRefusal } from '../template-refusal'
+
+const overflow = (msg: string) => Object.assign(new Error(msg), { statusCode: 400 })
+
+describe('a prompt longer than the context window', () => {
+ it('says that the message is too long, not "send it again"', () => {
+ const text = explainSendRefusal(overflow('request (42120 tokens) exceeds the available context size (16384 tokens), try increasing it'))!
+ expect(text.startsWith(CONTEXT_OVERFLOW_REFUSAL_SENTENCE)).toBe(true)
+ expect(text).not.toContain(BAD_REQUEST_SENTENCE)
+ expect(text).toContain('16384 tokens')
+ })
+
+ it('names the way out the desktop has', () => {
+ expect(CONTEXT_OVERFLOW_REFUSAL_SENTENCE).toMatch(/Context window/)
+ expect(CONTEXT_OVERFLOW_REFUSAL_SENTENCE).not.toMatch(/charged|send the message again/i)
+ })
+
+ it('also for the wordings of other servers', () => {
+ for (const m of ["This model's maximum context length is 8192 tokens", 'prompt is too long: 9000 tokens > 8192']) {
+ expect(explainSendRefusal(overflow(m))!.startsWith(CONTEXT_OVERFLOW_REFUSAL_SENTENCE)).toBe(true)
+ }
+ })
+
+ it('leaves any other 400 alone', () => {
+ expect(explainSendRefusal(overflow('invalid request'))!.startsWith(BAD_REQUEST_SENTENCE)).toBe(true)
+ })
+})
diff --git a/src/lib/template-refusal.ts b/src/lib/template-refusal.ts
index dca49cdf..8a1e843d 100644
--- a/src/lib/template-refusal.ts
+++ b/src/lib/template-refusal.ts
@@ -23,6 +23,7 @@
import { detailOf, withDetail } from './error-text'
import { httpStatusOf } from './http-status'
+import { isContextOverflow } from './context-overflow'
/**
* Wordings from the templates themselves plus the wrappers the engines put
@@ -48,6 +49,14 @@ export const BAD_REQUEST_SENTENCE =
'Nothing was sent to the model, so nothing was charged. Send the message again, and if it keeps ' +
'happening, start a new chat or pick another model.'
+/** A request longer than the window of the model. Sending it again cannot
+ * help, and a local model charges nothing: the general 400 sentence said
+ * both (T1 Nacht-Durchtest 3.0.9, 204K characters into a 16K window). */
+export const CONTEXT_OVERFLOW_REFUSAL_SENTENCE =
+ 'This message is longer than the model can read at once, so nothing was generated. Shorten it, ' +
+ 'attach long text with Docs instead of pasting it, or raise the Context window above the message box ' +
+ 'if this model offers one.'
+
/** A backend that names the temperature in its refusal (A33): one LU Cloud
* model takes 0 to 1 and answered 1.9 with a bare 400. */
const TEMPERATURE_MARKERS = /\btemperature\b/i
@@ -65,6 +74,7 @@ export const TEMPERATURE_REFUSAL_SENTENCE =
export function explainSendRefusal(err: unknown): string | null {
if (isTemplateRefusal(err)) return withDetail(TEMPLATE_REFUSAL_SENTENCE, err)
if (httpStatusOf(err) === 400 && TEMPERATURE_MARKERS.test(detailOf(err))) return withDetail(TEMPERATURE_REFUSAL_SENTENCE, err)
+ if (httpStatusOf(err) === 400 && isContextOverflow(detailOf(err))) return withDetail(CONTEXT_OVERFLOW_REFUSAL_SENTENCE, err)
if (httpStatusOf(err) === 400) return withDetail(BAD_REQUEST_SENTENCE, err)
return null
}