0 前言

目前实现一个Chain大多使用LCEL方式来实现,LangChain表达式语言(LCEL,LangChain Expression Language)是一种声明式方法,可以轻松地将多个组件链接成AI工作流。它通过Python原生操作符(如管道符 | )将组件连接成可执行流程,显著简化了AI应用的开发。

LLMChain目前已经被标记被过时,并将在1.0版本被废弃 LangChainDeprecationWarning: The class LLMChain was deprecated in LangChain 0.1.17 and will be removed in 1.0. Use RunnableSequence, e.g., prompt | llm`` instead. chainA = LLMChain(

但是LLMChain作为早期实现Chain的方案在维护旧代码场景方面还是有一定学习价值的。所以以下将使用基于LLMChain构建的Chain配合SimpleSequentialChainSequentialChain实现简单的工作流案例。

1 LLMChain 和 LCEL 各自的特点

LLMChain特点:

  1. 用于单次问答,输入一个 Prompt,输出 LLM 的响应
  2. 适合无上下文的简单任务(如翻译、摘要、分类等)
  3. 无记忆:无法自动维护聊天历史

LCEL特点:

  1. 用管道符号(|)直观串联组件,代码即流程,无需复杂的类封装
  2. 支持任意组件(模型、工具、解析器等)的串联、分支、并行等复杂流程
  3. 只需在模型中开启 streaming=True,配合回调函数即可实现实时输出
  4. 复杂流程(如 RAG、多轮对话、工具链等)

2 顺序链

顺序链(SequentialChain)允许将多个链顺序连接起来,每个Chain的输出作为下一个Chain的输入,
形成特定场景的流水线(Pipeline)。
顺序链有两种类型:

  • 单个输入/输出:对应着 SimpleSequentialChain
  • 多个输入/输出:对应着:SequentialChain

2.1 SimpleSequentialChain

SimpleSequentialChain是最简单的顺序链,多个链串联执行 ,每个步骤都有 单一的输入和输出,一个步骤的输出就是下一个步骤的输入,无需手动映射。
请添加图片描述

2.1.1 代码示例

以下将实现一个ChainA对知识的提问,返回问题的答案,然后将这个输出结果给到ChainB,它再拿着ChainA给的答案总结并按照要求处理和输出

import os

import dotenv
from langchain_classic.chains.llm import LLMChain
from langchain_classic.chains.sequential import SimpleSequentialChain
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

dotenv.load_dotenv()

os.environ["OPENAI_BASE_URL"] = os.getenv("QWEN_BASE_URL")
os.environ["OPENAI_API_KEY"] = os.getenv("QWEN_API_KEY")
# 获取对话模型
chat_model = ChatOpenAI(
    model="qwen-plus"
)

chainA_template = ChatPromptTemplate.from_messages([
    ("system", "你是一个计算机领域大佬"),
    ("human", "解释一下什么是{question},不超过500字")
])

chainB_template = ChatPromptTemplate.from_messages([
    ("system", "你是一个计算机老师,善于将复杂的计算机知识用简洁易懂的话告诉学生"),
    ("human", "怎么理解这段话,{description},不超过200字")
])

chainA = LLMChain(
    llm=chat_model,
    prompt=chainA_template,
    verbose=True
)

chainB = LLMChain(
    llm=chat_model,
    prompt=chainB_template,
    verbose=True
)

full_chain = SimpleSequentialChain(
    chains=[chainA, chainB],
    verbose=True
)
#
print(full_chain.invoke({"input": "什么是LangChain?"}))

2.1.2 运行结果

图中靛蓝色为ChainA输出的结果,接着第二次调用LLChain时,拿着ChainA给的结果进行提问,最后黄色部分是ChainB的输出结果,也是最终的输出结果
请添加图片描述

2.2 SequentialChain

SequentialChain:更通用的顺序链,具体来说:

  • 多变量支:允许不同子链有独立的输入/输出变量。
  • 灵活映射:需显式定义变量如何从一个链传递到下一个链。即精准地命名输入关键字和输出关键字,来明确链之间的关系。
  • 复杂流程控制:支持分支、条件逻辑(分别通过 input_variables 和output_variables 配置输入和输出)。

请添加图片描述

即可以随意指定多个输入和输出,而不会像SimpleSequentialChain那样默认单个输入和输出,并且不会上一个输出默认作为下一个输入,而是需要通过精确的变量名来指定

2.2.1 代码示例

以下示例通过对query_chain输入product,接着query_chain输出query_price,接着advertisement_chain拿着最开始输入的product和query_chain输出的query_price进行处理输出广告文案,同时把query_price和product也一同输出

import os

import dotenv
from langchain_classic.chains.llm import LLMChain
from langchain_classic.chains.sequential import SequentialChain
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

dotenv.load_dotenv()

os.environ["OPENAI_BASE_URL"] = os.getenv("QWEN_BASE_URL")
os.environ["OPENAI_API_KEY"] = os.getenv("QWEN_API_KEY")
# 获取对话模型
chat_model = ChatOpenAI(
    model="qwen-plus"
)

query_prompt_template = PromptTemplate.from_template(
    template="模拟查询{product}的市场价格,直接返回一个合理的数字,不要返回其他东西"
)

advertisement_prompt_template = PromptTemplate.from_template(
    template="为{product}(售价: {query_price}元)创作一篇100字以内的促销文案,要求突出产品卖点"
)

query_chain = LLMChain(
    llm=chat_model,
    prompt=query_prompt_template,
    output_key="query_price",
    verbose=True
)

advertisement_chain = LLMChain(
    llm=chat_model,
    prompt=advertisement_prompt_template,
    output_key="advertisement",
    verbose=True
)

sequential_chain = SequentialChain(
    chains=[query_chain, advertisement_chain],
    input_variables=["product"],
    output_variables=["query_price", "advertisement"],
    verbose=True
)

print(sequential_chain.invoke({"product": "iPhone16Pro"}))

2.2.2 运行结果

query_chain输出query_price为9999,接着advertisement_chain拿着iPhone16Pro和query_chain输出的9999进行处理输出广告得到结果
请添加图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐