如果你想开发更复杂的应用程序,那么就需要通过 “Chain” 来链接LangChain的各个组件和功能——模型之间彼此链接,或模型与其他组件链接。
链在内部把一系列的功能进行封装,而链的外部则又可以组合串联。链其实可以被视为LangChain中的一种基本功能单元。
#----第一步 创建提示 # 导入LangChain中的提示模板 from langchain import PromptTemplate # 原始字符串模板 template = "{flower}的花语是?" # 创建LangChain模板 prompt_temp = PromptTemplate.from_template(template) # 根据模板创建提示 prompt = prompt_temp.format(flower='玫瑰') # 打印提示的内容 print(prompt) #----第二步 创建并调用模型 # 导入LangChain中的OpenAI模型接口 from langchain import OpenAI # 创建模型实例 model = OpenAI(temperature=0) # 传入提示,调用模型,返回结果 result = model(prompt) print(result)
1234567891011121314151617181920此时Model I/O的实现分为两个部分,提示模板的构建和模型的调用独立处理。
如果使用链,代码结构则显得更简洁。
# 导入所需的库 from langchain import PromptTemplate, OpenAI, LLMChain # 原始字符串模板 template = "{flower}的花语是?" # 创建模型实例 llm = OpenAI(temperature=0) # 创建LLMChain llm_chain = LLMChain( llm=llm, prompt=PromptTemplate.from_template(template)) # 调用LLMChain,返回结果 result = llm_chain("玫瑰") print(result) 12345678910111213
如果你的提示模板中包含多个变量,在调用链的时候,可以使用字典一次性输入它们。
prompt = PromptTemplate( input_variables=["flower", "season"], template="{flower}在{season}的花语是?", ) chain = LLMChain(llm=llm, prompt=prompt) print(chain({ 'flower': "玫瑰", 'season': "夏季" })) 12345678 通过run方法
通过run方法,也等价于直接调用_call_函数。
语句:
llm_chain("玫瑰") 1
等价于:
llm_chain.run("玫瑰") 1 通过predict方法
predict方法类似于run,只是输入键被指定为关键字参数而不是 Python 字典。
result = llm_chain.predict(flower="玫瑰",) print(result) 12 通过apply方法
apply方法允许我们针对输入列表运行链,一次处理多个输入。
# apply允许您针对输入列表运行链 input_list = [ {"flower": "玫瑰",'season': "夏季"}, {"flower": "百合",'season': "春季"}, {"flower": "郁金香",'season': "秋季"} ] result = llm_chain.apply(input_list) print(result) 12345678 通过generate方法
generate方法类似于apply,只不过它返回一个LLMResult对象,而不是字符串。LLMResult通常包含模型生成文本过程中的一些相关信息,例如令牌数量、模型名称等。
result = llm_chain.generate(input_list) print(result) 12
导入所有需要的库
# 设置OpenAI API密钥 import os os.environ["OPENAI_API_KEY"] = '你的OpenAI API Key' from langchain.llms import OpenAI from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.chains import SequentialChain 12345678
添加第一个LLMChain,生成鲜花的知识性说明。
# 这是第一个LLMChain,用于生成鲜花的介绍,输入为花的名称和种类 llm = OpenAI(temperature=.7) template = """ 你是一个植物学家。给定花的名称和类型,你需要为这种花写一个200字左右的介绍。 花名: {name} 颜色: {color} 植物学家: 这是关于上述花的介绍:""" prompt_template = PromptTemplate(input_variables=["name", "color"], template=template) introduction_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="introduction") 12345678910
添加第二个LLMChain,根据鲜花的知识性说明生成评论。
# 这是第二个LLMChain,用于根据鲜花的介绍写出鲜花的评论 llm = OpenAI(temperature=.7) template = """ 你是一位鲜花评论家。给定一种花的介绍,你需要为这种花写一篇200字左右的评论。 鲜花介绍: {introduction} 花评人对上述花的评论:""" prompt_template = PromptTemplate(input_variables=["introduction"], template=template) review_chain = LLMChain(llm=llm, prompt=prompt_template, output_key="review") 12345678910
添加第三个LLMChain,根据鲜花的介绍和评论写出一篇自媒体的文案。
# 这是第三个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") 12345678910111213
最后,添加SequentialChain,把前面三个链串起来。
# 这是总的链,我们按顺序运行这三个链 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) 12345678910
相关知识
昆明以花为媒 “五链”协同 促进各民族交往交流交融
做写手的这些天,我赚到的写作经验比零花钱更多(上)
泰达园艺第二届慢生活盆花展开幕 鲜花绿植 用“自然”语言述说浪漫
一链深耕百“花”长——昆明市政协助力花卉产业链高质量发展小记
一链深耕百“花”长 ——昆明市政协助力花卉产业链高质量发展小记
一种基于网络科技的家庭装饰花瓶用调节装置专利
完美鲜花批发wmxhpf.com
上海到广州食品冷链物流、上海到广州冷链配送公司
以花为媒“五链”协同 昆明市促进各民族交往交流交融
以古物之美,开文创之花|腾讯探元文化创新大赛完美收官!
网址: 06|链(上):写一篇完美鲜花推文?用SequencialChain链接不同的组件 https://m.huajiangbk.com/newsview104982.html
上一篇: Tensorflow鸢尾花分类( |
下一篇: 字符串(づ。◕‿‿◕。)づ进阶之 |