首页 > 其他分享 >langchain基础(3)-chain

langchain基础(3)-chain

时间:2024-09-08 20:53:52浏览次数:6  
标签:prompt chain 基础 langchain llm import chains

1.LLMChain:一个链

from langchain.chains.llm import LLMChain

from langchain_community.llms.openai import OpenAI
from langchain.prompts.prompt import PromptTemplate

llm = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")
promptTemplate = PromptTemplate.from_template("你是起名大师,我家是{sex}宝,姓{firstName},请起3个好养的名字?")


# 多个参数
chain = LLMChain(
    llm=llm,
    prompt= promptTemplate,
    verbose=True,#打开日志
)
ret = chain.invoke({'sex': "男",'firstName': "王"})

print(ret)2.

2.SimpleSequentialChain:将一个链的输出作为下一个链的输入

from langchain.chains.llm import LLMChain
from langchain_community.llms import Tongyi

llm = Tongyi()

from langchain.prompts import ChatPromptTemplate
from langchain.chains.sequential import SimpleSequentialChain

# chain 1
first_prompt = ChatPromptTemplate.from_template("帮我给{product}的公司起一个响亮容易记忆的名字?")

chain_one = LLMChain(
    llm=llm,
    prompt=first_prompt,
    verbose=True,
)

# chain 2
second_prompt = ChatPromptTemplate.from_template("用5个词来描述一下这个公司名字:{company_name}")

chain_two = LLMChain(
    llm=llm,
    prompt=second_prompt,
    verbose=True,
)

# 实例化
simple_chain = SimpleSequentialChain(
    chains=[chain_one, chain_two],
    verbose=True,  # 打开日志

)

simple_chain.invoke("卖手机")

3.SequentialChain:允许我们定义多个链,并将它们链接在一起

from langchain.chains.llm import LLMChain
from langchain_community.llms import Tongyi

llm = Tongyi()

from langchain.prompts import ChatPromptTemplate
from langchain.chains.sequential import SequentialChain

#chain 1 任务:翻译成中文
first_prompt = ChatPromptTemplate.from_template("把下面内容翻译成中文:\n\n{content}")
chain_one = LLMChain(
    llm=llm,
    prompt=first_prompt,
    verbose=True,
    output_key="Chinese_Rview",
)

#chain 2 任务:对翻译后的中文进行总结摘要 input_key是上一个chain的output_key
second_prompt = ChatPromptTemplate.from_template("用一句话总结下面内容:\n\n{Chinese_Rview}")
chain_two = LLMChain(
    llm=llm,
    prompt=second_prompt,
    # verbose=True,
    output_key="Chinese_Summary",
)

#chain 3 任务:智能识别语言 input_key是上一个chain的output_key
third_prompt = ChatPromptTemplate.from_template("根据下面内容写5条评价信息:\n\n{Chinese_Summary}")
chain_three = LLMChain(
    llm=llm,
    prompt=third_prompt,
    # verbose=True,
    output_key="Language",
)

#chain 4 任务:针对摘要使用指定语言进行评论 input_key是上一个chain的output_key
fourth_prompt = ChatPromptTemplate.from_template("请使用指定的语言对以下内容进行回复:\n\n内容:{Chinese_Summary}\n\n语言:{Language}")
chain_four = LLMChain(
    llm=llm,
    prompt=fourth_prompt,
    verbose=True,
    output_key="Reply",
)

#overall 任务:翻译成中文->对翻译后的中文进行总结摘要->智能识别语言->针对摘要使用指定语言进行评论
overall_chain = SequentialChain(
    chains=[chain_one, chain_two, chain_three, chain_four],
    verbose=True,
    input_variables=["content"],
    output_variables=["Chinese_Rview", "Chinese_Summary", "Language"],
)

content = "I am a student of Cumulus Education, my course is artificial intelligence, I like this course, because I can get a high salary after graduation"
ret = overall_chain.invoke(content)
print(ret)

4. RouterChain:一个LLM和一个ConversationPromptTemplate组合在一起

from langchain.chains.llm import LLMChain
from langchain_community.llms import Tongyi

llm = Tongyi()
from langchain.prompts import PromptTemplate
from langchain.chains.conversation.base import ConversationChain
from langchain.chains.router.llm_router import LLMRouterChain,RouterOutputParser
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
from langchain.chains.router import MultiPromptChain

#物理链
physics_template = """您是一位非常聪明的物理教授.\n
您擅长以简洁易懂的方式回答物理问题.\n
当您不知道问题答案的时候,您会坦率承认不知道.\n
下面是一个问题:
{input}"""
physics_prompt = PromptTemplate.from_template(physics_template)

# 物理的任务
physicschain = LLMChain( llm=llm,prompt=physics_prompt)

#数学链
math_template = """您是一位非常优秀的数学教授.\n
您擅长回答数学问题.\n
您之所以如此优秀,是因为您能够将困难问题分解成组成的部分,回答这些部分,然后将它们组合起来,回答更广泛的问题.\n
下面是一个问题:
{input}"""

math_prompt = PromptTemplate.from_template(math_template)

###数学的任务
mathschain = LLMChain(llm=llm, prompt=math_prompt,)

# 默认任务
default_chain = ConversationChain(
    llm = llm,
    output_key="text"
)

# 组合路由任务
destination_chains = {}
destination_chains["physics"] = physicschain
destination_chains["math"] = mathschain

# 定义路由Chain
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations="physics:擅长回答物理问题\n math:擅长回答数学问题")
router_prompt = PromptTemplate(
    template=router_template,
    input_variables=["input"],
    output_parser=RouterOutputParser()
)
router_chain = LLMRouterChain.from_llm(
    llm,
    router_prompt
)
chain = MultiPromptChain(
    router_chain=router_chain,
    destination_chains=destination_chains,
    default_chain=default_chain,
    verbose=True
)
# question = "什么是牛顿第一定律?"
# print(router_chain.invoke(question))
# chain.run(question)

question = "2+2等于几?"
print(router_chain.invoke(question))
print(chain.run("2+2等于几?"))

标签:prompt,chain,基础,langchain,llm,import,chains
From: https://blog.csdn.net/Mooczx/article/details/141963768

相关文章

  • Java教程:入门基础【十万字详解】
    ✨博客主页:https://blog.csdn.net/m0_63815035?type=blog......
  • JAVA基础语法(一)
    1.功能的最小单元(1)功能的最小单位是一个一个方法。2.注释(1)单行注释格式://注释文字(2)多行注释格式:/*注释文字*/(3)被注释的文字不会被JVM解释执行,多行注释不能嵌套(4)由于编码问题导致编译失败(设置环境变量JAVA_TOOL_OPTLONS-Dfile.encoding=UTF-8)eg:3.字面量(1)e......
  • MySQL面试笔试题(基础题)
     1、取得每个部门最高薪水的人员的名称selectenamefromempe,(selectdeptno,max(sal)max_salfromempgroupbydeptno)each_dept_max_salwheree.deptno=each_dept_max_sal.deptnoande.sal=each_dept_max_sal.max_sal;2、哪些人的薪水在部门的平均薪水之上select......
  • 《动手学深度学习》笔记4——线性回归 + 基础优化算法
    李沐老师:线性回归是机器学习最基础的一个模型,也是我们理解之后所有深度学习模型的基础,所以我们从线性回归开始1.线性回归由于是案例引入,没有很难的知识点,咱直接贴上李沐老师的PPT:1.1线性模型--单层神经网络李沐老师:神经网络起源于神经科学,但现在深度学习的发展......
  • ComfyUI 基础教程(四) —— 应用 LoRA 模型控制图像生成特征
    前言上一篇文章讲述了ControlNet模型的使用,如果掌握了ControlNet的使用之后,本文所讲的Lora模型将会非常容易理解。一、LoRA模型的概念之前的文章中,在介绍模型种类时,简单介绍过Lora模型,LoRA模型全称是Low-Rank-AdaptaionofLargeLanguageModels。是一种用于微调......
  • 零基础快速上手HarmonyOS ArkTS开发5---从简单的页面开始2---使用List组件构建列表、G
    接着https://www.cnblogs.com/webor2006/p/18048248继续往下学习页面布局的知识。最近发现之前学习这一章节的内容在官方已经被下了,替换成了另外一个案例了(https://developer.huawei.com/consumer/cn/training/course/slightMooc/C101717497398588123):而且整个视频的风格也不一样......
  • C++入门基础(内容太干,噎住了)
     文章目录1.缺省参数2.函数重载2.1重载条件:1.参数类型不同2.参数个数不同3.参数类型顺序不同 2.2不做重载条件情况:1.返回值相同时2.当全缺省遇见无参数3.引用3.1引用特性:3.2引用的使用1.缺省参数1.缺省参数是声明或定义函数时为函数的参数指定⼀个缺省值。......
  • [Java基础]IO的同步和阻塞
    同步与异步什么是同步与异步呢?百度百科是这样定义的:同步指两个或两个以上随时间变化的量在变化过程中保持一定的相对关系。异步与同步相对(这解释让我无言相对)所以,我们需要明确的是同步与异步针对的是两个或者两个以上的事物。对于同步而言,一个任务(调用者)的完成需要依赖另一个......
  • redis基础——SpringDataRedis入门
    redis基础——SpringDataRedis入门最近在学习redis,学到了redis的java客户端,其中最常用的是Jedis和lettuce,而SpringDataRedis是spring整合了Jedis和lettuce的产物,它提供了RedisTemplate工具类,封装各种对redis的操作,将不同数据类型的API封装到了不同类型中,避免了代码臃肿。S......
  • MySQL基础
    存储引擎体系结构存储引擎增删改查,索引的实现方式基于表的,不是基于库的存储引擎文件innodbxxx.idbmyisamxxx.sdi(表结构),xxx.MYD(数据),xxx.MYI(索引)memoryxxx.sdi索引分类按字段特性分按物理存储结构分回表查询SQL性能分析查询操作频次......