原文:langchain源码分析-chains模块介绍【2】 - 知乎
这是对langchain源码剖析的系列文章,也有对应的本站视频和b站视频,建议读者可以结合视频和文章一起看。
chains对组件的一系列调用,因为在很多场景下,一个完成的功能需要拆分成多个组件调用,将多个组件组合在一起形成完整的pipleline。 组件的调用可以理解为是单个功能的实现,可以保证功能的灵活性,在很多的通用场景下都能使用,有利于功能的复用。 chains的调用,以完整任务为单位,贴合实际应用。 chains模块的功能整体分为两部分:
from langchain import PromptTemplate, OpenAI, LLMChain
prompt_template = "列举5个描述场景 {scene} 特点的词"
prompt = PromptTemplate(
input_variables=["scene"],
template=prompt_template
)
llm = OpenAI(temperature=0)
# 核心要素: 模型,prompt模板
llm_chain = LLMChain(
llm=llm,
prompt=prompt
)
print(llm_chain.input_keys)
# 调用call方法
# 返回输入和输出
output = llm_chain({"scene": "长江"})
print(output)
output = llm_chain.run({"scene": "矿泉水"})
print(output)
# 参数指定,只返回输出
output = llm_chain({"scene": "公园"}, return_only_outputs=True)
print(output)
# 参数指定,只返回输出, 不使用字典方式输入(对后续chain是有影响的)
# output = llm_chain("书包", return_only_outputs=True)
# print(output)
# apply方法: 多输入
input_list = [
{"scene": "房子"},
{"scene": "图书馆"}
]
# llm_chain.apply(input_list)
from langchain.chains import TransformChain, LLMChain, SimpleSequentialChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
import json
# transformer chain
def select_feature(inputs):
print("inputs:", inputs)
feature_str = inputs["multi_feature"]
single_feature = feature_str.split("*$*")[0]
return {"single_feature": single_feature}
transfor_chain = TransformChain(
input_variables=["multi_feature"],
output_variables=["single_feature"],
transform=select_feature,)
output = transfor_chain({'scene': '白云', 'multi_feature': '云朵*$*柔和*$*悠扬'}, return_only_outputs=True)
print(output)
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain
llm = OpenAI(temperature=0)
template = """ 你是一名小学语文老师,给定一个场景, 使用2-3个词描述其特点:
场景: {scene}
场景特点: """
prompt_template = PromptTemplate(input_variables=["scene"], template=template)
scene_chain = LLMChain(llm=llm, prompt=prompt_template)
llm = OpenAI(temperature=.7)
template = """你是一名小学语文老师,根据给定场景的特点,请造句,要求使用这些场景特点的词,
特点:{feature}
造句结果: """
prompt_template = PromptTemplate(input_variables=["feature"], template=template)
sentence_chain = LLMChain(llm=llm, prompt=prompt_template)
overall_chain = SimpleSequentialChain(chains=[scene_chain, sentence_chain], verbose=True)
review = overall_chain.run("沙滩")
llm = OpenAI(temperature=.7)
template = """ 你是一名小学语文老师,给定一个场景, 使用2-3个词描述其特点:
场景: {scene}
场景特点: """
prompt_template = PromptTemplate(input_variables=["scene"], template=template)
scene_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="feature")
llm = OpenAI(temperature=.7)
template = """你是一名小学语文老师,根据给定场景的特点,请造句,要求使用这些场景特点的词,
特点:{feature}
造句结果: """
prompt_template = PromptTemplate(input_variables=["feature"], template=template)
sentence_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="sentence")
llm = OpenAI(temperature=.7)
template = """你是一名小学语文老师,根据给定场景的特点,一次给出这些特点词的反义词,
特点:{feature}
反义词为: """
prompt_template = PromptTemplate(input_variables=["feature"], template=template)
antonym_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="antonym")
from langchain.chains import SequentialChain
overall_chain = SequentialChain(
chains=[scene_chain, sentence_chain, antonym_chain],
input_variables=["scene"],
# Here we return multiple variables
output_variables=["feature", "sentence", "antonym"],
verbose=True)
overall_chain({"scene":"白云"})
from langchain.chains import TransformChain, LLMChain, SimpleSequentialChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
import json
# first chain
llm = OpenAI(temperature=.7)
template = """ 你是一名小学语文老师,给定一个场景, 使用2-3个词描述其特点, 词时间使用*$*分割:
场景: {scene}
场景特点: """
prompt_template = PromptTemplate(input_variables=["scene"], template=template)
scene_chain = LLMChain(llm=llm, prompt=prompt_template,
output_key="multi_feature")
# transformer chain
def select_feature(inputs):
print("inputs:", inputs)
feature_str = inputs["multi_feature"]
single_feature = feature_str.split("*$*")[0]
return {"single_feature": single_feature}
transfor_chain = TransformChain(
input_variables=["multi_feature"],
output_variables=["single_feature"],
transform=select_feature,)
# third chain
template = """你是一名小学语文老师,根据给定场景的特点造句,
特点:{single_feature}
造句结果: """
prompt_template = PromptTemplate(input_variables=["single_feature"], template=template)
llm = OpenAI(temperature=.7)
sentence_chain = LLMChain(llm=llm, prompt=prompt_template,
output_key="sentence")
from langchain.chains import SequentialChain
overall_chain = SequentialChain(
chains=[scene_chain, transfor_chain, sentence_chain],
input_variables=["scene"],
# Here we return multiple variables
output_variables=["multi_feature", "single_feature", "sentence"],
verbose=True)
overall_chain({"scene":"白云"})