• Filter messages based on name, type or id.

    Parameters

    • messages: BaseMessage[]

      Sequence of BaseMessage objects to filter.

    • options: {
          excludeIds?: string[];
          excludeNames?: string[];
          excludeTypes?: ("function" | BaseMessage | "human" | "ai" | "generic" | "system" | "tool")[];
          includeIds?: string[];
          includeNames?: string[];
          includeTypes?: ("function" | BaseMessage | "human" | "ai" | "generic" | "system" | "tool")[];
      } = {}

      Optional filtering options.

      • Optional excludeIds?: string[]

        Message IDs to exclude.

      • Optional excludeNames?: string[]

        Messages names to exclude.

      • Optional excludeTypes?: ("function" | BaseMessage | "human" | "ai" | "generic" | "system" | "tool")[]

        Message types to exclude. Can be specified as string names (e.g. "system", "human", "ai", ...) or as BaseMessage classes (e.g. SystemMessage, HumanMessage, AIMessage, ...).

      • Optional includeIds?: string[]

        Message IDs to include.

      • Optional includeNames?: string[]

        Message names to include.

      • Optional includeTypes?: ("function" | BaseMessage | "human" | "ai" | "generic" | "system" | "tool")[]

        Message types to include. Can be specified as string names (e.g. "system", "human", "ai", ...) or as BaseMessage classes (e.g. SystemMessage, HumanMessage, AIMessage, ...).

    Returns BaseMessage[]

    A list of Messages that meets at least one of the include conditions and none of the exclude conditions. If no include conditions are specified then anything that is not explicitly excluded will be included.

    Throws

    If two incompatible arguments are provided.

    Example

    import { filterMessages, AIMessage, HumanMessage, SystemMessage } from "@langchain/core/messages";

    const messages = [
    new SystemMessage("you're a good assistant."),
    new HumanMessage({ content: "what's your name", id: "foo", name: "example_user" }),
    new AIMessage({ content: "steve-o", id: "bar", name: "example_assistant" }),
    new HumanMessage({ content: "what's your favorite color", id: "baz" }),
    new AIMessage({ content: "silicon blue" , id: "blah" }),
    ];

    filterMessages(messages, {
    includeNames: ["example_user", "example_assistant"],
    includeTypes: ["system"],
    excludeIds: ["bar"],
    });

    The above example would return:

    [
    new SystemMessage("you're a good assistant."),
    new HumanMessage({ content: "what's your name", id: "foo", name: "example_user" }),
    ]

Generated using TypeDoc