interface TrimMessagesFields {
    maxTokens: number;
    tokenCounter: BaseLanguageModel<any, BaseLanguageModelCallOptions> | ((messages: BaseMessage[]) => number) | ((messages: BaseMessage[]) => Promise<number>);
    allowPartial?: boolean;
    endOn?: MessageTypeOrClass | MessageTypeOrClass[];
    includeSystem?: boolean;
    startOn?: MessageTypeOrClass | MessageTypeOrClass[];
    strategy?: "first" | "last";
    textSplitter?: ((text: string) => string[]) | ((text: string) => Promise<string[]>) | _TextSplitterInterface;
}

Properties

maxTokens: number

Max token count of trimmed messages.

tokenCounter: BaseLanguageModel<any, BaseLanguageModelCallOptions> | ((messages: BaseMessage[]) => number) | ((messages: BaseMessage[]) => Promise<number>)

Function or LLM for counting tokens in an array of BaseMessages. If a BaseLanguageModel is passed in then BaseLanguageModel.getNumTokens() will be used.

allowPartial?: boolean

Whether to split a message if only part of the message can be included. If strategy: "last" then the last partial contents of a message are included. If strategy: "first" then the first partial contents of a message are included.

false

The message type to end on. If specified then every message after the last occurrence of this type is ignored. If strategy === "last" then this is done before we attempt to get the last maxTokens. If strategy === "first" then this is done after we get the first maxTokens. Can be specified as string names (e.g. "system", "human", "ai", ...) or as BaseMessage classes (e.g. SystemMessage, HumanMessage, AIMessage, ...). Can be a single type or an array of types.

includeSystem?: boolean

Whether to keep the SystemMessage if there is one at index 0. Should only be specified if strategy: "last".

false

The message type to start on. Should only be specified if strategy: "last". If specified then every message before the first occurrence of this type is ignored. This is done after we trim the initial messages to the last maxTokens. Does not apply to a SystemMessage at index 0 if includeSystem: true. Can be specified as string names (e.g. "system", "human", "ai", ...) or as BaseMessage classes (e.g. SystemMessage, HumanMessage, AIMessage, ...). Can be a single type or an array of types.

strategy?: "first" | "last"

Strategy for trimming.

  • "first": Keep the first <= n_count tokens of the messages.
  • "last": Keep the last <= n_count tokens of the messages.
"last"
textSplitter?: ((text: string) => string[]) | ((text: string) => Promise<string[]>) | _TextSplitterInterface

Function or BaseDocumentTransformer for splitting the string contents of a message. Only used if allowPartial: true. If strategy: "last" then the last split tokens from a partial message will be included. If strategy: "first" then the first split tokens from a partial message will be included. Token splitter assumes that separators are kept, so that split contents can be directly concatenated to recreate the original text. Defaults to splitting on newlines.

""