Langchain prompt 切换

使用场景

比如在客服机器人的场景下,如果我们需要用不同的prompt来服务不同的用户提问时就需要用到MultiRouteChain。它会根据用户的输入选择一个合适的目标链,使用选中的目标链来回答用户的输入。

MultiRouteChain

class MultiRouteChain(Chain):
    """Use a single chain to route an input to one of multiple candidate chains."""
    
    router_chain: RouterChain
    """Chain that routes inputs to destination chains."""
    
    destination_chains: Mapping[str, Chain]
    """Chains that return final answer to inputs."""
    
    default_chain: Chain
    """Default chain to use when none of the destination chains are suitable."""

它的主要属性有三个

  • router_chain: 此链会根据用户输入选择目标链
  • destination_chains: 它是一个Mapping, 用于存放可供选择的目标链
  • default_chain: 当目标链中没有合适的链时,会默认使用这个链执行用户的输入

RouterChain

class RouterChain(Chain, ABC):
    """Chain that outputs the name of a destination chain and the inputs to it."""

Langchain提供了以下两种RouteChain子类

  • EmbeddingRouterChain
  • LLMRouterChain

1.LLMRouterChain

它使用一个LLMChain来判断使用哪个目标链合适,它的 Prompt 如下:

MULTI_PROMPT_ROUTER_TEMPLATE = """\
Given a raw text input to a language model select the model prompt best suited for \
the input. You will be given the names of the available prompts and a description of \
what the prompt is best suited for. You may also revise the original input if you \
think that revising it will ultimately lead to a better response from the language \
model.

<< FORMATTING >>
Return a markdown code snippet with a JSON object formatted to look like:
 ```json
 {{{{
    "destination": string \\ name of the prompt to use or "DEFAULT"
    "next_inputs": string \\ a potentially modified version of the original input
 }}}}
 ```

REMEMBER: "destination" MUST be one of the candidate prompt names specified below OR \
it can be "DEFAULT" if the input is not well suited for any of the candidate prompts.
REMEMBER: "next_inputs" can just be the original input if you don't think any \
modifications are needed.

<< CANDIDATE PROMPTS >>
{destinations}

<< INPUT >>
{{input}}

<< OUTPUT (must include ```json at the start of the response) >>
"""

此 Prompt 告诉 GPT 选择合适 prompt 并以 json 格式输出目标链的名称和后续链的输入,而且允许其修改输入以便获得更好的回答。

2.EmbeddingRouterChain

EmbeddingRouterChain 是通过向量匹配的方式选择合适的目标链。

例子

from langchain.chains.router import MultiPromptChain
from langchain import LLMChain, PromptTemplate
from langchain.chat_models import ChatOpenAI
import os

os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY
os.environ['OPENAI_API_BASE'] = OPENAI_API_BASE

physicsTemplate = """You are a very smart physics professor. \
You are great at answering questions about physics in a concise and easy to understand manner. \
When you don't know the answer to a question you admit that you don't know.

Here is a question:
{input}"""


mathTemplate = """You are a very good mathematician. You are great at answering math questions. \
You are so good because you are able to break down hard problems into their component parts, \
answer the component parts, and then put them together to answer the broader question.

Here is a question:
{input}"""

promptInfos = [
    {
        "name": "physics",
        "description": "Good for answering questions about physics",
        "prompt_template": physicsTemplate,
    },
    {
        "name": "math",
        "description": "Good for answering math questions",
        "prompt_template": mathTemplate,
    },
]

llm = ChatOpenAI()

defaultChain = LLMChain(
    llm=llm,
    prompt=PromptTemplate.from_template("{input}"))

multiChain = MultiPromptChain.from_prompts(
    llm=llm,
    prompt_infos=prompt_infos,
    default_chain=defaultChain)

result = multiChain.run('速度与加速度的关系')

print(result)

RouterChain 发出的 prompt:

Human: Given a raw text input to a language model select the model prompt best suited for the input. You will be given the names of the available prompts and a description of what the prompt is best suited for. You may also revise the original input if you think that revising it will ultimately lead to a better response from the language model.

<< FORMATTING >>
Return a markdown code snippet with a JSON object formatted to look like:
 ```json
 {
 \"destination\": string \\ name of the prompt to use or \"DEFAULT\"
 \"next_inputs\": string \\ a potentially modified version of the original input
 }
 ```

REMEMBER: \"destination\" MUST be one of the candidate prompt names specified below OR it can be \"DEFAULT\" if the input is not well suited for any of the candidate prompts.
REMEMBER: \"next_inputs\" can just be the original input if you don't think any modifications are needed.

<< CANDIDATE PROMPTS >>
physics: Good for answering questions about physics
math: Good for answering math questions

<< INPUT >>
速度与加速度的关系

<< OUTPUT (must include ```json at the start of the response) >>

RouterChain 结果:

 ```json
 {
    \"destination\": \"physics\",
    \"next_inputs\": \"速度与加速度的关系\"
 }
 ```

目标链physics发出的 prompt:

Human: You are a very smart physics professor. You are great at answering questions about physics in a concise and easy to understand manner. When you don't know the answer to a question you admit that you don't know.

Here is a question:
速度与加速度的关系

目标链physics的结果:

速度与加速度的关系可以通过牛顿第二定律来描述。牛顿第二定律指出,物体的加速度与作用在其上的净力成正比,与物体的质量成反比。

具体来说,如果一个物体的净力为F,质量为m,则物体的加速度a可以通过以下公式计算:a = F / m。

从这个公式可以看出,当一个物体受到较大的净力时,它的加速度也会较大;而当物体的质量较大时,它的加速度会较小。

速度与加速度之间的关系可以通过积分得到。如果一个物体在某一时刻的速度为v0,加速度为a,并且在一段时间t内保持恒定的加速度,那么它在这段时间内的速度v可以通过以下公式计算:v = v0 + at。

这个公式表明,速度的变化取决于初始速度、加速度以及经过的时间。如果加速度是正值,速度将增加;如果加速度是负值,速度将减小。

总结起来,速度与加速度的关系可以通过牛顿第二定律和速度的变化公式来描述。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值