本文主要是介绍LangChain-07 Multiple Chains 多链串联,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Runnables can easily be used to string together multiple Chains
Runnables 可以很容易地用于将多个链串在一起
安装依赖
pip install --upgrade --quiet langchain-core langchain-community langchain-openai
编写代码
from operator import itemgetterfrom langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAIprompt1 = ChatPromptTemplate.from_template("what is the city {person} is from?")
prompt2 = ChatPromptTemplate.from_template("what country is the city {city} in? respond in {language}"
)model = ChatOpenAI(model="gpt-3.5-turbo",
)chain1 = prompt1 | model | StrOutputParser()chain2 = ({"city": chain1, "language": itemgetter("language")}| prompt2| model| StrOutputParser()
)message = chain2.invoke({"person": "obama", "language": "spanish"})
print(f"message: {message}")
观察代码
chain1 = prompt1 | model | StrOutputParser()chain2 = ({"city": chain1, "language": itemgetter("language")}| prompt2| model| StrOutputParser()
)
我们可以发现, chian1
和 chian2
可以很方便的串联在一起,方便我们进行扩展。
运行结果
➜ python3 test07.py
message: Chicago, Illinois, se encuentra en los Estados Unidos.
这篇关于LangChain-07 Multiple Chains 多链串联的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!