news 2026/6/3 11:52:47

LangChain Messages模块

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
LangChain Messages模块

1. 消息核心概念

消息(Messages)是LangChain中模型交互的基本上下文单元,代表着模型输入和输出的结构化数据载体。每个消息对象包含三个核心组成部分:

  • 角色(Role):标识消息类型(如system、user、assistant)
  • 内容(Content):消息的实际内容,支持文本、图像、音频、文档等
  • 元数据(Metadata):可选字段,如响应信息、消息ID和令牌使用情况

在LangChain架构中,消息既是对话状态的表现形式,也是模型调用的标准接口,为LLM交互提供了统一的抽象层。

2. 消息类型详解

2.1 系统消息(SystemMessage)

系统消息用于引导模型行为设置交互上下文,是对话的初始化指令:

fromlangchain.messagesimportSystemMessage# 基础指令system_msg=SystemMessage("You are a helpful coding assistant.")# 详细角色定义detailed_system_msg=SystemMessage(""" You are a senior Python developer with expertise in web frameworks. Always provide code examples and explain your reasoning. Be concise but thorough in your explanations. """)

2.2 人类消息(HumanMessage)

人类消息代表用户输入,支持多模态内容:

fromlangchain.messagesimportHumanMessage# 文本内容human_msg=HumanMessage("What is machine learning?")# 带元数据的人类消息human_msg_with_metadata=HumanMessage(content="Hello!",name="alice",# 可选:标识不同用户id="msg_123",# 可选:用于追踪的唯一标识符)

2.3 AI消息(AIMessage)

AI消息表示模型输出,包含响应内容、工具调用和元数据:

fromlangchain.messagesimportAIMessage# 从模型获取的响应response=model.invoke("Explain AI")print(type(response))# <class 'langchain.messages.AIMessage'># 手动创建AI消息(用于对话历史)manual_ai_msg=AIMessage("I'd be happy to help you with that question!")

2.4 工具消息(ToolMessage)

工具消息用于传递工具执行结果给模型:

fromlangchain.messagesimportToolMessage# 创建工具消息tool_message=ToolMessage(content="Sunny, 72°F",# 工具执行结果tool_call_id="call_123",# 必须与AI消息中的工具调用ID匹配name="get_weather",# 被调用工具的名称artifact={"document_id":"doc_123","page":0}# 不发送给模型的附加数据)

3. 基本使用方式

3.1 三种调用格式

文本提示(适用于单次独立请求):

response=model.invoke("Write a haiku about spring")

消息对象列表(适用于多轮对话):

fromlangchain.messagesimportSystemMessage,HumanMessage,AIMessage messages=[SystemMessage("You are a poetry expert"),HumanMessage("Write a haiku about spring"),AIMessage("Cherry blossoms bloom...")]response=model.invoke(messages)

字典格式(OpenAI兼容格式):

messages=[{"role":"system","content":"You are a poetry expert"},{"role":"user","content":"Write a haiku about spring"},{"role":"assistant","content":"Cherry blossoms bloom..."}]response=model.invoke(messages)

3.2 完整工作流程示例

fromlangchain.chat_modelsimportinit_chat_modelfromlangchain.messagesimportHumanMessage,AIMessage,SystemMessage# 初始化模型model=init_chat_model("gpt-5-nano")# 创建消息序列system_msg=SystemMessage("You are a helpful assistant.")human_msg=HumanMessage("Hello, how are you?")# 调用模型messages=[system_msg,human_msg]response=model.invoke(messages)# 返回AIMessage

4. 高级功能特性

4.1 工具调用

当模型绑定工具时,AI消息中会包含工具调用信息:

fromlangchain.chat_modelsimportinit_chat_model model=init_chat_model("gpt-5-nano")# 定义工具函数defget_weather(location:str)->str:"""Get the weather at a location."""...# 绑定工具并调用model_with_tools=model.bind_tools([get_weather])response=model_with_tools.invoke("What's the weather in Paris?")# 处理工具调用fortool_callinresponse.tool_calls:print(f"Tool:{tool_call['name']}")print(f"Args:{tool_call['args']}")print(f"ID:{tool_call['id']}")

4.2 令牌使用统计

AI消息包含详细的令牌使用元数据:

response=model.invoke("Hello!")print(response.usage_metadata)# 输出示例:# {# 'input_tokens': 8,# 'output_tokens': 304,# 'total_tokens': 312,# 'input_token_details': {'audio': 0, 'cache_read': 0},# 'output_token_details': {'audio': 0, 'reasoning': 256}# }

4.3 流式响应处理

处理流式响应时,接收的是AIMessageChunk对象:

chunks=[]full_message=Noneforchunkinmodel.stream("Hi"):chunks.append(chunk)print(chunk.text)full_message=chunkiffull_messageisNoneelsefull_message+chunk

5. 内容格式详解

5.1 内容表示方式

消息内容支持三种表示形式:

fromlangchain.messagesimportHumanMessage# 1. 字符串形式human_message=HumanMessage("Hello, how are you?")# 2. 提供者原生格式human_message=HumanMessage(content=[{"type":"text","text":"Hello, how are you?"},{"type":"image_url","image_url":{"url":"https://example.com/image.jpg"}}])# 3. 标准内容块(类型安全接口)human_message=HumanMessage(content_blocks=[{"type":"text","text":"Hello, how are you?"},{"type":"image","url":"https://example.com/image.jpg"},])

5.2 多模态内容支持

LangChain支持多种多模态输入格式:

# 从URL加载图像message={"role":"user","content":[{"type":"text","text":"Describe the content of this image."},{"type":"image","url":"https://example.com/path/to/image.jpg"},]}# 从base64数据加载message={"role":"user","content":[{"type":"text","text":"Describe the content of this image."},{"type":"image","base64":"AAAAIGZ0eXBtcDQyAAAAAGlzb21tcDQyAAACAGlzb2...","mime_type":"image/jpeg",},]}# 使用提供者管理的文件IDmessage={"role":"user","content":[{"type":"text","text":"Describe the content of this image."},{"type":"image","file_id":"file-abc123"},]}

6. 最佳实践指南

6.1 消息组织原则

  1. 明确角色分配:正确使用系统、用户和助手角色
  2. 合理使用元数据:为消息添加ID和名称以支持追踪和调试
  3. 保持对话状态:将完整对话历史作为消息序列传递

6.2 性能优化建议

  1. 使用内容块标准表示:跨提供者的一致接口
  2. 合理设置环境变量:使用LC_OUTPUT_VERSION="v1"以获得标准内容块表示
  3. 监控令牌使用:通过usage_metadata分析成本和使用模式

6.3 错误处理策略

  1. 验证工具调用ID匹配:确保ToolMessagetool_call_id与AI消息中的调用ID一致
  2. 检查模型支持能力:确认目标模型支持所需的多模态格式和文件类型
  3. 处理提供者差异:注意不同提供者对消息字段(如name)的支持差异

总结

LangChain的Messages模块提供了强大而灵活的消息抽象层,支持从简单的文本对话到复杂的多模态交互和工具调用。通过掌握消息的核心概念、类型系统和使用模式,开发者可以构建出高效、可靠且可维护的LLM应用

消息作为LangChain中模型交互的统一接口,其设计平衡了灵活性类型安全,同时保持了与主流提供者API的兼容性。在实际应用中,建议根据具体需求选择适当的消息格式和调用方式,并结合最佳实践进行优化。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/30 7:46:39

【深度学习】YOLO模型优化之过拟合(数据增强/正则化/早停)

本文是针对性解决 YOLO 模型的过拟合问题&#xff0c;核心表现为训练集精度高、验证集精度低&#xff08;差距&#xff1e;10%&#xff09;&#xff0c;或训练集损失持续下降、验证集损失先降后升。本文将从 ** 数据增强&#xff08;扩充样本多样性&#xff0c;从源头防过拟合&…

作者头像 李华
网站建设 2026/6/1 15:52:35

Vue3 H5移动端开发实战:5个技巧让你效率翻倍

Vue3 H5移动端开发实战&#xff1a;5个技巧让你效率翻倍 【免费下载链接】vue3-h5-template &#x1f331; A ready-to-use mobile project base template built with the Vue3, Vant, and Vite. | 基于 Vue3、Vite4、TypeScript/JavaScript、Tailwindcss、Vant4&#xff0c;开…

作者头像 李华
网站建设 2026/5/30 17:42:45

CSANMT模型部署最佳实践:环境配置与优化

CSANMT模型部署最佳实践&#xff1a;环境配置与优化 &#x1f310; AI 智能中英翻译服务 (WebUI API) 项目背景与技术定位 随着全球化进程加速&#xff0c;高质量的机器翻译需求日益增长。传统统计机器翻译&#xff08;SMT&#xff09;在语义连贯性和表达自然度上存在明显短…

作者头像 李华
网站建设 2026/5/30 18:28:38

格力空调智能控制终极指南:免费快速接入Home Assistant

格力空调智能控制终极指南&#xff1a;免费快速接入Home Assistant 【免费下载链接】HomeAssistant-GreeClimateComponent Custom Gree climate component written in Python3 for Home Assistant. Controls ACs supporting the Gree protocol. 项目地址: https://gitcode.co…

作者头像 李华
网站建设 2026/5/30 18:28:37

5个高可用OCR开源镜像推荐:支持中英文识别,一键部署

5个高可用OCR开源镜像推荐&#xff1a;支持中英文识别&#xff0c;一键部署 &#x1f4d6; OCR 文字识别技术的演进与需求 随着数字化进程加速&#xff0c;从纸质文档到电子数据的转换已成为企业自动化、知识管理乃至AI训练数据构建的关键环节。光学字符识别&#xff08;OCR&am…

作者头像 李华
网站建设 2026/5/30 17:56:27

Z-Image-Turbo模型压缩实战:在边缘设备上的优化部署

Z-Image-Turbo模型压缩实战&#xff1a;在边缘设备上的优化部署 在物联网和边缘计算场景中&#xff0c;AI绘图能力的部署常常面临资源受限的挑战。本文将详细介绍如何通过Z-Image-Turbo模型压缩技术&#xff0c;在边缘设备上实现高效的AI绘图推理。这类任务通常需要GPU环境&…

作者头像 李华