LLM Chains vs Single Calls 在 n8n 中:我的 Caption Generation 实验

发布: (2026年2月8日 GMT+8 00:00)
5 分钟阅读
原文: Dev.to

Source: Dev.to

请提供您希望翻译的完整文本内容,我将为您翻译成简体中文并保留原有的格式、Markdown 语法以及技术术语。谢谢!

两种方法

方法 A:三步链(俳句)

将任务拆分为微步骤:

  1. 从数据中提取关键点
  2. 使用步骤 1 的输出草拟结构
  3. 润色并添加元数据(标签、号召性用语等)。

方法 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‑ChainSonnet Single
上下文感知步骤之间的碎片整体理解
语气一致性拼接而成,参差不齐从头到尾统一
输出质量信息丰富但生硬自然、引人入胜、流畅
成本约便宜 3×更高但值得

结论:对于字幕(或任何创意写作),上下文的连贯性胜过适度的成本节省。

模式与使用场景指南

模式使用场景示例
低阶模型 + 链式明确的角色分离数据提取、分类、格式化
中/高阶模型 + 单一依赖上下文的创意标题、文章、文案写作

关键要点

  • 上下文 > 成本:碎片化的链条会打断创意任务的叙事流。
  • 数据质量重要:提供丰富信息是必需的;链式结构无法弥补缺失的上下文。
  • Haiku 的定位:在速度和高度结构化的任务上表现出色,但 Sonnet 在需要“感受”的情况下更胜一筹。

参考文献

  • n8n 高级 AI 文档
  • Claude Sonnet 4.5 公告
  • Claude Haiku 在 AWS Bedrock 上
0 浏览
Back to Blog

相关文章

阅读更多 »

UX/UI 排版

Typography 是指什么?- 使用哪种字体 - 在什么位置多大 - 多粗 - 行间距 - …