当前位置: 首页 > news >正文

专业管道疏通网站建设图片文山建设5G网站

专业管道疏通网站建设图片,文山建设5G网站,展示产品的网站 个人备案还是企业,推广网页的策划案分类目录#xff1a;《自然语言处理从入门到应用》总目录 语言模型以文本作为输入#xff0c;这段文本通常被称为提示#xff08;Prompt#xff09;。通常情况下#xff0c;这不仅仅是一个硬编码的字符串#xff0c;而是模板、示例和用户输入的组合。LangChain提供了多个…分类目录《自然语言处理从入门到应用》总目录 语言模型以文本作为输入这段文本通常被称为提示Prompt。通常情况下这不仅仅是一个硬编码的字符串而是模板、示例和用户输入的组合。LangChain提供了多个类和函数以便轻松构建和处理提示。 提示模板的概念 提示模板是指一种可复制的生成提示的方式。它包含一个文本字符串模板可以从最终用户处接收一组参数并生成提示。提示模板可能包含以下内容 对语言模型的指令少量示例以帮助语言模型生成更好的回复对语言模型的问题 以下代码片段是包含了一个提示模板的示例 from langchain import PromptTemplatetemplate 我希望你能充当新公司的命名顾问。 一个生产{product}的公司的好名字是什么 prompt PromptTemplate(input_variables[product],templatetemplate, ) prompt.format(product彩色袜子) # - 我希望你能充当新公司的命名顾问。 # - 一个生产彩色袜子的公司的好名字是什么创建提示模板 我们可以使用PromptTemplate类创建简单的硬编码提示。提示模板可以接受任意数量的输入变量并且可以进行格式化以生成提示。 from langchain import PromptTemplate# 没有输入变量的提示示例 no_input_prompt PromptTemplate(input_variables[], template给我讲个笑话。) no_input_prompt.format() # - 给我讲个笑话。# 一个输入变量的提示示例 one_input_prompt PromptTemplate(input_variables[adjective], template给我讲个{adjective}的笑话。) one_input_prompt.format(adjective有趣的) # - 给我讲个有趣的笑话。# 多个输入变量的示例提示 multiple_input_prompt PromptTemplate(input_variables[adjective, content], template给我讲一个{adjective}的关于{content}的笑话。 ) multiple_input_prompt.format(adjective有趣的, content小鸡) # - 给我讲一个有趣的关于小鸡的笑话。如果我们不想手动指定input_variables还可以使用from_template类方法创建PromptTemplate。LangChain将根据传递的template自动推断input_variables。 template 给我讲一个{adjective}的关于{content}的笑话。 prompt_template PromptTemplate.from_template(template) prompt_template.input_variables # - [adjective, content] prompt_template.format(adjective有趣的, content小鸡) # - 给我讲一个有趣的关于小鸡的笑话。我们可以创建自定义的提示模板以任何您想要的方式格式化提示。 模板格式 默认情况下PromptTemplate将提供的模板视为Python f-string。我们可以通过template_format参数指定其他模板格式 # 在运行此代码之前请确保已安装jinja2jinja2_template 告诉我一个{{ adjective }}的笑话关于{{ content }} prompt_template PromptTemplate.from_template(templatejinja2_template, template_formatjinja2)prompt_template.format(adjective有趣的, content鸡) # - 告诉我一个有趣的笑话关于鸡目前PromptTemplate仅支持jinja2和f-string模板格式。如果您希望使用其他模板格式我们可以在GitHub页面上提交需求。 验证模板 默认情况下PromptTemplate将通过检查input_variables是否与模板中定义的变量匹配来验证template字符串。您可以通过将validate_template设置为False来禁用此行为。 template 我正在学习LangChain因为{reason}。prompt_template PromptTemplate(templatetemplate, input_variables[reason, foo]) # 由于存在额外的变量而引发ValueError prompt_template PromptTemplate(templatetemplate, input_variables[reason, foo], validate_templateFalse) # 不会引发错误序列化模板 我们可以将PromptTemplate保存到本地文件系统中的文件中。LangChain将通过文件扩展名自动推断文件格式。目前LangChain支持将模板保存为YAML和JSON文件。 prompt_template.save(awesome_prompt.json) # 保存为JSON文件 from langchain.prompts import load_prompt loaded_prompt load_prompt(awesome_prompt.json)assert prompt_template loaded_promptLangChain还支持从LangChainHub加载提示模板其中包含一系列我们可以在项目中使用的有用提示。 from langchain.prompts import load_promptprompt load_prompt(lc://prompts/conversation/prompt.json) prompt.format(history, input1 1等于多少)向模板添加Few Shot示例 Few Shot示例是一组可以帮助语言模型生成更好响应的示例。要使用Few Shot示例生成提示可以使用FewShotPromptTemplate。此类接受PromptTemplate和Few Shot示例列表。然后它使用Few Shot示例格式化提示模板。 在下面示例中我们将创建一个生成单词反义词的提示。 from langchain import PromptTemplate, FewShotPromptTemplate# 首先创建Few Shot示例列表 examples [{word: happy, antonym: sad},{word: tall, antonym: short}, ]# 接下来我们指定用于格式化示例的模板。 # 我们使用PromptTemplate类来实现这个目的。 example_formatter_template Word: {word} Antonym: {antonym} example_prompt PromptTemplate(input_variables[word, antonym],templateexample_formatter_template, )# 最后创建FewShotPromptTemplate对象。 few_shot_prompt FewShotPromptTemplate(# 这些是我们要插入到提示中的示例。examplesexamples,# 这是我们在将示例插入到提示中时要使用的格式。example_promptexample_prompt,# 前缀是出现在提示中示例之前的一些文本。# 通常这包括一些说明。prefixGive the antonym of every input\n,# 后缀是出现在提示中示例之后的一些文本。# 通常这是用户输入的地方。suffixWord: {input}\nAntonym: ,# 输入变量是整个提示期望的变量。input_variables[input],# 示例分隔符是我们将前缀、示例和后缀连接在一起的字符串。example_separator\n, )# 现在我们可以使用format方法生成一个提示。 print(few_shot_prompt.format(inputbig)) # - Give the antonym of every input # - # - Word: happy # - Antonym: sad # - # - Word: tall # - Antonym: short # - # - Word: big # - Antonym:选择用于提示模板的示例 如果您有大量示例可以使用ExampleSelector选择一部分对语言模型来说最具信息量的示例。这将帮助您生成更有可能获得良好回应的提示。下面我们将使用LengthBasedExampleSelector它根据输入的长度选择示例。当我们担心构建的提示会超过上下文窗口的长度时这将非常有用。对于较长的输入它会选择较少的示例进行包含而对于较短的输入它会选择更多的示例。我们将继续使用前面部分的示例但这次我们将使用LengthBasedExampleSelector来选择示例。 from langchain.prompts.example_selector import LengthBasedExampleSelector# 这里是一些虚构任务的大量示例该任务是创建反义词。 examples [{word: happy, antonym: sad},{word: tall, antonym: short},{word: energetic, antonym: lethargic},{word: sunny, antonym: gloomy},{word: windy, antonym: calm}, ]# 我们将使用LengthBasedExampleSelector来选择示例。 example_selector LengthBasedExampleSelector(# 这些是可供选择的示例。examplesexamples, # 这是用于格式化示例的PromptTemplate。example_promptexample_prompt, # 这是格式化示例的最大长度。max_length25# 这是用于获取字符串长度的函数用于确定包含哪些示例。在这里被注释掉因为如果没有指定默认提供了该函数。# get_text_length: Callable[[str], int] lambda x: len(re.split(\n| , x)) )# 现在我们可以使用example_selector创建FewShotPromptTemplate。 dynamic_prompt FewShotPromptTemplate(# 我们提供一个ExampleSelector而不是示例。example_selectorexample_selector,example_promptexample_prompt,prefix给出每个输入的反义词,suffix单词{input}\n反义词,input_variables[input],example_separator\n\n, )# 现在我们可以使用format方法生成提示。 print(dynamic_prompt.format(inputbig)) # - 给出每个输入的反义词 # - # - 单词happy # - 反义词sad # - # - 单词tall # - 反义词short # - # - 单词energetic # - 反义词lethargic # - # - 单词sunny # - 反义词gloomy # - # - 单词windy # - 反义词平静 python print(dynamic_prompt.format(inputbig)) # - 给出每个输入的反义词 # - # - 单词happy # - 反义词sad # - # - 单词tall # - 反义词short # - # - 单词energetic # - 反义词lethargic # - # - 单词sunny # - 反义词gloomy # - # - 单词windy # - 反义词calm # - # - 单词big # - 反义词平静 相反如果我们提供一个非常长的输入LengthBasedExampleSelector会选择较少的示例包含在提示中。long_string big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else print(dynamic_prompt.format(inputlong_string)) # - 给出每个输入的反义词 # - # - 单词happy # - 反义词sad # - # - 单词big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else # - 反义词平静LangChain附带了一些示例选择器供我们使用有关如何使用示例选择器可以参考《自然语言处理从入门到应用——LangChain提示Prompts》系列的后续文章。除此之外我们还可以创建自定义示例选择器根据我们想要的任何条件选择示例。 参考文献 [1] LangChain ️ 中文网跟着LangChain一起学LLM/GPT开发https://www.langchain.com.cn/ [2] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架http://www.cnlangchain.com/
http://www.pierceye.com/news/767353/

相关文章:

  • 建设一个网站要多少费用吗wordpress 缓存首页
  • 绵阳网站排名深圳哪家网页设计好
  • 软件 开发公司宿迁seo优化
  • 网站开发demo版本做网站服务器的配置
  • 网页游戏排行2013伊克昭盟seo
  • 单页站如何做网站seo优化建e网卧室设计效果图
  • 免费做网站的app巩义seo
  • 做金融服务网站赚钱阿里巴巴网站建设论文
  • 四川做网站的公司哪家好免费团购网站模板
  • 网站建设动漫网站模板怎么做的
  • 西安网站制作公司官网wordpress证书关闭
  • 北网站建设优化seo是什么意思
  • 中国seo网站长沙城乡建设网站
  • 没有相应营业执照怎么做网站重庆网站设计公司排名
  • 企业网站手机版商城网站有什么好处
  • 推荐一本学做网站的书温州阀门网站建设
  • 用户要承担暖气费的税吗太原优化排名推广
  • Wordpress外贸网站搭建公司宿迁建设网站
  • 学校网站建设的意义和应用山东淄博网站建设
  • 莱芜网站建设价格低网站vps
  • 长治做网站哪家好赣州注册公司
  • 网站开发从入门到精通做h5的网站哪个好
  • 免费公司网站如何建立设计个人网站好备案吗
  • 建网站和做微信哪个好在线识别图片百度识图
  • php网站开发如何实现删除功能大连大连建设工程信息网站
  • 表格模板免费下载网站wordpress 插件位置
  • wordpress小白能学会吗汕头做网站优化公司
  • 军队营房基础建设网站重庆做网站个人
  • 网站建设怎样中英文网站备案是空间备案还是域名备案
  • 陕西网站制作人力资源服务外包