各种chain的介绍
- 串联式编排调用链:SequentialChain
- 流水线 胶水代码逻辑处理
- 具备编排逻辑 串行 one by one的调用
- 上一个chain的输出 作为 下一个chain的输入
- 超长文本的转换 Transform Chain
- pdf文件处理
- 提供了套壳的能力 将python处理字符串的能力 套用进来 完成数据的格式化处理
- 实现条件判断的路由链:RouterChain
- 复杂逻辑 条件判断
- 组合routerchain 目标链 通过条件判断 选择对应的目标链进行调用
Sequential Chain
串联式调用语言模型(将一个调用的输出作为另一个调用的输入)。
顺序链(Sequential Chain )允许用户连接多个链并将它们组合成执行特定场景的流水线(Pipeline)。有两种类型的顺序链:
- SimpleSequentialChain:最简单形式的顺序链,每个步骤都具有单一输入/输出,并且一个步骤的输出是下一个步骤的输入。
- SequentialChain:更通用形式的顺序链,允许多个输入/输出。
示例- 使用 SimpleSequentialChain 实现戏剧摘要和评论(单输入/单输出
chain1 定义 synopsis_chain
这是一个 LLMChain,用于根据剧目的标题撰写简介
# 这是一个 LLMChain,用于根据剧目的标题撰写简介。
llm = OpenAI(temperature=0.7, max_tokens=1000)
template = """你是一位剧作家。根据戏剧的标题,你的任务是为该标题写一个简介。
标题:{title}
剧作家:以下是对上述戏剧的简介:"""
prompt_template = PromptTemplate(input_variables=["title"], template=template)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template)
chain2 定义review chain
这是一个LLMChain,用于根据剧情简介撰写一篇戏剧评论。
# 这是一个LLMChain,用于根据剧情简介撰写一篇戏剧评论。
# llm = OpenAI(temperature=0.7, max_tokens=1000)
template = """你是《纽约时报》的戏剧评论家。根据剧情简介,你的工作是为该剧撰写一篇评论。
剧情简介:
{synopsis}
以下是来自《纽约时报》戏剧评论家对上述剧目的评论:"""
prompt_template = PromptTemplate(input_variables=["synopsis"], template=template)
review_chain = LLMChain(llm=llm, prompt=prompt_template)
SimpleSequentialChain 完整流程图
完整代码示例
import os
from langchain_openai import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SimpleSequentialChain
api_key = 'sk-xxx'
os.environ["OPENAI_API_KEY"] = api_key
serp_api = 'xxx'
os.environ["SERPAPI_API_KEY"] = serp_api
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0.7, max_tokens=1000)
def get_synopsis_chain():
# 这是一个 LLMChain,用于根据剧目的标题撰写简介。
template = """你是一位剧作家。根据戏剧的标题,你的任务是为该标题写一个简介。
标题:{title}
剧作家:以下是对上述戏剧的简介:"""
prompt_template = PromptTemplate(input_variables=["title"], template=template)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template)
return synopsis_chain
def get_review_chain():
# 这是一个LLMChain,用于根据剧情简介撰写一篇戏剧评论。
# llm = OpenAI(temperature=0.7, max_tokens=1000)
template = """你是《纽约时报》的戏剧评论家。根据剧情简介,你的工作是为该剧撰写一篇评论。
剧情简介:
{synopsis}
以下是来自《纽约时报》戏剧评论家对上述剧目的评论:"""
prompt_template = PromptTemplate(input_variables=["synopsis"], template=template)
review_chain = LLMChain(llm=llm, prompt=prompt_template)
return review_chain
def main():
# 这是一个SimpleSequentialChain,按顺序运行这两个链
synopsis_chain = get_synopsis_chain()
review_chain = get_review_chain()
overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain], verbose=True)
review = overall_chain.run("三体人不是无法战胜的")
print(review)
if __name__ == "__main__":
main()
输出内容
示例-使用 SequentialChain 实现戏剧摘要和评论(多输入/多输出)
import os
from langchain_openai import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.chains import SequentialChain
api_key = 'sk-xx'
os.environ["OPENAI_API_KEY"] = api_key
serp_api = 'xxx'
os.environ["SERPAPI_API_KEY"] = serp_api
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0.7, max_tokens=1000)
def get_synopsis_chain():
# # 这是一个 LLMChain,根据剧名和设定的时代来撰写剧情简介。
template = """你是一位剧作家。根据戏剧的标题和设定的时代,你的任务是为该标题写一个简介。
标题:{title}
时代:{era}
剧作家:以下是对上述戏剧的简介:"""
prompt_template = PromptTemplate(input_variables=["title", "era"], template=template)
# output_key
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="synopsis", verbose=True)
return synopsis_chain
def get_review_chain():
# 这是一个LLMChain,用于根据剧情简介撰写一篇戏剧评论。
template = """你是《纽约时报》的戏剧评论家。根据该剧的剧情简介,你需要撰写一篇关于该剧的评论。
剧情简介:
{synopsis}
来自《纽约时报》戏剧评论家对上述剧目的评价:"""
prompt_template = PromptTemplate(input_variables=["synopsis"], template=template)
review_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="review", verbose=True)
return review_chain
def main():
# 这是一个SimpleSequentialChain,按顺序运行这两个链
synopsis_chain = get_synopsis_chain()
review_chain = get_review_chain()
m_overall_chain = SequentialChain(
chains=[synopsis_chain, review_chain],
input_variables=["era", "title"],
# Here we return multiple variables
output_variables=["synopsis", "review"],
verbose=True)
result = m_overall_chain({"title":"三体人不是无法战胜的", "era": "二十一世纪的新中国"})
print(result)
if __name__ == "__main__":
main()
输出结果