跳到主要内容

如何合并连续的相同类型消息

mergeMessageRuns 函数在 @langchain/core 版本 0.2.8 及更高版本中可用。

某些模型不支持传递连续的相同类型消息(也称为“相同类型消息的运行”)。

mergeMessageRuns 实用程序可以轻松合并连续的相同类型消息。

基本用法

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

const messages = [
new SystemMessage("you're a good assistant."),
new SystemMessage("you always respond with a joke."),
new HumanMessage({
content: [{ type: "text", text: "i wonder why it's called langchain" }],
}),
new HumanMessage("and who is harrison chasing anyways"),
new AIMessage(
'Well, I guess they thought "WordRope" and "SentenceString" just didn\'t have the same ring to it!'
),
new AIMessage(
"Why, he's probably chasing after the last cup of coffee in the office!"
),
];

const merged = mergeMessageRuns(messages);
console.log(
merged
.map((x) =>
JSON.stringify(
{
role: x._getType(),
content: x.content,
},
null,
2
)
)
.join("\n\n")
);
{
"role": "system",
"content": "you're a good assistant.\nyou always respond with a joke."
}

{
"role": "human",
"content": [
{
"type": "text",
"text": "i wonder why it's called langchain"
},
{
"type": "text",
"text": "and who is harrison chasing anyways"
}
]
}

{
"role": "ai",
"content": "Well, I guess they thought \"WordRope\" and \"SentenceString\" just didn't have the same ring to it!\nWhy, he's probably chasing after the last cup of coffee in the office!"
}

请注意,如果要合并的消息之一的内容是内容块列表,则合并后的消息将包含内容块列表。如果要合并的两个消息都包含字符串内容,则将使用换行符将它们连接起来。

链接

mergeMessageRuns 可以以命令式(如上所述)或声明式方式使用,使其易于与链中的其他组件进行组合。

import { ChatAnthropic } from "@langchain/anthropic";
import { mergeMessageRuns } from "@langchain/core/messages";

const llm = new ChatAnthropic({
model: "claude-3-sonnet-20240229",
temperature: 0,
});
// Notice we don't pass in messages. This creates
// a RunnableLambda that takes messages as input
const merger = mergeMessageRuns();
const chain = merger.pipe(llm);
await chain.invoke(messages);
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: [],
additional_kwargs: {
id: 'msg_01LsdS4bjQ3EznH7Tj4xujV1',
type: 'message',
role: 'assistant',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: [Object]
},
tool_calls: [],
usage_metadata: { input_tokens: 84, output_tokens: 3, total_tokens: 87 },
invalid_tool_calls: [],
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: [],
name: undefined,
additional_kwargs: {
id: 'msg_01LsdS4bjQ3EznH7Tj4xujV1',
type: 'message',
role: 'assistant',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: { input_tokens: 84, output_tokens: 3 }
},
response_metadata: {
id: 'msg_01LsdS4bjQ3EznH7Tj4xujV1',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: { input_tokens: 84, output_tokens: 3 }
},
id: undefined,
tool_calls: [],
invalid_tool_calls: [],
usage_metadata: { input_tokens: 84, output_tokens: 3, total_tokens: 87 }
}

查看LangSmith 跟踪,我们可以看到消息在传递给模型之前已合并。

仅查看合并器,我们可以看到它是一个 Runnable 对象,可以像所有 Runnable 一样调用。

await merger.invoke(messages);
[
SystemMessage {
lc_serializable: true,
lc_kwargs: {
content: "you're a good assistant.\nyou always respond with a joke.",
name: undefined,
additional_kwargs: {},
response_metadata: {},
id: undefined
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: "you're a good assistant.\nyou always respond with a joke.",
name: undefined,
additional_kwargs: {},
response_metadata: {},
id: undefined
},
HumanMessage {
lc_serializable: true,
lc_kwargs: {
content: [Array],
name: undefined,
additional_kwargs: {},
response_metadata: {},
id: undefined
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: [ [Object], [Object] ],
name: undefined,
additional_kwargs: {},
response_metadata: {},
id: undefined
},
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: `Well, I guess they thought "WordRope" and "SentenceString" just didn't have the same ring to it!\n` +
"Why, he's probably chasing after the last cup of coffee in the office!",
name: undefined,
additional_kwargs: {},
response_metadata: {},
id: undefined,
tool_calls: [],
invalid_tool_calls: [],
usage_metadata: undefined
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: `Well, I guess they thought "WordRope" and "SentenceString" just didn't have the same ring to it!\n` +
"Why, he's probably chasing after the last cup of coffee in the office!",
name: undefined,
additional_kwargs: {},
response_metadata: {},
id: undefined,
tool_calls: [],
invalid_tool_calls: [],
usage_metadata: undefined
}
]

API 参考

有关所有参数的完整描述,请前往API 参考


此页面是否有帮助?


您也可以在 GitHub 上留下详细的反馈 GitHub.