通过langchain的LLMChain和SimpleSequentialChain学习一项新技能

我们在年轻或者年老的时候都会学习不同的新技能,小时候在幼儿园经常要学习如何尿尿的更远,年老的时候要学习如何不把尿尿到裤子里,总之活到老学到老,总归有一项新技能等待着你去学习去探索,当LLM出现之后学习的方法也发生了一定的改变,各种技能精熟的老师遍地都是,如温馨一盐,同意倩文,心肺性火等等,我们又可以利用狼晨把以上的乐乐米玩弄于故障之中,以下是在极客时间学习langchain的一些小例子之一,利用llm+openai学习一项新技能。

代码如下

from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain

skill 链

llm = OpenAI(temperature=1)
template = “”"Your job is to provide a simple learning step for the skill that the user suggests.
% USER SKILL
{user_skill}

YOUR RESPONSE:
“”"
prompt_template = PromptTemplate(input_variables=[“user_skill”], template=template)
skill_chain = LLMChain(llm=llm, prompt=prompt_template)

learning_step 链

template = “”"Given a skill, provide a simple learning step on how to acquire that skill.
% SKILL
{user_learning_step}

YOUR RESPONSE:
“”"
prompt_template = PromptTemplate(input_variables=[“user_learning_step”], template=template)
learning_step_chain = LLMChain(llm=llm, prompt=prompt_template)

通过 SimpleSequentialChain 串联起来,第一个答案会被替换第二个中的user_learning_step,然后再进行询问

overall_chain = SimpleSequentialChain(chains=[skill_chain, learning_step_chain], verbose=True)
review = overall_chain.run(“Python编程”)

代码解释:

首先,我们从langchain.llms中导入OpenAI,从langchain.chains中导入LLMChain和SimpleSequentialChain,从langchain.prompts中导入PromptTemplate。
接着,我们创建一个名为skill_chain的LLMChain实例,它负责根据用户提供的技能生成一个简单的学习步骤。我们定义了一个包含user_skill变量的PromptTemplate,并将其与llm一起传递给LLMChain。
然后,我们创建一个名为learning_step_chain的LLMChain实例,它负责根据给定的技能生成简单的学习步骤。我们定义了一个包含user_learning_step变量的PromptTemplate,并将其与llm一起传递给LLMChain。
最后,我们使用SimpleSequentialChain将skill_chain和learning_step_chain串联起来。当我们运行overall_chain时,它首先调用skill_chain来生成简单的学习步骤,然后将结果传递给learning_step_chain以生成更详细的学习步骤。
这个示例展示了如何使用Langchain的SimpleSequentialChain将两个LLMChain串联起来,实现根据用户提供的技能生成简单的学习步骤的功能。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值