一个人完成网站建设,安徽省高路建设有限公司网站,北京网站排行榜,网站开发的基本技术路线前些天发现了一个巨牛的人工智能学习网站#xff0c;通俗易懂#xff0c;风趣幽默#xff0c;忍不住分享一下给大家。点击跳转到网站。
让 LLM 自动选择不同的 Prompt
在上一篇文章中#xff0c;我们学会了如何让 langchain 来自动选择不同的 LLM Chain#xff0c;以便回…前些天发现了一个巨牛的人工智能学习网站通俗易懂风趣幽默忍不住分享一下给大家。点击跳转到网站。
让 LLM 自动选择不同的 Prompt
在上一篇文章中我们学会了如何让 langchain 来自动选择不同的 LLM Chain以便回答不同的问题只需要使用 RouterChain 和 MultiPromptChain 就可以实现这一功能。
但 MultiPromptChain 被设计出来并不只是为了实现不同 LLM Chain 的选择我们还能用它来实现让 LLM 选择不同的 Prompt原理跟 RouterChain 差不多只不过选择的是 Prompt 而不是 LLM Chain。 也就是说其实另外一种场景是使用相同的大语言模型只是让它选择不同的 Prompt 来回答问题。
例子
下面是一个例子我们使用 MultiPromptChain 来让 LLM 自动选择不同的 Prompt 来回答问题
当我们问关于 Python 编程的问题时LLM 会选择 Python 的 Prompt 来回答。当我们问关于 Golang 编程的问题时LLM 会选择 Golang 的 Prompt 来回答。
from langchain.chains.router import MultiPromptChain
from langchain_openai import ChatOpenAIpy_template
你是一名 Python 工程师擅长解答关于 Python 编程的问题。
下面是需要你来回答的问题
{input}
go_template
你是一名 Golang 工程师擅长解答关于 Golang 编程的问题。
下面是需要你来回答的问题
{input}
prompt_infos [{name: python,description: 适合回答关于 Python 编程的问题,prompt_template: py_template,},{name: golang,description: 适合回答关于 Golang 编程的问题,prompt_template: go_template,}
]chain MultiPromptChain.from_prompts(llmChatOpenAI(modelgpt-3.5-turbo, temperature0),prompt_infosprompt_infos,verboseTrue
)print(chain.invoke({input: 如何在 Python 中定义一个函数}))原理
既然涉及到自动选择不同的 Prompt 的操作其实底层还是使用了 RouterChain如果我们去看 from_prompts 代码发现跟前一篇文章使用的是相同的 Prompt 也就是 MULTI_PROMPT_ROUTER_TEMPLATE。
构建一个 router_prompt使用 MULTI_PROMPT_ROUTER_TEMPLATE 模板将所有 Prompt 的信息传入。使用 RouterChain 构建一个 RouterChain并将 router_prompt 传入。构建 destination_chains这一步会为不同的 Prompt 创建一个 LLMChain。创建一个 default_chain这个链会在没有匹配到任何 Prompt 时触发。创建一个 MultiPromptChain 实例将 RouterChain 和 default_chain 传入。
实际调用 chain.invoke 的时候会经历如下过程
将 RouterChain 的 Prompt格式化之后的带有我们的 Prompt 简易描述传递给 LLM让 LLM 选择一个 LLMChain 来处理。LLM 会根据输入的 Prompt 选择一个 LLMChain然后调用这个 LLMChain 对应某个具体的 Prompt也就是上面 prompt_infos 中的一个来处理输入。如果没有匹配到任何 Prompt则会调用 default_chain 来处理输入。再次调用 LLM让 LLM 回答用户的问题最终我们会得到一个回答。
自动选择 Prompt 的 Prompt
我们可以在 LangSmith 中看到实际发送给 LLM 选择 Prompt 的 Prompt 是怎样的
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 DEFAULTnext_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 dont think any modifications are needed. CANDIDATE PROMPTS
python: 适合回答关于 Python 编程的问题
golang: 适合回答关于 Golang 编程的问题 INPUT
如何在 Python 中定义一个函数 OUTPUT (must include json at the start of the response) OUTPUT (must end with ) 说明
先是一个简单的引导语句告诉模型你将给它一个输入它需要根据这个输入选择最适合的模型。指定输出的格式告诉模型输出应该是一个 JSON 对象。一些关于输出的额外说明比如如果没有匹配到任何 Prompt则应该返回 DEFAULT。接着是所有可选的 Prompt以及它们的描述。最后是用户输入的问题。
LLM 在拿到这个 Prompt 之后会进行分析推理然后选择一个最适合的 Prompt然后返回给我们。 当然拿到选择的具体的 Prompt 之后并不是拿到了最终的答案接着使用选中的 Prompt 以及用户的问题再次调用 LLM最终得到一个回答。
总结
MultiPromptChain 是对 RouterChain 的一个扩展它可以让 LLM 选择不同的 Prompt 来回答问题这样我们可以更灵活地使用不同的 Prompt 来回答问题。 而 RouterChain 是可以自动选择不同的大模型来回答问题。也就是说
如果我们只是想让 LLM 选择不同的 Prompt 来回答问题可以使用 MultiPromptChain。如果我们想让 LLM 选择不同的大模型来回答问题可以使用 RouterChain 结合 MultiPromptChain 来实现。