• Merge consecutive Messages of the same type.

    NOTE: ToolMessages are not merged, as each has a distinct tool call id that can't be merged.

    Returns Runnable<BaseMessage[], BaseMessage[]>

    List of BaseMessages with consecutive runs of message types merged into single messages, or a RunnableLambda which returns a list of BaseMessages If two messages being merged both have string contents, the merged content is a concatenation of the two strings with a new-line separator. If at least one of the messages has a list of content blocks, the merged content is a list of content blocks.

    Example

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

    const messages = [
    new SystemMessage("you're a good assistant."),
    new HumanMessage({ content: "what's your favorite color", id: "foo" }),
    new HumanMessage({ content: "wait your favorite food", id: "bar" }),
    new AIMessage({
    content: "my favorite colo",
    tool_calls: [{ name: "blah_tool", args: { x: 2 }, id: "123" }],
    id: "baz",
    }),
    new AIMessage({
    content: [{ type: "text", text: "my favorite dish is lasagna" }],
    tool_calls: [{ name: "blah_tool", args: { x: -10 }, id: "456" }],
    id: "blur",
    }),
    ];

    mergeMessageRuns(messages);

    The above example would return:

    [
    new SystemMessage("you're a good assistant."),
    new HumanMessage({
    content: "what's your favorite colorwait your favorite food",
    id: "foo",
    }),
    new AIMessage({
    content: [
    { type: "text", text: "my favorite colo" },
    { type: "text", text: "my favorite dish is lasagna" },
    ],
    tool_calls: [
    { name: "blah_tool", args: { x: 2 }, id: "123" },
    { name: "blah_tool", args: { x: -10 }, id: "456" },
    ],
    id: "baz",
    }),
    ]
  • Parameters

    Returns BaseMessage[]

Generated using TypeDoc