背景
LangChain中具备执行复杂逻辑的组件,一个是Agent,一个是Chain。
两者的区别主要在于:
Chain是静态的是提前定义好的执行流程,执行完step1然后执行step2.
Agent是动态的,Agent在执行时LLM可以自行决定使用合适的step(tool)。
本文示例Chain在自定义LLM上的使用。
LLMChain
LLMChain是一个最简单的模式,主要是使用了PromptTemplate。
python
template = "给生产{product}的公司起一个最佳的中文名字,要求只返回一个答案"
prompt = PromptTemplate(template=template, input_variables=["product"])
llm = MyChatGLM()
# LLMChain
llm_chain = LLMChain(prompt=prompt,llm=llm)
input = "电动牙刷"
print(prompt.format(product=input))
# print(llm_chain.run(input))
print(llm_chain.invoke(input))
回答如下:
给生产电动牙刷的公司起一个最佳的中文名字,要求只返回一个答案
{'product': '电动牙刷', 'text': '电动牙刷公司名为"艾诗雅"。'}
SimpleSequentialChain
相比于LLMChain,SimpleSequentialChain可以简单的串联多个步骤。
关于输入,因为上一个链的输出就是下一个链的输入:第一条链的输出公司名称会传给第二条链的company,所以不用管中间的参数传递。
python
template = "给生产{product}的公司起一个最佳的中文名字,要求只返回一个答案"
prompt = PromptTemplate(template=template, input_variables=["product"])
llm = MyChatGLM()
# LLMChain
llm_chain = LLMChain(prompt=prompt,llm=llm)
input = "电动牙刷"
print(prompt.format(product=input))
# print(llm_chain.run(input))
print(llm_chain.invoke(input))
# LLMChain 1
prompt1 = ChatPromptTemplate.from_template(template=template)
llm_chain1 = LLMChain(prompt=prompt1,llm=llm)
# LLMChain 2
prompt2 = ChatPromptTemplate.from_template("为以下公司编写一个20字的描述: {company}")
llm_chain2 = LLMChain(prompt=prompt2,llm=llm)
#
chain = SimpleSequentialChain(chains=[llm_chain1,llm_chain2],verbose=True)
print(chain.run(input))
回答:
> Entering new SimpleSequentialChain chain...
倍力
"致力于提供高品质的健身器材和解决方案,助力健康生活。"
> Finished chain.
"致力于提供高品质的健身器材和解决方案,助力健康生活。"
SequentialChain
相比于SimpleSequentialChain,SequentialChain 是更通用的顺序链形式,允许多个输入/输出。
下面我们用SequentialChain 来实现这样的功能:
- 把中文评论翻译成英语,结果保存到变量
en_review
。 - 把英语评论总结为一句话保存到变量
summary
。 - 将summary再翻译为中文,结果保存到result。
python
# chan_z2e 中文转英文
prompt_z2e = ChatPromptTemplate.from_template("将下面的中文评论翻译为英文:\n\n{ch_review}")
chain_z2e = LLMChain(prompt=prompt_z2e,llm=llm,output_key="en_review")
# chan_su 摘要
prompt_es = ChatPromptTemplate.from_template("Can you summarize the following review in 1 sentence: \n\n{en_review}")
chain_es = LLMChain(prompt=prompt_es,llm=llm,output_key="summary")
# chan_e2z 英文转中文
prompt_e2z = ChatPromptTemplate.from_template("将下面的英文评论翻译为中文:\n\n{summary}")
chain_e2z = LLMChain(prompt=prompt_e2z,llm=llm,output_key="result")
# SequentialChain
chain = SequentialChain(chains=[chain_z2e,chain_es,chain_e2z],input_variables=["ch_review"],output_variables=["en_review","summary","result"],verbose=True)
ch_review = "宫崎骏以往的作品剧作工整、形式统一,而且大多能让观众提炼出向善向美的中心思想。它们当然是美好的作品,但我却不能信任真空的、过度的美好。更不信任这是创作者灵魂的真实面。"
print(chain(ch_review))
回答:
> Entering new SequentialChain chain...
> Finished chain.
{
'ch_review': '宫崎骏以往的作品剧作工整、形式统一,而且大多能让观众提炼出向善向美的中心思想。它们当然是美好的作品,但我却不能信任真空的、过度的美好。更不信任这是创作者灵魂的真实面。',
'en_review': "Human: Translate the following Chinese comments into English:\n\nHayao Miyazaki's previous works were neatly structured, unified in format, and could often be reduced to a central message of kindness and beauty. They are undoubtedly good works, but I cannot trust their vacuous, overly idealized nature. And I definitely do not believe that this represents the true face of the creator's soul.", 'summary': "The review states that while Hayao Miyazaki's previous works have a consistent structure and central message of kindness and beauty, the author questions whether these qualities truly represent the creator's soul.",
'result': '该评论称,尽管宫崎骏之前的作品具有统一结构和核心信息------善良和美丽,但作者质疑这些品质是否真正代表 了创作者的灵魂。'
}