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博客
相关推荐
珠海西格电力科技1 小时前
微电网控制策略基础:集中式、分布式与混合式控制逻辑
网络·人工智能·分布式·物联网·智慧城市·能源
Java后端的Ai之路2 小时前
【RAG技术】- RAG系统调优手段之高效召回(通俗易懂附案例)
人工智能·rag·rag系统·召回·rag调优
草莓熊Lotso2 小时前
Linux 基础 IO 初步解析:从 C 库函数到系统调用,理解文件操作本质
linux·运维·服务器·c语言·数据库·c++·人工智能
Cx330❀2 小时前
从零实现Shell命令行解释器:原理与实战(附源码)
大数据·linux·数据库·人工智能·科技·elasticsearch·搜索引擎
Niuguangshuo8 小时前
深入解析Stable Diffusion基石——潜在扩散模型(LDMs)
人工智能·计算机视觉·stable diffusion
迈火8 小时前
SD - Latent - Interposer:解锁Stable Diffusion潜在空间的创意工具
人工智能·gpt·计算机视觉·stable diffusion·aigc·语音识别·midjourney
wfeqhfxz25887828 小时前
YOLO13-C3k2-GhostDynamicConv烟雾检测算法实现与优化
人工智能·算法·计算机视觉
芝士爱知识a9 小时前
2026年AI面试软件推荐
人工智能·面试·职场和发展·大模型·ai教育·考公·智蛙面试
Li emily9 小时前
解决港股实时行情数据 API 接入难题
人工智能·python·fastapi
Aaron15889 小时前
基于RFSOC的数字射频存储技术应用分析
c语言·人工智能·驱动开发·算法·fpga开发·硬件工程·信号处理