fix: set high default pruning length, let server throw error unless context length known

This commit is contained in:
Dallin Romney 2025-07-19 19:39:09 +02:00
parent 643874b7d0
commit 194b802ed3
3 changed files with 16 additions and 9 deletions

View file

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

View file

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

View file

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