Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

消息是 LangChain 中模型上下文的基本单位。它们代表模型的输入和输出,携带与 LLM 交互时表示对话状态所需的内容和元数据。 消息是包含以下内容的对象:
  • 角色 - 标识消息类型(例如 systemuser
  • 内容 - 代表消息的实际内容(如文本、图像、音频、文档等)
  • 元数据 - 可选字段,如响应信息、消息 ID 和 Token 使用情况
LangChain 提供了一种适用于所有模型提供商的标准消息类型,确保无论调用哪个模型都能保持一致的行为。

基本用法

使用消息最简单的方式是创建消息对象并在调用时将它们传递给模型。
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage, 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

文本提示词

文本提示词是字符串——适用于不需要保留对话历史的简单生成任务。
response = model.invoke("Write a haiku about spring")
在以下情况使用文本提示词:
  • 你有一个单独的、独立的请求
  • 你不需要对话历史
  • 你想要最少的代码复杂性

消息提示词

或者,你可以通过提供消息对象列表来向模型传递消息列表。
from langchain.messages import SystemMessage, HumanMessage, AIMessage

messages = [
    SystemMessage("You are a poetry expert"),
    HumanMessage("Write a haiku about spring"),
    AIMessage("Cherry blossoms bloom...")
]
response = model.invoke(messages)
在以下情况使用消息提示词:
  • 管理多轮对话
  • 处理多模态内容(图像、音频、文件)
  • 包含系统指令

字典格式

你也可以直接使用 OpenAI chat completions 格式指定消息。
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)

消息类型

系统消息

SystemMessage 代表一组初始指令,用于预设模型的行为。你可以使用系统消息来设定语气、定义模型的角色,并建立响应的准则。
基本指令
system_msg = SystemMessage("You are a helpful coding assistant.")

messages = [
    system_msg,
    HumanMessage("How do I create a REST API?")
]
response = model.invoke(messages)
详细角色设定
from langchain.messages import SystemMessage, HumanMessage

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.
""")

messages = [
    system_msg,
    HumanMessage("How do I create a REST API?")
]
response = model.invoke(messages)

人类消息

HumanMessage 代表用户输入和交互。它们可以包含文本、图像、音频、文件以及任何数量的多模态内容

文本内容

response = model.invoke([
  HumanMessage("What is machine learning?")
])

消息元数据

添加元数据
human_msg = HumanMessage(
    content="Hello!",
    name="alice",  # 可选:标识不同的用户
    id="msg_123",  # 可选:用于追踪的唯一标识符
)
name 字段的行为因提供商而异——有些用它来识别用户,有些则忽略它。要查看详情,请参阅模型提供商的参考文档

AI 消息

AIMessage 代表模型调用的输出。它们可以包含多模态数据、工具调用和你可以随后访问的提供商特定元数据。
response = model.invoke("Explain AI")
print(type(response))  # <class 'langchain.messages.AIMessage'>
AIMessage 对象在调用模型时返回,其中包含响应中所有关联的元数据。 提供商对消息类型的权重/上下文化方式不同,这意味着有时手动创建一个新的 AIMessage 对象并将其插入消息历史中(就像它来自模型一样)是有帮助的。
from langchain.messages import AIMessage, SystemMessage, HumanMessage

# 手动创建 AI 消息(例如,用于对话历史)
ai_msg = AIMessage("I'd be happy to help you with that question!")

# 添加到对话历史
messages = [
    SystemMessage("You are a helpful assistant"),
    HumanMessage("Can you help me?"),
    ai_msg,  # 插入,就像它来自模型
    HumanMessage("Great! What's 2+2?")
]

response = model.invoke(messages)
text
string
消息的文本内容。
content
string | dict[]
消息的原始内容。
content_blocks
ContentBlock[]
消息的标准化内容块
tool_calls
dict[] | None
模型发起的工具调用。如果没有调用工具则为空。
id
string
消息的唯一标识符(由 LangChain 自动生成或在提供商响应中返回)
usage_metadata
dict | None
消息的使用元数据,可用时包含 Token 计数。
response_metadata
ResponseMetadata | None
消息的响应元数据。

工具调用

当模型发起工具调用时,它们包含在 AIMessage 中:
from langchain.chat_models import init_chat_model

model = init_chat_model("gpt-5-nano")

def get_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?")

for tool_call in response.tool_calls:
    print(f"Tool: {tool_call['name']}")
    print(f"Args: {tool_call['args']}")
    print(f"ID: {tool_call['id']}")
其他结构化数据,如推理或引用,也可以出现在消息内容中。

Token 使用情况

AIMessage 可以在其 usage_metadata 字段中保存 Token 计数和其他使用元数据:
from langchain.chat_models import init_chat_model

model = init_chat_model("gpt-5-nano")

response = model.invoke("Hello!")
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}}
有关详情,请参阅 UsageMetadata

流式输出和块

在流式输出期间,你会收到 AIMessageChunk 对象,它们可以组合成完整的消息对象:
chunks = []
full_message = None
for chunk in model.stream("Hi"):
    chunks.append(chunk)
    print(chunk.text)
    full_message = chunk if full_message is None else full_message + chunk

工具消息

对于支持工具调用的模型,AI 消息可以包含工具调用。工具消息用于将单个工具执行的结果传回给模型。 工具可以直接生成 ToolMessage 对象。以下展示一个简单示例。更多内容请阅读工具指南
from langchain.messages import AIMessage
from langchain.messages import ToolMessage

# 模型发起工具调用后
# (此处,我们为简洁起见手动创建消息进行演示)
ai_message = AIMessage(
    content=[],
    tool_calls=[{
        "name": "get_weather",
        "args": {"location": "San Francisco"},
        "id": "call_123"
    }]
)

# 执行工具并创建结果消息
weather_result = "Sunny, 72°F"
tool_message = ToolMessage(
    content=weather_result,
    tool_call_id="call_123"  # 必须与调用 ID 匹配
)

# 继续对话
messages = [
    HumanMessage("What's the weather in San Francisco?"),
    ai_message,  # 模型的工具调用
    tool_message,  # 工具执行结果
]
response = model.invoke(messages)  # 模型处理结果
content
string
required
工具调用的字符串化输出。
tool_call_id
string
required
此消息响应的工具调用 ID。必须与 AIMessage 中工具调用的 ID 匹配。
name
string
required
被调用的工具名称。
artifact
dict
不发送给模型但可以通过编程方式访问的附加数据。
artifact 字段存储不会发送给模型但可以通过编程方式访问的补充数据。这对于存储原始结果、调试信息或用于下游处理的数据非常有用,而不会使模型的上下文变得混乱。
例如,检索工具可以从文档中检索一段文字供模型参考。消息 content 包含模型将引用的文本,而 artifact 可以包含应用程序可以使用的文档标识符或其他元数据(例如,用于渲染页面)。参见下面的示例:
from langchain.messages import ToolMessage

# 发送给模型
message_content = "It was the best of times, it was the worst of times."

# Artifact 可供下游使用
artifact = {"document_id": "doc_123", "page": 0}

tool_message = ToolMessage(
    content=message_content,
    tool_call_id="call_123",
    name="search_books",
    artifact=artifact,
)
有关使用 LangChain 构建检索智能体的端到端示例,请参阅 RAG 教程

消息内容

你可以将消息的内容视为发送给模型的数据载荷。消息有一个 content 属性,它是松散类型的,支持字符串和无类型对象列表(例如字典)。这允许在 LangChain 聊天模型中直接支持提供商原生结构,如多模态内容和其他数据。 另外,LangChain 为文本、推理、引用、多模态数据、服务器端工具调用和其他消息内容提供了专用的内容类型。参见下面的内容块 LangChain 聊天模型在 content 属性中接受消息内容。 它可以包含以下之一:
  1. 字符串
  2. 提供商原生格式的内容块列表
  3. LangChain 标准内容块列表
参见下面使用多模态输入的示例:
from langchain.messages import HumanMessage

# 字符串内容
human_message = HumanMessage("Hello, how are you?")

# 提供商原生格式(例如 OpenAI)
human_message = HumanMessage(content=[
    {"type": "text", "text": "Hello, how are you?"},
    {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
])

# 标准内容块列表
human_message = HumanMessage(content_blocks=[
    {"type": "text", "text": "Hello, how are you?"},
    {"type": "image", "url": "https://example.com/image.jpg"},
])
在初始化消息时指定 content_blocks 仍然会填充消息的 content,但提供了一个类型安全的接口。

标准内容块

LangChain 提供了一种跨提供商工作的消息内容标准表示。 消息对象实现了一个 content_blocks 属性,它会惰性解析 content 属性为标准的、类型安全的表示。例如,从 ChatAnthropicChatOpenAI 生成的消息将包含各自提供商格式的 thinkingreasoning 块,但可以被惰性解析为一致的 ReasoningContentBlock 表示:
from langchain.messages import AIMessage

message = AIMessage(
    content=[
        {"type": "thinking", "thinking": "...", "signature": "WaUjzkyp..."},
        {"type": "text", "text": "..."},
    ],
    response_metadata={"model_provider": "anthropic"}
)
message.content_blocks
[{'type': 'reasoning',
  'reasoning': '...',
  'extras': {'signature': 'WaUjzkyp...'}},
 {'type': 'text', 'text': '...'}]
请参阅集成指南以开始使用你选择的推理提供商。
序列化标准内容如果 LangChain 之外的应用程序需要访问标准内容块表示,你可以选择将内容块存储在消息内容中。为此,你可以将 LC_OUTPUT_VERSION 环境变量设置为 v1。或者,使用 output_version="v1" 初始化任何聊天模型:
from langchain.chat_models import init_chat_model

model = init_chat_model("gpt-5-nano", output_version="v1")

多模态

多模态指的是处理不同形式数据的能力,如文本、音频、图像和视频。LangChain 包含这些数据的标准类型,可以跨提供商使用。 聊天模型可以接受多模态数据作为输入并将其作为输出生成。以下展示了包含多模态数据的输入消息的简短示例。
额外的键可以在内容块的顶层包含,也可以嵌套在 "extras": {"key": value} 中。OpenAIAWS Bedrock Converse 等提供商需要 PDF 的文件名。有关具体信息,请参阅你选择的模型的提供商页面
# 从 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",
        },
    ]
}

# 从提供商管理的文件 ID
message = {
    "role": "user",
    "content": [
        {"type": "text", "text": "Describe the content of this image."},
        {"type": "image", "file_id": "file-abc123"},
    ]
}
并非所有模型都支持所有文件类型。请查看模型提供商的参考文档了解支持的格式和大小限制。

内容块参考

内容块表示为(创建消息时或访问 content_blocks 属性时的)类型化字典列表。列表中的每个项目必须符合以下块类型之一:
用途: 标准文本输出
type
string
required
始终为 "text"
text
string
required
文本内容
annotations
object[]
文本的注释列表
extras
object
附加的提供商特定数据
示例:
{
    "type": "text",
    "text": "Hello world",
    "annotations": []
}
用途: 模型推理步骤
type
string
required
始终为 "reasoning"
reasoning
string
推理内容
extras
object
附加的提供商特定数据
示例:
{
    "type": "reasoning",
    "reasoning": "The user is asking about...",
    "extras": {"signature": "abc123"},
}
用途: 图像数据
type
string
required
始终为 "image"
url
string
指向图像位置的 URL。
base64
string
Base64 编码的图像数据。
id
string
此内容块的唯一标识符(由提供商或 LangChain 生成)。
mime_type
string
图像 MIME 类型(例如 image/jpegimage/png)。base64 数据必需。
用途: 音频数据
type
string
required
始终为 "audio"
url
string
指向音频位置的 URL。
base64
string
Base64 编码的音频数据。
id
string
此内容块的唯一标识符(由提供商或 LangChain 生成)。
mime_type
string
音频 MIME 类型(例如 audio/mpegaudio/wav)。base64 数据必需。
用途: 视频数据
type
string
required
始终为 "video"
url
string
指向视频位置的 URL。
base64
string
Base64 编码的视频数据。
id
string
此内容块的唯一标识符(由提供商或 LangChain 生成)。
mime_type
string
视频 MIME 类型(例如 video/mp4video/webm)。base64 数据必需。
用途: 通用文件(PDF 等)
type
string
required
始终为 "file"
url
string
指向文件位置的 URL。
base64
string
Base64 编码的文件数据。
id
string
此内容块的唯一标识符(由提供商或 LangChain 生成)。
mime_type
string
文件 MIME 类型(例如 application/pdf)。base64 数据必需。
用途: 文档文本(.txt.md
type
string
required
始终为 "text-plain"
text
string
文本内容
mime_type
string
文本的 MIME 类型(例如 text/plaintext/markdown
用途: 函数调用
type
string
required
始终为 "tool_call"
name
string
required
要调用的工具名称
args
object
required
传递给工具的参数
id
string
required
此工具调用的唯一标识符
示例:
{
    "type": "tool_call",
    "name": "search",
    "args": {"query": "weather"},
    "id": "call_123"
}
用途: 流式工具调用片段
type
string
required
始终为 "tool_call_chunk"
name
string
正在调用的工具名称
args
string
部分工具参数(可能是不完整的 JSON)
id
string
工具调用标识符
index
number | string
此块在流中的位置
用途: 格式错误的调用,用于捕获 JSON 解析错误。
type
string
required
始终为 "invalid_tool_call"
name
string
未能调用的工具名称
args
object
传递给工具的参数
error
string
出错原因的描述
用途: 在服务器端执行的工具调用。
type
string
required
始终为 "server_tool_call"
id
string
required
与工具调用关联的标识符。
name
string
required
要调用的工具名称。
args
string
required
部分工具参数(可能是不完整的 JSON)
用途: 流式服务器端工具调用片段
type
string
required
始终为 "server_tool_call_chunk"
id
string
与工具调用关联的标识符。
name
string
正在调用的工具名称
args
string
部分工具参数(可能是不完整的 JSON)
index
number | string
此块在流中的位置
用途: 搜索结果
type
string
required
始终为 "server_tool_result"
tool_call_id
string
required
对应服务器端工具调用的标识符。
id
string
与服务器端工具结果关联的标识符。
status
string
required
服务器端工具的执行状态。"success""error"
output
已执行工具的输出。
用途: 提供商特定的逃生通道
type
string
required
始终为 "non_standard"
value
object
required
提供商特定的数据结构
用法: 用于实验性或提供商独有的功能
其他提供商特定的内容类型可在每个模型提供商的参考文档中找到。
API 参考中查看规范类型定义。
内容块作为消息的新属性在 LangChain v1 中引入,用于标准化跨提供商的内容格式,同时保持与现有代码的向后兼容性。内容块不是 content 属性的替代品,而是一个可用于以标准化格式访问消息内容的新属性。

与聊天模型一起使用

聊天模型接受消息对象序列作为输入,并返回 AIMessage 作为输出。交互通常是无状态的,因此简单的对话循环涉及使用不断增长的消息列表来调用模型。 参阅以下指南了解更多: