跳至主要内容

如何过滤消息

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

在更复杂的链和代理中,我们可能会使用消息列表跟踪状态。此列表可以开始积累来自多个不同模型、发言人、子链等的 messages,并且我们可能只想将此完整 messages 列表的子集传递到链/代理中的每个模型调用。

filterMessages 实用程序使您可以轻松地按类型、ID 或名称过滤消息。

基本用法

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

const messages = [
new SystemMessage({ content: "you are a good assistant", id: "1" }),
new HumanMessage({ content: "example input", id: "2", name: "example_user" }),
new AIMessage({
content: "example output",
id: "3",
name: "example_assistant",
}),
new HumanMessage({ content: "real input", id: "4", name: "bob" }),
new AIMessage({ content: "real output", id: "5", name: "alice" }),
];

filterMessages(messages, { includeTypes: ["human"] });
[
HumanMessage {
lc_serializable: true,
lc_kwargs: {
content: 'example input',
id: '2',
name: 'example_user',
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'example input',
name: 'example_user',
additional_kwargs: {},
response_metadata: {},
id: '2'
},
HumanMessage {
lc_serializable: true,
lc_kwargs: {
content: 'real input',
id: '4',
name: 'bob',
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'real input',
name: 'bob',
additional_kwargs: {},
response_metadata: {},
id: '4'
}
]
filterMessages(messages, {
excludeNames: ["example_user", "example_assistant"],
});
[
SystemMessage {
lc_serializable: true,
lc_kwargs: {
content: 'you are a good assistant',
id: '1',
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'you are a good assistant',
name: undefined,
additional_kwargs: {},
response_metadata: {},
id: '1'
},
HumanMessage {
lc_serializable: true,
lc_kwargs: {
content: 'real input',
id: '4',
name: 'bob',
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'real input',
name: 'bob',
additional_kwargs: {},
response_metadata: {},
id: '4'
},
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: 'real output',
id: '5',
name: 'alice',
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'real output',
name: 'alice',
additional_kwargs: {},
response_metadata: {},
id: '5',
tool_calls: [],
invalid_tool_calls: [],
usage_metadata: undefined
}
]
filterMessages(messages, {
includeTypes: [HumanMessage, AIMessage],
excludeIds: ["3"],
});
[
HumanMessage {
lc_serializable: true,
lc_kwargs: {
content: 'example input',
id: '2',
name: 'example_user',
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'example input',
name: 'example_user',
additional_kwargs: {},
response_metadata: {},
id: '2'
},
HumanMessage {
lc_serializable: true,
lc_kwargs: {
content: 'real input',
id: '4',
name: 'bob',
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'real input',
name: 'bob',
additional_kwargs: {},
response_metadata: {},
id: '4'
},
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: 'real output',
id: '5',
name: 'alice',
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'real output',
name: 'alice',
additional_kwargs: {},
response_metadata: {},
id: '5',
tool_calls: [],
invalid_tool_calls: [],
usage_metadata: undefined
}
]

链接

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

import { ChatAnthropic } from "@langchain/anthropic";

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 filter_ = filterMessages({
excludeNames: ["example_user", "example_assistant"],
end,
});
const chain = filter_.pipe(llm);
await chain.invoke(messages);
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: [],
additional_kwargs: {
id: 'msg_01S2LQc1NLhtPHurW3jNRsCK',
type: 'message',
role: 'assistant',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: [Object]
},
tool_calls: [],
usage_metadata: { input_tokens: 16, output_tokens: 3, total_tokens: 19 },
invalid_tool_calls: [],
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: [],
name: undefined,
additional_kwargs: {
id: 'msg_01S2LQc1NLhtPHurW3jNRsCK',
type: 'message',
role: 'assistant',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: { input_tokens: 16, output_tokens: 3 }
},
response_metadata: {
id: 'msg_01S2LQc1NLhtPHurW3jNRsCK',
model: 'claude-3-sonnet-20240229',
stop_reason: 'end_turn',
stop_sequence: null,
usage: { input_tokens: 16, output_tokens: 3 }
},
id: undefined,
tool_calls: [],
invalid_tool_calls: [],
usage_metadata: { input_tokens: 16, output_tokens: 3, total_tokens: 19 }
}

查看 LangSmith 跟踪,我们可以看到 messages 在传递到模型之前会被过滤。

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

await filter_.invoke(messages);
[
SystemMessage {
lc_serializable: true,
lc_kwargs: {
content: 'you are a good assistant',
id: '1',
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'you are a good assistant',
name: undefined,
additional_kwargs: {},
response_metadata: {},
id: '1'
},
HumanMessage {
lc_serializable: true,
lc_kwargs: {
content: 'real input',
id: '4',
name: 'bob',
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'real input',
name: 'bob',
additional_kwargs: {},
response_metadata: {},
id: '4'
},
AIMessage {
lc_serializable: true,
lc_kwargs: {
content: 'real output',
id: '5',
name: 'alice',
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: {},
response_metadata: {}
},
lc_namespace: [ 'langchain_core', 'messages' ],
content: 'real output',
name: 'alice',
additional_kwargs: {},
response_metadata: {},
id: '5',
tool_calls: [],
invalid_tool_calls: [],
usage_metadata: undefined
}
]

API 参考

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


此页面是否有帮助?


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