以下将chain_one 的结果输入到chain_two .
import ChatGLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import SimpleSequentialChain
llm = ChatGLM.ChatGLM_LLM()
prompt1 = ChatPromptTemplate.from_template(
"tell me a story about {what},no more than 100 words"
)
chain_one = prompt1 | llm | StrOutputParser()
prompt2 = ChatPromptTemplate.from_template("what is this story talk about {expression},no more than 10 words")
chain_two = (
{"expression": chain_one}
| prompt2
| llm
| StrOutputParser()
)
print(chain_two.invoke({"what": "dog"}))
也可以使用 SimpleSequentialChain:
import ChatGLM
from langchain.chains import LLMChain
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains import SimpleSequentialChain
from operator import itemgetter
from langchain.chains import ConversationChain
llm = ChatGLM.ChatGLM_LLM()
prompt = ChatPromptTemplate.from_template("""
give the count of the words in the sentence: {input} .
current conversation:{history}
"""
)
prompt1= ChatPromptTemplate.from_template("""
give me answer about {input}, no more than 50 words
"""
)
chain_one = LLMChain(
llm=llm,
prompt=prompt1,
verbose=True
)
conversation = ConversationChain(llm=llm, prompt=prompt, verbose=True)
chain_3 = SimpleSequentialChain(chains = [chain_one,conversation])
print(chain_3.invoke("who is Obama"))