From 194b802ed3b1c67afeb6abfa7153401acecd51b6 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sat, 19 Jul 2025 19:39:09 +0200 Subject: [PATCH] fix: set high default pruning length, let server throw error unless context length known --- core/llm/constants.ts | 2 ++ core/llm/countTokens.ts | 10 ++++++---- core/llm/index.ts | 13 ++++++++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/core/llm/constants.ts b/core/llm/constants.ts index 90de5c64d..b6759663d 100644 --- a/core/llm/constants.ts +++ b/core/llm/constants.ts @@ -1,6 +1,7 @@ const DEFAULT_MAX_TOKENS = 4096; const DEFAULT_CONTEXT_LENGTH = 32_768; const DEFAULT_TEMPERATURE = 0.5; +const DEFAULT_PRUNING_LENGTH = 128000; const DEFAULT_ARGS = { maxTokens: DEFAULT_MAX_TOKENS, @@ -24,5 +25,6 @@ export { DEFAULT_MAX_BATCH_SIZE, DEFAULT_MAX_CHUNK_SIZE, DEFAULT_MAX_TOKENS, + DEFAULT_PRUNING_LENGTH, PROXY_URL, }; diff --git a/core/llm/countTokens.ts b/core/llm/countTokens.ts index 852638d1c..03a106144 100644 --- a/core/llm/countTokens.ts +++ b/core/llm/countTokens.ts @@ -17,6 +17,7 @@ import { import { renderChatMessage } from "../util/messageContent.js"; import { AsyncEncoder, LlamaAsyncEncoder } from "./asyncEncoder.js"; +import { DEFAULT_PRUNING_LENGTH } from "./constants.js"; import llamaTokenizer from "./llamaTokenizer.js"; interface Encoding { encode: Tiktoken["encode"]; @@ -409,14 +410,14 @@ function pruneRawPromptFromTop( function compileChatMessages({ modelName, msgs, - contextLength, + knownContextLength, maxTokens, supportsImages, tools, }: { modelName: string; msgs: ChatMessage[]; - contextLength: number; + knownContextLength: number | undefined; maxTokens: number; supportsImages: boolean; tools?: Tool[]; @@ -465,6 +466,7 @@ function compileChatMessages({ toolTokens = countToolsTokens(tools, modelName); } + const contextLength = knownContextLength ?? DEFAULT_PRUNING_LENGTH; const countingSafetyBuffer = getTokenCountingBufferSafety(contextLength); const minOutputTokens = Math.min(MIN_RESPONSE_TOKENS, maxTokens); @@ -480,12 +482,12 @@ function compileChatMessages({ inputTokensAvailable -= lastMessagesTokens; // Make sure there's enough context for the non-excludable items - if (inputTokensAvailable < 0) { + if (knownContextLength !== undefined && inputTokensAvailable < 0) { throw new Error( `Not enough context available to include the system message, last user message, and tools. There must be at least ${minOutputTokens} tokens remaining for output. Request had the following token counts: - - contextLength: ${contextLength} + - contextLength: ${knownContextLength} - counting safety buffer: ${countingSafetyBuffer} - tools: ~${toolTokens} - system message: ~${systemMsgTokens} diff --git a/core/llm/index.ts b/core/llm/index.ts index fa16b8591..4d31e0902 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -145,7 +145,7 @@ export abstract class BaseLLM implements ILLM { baseChatSystemMessage?: string; basePlanSystemMessage?: string; baseAgentSystemMessage?: string; - contextLength: number; + _contextLength: number | undefined; maxStopWords?: number | undefined; completionOptions: CompletionOptions; requestOptions?: RequestOptions; @@ -217,8 +217,7 @@ export abstract class BaseLLM implements ILLM { this.baseAgentSystemMessage = options.baseAgentSystemMessage; this.basePlanSystemMessage = options.basePlanSystemMessage; this.baseChatSystemMessage = options.baseChatSystemMessage; - this.contextLength = - options.contextLength ?? llmInfo?.contextLength ?? DEFAULT_CONTEXT_LENGTH; + this._contextLength = options.contextLength ?? llmInfo?.contextLength; this.maxStopWords = options.maxStopWords ?? this.maxStopWords; this.completionOptions = { ...options.completionOptions, @@ -292,6 +291,10 @@ export abstract class BaseLLM implements ILLM { this.autocompleteOptions = options.autocompleteOptions; } + get contextLength() { + return this._contextLength ?? DEFAULT_CONTEXT_LENGTH; + } + getConfigurationStatus() { return LLMConfigurationStatuses.VALID; } @@ -895,7 +898,7 @@ export abstract class BaseLLM implements ILLM { return compileChatMessages({ modelName: completionOptions.model, msgs: message, - contextLength: this.contextLength, + knownContextLength: this._contextLength, maxTokens: completionOptions.maxTokens ?? DEFAULT_MAX_TOKENS, supportsImages: this.supportsImages(), tools: options.tools, @@ -944,7 +947,7 @@ export abstract class BaseLLM implements ILLM { const { compiledChatMessages } = compileChatMessages({ modelName: completionOptions.model, msgs: _messages, - contextLength: this.contextLength, + knownContextLength: this._contextLength, maxTokens: completionOptions.maxTokens ?? DEFAULT_MAX_TOKENS, supportsImages: this.supportsImages(), tools: options.tools,