langchain基础(3)-chain

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等于几?"))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值