LLM Chains vs Single Calls 在 n8n 中:我的 Caption Generation 实验
发布: (2026年2月8日 GMT+8 00:00)
5 分钟阅读
原文: Dev.to
Source: Dev.to
请提供您希望翻译的完整文本内容,我将为您翻译成简体中文并保留原有的格式、Markdown 语法以及技术术语。谢谢!
两种方法
方法 A:三步链(俳句)
将任务拆分为微步骤:
- 从数据中提取关键点。
- 使用步骤 1 的输出草拟结构。
- 润色并添加元数据(标签、号召性用语等)。
方法 B:一次性完整调用(十四行诗)
将整个提示一次性发送给更强大的模型,让它处理整个流程。
测试脚本(在 n8n 之外)
index.mjs
# Setup
npm init -y
npm i @anthropic-ai/sdk dotenv
export ANTHROPIC_API_KEY="your_key_here"
import "dotenv/config";
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
// Sample data structure
const image_details = [
{ order: 1, attributeA: "value1", attributeB: "value2" },
{ order: 2, attributeA: "value3", attributeB: "value4" },
];
async function callClaude({ model, system, prompt }) {
const res = await client.messages.create({
model,
max_tokens: 1000,
system,
messages: [{ role: "user", content: prompt }],
});
return res.content?.text ?? "";
}
// Approach A: Haiku 3‑step chain
async function haikuChain() {
const model = "claude-haiku-4-5";
const system = "You are a caption editor. Be concise.";
// Step 1: Extract data
const step1 = await callClaude({
model,
system,
prompt: `Extract key descriptions from this data:\n${JSON.stringify({ image_details }, null, 2)}`,
});
// Step 2: Create structure
const step2 = await callClaude({
model,
system,
prompt: `Based on this, create hook + body + CTA:\n${step1}`,
});
// Step 3: Add tags
const step3 = await callClaude({
model,
system,
prompt: `Add hashtags to this caption:\n${step2}`,
});
return { step1, step2, final: step3 };
}
// Approach B: Sonnet single call
async function sonnetSingle() {
const model = "claude-sonnet-4-5";
const system = "You are a caption editor. Be concise.";
return await callClaude({
model,
system,
prompt: `Create a complete caption (hook + body + bullets + CTA + tags) from:\n${JSON.stringify({ image_details }, null, 2)}`,
});
}
// Run both
const resultA = await haikuChain();
const resultB = await sonnetSingle();
console.log("=== Haiku Chain ===\n", resultA.final);
console.log("\n=== Sonnet Single ===\n", resultB);
在 n8n 的 Code 节点中准备数据
// Wrong: LLM can't work with IDs alone
const image_details = transformedImages.map(img => ({ id: img.id }));
// Right: Provide the full content the LLM needs
const image_details = transformedImages.map((img, index) => ({
order: index + 1,
...img, // Spread ALL attributes the LLM needs
}));
return [{ json: { image_details } }];
根本原因:仅传递了图像 ID,导致模型缺乏任何描述性上下文。
解决方案:展开整个对象(...img),使 LLM 接收所有相关属性。
指标比较
| 指标 | Haiku 3‑Chain | Sonnet Single |
|---|---|---|
| 上下文感知 | 步骤之间的碎片 | 整体理解 |
| 语气一致性 | 拼接而成,参差不齐 | 从头到尾统一 |
| 输出质量 | 信息丰富但生硬 | 自然、引人入胜、流畅 |
| 成本 | 约便宜 3× | 更高但值得 |
结论:对于字幕(或任何创意写作),上下文的连贯性胜过适度的成本节省。
模式与使用场景指南
| 模式 | 使用场景 | 示例 |
|---|---|---|
| 低阶模型 + 链式 | 明确的角色分离 | 数据提取、分类、格式化 |
| 中/高阶模型 + 单一 | 依赖上下文的创意 | 标题、文章、文案写作 |
关键要点:
- 上下文 > 成本:碎片化的链条会打断创意任务的叙事流。
- 数据质量重要:提供丰富信息是必需的;链式结构无法弥补缺失的上下文。
- Haiku 的定位:在速度和高度结构化的任务上表现出色,但 Sonnet 在需要“感受”的情况下更胜一筹。
参考文献
- n8n 高级 AI 文档
- Claude Sonnet 4.5 公告
- Claude Haiku 在 AWS Bedrock 上