如何过滤消息
filterMessages
函数在 @langchain/core
版本 0.2.8
及更高版本中可用。
在更复杂的链和代理中,我们可能会使用消息列表来跟踪 state。此列表可以开始累积来自多个不同模型、发言者、子链等的 消息,我们可能只想将此完整消息列表的子集传递给链/代理中的每次模型调用。
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 跟踪,我们可以看到,在将消息传递给模型之前,会对其进行过滤。
仅查看过滤器_,我们可以看到它是一个 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 参考。