Sequence of Message-like objects to merge.
List of BaseMessages with consecutive runs of message types merged into single messages. 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.
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",
toolCalls: [new ToolCall({ name: "blah_tool", args: { x: 2 }, id: "123" })],
id: "baz",
}),
new AIMessage({
content: [{ type: "text", text: "my favorite dish is lasagna" }],
toolCalls: [new ToolCall({ 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 color\nwait your favorite food", id: "foo" }),
new AIMessage({
content: [
"my favorite colo",
{ type: "text", text: "my favorite dish is lasagna" }
],
toolCalls: [
new ToolCall({ name: "blah_tool", args: { x: 2 }, id: "123" }),
new ToolCall({ name: "blah_tool", args: { x: -10 }, id: "456" })
],
id: "baz"
}),
]
Generated using TypeDoc
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.