外观
当你使用接受多模态输入和工具结果或返回多模态输出的大型语言模型时,Deep Agents 支持多模态工作流。你可以将图像和其他媒体附加到用户消息,使用内置的 read_file 工具读取非文本文件,并从自定义工具返回多模态内容。
内置的上下文压缩主要面向文本。请据此规划多模态工作负载:将大型媒体存储在后端中,并在可能时传递引用。
多模态用户输入
在你发送给智能体的 messages 中传递多模态内容,使用与 LangChain 对话模型相同的标准内容块:
python
result = agent.invoke({
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "What is in this screenshot?"},
{"type": "image", "url": "https://example.com/screenshot.png"},
],
}],
})ts
const result = await agent.invoke({
messages: [
{
role: "user",
content: [
{ type: "text", text: "What is in this screenshot?" },
{ type: "image", url: "https://example.com/screenshot.png" },
],
},
],
});有关块类型、提供商特定要求以及更多示例(PDF、音频、视频),请参阅多模态消息。
内置的 read_file 工具
框架(harness)的 read_file 工具为受支持的多模态文件返回标准内容块,而不是纯文本。当所选模型支持相应的模态时,智能体可以检查存储在其文件系统中的图像、文档和媒体。请查阅提供商文档,了解你的模型支持的 MIME 类型。
支持的多模态文件扩展名
| 类型 | 扩展名 |
|---|---|
| 图像 | .png, .jpg, .jpeg, .gif, .webp, .heic, .heif |
| 视频 | .mp4, .mpeg, .mov, .avi, .flv, .mpg, .webm, .wmv, .3gpp |
| 音频 | .wav, .mp3, .aiff, .aac, .ogg, .flac |
| 文件 | .pdf, .ppt, .pptx |
自定义工具输出
自定义工具 可以包含多模态文件,例如图像:
python
from langchain.tools import tool
@tool
def capture_screenshot() -> list[dict]:
"""Capture a screenshot of the current page."""
return [
{"type": "text", "text": "Screenshot of the current page:"},
{"type": "image", "url": "https://example.com/page.png"},
]ts
import { tool } from "langchain";
import { z } from "zod";
const captureScreenshot = tool(
async () => [
{ type: "text", text: "Screenshot of the current page:" },
{ type: "image", url: "https://example.com/page.png" },
],
{
name: "capture_screenshot",
description: "Capture a screenshot of the current page.",
schema: z.object({}),
},
);返回值会被转换为模型在下一轮读取的 ToolMessage。使用结果消息上的 content_blocks 访问规范化表示。有关返回类型选项、序列化行为和 MCP 示例,请参阅工具返回值和多模态工具内容。
上下文压缩与多模态内容
内置的卸载和摘要是针对文本和消息历史进行优化的:
卸载 只衡量文本 token。非文本块(包括图像)会被保留在替换消息中,而不是被压缩。只包含图像的消息不会仅根据图像大小被卸载。
摘要 将较旧的消息压缩为纯文本摘要。该范围内的图像、音频、视频和文件块不会被保留——模型只会看到摘要器写入的内容。低于保留阈值的近期消息保持不变。
当摘要运行时,较早轮次中的媒体块会从活动上下文中退出:
python
# Before — model receives image blocks in older turns
[
HumanMessage(
content=[
{"type": "text", "text": "What trends do you see in this chart?"},
{"type": "image", "base64": IMG, "mime_type": "image/png"},
]
),
ToolMessage(
content=[
{"type": "text", "text": "Updated chart:"},
{"type": "image", "base64": IMG, "mime_type": "image/png"},
],
tool_call_id="call_chart_1",
),
AIMessage(content="Revenue rose in Q3 based on the chart trend."),
HumanMessage(content="Reply with one sentence summarizing our analysis."),
]
# After — those turns collapse to text; image blocks are gone
{"content": (
"User asked about trends in a chart screenshot. "
"Tool returned an updated chart. Agent identified Q3 revenue growth."
)}ts
// Before — model receives image blocks in older turns
void {
role: "user",
content: [
{ type: "text", text: "What trends do you see in this chart?" },
{ type: "image", url: "https://example.com/chart.png" },
],
};
void {
role: "tool",
content: [
{ type: "text", text: "Updated chart:" },
{ type: "image", url: "https://example.com/chart-v2.png" },
],
};
// After — those turns collapse to text; image blocks are gone
void {
content:
"User asked about trends in a chart screenshot. " +
"Tool returned an updated chart. Agent identified Q3 revenue growth.",
};原始对话仍会以文本形式写入文件系统。有关触发条件、保留阈值和完整流程,请参阅[摘要](/oss/deepagents/context-engineering#summarization)。
对于多模态密集的工作负载:
- 将图像、截图和图表存储在文件系统后端或外部对象存储中,然后通过消息传递文件路径或 URL。
- 在长时间运行的对话中,优先使用引用而不是 base64 编码的图像块。
- 使用子智能体处理图像密集的检查,让主智能体收到紧凑的文本结果。
- 当你的提供商对图像收取大量 token 时,调整摘要阈值或提供自定义 token 计数器。
有关卸载阈值、摘要触发条件和自定义选项,请参阅上下文压缩。