Chain
所谓链,顾名思义,就是把LangChain中的各个部分串起来,形成一个模块化的东西。
- 首先LangChain通过设计好的接口,实现一个具体的链的功能。例如,LLM链(LLMChain)能够接受用户输入,使用 PromptTemplate 对其进行格式化,然后将格式化的响应传递给 LLM。这就相当于把整个Model I/O的流程封装到链里面。
- 实现了链的具体功能之后,我们可以通过将多个链组合在一起,或者将链与其他组件组合来构建更复杂的链。
链其实可以被视为LangChain中的一种基本功能单元。
| 预制链: |
|---|
最简单的链:LLMChain
LLMChain相当于把Model I/O放在一个链中整体操作。
from langchain_openai import ChatOpenAI
import os
from langchain import PromptTemplate, LLMChain
template = "{flower}的花语是?"
prompt_temp = PromptTemplate.from_template(template)
model = ChatOpenAI(temperature=0, model=os.environ.get("LLM_MODELEND"))
-----------------不用Chain-------------------
prompt = prompt_temp.format(flower="玫瑰")
result = model.predict(prompt)
print(result)
-----------------用Chain---------------------
chain = LLMChain(llm=model, prompt=prompt_temp)
result = chain("玫瑰")
print(result)
链的调用方式
直接调用
像函数一样调用一个chain对象,实际上会调用该对象内部实现的__call__方法。llm_chain({'flower': "玫瑰", 'season': "夏季" }))
通过run方法
通过run方法,也等价于直接调用_call_函数。llm_chain.run("玫瑰")<=>llm_chain("玫瑰")
通过predict方法
predict方法类似于run,只是输入键被指定为关键字参数而不是 Python 字典。llm_chain.predict(flower="玫瑰")
通过apply方法
apply方法允许我们针对输入列表运行链,一次处理多个输入。
input_list = [
{"flower": "玫瑰",'season': "夏季"},
{"flower": "百合",'season': "春季"},
{"flower": "郁金香",'season': "秋季"}
]
result = llm_chain.apply(input_list)
通过generate方法
generate方法类似于apply,只不过它返回一个LLMResult对象,而不是字符串。LLMResult通常包含模型生成文本过程中的一些相关信息,例如令牌数量、模型名称等。llm_chain.generate(input_list)
Sequential Chain:顺序链
"""
本文件是【链(上):写一篇完美鲜花推文?用SequencialChain链接不同的组件】章节的配套代码,课程链接:https://juejin.cn/book/7387702347436130304/section/7388070991978561588
您可以点击最上方的“运行“按钮,直接运行该文件;更多操作指引请参考Readme.md文件。
"""
# 设置OpenAI API密钥
import os
# 导入所需要的库
from langchain.chains import LLMChain, SequentialChain
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
# 第一个LLMChain:生成鲜花的介绍
llm = ChatOpenAI(
temperature=0.7,
model=os.environ.get("LLM_MODELEND"),
)
template = """
你是一个植物学家。给定花的名称和类型,你需要为这种花写一个200字左右的介绍。
花名: {name}
颜色: {color}
植物学家: 这是关于上述花的介绍:"""
prompt_template = PromptTemplate(input_variables=["name", "color"], template=template)
introduction_chain = LLMChain(
llm=llm, prompt=prompt_template, output_key="introduction"
)
# 第二个LLMChain:根据鲜花的介绍写出鲜花的评论
template = """
你是一位鲜花评论家。给定一种花的介绍,你需要为这种花写一篇200字左右的评论。
鲜花介绍:
{introduction}
花评人对上述花的评论:"""
prompt_template = PromptTemplate(input_variables=["introduction"], template=template)
review_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="review")
# 第三个LLMChain:根据鲜花的介绍和评论写出一篇自媒体的文案
template = """
你是一家花店的社交媒体经理。给定一种花的介绍和评论,你需要为这种花写一篇社交媒体的帖子,300字左右。
鲜花介绍:
{introduction}
花评人对上述花的评论:
{review}
社交媒体帖子:
"""
prompt_template = PromptTemplate(
input_variables=["introduction", "review"], template=template
)
social_post_chain = LLMChain(
llm=llm, prompt=prompt_template, output_key="social_post_text"
)
# 总的链:按顺序运行三个链
overall_chain = SequentialChain(
chains=[introduction_chain, review_chain, social_post_chain],
input_variables=["name", "color"],
output_variables=["introduction", "review", "social_post_text"],
verbose=True,
)
# 运行链并打印结果
result = overall_chain({"name": "玫瑰", "color": "黑色"})
print(result)