合并消息运行#
- langchain_core.messages.utils.merge_message_runs(messages: Sequence[MessageLikeRepresentation] | None = None, **kwargs: Any) list[BaseMessage] | Runnable[Sequence[MessageLikeRepresentation], list[BaseMessage]] [source]#
合并相同类型的连续消息。
注意: ToolMessages 不会被合并,因为每个都有一个独特的工具调用 ID,无法合并。
- Args:
消息:要合并的类似消息对象的序列。 chunk_separator:指定在消息块之间插入的字符串。 默认是“
“.
- Returns:
将连续运行的消息类型合并为单个消息的BaseMessages列表。默认情况下,如果两个要合并的消息都有字符串内容,则合并后的内容是这两个字符串的串联,中间用换行符分隔。可以通过指定
chunk_separator
来控制插入在消息块之间的分隔符。如果至少有一个消息具有内容块列表,则合并后的内容是内容块列表。- Example:
from langchain_core.messages import ( merge_message_runs, AIMessage, HumanMessage, SystemMessage, ToolCall, ) messages = [ SystemMessage("you're a good assistant."), HumanMessage("what's your favorite color", id="foo",), HumanMessage("wait your favorite food", id="bar",), AIMessage( "my favorite colo", tool_calls=[ToolCall(name="blah_tool", args={"x": 2}, id="123", type="tool_call")], id="baz", ), AIMessage( [{"type": "text", "text": "my favorite dish is lasagna"}], tool_calls=[ToolCall(name="blah_tool", args={"x": -10}, id="456", type="tool_call")], id="blur", ), ] merge_message_runs(messages)
[ SystemMessage("you're a good assistant."), HumanMessage("what's your favorite color\nwait your favorite food", id="foo",), AIMessage( [ "my favorite colo", {"type": "text", "text": "my favorite dish is lasagna"} ], tool_calls=[ ToolCall({"name": "blah_tool", "args": {"x": 2}, "id": "123", "type": "tool_call"}), ToolCall({"name": "blah_tool", "args": {"x": -10}, "id": "456", "type": "tool_call"}) ] id="baz" ), ]
- Parameters:
messages (Union[Sequence[MessageLikeRepresentation], None])
kwargs (Any)
- Return type:
联合[列表[BaseMessage], Runnable[序列[MessageLikeRepresentation], 列表[BaseMessage]]]
使用 merge_message_runs 的示例