—1—
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: var(--articleFontsize);letter-spacing: 0.034em;text-align: justify;">LangChain 6大模块是什么?
—2—
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: var(--articleFontsize);letter-spacing: 0.034em;text-align: justify;">Chains 深度剖析
这种类型的 Chain 应用起来很简单也可以说是后续要介绍 Chain 的基础,但其功能是足够强大的。通过 LLMChain 可以直接将数据、Prompt、以及想要应用的大模型串到一起,以下是 LLMChain 的例子。
fromlangchainimportPromptTemplate,OpenAI,LLMChain
prompt_template="Whatisagoodnameforacompanythatmakes{product}?"
llm=OpenAI(temperature=0)
chain=LLMChain(
llm=llm,
prompt=PromptTemplate.from_template(prompt_template)
)
print(chain("colorfulsocks"))
#输出结果'Socktastic!'
在这个例子中,我们首先初始化了一个 Prompt 字符串模版,并初始化大模型,然后利用 Chain 将模型运行起来。在「Chain 将模型运行起来」这个过程中:Chain 将会格式化 Prompt,然后将它传递给 LLM。
不同于基本的 LLMChain,Sequential Chain(序列链)是由一系列的链组合而成的,序列链有两种类型,一种是单个输入输出,另一个则是多个输入输出。先来看第一种单个输入输出的示例代码:
在这个示例中,创建了两条 Chain,并且让第一条 Chain 接收一个虚构剧本的标题,输出该剧本的概要,作为第二条 Chain 的输入,然后生成一个虚构评论。通过 Sequential Chains 可以简单实现这一需求。
第一条 Chain:
#ThisisanLLMChaintowriteasynopsisgivenatitleofaplay.
fromlangchainimportPromptTemplate,OpenAI,LLMChain
llm=OpenAI(temperature=.7)
template="""Youareaplaywright.Giventhetitleofplay,itisyourjobtowriteasynopsisforthattitle.
Title:{title}
Playwright:Thisisasynopsisfortheaboveplay:"""
prompt_template=PromptTemplate(input_variables=["title"],template=template)
synopsis_chain=LLMChain(llm=llm,prompt=prompt_template)
第二条 Chain:
#ThisisanLLMChaintowriteareviewofaplaygivenasynopsis.
fromlangchainimportPromptTemplate,OpenAI,LLMChain
llm=OpenAI(temperature=.7)
template="""YouareaplaycriticfromtheNewYorkTimes.Giventhesynopsisofplay,itisyourjobtowriteareviewforthatplay.
PlaySynopsis:
{synopsis}
ReviewfromaNewYorkTimesplaycriticoftheaboveplay:"""
prompt_template=PromptTemplate(input_variables=["synopsis"],template=template)
review_chain=LLMChain(llm=llm,prompt=prompt_template)
最后利用 SimpleSequentialChain 即可将两个 Chains 直接串联起来:
fromlangchain.chainsimportSimpleSequentialChain
overall_chain=SimpleSequentialChain(chains=[synopsis_chain,review_chain],verbose=True)
print(review=overall_chain.run("Tragedyatsunsetonthebeach"))
可以看到对于单个输入输出的顺序链,就是将两个 Chain 作为参数传给 SimpleSequentialChain 即可,无需复杂的声明。
除了单个输入输出的模式,序列链还支持更为复杂的多个输入输出,对于多输入输出模式来说,最应该需要关注的就是输入关键字和输出关键字,它们需要十分的精准,才能够保证 Chain 的识别与应用,以一个 Demo 为例:
fromlangchainimportPromptTemplate,OpenAI,LLMChain
llm=OpenAI(temperature=.7)
template="""Youareaplaywright.Giventhetitleofplayandtheeraitissetin,itisyourjobtowriteasynopsisforthattitle.
Title:{title}
Era:{era}
Playwright:Thisisasynopsisfortheaboveplay:"""
prompt_template=PromptTemplate(input_variables=["title",'era'],template=template)
synopsis_chain=LLMChain(llm=llm,prompt=prompt_template,output_key="synopsis")
#第一条chain
fromlangchainimportPromptTemplate,OpenAI,LLMChain
llm=OpenAI(temperature=.7)
template="""YouareaplaycriticfromtheNewYorkTimes.Giventhesynopsisofplay,itisyourjobtowriteareviewforthatplay.
PlaySynopsis:
{synopsis}
ReviewfromaNewYorkTimesplaycriticoftheaboveplay:"""
prompt_template=PromptTemplate(input_variables=["synopsis"],template=template)
review_chain=LLMChain(llm=llm,prompt=prompt_template,output_key="review")
#第二条chain
fromlangchain.chainsimportSequentialChain
overall_chain=SequentialChain(
chains=[synopsis_chain,review_chain],
input_variables=["era","title"],
#Herewereturnmultiplevariables
output_variables=["synopsis","review"],
verbose=True)
#第三条chain
overall_chain({"title":"Tragedyatsunsetonthebeach","era":"VictorianEngland"})
对于每一个Chain在定义的时候,都需要关注其output_key 和input_variables,按照顺序将其指定清楚。最终在运行 Chain 时我们只需要指定第一个 Chain 中需要声明的变量。
再介绍一个经常会用到的场景,比如:我们目前有三类 Chain,分别对应三种学科的问题解答。我们的输入内容也是与这三种学科对应,但是随机的,比如:第一次输入数学问题,第二次有可能是历史问题......这时候期待的效果是:可以根据输入的内容是什么,自动将其应用到对应的子链中。RouterChain 就为我们提供了这样一种能力,它会首先决定将要传递下去的子链,然后把输入传递给那个链。并且在设置的时候需要为其设置默认 Chain,以兼容输入内容不满足任意一项时的情况。
physics_template="""Youareaverysmartphysicsprofessor.\
Youaregreatatansweringquestionsaboutphysicsinaconciseandeasytounderstandmanner.\
Whenyoudon'tknowtheanswertoaquestionyouadmitthatyoudon'tknow.
Hereisaquestion:
{input}"""
math_template="""Youareaverygoodmathematician.Youaregreatatansweringmathquestions.\
Youaresogoodbecauseyouareabletobreakdownhardproblemsintotheircomponentparts,\
answerthecomponentparts,andthenputthemtogethertoanswerthebroaderquestion.
Hereisaquestion:
{input}"""
如上述有一个物理学和数学的 Prompt:
prompt_infos=[
{
"name":"physics",
"description":"Goodforansweringquestionsaboutphysics",
"prompt_template":physics_template
},
{
"name":"math",
"description":"Goodforansweringmathquestions",
"prompt_template":math_template
}
]
然后需要声明这两个 Prompt 的基本信息。
fromlangchainimportConversationChain,LLMChain,PromptTemplate,OpenAI
llm=OpenAI()
destination_chains={}
forp_infoinprompt_infos:
name=p_info["name"]
prompt_template=p_info["prompt_template"]
prompt=PromptTemplate(template=prompt_template,input_variables=["input"])
chain=LLMChain(llm=llm,prompt=prompt)
destination_chains[name]=chain
default_chain=ConversationChain(llm=llm,output_key="text")
最后将其运行到 RouterChain 中即可,我们此时在输入的时候 Chain 就会根据 input 的内容进行相应的选择最为合适的 Prompt。
fromlangchain.chains.router.llm_routerimportLLMRouterChain,RouterOutputParser
fromlangchain.chains.router.multi_prompt_promptimportMULTI_PROMPT_ROUTER_TEMPLATE
#Createalistofdestinations
destinations=[f"{p['name']}:{p['description']}"forpinprompt_infos]
destinations_str="\n".join(destinations)
#Createaroutertemplate
router_template=MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=destinations_str)
router_prompt=PromptTemplate(
template=router_template,
input_variables=["input"],
output_parser=RouterOutputParser(),
)
router_chain=LLMRouterChain.from_llm(llm,router_prompt)
chain=MultiPromptChain(
router_chain=router_chain,
destination_chains=destination_chains,
default_chain=default_chain,
verbose=True,
)
print(chain.run('什么是黑体辐射'))
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |