AI Agent开发学习系列 - langchain之Chains的使用(3):SequentialChain
·
SequentialChain 是 LangChain 中用于“顺序多步推理”的链式工具,可以将多个子链(如 LLMChain)串联起来,前一个链的输出作为后一个链的输入,实现复杂的多步推理流程。它适合需要多个步骤、每步依赖上一步结果的场景。
典型用法举例
假设你有如下需求:
- 输入水果名,先生成颜色;
- 再根据颜色生成描述;
- 再将描述翻译成英文;
- 最后对英文描述进行英文评论。
#SequentialChain支持多个链路的顺序执行
from langchain.chains import LLMChain
from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain.chains import SequentialChain
import os
from pydantic import SecretStr
from dotenv import load_dotenv
load_dotenv() # 自动加载 .env 文件
llm = ChatOpenAI(
api_key=SecretStr(os.environ.get("HUNYUAN_API_KEY", "")),
base_url="https://api.hunyuan.cloud.tencent.com/v1",
model="hunyuan-lite",
temperature=0,
)
#chain 1: 说明一个水果的颜色。
first_prompt = ChatPromptTemplate.from_template("请说明{fruit}的颜色。")
chain_one = LLMChain(
llm=llm,
prompt=first_prompt,
verbose=True,
output_key="color"
)
#chain 2: 形容一下这个颜色的特征。
second_prompt = ChatPromptTemplate.from_template("请形容一下{color}的特征。")
chain_two = LLMChain(
llm=llm,
prompt=second_prompt,
verbose=True,
output_key="description"
)
#chain 3: 将特征翻译成英文。
third_prompt = ChatPromptTemplate.from_template("请将{description}翻译成英文。")
chain_three = LLMChain(
llm=llm,
prompt=third_prompt,
verbose=True,
output_key="english_description"
)
#chain 4: 针对上述英文描述进行简短的英文评论。
fourth_prompt = ChatPromptTemplate.from_template("请针对{english_description}进行简短的英文评论,评论中要提到对{color}的喜好描述。")
chain_four = LLMChain(
llm=llm,
prompt=fourth_prompt,
verbose=True,
output_key="comment"
)
# 将多个chain串联起来
overall_chain = SequentialChain(
chains=[chain_one, chain_two, chain_three, chain_four],
verbose=True,
input_variables=["fruit"],
output_variables=["color", "description", "english_description", "comment"]
)
overall_chain("苹果")
结果:
> Entering new SequentialChain chain...
> Entering new LLMChain chain...
Prompt after formatting:
Human: 请说明苹果的颜色。
> Finished chain.
> Entering new LLMChain chain...
Prompt after formatting:
Human: 请形容一下苹果的颜色有很多种,包括红色、绿色和黄色。不同品种的苹果颜色可能会有所不同。例如,红富士苹果通常是红色的,青苹果是绿色的,而金帅苹果则是黄色的。此外,有些苹果可能呈现出双色,即红色和绿色同时存在。总的来说,苹果的颜色多种多样,取决于其品种和成熟程度。的特征。
> Finished chain.
> Entering new LLMChain chain...
Prompt after formatting:
Human: 请将苹果的颜色确实多种多样,这主要归功于苹果树的品种差异以及它们在成熟过程中的颜色变化。以下是对苹果颜色的详细描述:
1. **红色苹果**:红色苹果是最常见的颜色之一,它们的颜色鲜艳且引人注目。红富士苹果是其中的代表,它不仅味道鲜美,而且果肉紧实,口感极佳。除了红富士,还有其他一些红色苹果品种,如红玉、红玫瑰等。
2. **绿色苹果**:绿色苹果的代表品种是青苹果,这种苹果口感脆爽,略带酸味。青苹果的颜色较为清新,适合喜欢酸甜口味的人。此外,青苹果中还有一些特殊的品种,如金色圣女苹果,虽然名字中有“金”,但实际上它是绿色的。
3. **黄色苹果**:黄色苹果的代表品种是金帅苹果,它的果皮呈现金黄色,果肉细腻多汁,味道香甜。除了金帅,还有其他一些黄色苹果品种,如金色美人苹果等。
4. **双色苹果**:有些苹果可能同时呈现出红色和绿色的双色特征,这种苹果通常被称为“双色苹果”或“彩色苹果”。双色苹果的出现是由于苹果在成熟过程中色素细胞的变化所致。这种苹果具有一定的观赏价值,也增加了苹果的多样性。
除了上述颜色外,还有一些特殊情况下苹果会出现其他颜色,如紫色、粉色等。这些颜色通常是由于苹果在生长过程中受到环境因素的影响,如土壤、气候等。
总之,苹果的颜色多种多样,这主要取决于其品种和成熟程度。不同颜色的苹果具有不同的口感和风味特点,为人们提供了丰富的选择。翻译成英文。
> Finished chain.
> Entering new LLMChain chain...
Prompt after formatting:
Human: 请针对Apple colors are indeed diverse, mainly due to the differences in apple tree varieties and the color changes they undergo during maturity. Here is a detailed description of apple colors:
1. **Red Apples**: Red apples are one of the most common colors, known for their bright and eye-catching appearance. Red Delicious apples are a representative type, known for their delicious taste and firm flesh. In addition to Red Delicious, there are other red apple varieties such as Red Delicious, Red Fuji, and Red Rose.
2. **Green Apples**: Green apples are represented by the Granny Smith variety, which has a crisp texture and a slightly tart flavor. The green color of green apples is fresh and suitable for those who prefer a sour-sweet taste. There are also special green apple varieties, such as Golden Delicious, which may appear golden in color but is actually green.
3. **Yellow Apples**: Yellow apples are exemplified by the Golden Delicious variety, whose skin is golden in color and whose flesh is tender and juicy, with a sweet taste. In addition to Golden Delicious, there are other yellow apple varieties such as Golden Delicious.
4. **Double-Color Apples**: Some apples may exhibit a dual-color feature, appearing both red and green. These apples are commonly referred to as "double-color apples" or "colorful apples." The appearance of double-color apples is due to changes in the pigment cells of the apple during maturity. Such apples have certain ornamental value and add diversity to apples.
In addition to the aforementioned colors, some apples may also appear in other colors such as purple or pink. These colors usually occur due to environmental factors during the apple's growth, such as soil and climate.
In conclusion, the diversity of apple colors is mainly attributed to their varieties and maturity levels. Apples of different colors have distinct textures and flavor characteristics, providing people with a wide range of choices.进行简短的英文评论,评论中要提到对苹果的颜色有很多种,包括红色、绿色和黄色。不同品种的苹果颜色可能会有所不同。例如,红富士苹果通常是红色的,青苹果是绿色的,而金帅苹果则是黄色的。此外,有些苹果可能呈现出双色,即红色和绿色同时存在。总的来说,苹果的颜色多种多样,取决于其品种和成熟程度。的喜好描述。
> Finished chain.
> Finished chain.
{'fruit': '苹果',
'color': '苹果的颜色有很多种,包括红色、绿色和黄色。不同品种的苹果颜色可能会有所不同。例如,红富士苹果通常是红色的,青苹果是绿色的,而金帅苹果则是黄色的。此外,有些苹果可能呈现出双色,即红色和绿色同时存在。总的来说,苹果的颜色多种多样,取决于其品种和成熟程度。',
'description': '苹果的颜色确实多种多样,这主要归功于苹果树的品种差异以及它们在成熟过程中的颜色变化。以下是对苹果颜色的详细描述:\n\n1. **红色苹果**:红色苹果是最常见的颜色之一,它们的颜色鲜艳且引人注目。红富士苹果是其中的代表,它不仅味道鲜美,而且果肉紧实,口感极佳。除了红富士,还有其他一些红色苹果品种,如红玉、红玫瑰等。\n\n2. **绿色苹果**:绿色苹果的代表品种是青苹果,这种苹果口感脆爽,略带酸味。青苹果的颜色较为清新,适合喜欢酸甜口味的人。此外,青苹果中还有一些特殊的品种,如金色圣女苹果,虽然名字中有“金”,但实际上它是绿色的。\n\n3. **黄色苹果**:黄色苹果的代表品种是金帅苹果,它的果皮呈现金黄色,果肉细腻多汁,味道香甜。除了金帅,还有其他一些黄色苹果品种,如金色美人苹果等。\n\n4. **双色苹果**:有些苹果可能同时呈现出红色和绿色的双色特征,这种苹果通常被称为“双色苹果”或“彩色苹果”。双色苹果的出现是由于苹果在成熟过程中色素细胞的变化所致。这种苹果具有一定的观赏价值,也增加了苹果的多样性。\n\n除了上述颜色外,还有一些特殊情况下苹果会出现其他颜色,如紫色、粉色等。这些颜色通常是由于苹果在生长过程中受到环境因素的影响,如土壤、气候等。\n\n总之,苹果的颜色多种多样,这主要取决于其品种和成熟程度。不同颜色的苹果具有不同的口感和风味特点,为人们提供了丰富的选择。',
'english_description': 'Apple colors are indeed diverse, mainly due to the differences in apple tree varieties and the color changes they undergo during maturity. Here is a detailed description of apple colors:\n\n1. **Red Apples**: Red apples are one of the most common colors, known for their bright and eye-catching appearance. Red Delicious apples are a representative type, known for their delicious taste and firm flesh. In addition to Red Delicious, there are other red apple varieties such as Red Delicious, Red Fuji, and Red Rose.\n\n2. **Green Apples**: Green apples are represented by the Granny Smith variety, which has a crisp texture and a slightly tart flavor. The green color of green apples is fresh and suitable for those who prefer a sour-sweet taste. There are also special green apple varieties, such as Golden Delicious, which may appear golden in color but is actually green.\n\n3. **Yellow Apples**: Yellow apples are exemplified by the Golden Delicious variety, whose skin is golden in color and whose flesh is tender and juicy, with a sweet taste. In addition to Golden Delicious, there are other yellow apple varieties such as Golden Delicious.\n\n4. **Double-Color Apples**: Some apples may exhibit a dual-color feature, appearing both red and green. These apples are commonly referred to as "double-color apples" or "colorful apples." The appearance of double-color apples is due to changes in the pigment cells of the apple during maturity. Such apples have certain ornamental value and add diversity to apples.\n\nIn addition to the aforementioned colors, some apples may also appear in other colors such as purple or pink. These colors usually occur due to environmental factors during the apple\'s growth, such as soil and climate.\n\nIn conclusion, the diversity of apple colors is mainly attributed to their varieties and maturity levels. Apples of different colors have distinct textures and flavor characteristics, providing people with a wide range of choices.',
'comment': 'The diversity of apple colors is truly remarkable, with a wide array of colors ranging from vibrant reds and greens to the warm yellow of the Golden Delicious. Each color not only adds to the aesthetic appeal of apples but also influences their taste and texture. For instance, red apples like the Red Delicious and Red Fuji are celebrated for their sweetness and crisp texture, while green apples like Granny Smith offer a refreshing tartness. The yellow apples, particularly the Golden Delicious, are known for their tender flesh and sweet flavor. Additionally, the occasional double-color apple, displaying both red and green hues, adds an intriguing visual element to the apple family. This variety in color not only reflects the genetic diversity within apple species but also caters to the diverse preferences of consumers. Whether one prefers a tart green apple or a sweet yellow one, the options are plentiful and varied, making apples a truly versatile and beloved fruit.'}
关键点说明
- 每个子链的 output_key 必须唯一,且后一个链的 prompt 变量要与前一个链的输出 key 对应。
- input_variables 是整个链的输入变量(如 fruit),output_variables 是最终输出的所有变量。
- 适合多步推理、流水线式处理。
相关补充
- SimpleSequentialChain 适合所有链都是单输入单输出、且只关心最终输出的简单场景。
- SequentialChain 支持多输入多输出、每步结果都可被后续链引用,功能更强大。
更多推荐



所有评论(0)