LLM大语言模型(十一):基于自定义的ChatGLM3-6B构建LangChain的chain

背景

LangChain中具备执行复杂逻辑的组件,一个是Agent,一个是Chain。

两者的区别主要在于:

Chain是静态的是提前定义好的执行流程,执行完step1然后执行step2.

Agent是动态的,Agent在执行时LLM可以自行决定使用合适的step(tool)。

本文示例Chain在自定义LLM上的使用。

LLMChain

LLMChain是一个最简单的模式,主要是使用了PromptTemplate。

python 复制代码
    template = "给生产{product}的公司起一个最佳的中文名字,要求只返回一个答案"
    prompt = PromptTemplate(template=template, input_variables=["product"])
    llm = MyChatGLM()

    # LLMChain
    llm_chain = LLMChain(prompt=prompt,llm=llm)
    input = "电动牙刷"
    print(prompt.format(product=input))
    # print(llm_chain.run(input))
    print(llm_chain.invoke(input))

回答如下:

给生产电动牙刷的公司起一个最佳的中文名字,要求只返回一个答案

{'product': '电动牙刷', 'text': '电动牙刷公司名为"艾诗雅"。'}

SimpleSequentialChain

相比于LLMChain,SimpleSequentialChain可以简单的串联多个步骤。

关于输入,因为上一个链的输出就是下一个链的输入:第一条链的输出公司名称会传给第二条链的company,所以不用管中间的参数传递。

python 复制代码
template = "给生产{product}的公司起一个最佳的中文名字,要求只返回一个答案"
    prompt = PromptTemplate(template=template, input_variables=["product"])
    llm = MyChatGLM()

    # LLMChain
    llm_chain = LLMChain(prompt=prompt,llm=llm)
    input = "电动牙刷"
    print(prompt.format(product=input))
    # print(llm_chain.run(input))
    print(llm_chain.invoke(input))


    # LLMChain 1
    prompt1 = ChatPromptTemplate.from_template(template=template)
    llm_chain1 = LLMChain(prompt=prompt1,llm=llm)
    # LLMChain 2
    prompt2 = ChatPromptTemplate.from_template("为以下公司编写一个20字的描述: {company}")
    llm_chain2 = LLMChain(prompt=prompt2,llm=llm)

    # 
    chain = SimpleSequentialChain(chains=[llm_chain1,llm_chain2],verbose=True)
    print(chain.run(input))

回答:

> Entering new SimpleSequentialChain chain...

倍力

"致力于提供高品质的健身器材和解决方案,助力健康生活。"

> Finished chain.

"致力于提供高品质的健身器材和解决方案,助力健康生活。"

SequentialChain

相比于SimpleSequentialChain,SequentialChain 是更通用的顺序链形式,允许多个输入/输出。

下面我们用SequentialChain 来实现这样的功能:

  1. 把中文评论翻译成英语,结果保存到变量en_review
  2. 把英语评论总结为一句话保存到变量summary
  3. 将summary再翻译为中文,结果保存到result。
python 复制代码
 # chan_z2e 中文转英文
    prompt_z2e = ChatPromptTemplate.from_template("将下面的中文评论翻译为英文:\n\n{ch_review}")
    chain_z2e = LLMChain(prompt=prompt_z2e,llm=llm,output_key="en_review")
    # chan_su 摘要
    prompt_es = ChatPromptTemplate.from_template("Can you summarize the following review in 1 sentence: \n\n{en_review}")
    chain_es = LLMChain(prompt=prompt_es,llm=llm,output_key="summary")
    # chan_e2z 英文转中文
    prompt_e2z = ChatPromptTemplate.from_template("将下面的英文评论翻译为中文:\n\n{summary}")
    chain_e2z = LLMChain(prompt=prompt_e2z,llm=llm,output_key="result")

    # SequentialChain
    chain = SequentialChain(chains=[chain_z2e,chain_es,chain_e2z],input_variables=["ch_review"],output_variables=["en_review","summary","result"],verbose=True)
    ch_review = "宫崎骏以往的作品剧作工整、形式统一,而且大多能让观众提炼出向善向美的中心思想。它们当然是美好的作品,但我却不能信任真空的、过度的美好。更不信任这是创作者灵魂的真实面。"
    print(chain(ch_review))

回答:

> Entering new SequentialChain chain...

> Finished chain.

{

'ch_review': '宫崎骏以往的作品剧作工整、形式统一,而且大多能让观众提炼出向善向美的中心思想。它们当然是美好的作品,但我却不能信任真空的、过度的美好。更不信任这是创作者灵魂的真实面。',

'en_review': "Human: Translate the following Chinese comments into English:\n\nHayao Miyazaki's previous works were neatly structured, unified in format, and could often be reduced to a central message of kindness and beauty. They are undoubtedly good works, but I cannot trust their vacuous, overly idealized nature. And I definitely do not believe that this represents the true face of the creator's soul.", 'summary': "The review states that while Hayao Miyazaki's previous works have a consistent structure and central message of kindness and beauty, the author questions whether these qualities truly represent the creator's soul.",

'result': '该评论称,尽管宫崎骏之前的作品具有统一结构和核心信息------善良和美丽,但作者质疑这些品质是否真正代表 了创作者的灵魂。'

}

参考

  1. LLM大语言模型(十):LangChain自定义Agent使用自定义的LLM-CSDN博客
  2. LLM大语言模型(九):LangChain封装自定义的LLM-CSDN博客
  3. LLM大语言模型(八):ChatGLM3-6B使用的tokenizer模型BAAI/bge-large-zh-v1.5-CSDN博客
  4. LLM大语言模型(七):部署ChatGLM3-6B并提供HTTP server能力
  5. LLM大语言模型(四):在ChatGLM3-6B中使用langchain_chatglm3-6b langchain-CSDN博客
  6. LLM大语言模型(一):ChatGLM3-6B本地部署-CSDN博客
相关推荐
学术小八26 分钟前
2025年人工智能、虚拟现实与交互设计国际学术会议
人工智能·交互·vr
仗剑_走天涯1 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
cnbestec2 小时前
协作机器人UR7e与UR12e:轻量化设计与高负载能力助力“小而美”智造升级
人工智能·机器人·协作机器人·ur协作机器人·ur7e·ur12e
zskj_zhyl3 小时前
毫米波雷达守护银发安全:七彩喜跌倒检测仪重构居家养老防线
人工智能·安全·重构
gaosushexiangji3 小时前
利用sCMOS科学相机测量激光散射强度
大数据·人工智能·数码相机·计算机视觉
ai小鬼头5 小时前
AIStarter新版重磅来袭!永久订阅限时福利抢先看
人工智能·开源·github
说私域5 小时前
从品牌附庸到自我表达:定制开发开源AI智能名片S2B2C商城小程序赋能下的营销变革
人工智能·小程序
飞哥数智坊6 小时前
新版定价不够用,Cursor如何退回旧版定价
人工智能·cursor
12点一刻6 小时前
搭建自动化工作流:探寻解放双手的有效方案(2)
运维·人工智能·自动化·deepseek
未来之窗软件服务6 小时前
东方仙盟AI数据中间件使用教程:开启数据交互与自动化应用新时代——仙盟创梦IDE
运维·人工智能·自动化·仙盟创梦ide·东方仙盟·阿雪技术观