Skip to main content

Trim Chat Messages Node

Trim Chat Messages Node Screenshot

Overview

The Trim Chat Messages Node is used to manage the length of chat message chains in terms of tokens. It takes an array of chat messages and trims messages from either the beginning or the end of the list until the total length of the messages is under the configured token length. This is particularly useful for setting up infinite message chains that stay under the language model's context limit.

Inputs

TitleData TypeDescriptionDefault ValueNotes
Inputchat-message[]The array of chat messages to trim.(required)

Example 1: Trim a long chat message chain

  1. Create an Array Node and set the value to an array of chat messages that exceeds a certain token count.

  2. Create a Trim Chat Messages Node and connect the Array Node to its Input port. Set the Max Token Count to a value lower than the total token count of the chat messages.

  3. Run the graph. The Trimmed output of the Trim Chat Messages Node should contain an array of chat messages with a total token count that does not exceed the configured Max Token Count.

    Trim Chat Messages Node Example 1

Error Handling

The Trim Chat Messages Node does not have any notable error handling behavior. If the input is not an array of chat messages, it will be coerced into one.

FAQ

Q: What happens if the Max Token Count is set to a value lower than the token count of a single chat message?

A: The Trim Chat Messages Node will keep removing messages until the total token count is under the Max Token Count. If a single chat message exceeds the Max Token Count, the Trimmed output will be an empty array.

Q: Can I use the Trim Chat Messages Node to trim an array of strings or other data types?

A: No, the Trim Chat Messages Node is specifically designed to work with arrays of chat messages. If you need to trim an array of other data types, you may need to use a different node or a custom solution.

See Also