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博客
相关推荐
学历真的很重要2 小时前
VsCode+Roo Code+Gemini 2.5 Pro+Gemini Balance AI辅助编程环境搭建(理论上通过多个Api Key负载均衡达到无限免费Gemini 2.5 Pro)
前端·人工智能·vscode·后端·语言模型·负载均衡·ai编程
普通网友2 小时前
微服务注册中心与负载均衡实战精要,微软 2025 年 8 月更新:对固态硬盘与电脑功能有哪些潜在的影响。
人工智能·ai智能体·技术问答
苍何2 小时前
一人手搓!AI 漫剧从0到1详细教程
人工智能
苍何2 小时前
Gemini 3 刚刷屏,蚂蚁灵光又整活:一句话生成「闪游戏」
人工智能
leo03082 小时前
【LLM微调】拒绝“假装聪明”:SFTTrainer 中 completion_only_loss 新旧版本用法详解
llm·sft·huggingface·trl
ariesjzj2 小时前
DeepSeek时代的Large-scale LLM推理
大模型·llm·deepseek·推理优化·大规模ep
苍何2 小时前
越来越对 AI 做的 PPT 敬佩了!(附7大用法)
人工智能
苍何2 小时前
超全Nano Banana Pro 提示词案例库来啦,小白也能轻松上手
人工智能
t梧桐树t3 小时前
简单实现一个LangChain Agent
langchain
阿杰学AI3 小时前
AI核心知识39——大语言模型之World Model(简洁且通俗易懂版)
人工智能·ai·语言模型·aigc·世界模型·world model·sara