《2026 LangChain零基础入门:用AI应用框架快速搭建智能助手》第3课:Chains 与 LCEL —— 把多个 Prompt 串成流水线,实现多步智能生成
《2026 LangChain零基础入门:用AI应用框架快速搭建智能助手》第3课:Chains 与 LCEL —— 把多个 Prompt 串成流水线,实现多步智能生成
大家好,我是链上杯子(CSDN:链上杯子)。
失业一年了,天天想着怎么翻身。上节课用 PromptTemplate 生成单步内容后,我开始想:如果能让 AI 先脑暴大纲、再写正文、再润色结尾,会不会更专业?LangChain 的 Chains 就是干这个的——把多个步骤像流水线一样串起来。第一次跑通 SequentialChain,看到模型一步步输出完整文章,感觉像给 AI 加了个“生产车间”。
本课目标:
学会使用 LangChain 的 Chains(尤其是 SequentialChain 和 LCEL),把多个 Prompt + 模型调用串联成多步流程。
学完这节课,你就能实现“输入一个主题 → 先生成大纲 → 再写正文 → 最后润色”的智能生成链,输出质量明显提升。
核心代码实战
1. 最简单的 SequentialChain —— 两步链:先生成大纲,再写正文
from langchain_openai import ChatOpenAI
from langchain_classic.prompts import PromptTemplate
from langchain_classic.chains import LLMChain, SequentialChain
# 初始化模型(替换成你的真实 Key)
llm = ChatOpenAI(
openai_api_key="", # ←←← 这里替换成你的 DeepSeek API Key
openai_api_base="https://api.deepseek.com/v1",
model="deepseek-chat",
temperature=0.7
)
# 第一步:生成大纲
outline_prompt = PromptTemplate.from_template(
"你是一个专业小说编辑。\n"
"请为主题「{topic}」生成一个简洁的故事大纲,包括开头、中间、高潮、结尾。\n"
"输出格式:用编号 1-4 列出。"
)
outline_chain = LLMChain(llm=llm, prompt=outline_prompt, output_key="outline")
# 第二步:根据大纲写正文
story_prompt = PromptTemplate.from_template(
"你是一个小说写手。\n"
"请根据以下大纲写一篇完整短篇故事,字数约{length}字。\n"
"大纲:\n{outline}\n"
"风格:{style}。\n"
"请直接输出故事正文。"
)
story_chain = LLMChain(llm=llm, prompt=story_prompt, output_key="story")
# 串成 SequentialChain
overall_chain = SequentialChain(
chains=[outline_chain, story_chain],
input_variables=["topic", "length", "style"],
output_variables=["outline", "story"]
)
# 运行整个链
result = overall_chain({
"topic": "量子纠缠的爱情",
"length": 600,
"style": "冷峻科幻 + 细腻情感"
})
print("=== 生成的大纲 ===")
print(result["outline"])
print("\n=== 完整故事 ===")
print(result["story"])
运行后会先输出大纲,再输出基于大纲写的完整故事。
小知识点:
- SequentialChain 按顺序执行多个链,前一个的输出(output_key)会自动传给下一个。
- input_variables 是入口变量,output_variables 是最终结果 key。
2. 使用 LCEL(LangChain Expression Language)—— 更现代、更灵活的写法
LCEL 是 LangChain 推荐的新方式,用 | 运算符串链,像管道一样直观。
from langchain_core.output_parsers import StrOutputParser
from langchain_classic.prompts import PromptTemplate
# 第一步 Prompt + LLM + Parser
outline_chain_lcel = (
PromptTemplate.from_template(
"为主题「{topic}」生成一个简洁大纲,包括4个部分。"
)
| llm
| StrOutputParser() # 把 AIMessage 转成纯字符串
)
# 第二步 Prompt + LLM + Parser
story_chain_lcel = (
PromptTemplate.from_template(
"根据以下大纲写一篇约{length}字的故事:\n{outline}\n风格:{style}"
)
| llm
| StrOutputParser()
)
# 整体 LCEL 链(用 .pipe() 或 | 连接)
full_chain_lcel = outline_chain_lcel | story_chain_lcel
# 运行
result_lcel = full_chain_lcel.invoke({
"topic": "失落的城市",
"length": 500,
"style": "文艺怀旧"
})
print("LCEL 生成的故事:")
print(result_lcel)
LCEL 优势:
- 写法简洁,像 Unix 管道
- 支持并行、分支、自定义组件
- 后续加 Memory、Agent、Tool 都兼容
3. 互动多步链 —— 用户输入主题
topic = input("故事主题:")
length = input("大约字数(数字):")
style = input("风格:")
result = overall_chain({
"topic": topic,
"length": int(length),
"style": style
})
print("\n大纲:")
print(result["outline"])
print("\n故事:")
print(result["story"])
小练习(2 道)
练习1(基础)
复制第1段 SequentialChain,增加第三步“润色链”:
- 新 Prompt:“请润色以下故事,使语言更流畅、情感更细腻:\n{story}”
- 运行后输出最终润色版。
观察三步链的输出质量提升。
练习2(进阶)
用 LCEL 方式重写第1段的两步链。
再加一个并行分支:用 | 同时生成“中文版”和“英文版”故事(提示词中加 language 参数)。
提示:可以用 RunnableParallel 实现并行(查官方文档或简单拼接两条链)。
本课小结
本课学习了 SequentialChain(顺序执行)和 LCEL(管道式写法),实现了多步 Prompt 流水线:先生成大纲、再写正文。
Chains 是 LangChain 的核心能力之一,把单步调用升级成智能流程,输出更结构化、更高质量。
下节预告
下一课:Memory 与 ConversationChain —— 让 AI “记住”对话历史,实现真正的多轮连续聊天。
欢迎在评论区贴出你的多步链运行结果、大纲+故事对比,或任何问题~
如果觉得这篇有用,欢迎点赞或关注,一起玩转 LangChain!
更多推荐



所有评论(0)