网站建设 协议书 doc网站优化网站
网站建设 协议书 doc,网站优化网站,厦门企业网站建设专家,phpcms建设网站原文地址#xff1a;【LangChain系列 4】Model I/O——Prompts概述
本文速读#xff1a; Prompt模版 样本选择器 Prompts简单来讲就是一组指令或文本输入#xff0c;语言模型理解它的意思后#xff0c;给出一个输出响应。
LangChain提供了一些模块可以让我们更方便地使…原文地址【LangChain系列 4】Model I/O——Prompts概述
本文速读 Prompt模版 样本选择器 Prompts简单来讲就是一组指令或文本输入语言模型理解它的意思后给出一个输出响应。
LangChain提供了一些模块可以让我们更方便地使用prompts Prompt templates参数化模版输入 Example selectors动态选择prompt示例 01 Prompt模版 Prompt templates可以理解为预先定义好的参数化的prompt可以动态传入不同的参数值而成为prompt它是一个模版可以包含命令、少样本示例、具体上下文和特定的问题。
LangChain封装好了很多相关的工具可以让我们方便地使用prompt。
PromptTemplate示例
from langchain import PromptTemplateprompt_template PromptTemplate.from_template(Tell me a {adjective} joke about {content}.
)
prompt_template.format(adjectivefunny, contentchickens)prompt_template格式化后为
Tell me a funny joke about chickens.
另外我们还可以明确地指定参数名字比如
invalid_prompt PromptTemplate(input_variables[adjective, content],templateTell me a {adjective} joke about {content}.
)
这样的好处就是会去验证参数是否正确可以减少犯错以上两种方式都可以你可以根据个人喜好选择。
ChatPromptTemplate示例 from langchain.prompts import ChatPromptTemplatetemplate ChatPromptTemplate.from_messages([(system, You are a helpful AI bot. Your name is {name}.),(human, Hello, how are you doing?),(ai, Im doing well, thanks!),(human, {user_input}),
])messages template.format_messages(nameBob,user_inputWhat is your name?
)
ChatPromptTemplate可以接收一系列message每个message是一个二元组包括角色和内容两个部分。
除了二元组的写法另一种写法就是BaseMessage或MessagePromptTemplate。
from langchain.prompts import ChatPromptTemplate
from langchain.prompts.chat import SystemMessage, HumanMessagePromptTemplate
from langchain.chat_models import ChatOpenAItemplate ChatPromptTemplate.from_messages([SystemMessage(content(You are a helpful assistant that re-writes the users text to sound more upbeat.)),HumanMessagePromptTemplate.from_template({text}),]
)llm ChatOpenAI(openai_api_keyxxx)
rsp llm(template.format_messages(texti dont like eating tasty things.))
print(rsp)
运行代码输出结果
contentI absolutely adore indulging in delicious treats! additional_kwargs{} exampleFalse
以上是prompt模版的简单使用示例LangChain还封装了很多不同类别模版与功能 连接特征库 自定义propmt模版 少样本prompt模版 输出模版 MessagePromptTemplate 部分prompt模版 prompt组合 prompt序列化 prompt模版校验 02 样本选择器 样本选择器(Example Selectors)是什么呢假如你有大量样本但如果将样本都放到prompt的话就会超过prompt的token限制那么你就需要选择最适合的样本放到prompt中而样本选择器就是为了解决这个问题的样本选择器的作用就是从大量样本中选择最适合的样本。
样本选择器 的基类如下
class BaseExampleSelector(ABC):Interface for selecting examples to include in prompts.abstractmethoddef select_examples(self, input_variables: Dict[str, str]) - List[dict]:Select which examples to use based on the inputs.
当我们需要自定义一个样本选择器时唯一需要做的就是实现select_examples方法该方法接收input_variables参数返回一个样本列表。同时LangChain也提供了一些 样本选择器 可以直接使用。 本文小结
本文介绍了Model I/O 中的输入prompts主要包括 prompt模版 和 样本选择器 两个模块让我们对这两个模块的功能和使用有了一个基本的概念后续文章将详细介绍它们的用法与实践。 更多最新文章请关注公众号大白爱爬山
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/88235.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!