顺序链 Sequential
在调用语言模型之后,下一步是对语言模型进行一系列的调用。当您希望将一个调用的输出作为另一个调用的输入时,这将非常有用。
在这个笔记本中,我们将通过一些示例来演示如何使用顺序链来实现这一点。顺序链允许您连接多个链,并将它们组合成执行某个特定场景的流水线。有两种类型的顺序链:
SimpleSequentialChain
:顺序链的最简单形式,每个步骤都有一个单一的输入/输出,一个步骤的输出是下一个步骤的输入。SequentialChain
:顺序链的更一般形式,允许多个输入/输出。
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
# 这是一个LLMChain,根据剧本的标题来写一个简介。
llm = OpenAI(temperature=.7)
template = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title.
Title: {title}
Playwright: This is a synopsis for the above play:"""
prompt_template = PromptTemplate(input_variables=["title"], template=template)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template)
# 这是一个LLMChain,根据剧本的简介来写一篇评论。
llm = OpenAI(temperature=.7)
template = """You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
Play Synopsis:
{synopsis}
Review from a New York Times play critic of the above play:"""
prompt_template = PromptTemplate(input_variables=["synopsis"], template=template)
review_chain = LLMChain(llm=llm, prompt=prompt_template)
# 这是一个总体链,我们按顺序运行这两个链。
from langchain.chains import SimpleSequentialChain
overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain], verbose=True)
review = overall_chain.run("Tragedy at sunset on the beach")
print(review)
顺序链 Sequential Chain
当然,并不是所有的顺序链都像将一个字符串作为参数传递并获得一个字符串作为所有步骤的输出那样简单。在下一个示例中,我们将尝试更复杂的链,这些链涉及多个输入,并且还有多个最终输出。
特别重要的是我们如何命名输入/输出变量名。在上面的示例中,我们不需要考虑这个问题,因为我们只是直接将一个链的输出作为下一个链的输入传递,但在这里我们确实需要考虑这个问题,因为我们有多个输入。
# 这是一个LLMChain,根据剧本的标题和设置的时代来写一个简介。
llm = OpenAI(temperature=.7)
template = """You are a playwright. Given the title of play and the era it is set in, it is your job to write a synopsis for that title.
Title: {title}
Era: {era}
Playwright: This is a synopsis for the above play:"""
prompt_template = PromptTemplate(input_variables=["title", "era"], template=template)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="synopsis")
# 这是一个LLMChain,根据剧本的简介来写一篇评论。
llm = OpenAI(temperature=.7)
template = """You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
Play Synopsis:
{synopsis}
Review from a New York Times play critic of the above play:"""
prompt_template = PromptTemplate(input_variables=["synopsis"], template=template)
review_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="review")
# 这是一个总体链,我们按顺序运行这两个链。
from langchain.chains import SequentialChain
overall_chain = SequentialChain(
chains=[synopsis_chain, review_chain],
input_variables=["era", "title"],
# 这里我们返回多个变量
output_variables=["synopsis", "review"],
verbose=True)
overall_chain({"title":"Tragedy at sunset on the beach", "era": "Victorian England"})
顺序链中的记忆
有时候你可能想要在链的每个步骤或链的后面的某个部分传递一些上下文,但是维护和链接输入/输出变量可能会很混乱。使用SimpleMemory
是一种方便的方式来管理这个上下文并简化你的链。
例如,使用之前的playwright SequentialChain,假设你想要包含一些关于剧目的日期、时间和地点的上下文,并使用生成的剧情简介和评论来创建一些社交媒体的帖子文本。你可以将这些新的上下文变量添加为input_variables
,或者我们可以在链中添加一个SimpleMemory
来管理这个上下文:
from langchain.chains import SequentialChain
from langchain.memory import SimpleMemory
llm = OpenAI(temperature=.7)
template = """You are a social media manager for a theater company. Given the title of play, the era it is set in, the date,time and location, the synopsis of the play, and the review of the play, it is your job to write a social media post for that play.
Here is some context about the time and location of the play:
Date and Time: {time}
Location: {location}
Play Synopsis:
{synopsis}
Review from a New York Times play critic of the above play:
{review}
Social Media Post:
"""
prompt_template = PromptTemplate(input_variables=["synopsis", "review", "time", "location"], template=template)
social_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="social_post_text")
overall_chain = SequentialChain(
memory=SimpleMemory(memories={"time": "December 25th, 8pm PST", "location": "Theater in the Park"}),
chains=[synopsis_chain, review_chain, social_chain],
input_variables=["era", "title"],
# Here we return multiple variables
output_variables=["social_post_text"],
verbose=True)
overall_chain({"title":"Tragedy at sunset on the beach", "era": "Victorian England"})
> 进入新的SequentialChain链...
> 完成链。
{
'title': 'Tragedy at sunset on the beach',
'era': 'Victorian England',
'time': 'December 25th, 8pm PST',
'location': 'Theater in the Park',
'social_post_text': "\nSpend your Christmas night with us at Theater in the Park and experience the heartbreaking story of love and loss that is 'A Walk on the Beach'. Set in Victorian England, this romantic tragedy follows the story of Frances and Edward, a young couple whose love is tragically cut short. Don't miss this emotional and thought-provoking production that is sure to leave you in tears. #AWalkOnTheBeach #LoveAndLoss #TheaterInThePark #VictorianEngland"}