本文主要是介绍langchain入门系列之四 链结构的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
所谓的链结构,就是在相同的标准下,不同模块组成一条处理任务的流水线。优点是链提高了模块的标准化和复用性,缺点是增加了工程的复杂度和冗余。
本文着重介绍三种链接,langchain的链结构不止本文所介绍的,感兴趣的同学可以查看官方文档解惑,本文前提在于,读者已经阅读过我前面写的文章,若还没有,可能会有阅读上的障碍。
单链结构
定义prompt →定义llm→定义chain→运行predict
# LLM chain
import os
from langchain_community.chat_models import QianfanChatEndpoint
from langchain.prompts import PromptTemplate# 设置API
os.environ["QIANFAN_AK"] = ""
os.environ["QIANFAN_SK"] = ""llm = QianfanChatEndpoint(model="ERNIE-Bot-turbo")prompt = PromptTemplate.from_template("给我讲一个关于{对象}的黑色幽默笑话")
# prompt.format(对象="猫")from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)
print(chain.run("狗"))
多链结构
定义prompt →定义llms/embeddings→定义chain(多链并行,选择其中一条链进行处理)→运行predict
import os
from langchain_community.llms import QianfanLLMEndpoint# 设置API
os.environ["QIANFAN_AK"] = ""
os.environ["QIANFAN_SK"] = ""# 实例化(大模型)
llm = QianfanLLMEndpoint()geography_template = "你将扮演一位地理知识丰富的地理学家,你尤其对中国的地理人文知识了解透彻,你将回答用户提出的关于地理方面的问题,这里是问题{input}"
math_template = "你将扮演一位经验丰富的数学老师,你尤其擅长中小学方面的数学知识,你将回答用户提出的数学问题,这里是问题{input}"
prompt_infos = [{"name":"地理","description":"擅长地理","prompt_template":geography_template},{"name":"数学","description":"擅长数学","prompt_template":math_template},
]from langchain.chains import ConversationChain
from langchain.chains.router import MultiPromptChain
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChaindestination_chains = {}
# 对每一条单链进行初始化
for p in prompt_infos:name = p['name']prompt_template = p['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")from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
# 配置router
destinations = [f"{p['name']}:{p['description']}" for p in prompt_infos]
destinations_str = '\n'.join(destinations)
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=llm, prompt=router_prompt)from langchain.chains.router import MultiPromptChain
chain = MultiPromptChain(router_chain=router_chain,destination_chains=destination_chains,default_chain=default_chain,verbose=True,
)
print(chain.run("敦煌在中国哪个省?"))
------------------------------------
> Entering new MultiPromptChain chain...
None: {'input': '敦煌在中国哪个省份?'}
> Finished chain.
敦煌位于中国甘肃省。
由上面代码可以知道,多链结构至少会调两次llm。
多链顺序结构
定义prompt →定义llms/embeddings→定义chain(多链串行,按顺序执行)→运行predict
import os
from langchain_community.llms import QianfanLLMEndpoint# 设置API
os.environ["QIANFAN_AK"] = ""
os.environ["QIANFAN_SK"] = ""# 实例化(大模型)
llm = QianfanLLMEndpoint()from langchain.prompts import PromptTemplate
from langchain.chains import LLMChaintemplate = """你将扮演一位成语出题人,你将从你丰富的成语知识库里,给出三个关于{input}相关的成语"""
prompt_template = PromptTemplate(input_variables=["input"], template=template)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template)from langchain.prompts import PromptTemplate
from langchain.chains import LLMChaintemplate = """你将扮演一位成语分析者,擅长从多个维度分析成语,并且从中挑选出寓意最好的成语,成语分析:{synopsis},对上面的成语进行分析,挑选出寓意最好的成语"""
prompt_template = PromptTemplate(input_variables=["input"], template=template)
select_chain = LLMChain(llm=llm, prompt=prompt_template)from langchain.chains import SimpleSequentialChain
overall_chain = SimpleSequentialChain(chains=[synopsis_chain, select_chain], verbose=True)select = overall_chain.run("结婚")
-------------------------------------------Entering new SimpleSequentialChain chain...
以下是三个与结婚相关的成语:1. 喜结连理:这个成语形容两个人之间美好的姻缘和因缘关系。它常用来祝贺两人成婚的场景,传达着美满、幸福和长久的祝愿。
2. 百年好合:这个成语常用于祝福新婚夫妇的婚姻美满、幸福长久。它强调了婚姻的持久和美好,是表达祝福和喜悦的常用成语。
3. 共结连理枝:这个成语比喻两个人结为夫妻,是美好的结合,与“喜结连理”意思相近,但表达上略显浪漫。这些成语在汉语中经常被用来描述和祝福美满的婚姻,都充满了喜庆和幸福的色彩。
在分析这三个与结婚相关的成语后,我认为寓意最好的成语是“百年好合”。首先,“喜结连理”和“共结连理枝”两个成语都强调了两个人因缘际会、结为夫妻的美好关系,寓意着美满和幸福。然而,“百年好合”在这个基础上更进一步,它不仅表达了夫妻关系的和谐与美满,还特别强调了婚姻的持久性。在中国文化中,百年被视为一个很长的时间跨度,象征着长久的承诺和持续的幸福。因此,“百年好合”寓意着夫妻之间的长久美满和永恒的幸福,这是一个对婚姻最深切的祝福和期许。综上所述,我认为“百年好合”是这三个成语中寓意最好的一个。> Finished chain.
这篇关于langchain入门系列之四 链结构的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!