LangChain Chain链的串行以及SimpleSequentialChain的使用

文章介绍了如何使用ChatGLM库中的不同链式结构,如SimpleSequentialChain和LLMChain,来创建对话式故事生成和文本计数功能,通过模板和解析器处理用户输入并获取相应输出。
摘要由CSDN通过智能技术生成

以下将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"))



  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值