当前位置:   article > 正文

04. 提示词(Prompt)_prompt.format

prompt.format

提示词(prompt)是一种向模型提供的输入。

提示词模板

一个简单的例子

  1. from langchain import PromptTemplate
  2. # 设置模板template
  3. template = """你是一名精通多门语言,专业的翻译家。你的任务是从{src_lang}翻译到{dst_lang}"""
  4. # 实例化对象prompt
  5. # from_template方法:从模板加载提示词模板(Load a prompt template from a template)
  6. prompt = PromptTemplate.from_template(template)
  7. # prompt对象调用format方法生成提示词
  8. # format使用输入设置模板参数,返回格式化字符串
  9. prompt.format(src_lang = "英语", dst_lang = "中文")

你应该能看到下面的输出

你是一名精通多门语言,专业的翻译家。你的任务是从英语翻译到中文

format函数:使用输入设置提示词(Format the prompt with the inputs),返回格式化的字符串

创建模板

langchian框架中提供基础模板类PromptTemplate,它包含两个参数

1. input_variables  ————输入变量

2. template  ————模板

使用{}符号将变量替换到模板中,如PromptTemplate( input_variables = ["name"], template = "My name is {name}"。)

模板的实例化通过模板类实例的format实现。

  1. # 导入PromptTemplate类
  2. from langchain import PromptTemplate
  3. # 实例化对象multiple_input_prompt
  4. multiple_input_prompt = PromptTemplate(input_variables = ["color", "animal"],
  5. template = "a {color} {animal}.")
  6. # 调用format函数,生成提示词
  7. multiple_input_prompt.format(color = "black", animal = "bear")

应该能看到如下输出

a black bear.

聊天提示词模板

聊天模型如GPT,输入一系列消息列表,每条消息都有对应的角色,这个消息列表通常以一定的格式串联,构成模型的输入,也就是提示词。

在OpenAI的Chat Completion API中,聊天消息和assistant,human,system角色关联

langchain提供了一些列模板,更简洁的使用构建和处理提示词。官方文档提醒,在于聊天模型交互时,优先使用这些与聊天相关的模板,有助于提高模型效率。

SystemMessagePromptTemplate,AIMessagePromptTemplate,AssistantMessagePromptTemplate分别构建不同角色的提示词模板。

  1. from langchain.prompts import (
  2. ChatPromptTemplate,
  3. PromptTemplate,
  4. SystemMessagePromptTemplate,
  5. AIMessagePromptTemplate,
  6. HumanMessagePromptTemplate,
  7. )
  8. from langchain.schema import (
  9. SystemMessage,
  10. AIMessage,
  11. HumanMessage
  12. )
  13. # system提示词
  14. system_message = "You are a professional translator that translate {src_lang} to {dst_lang}"
  15. # 从system_message中加载提示词模板
  16. system_message_prompt = SystemMessagePromptTemplate.from_template(system_message)
  17. # human提示词是用户输入
  18. human_message = "{user_input}"
  19. human_message_prompt = HumanMessagePromptTemplate.from_template(human_message)
  20. chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
  21. chat_prompt.format_prompt(
  22. src_lang = "English",
  23. dst_lang = "Chinese",
  24. user_input = "Did you eat in this morning"
  25. ).to_messages()
[SystemMessage(content='You are a professional translator that translate English to Chinese', additional_kwargs={}), HumanMessage(content='Did you eat in this morning', additional_kwargs={}, example=False)]

样本选择器

在开发LLM相关应用中,可能需要从大量样本数据中,选择部分数据包含在提示词中。样本选择器(Example Selector)正是应对这种需求,通常与提示词配合使用。langchain框架封装了基础选择器BaseExampleSelector。

本文以基于长度的样本选择器(输入越长,选择样本越少;输入越短,选择样本越多)

LengthBaseExampleSelector为例,进行演示。

  1. from langchain.prompts import PromptTemplate
  2. from langchain.prompts import FewShotPromptTemplate
  3. from langchain.prompts.example_selector import LengthBasedExampleSelector
  4. # 样本
  5. examples = [
  6. {"input":"happy", "output":"sad"},
  7. {"input":"tall", "output":"short"},
  8. {"input":"energetic", "output":"lethargic"},
  9. {"input":"sunny", "output":"gloomy"},
  10. {"input":"windy", "output":"calm"}
  11. ]
  12. # 实例化提示词模板
  13. example_prompt = PromptTemplate(
  14. input_variables = ["input", "output"],
  15. template = "Input: {input}\nOutput: {output}"
  16. )
  17. # 实例化样本选择器
  18. example_selector = LengthBasedExampleSelector(
  19. # 可选的样本数据
  20. examples = examples,
  21. # 提示词模板
  22. example_prompt = example_prompt,
  23. # 格式化的样本数据的最大长度,通过get_text_length函数统计
  24. max_length = 25
  25. )
  26. # 实例化模板
  27. dynamic_prompt = FewShotPromptTemplate(
  28. example_prompt = example_prompt,
  29. example_selector = example_selector,
  30. prefix = "Give the antonym of every input",
  31. suffix = "Input:{adjective}\nOutput: ",
  32. input_variables = ["adjective"]
  33. )
  34. print(dynamic_prompt.format(adjective = "big"))

你应该能看到如下输出:

Give the antonym of every input

Input: happy
Output: sad

Input: tall
Output: short

Input: energetic
Output: lethargic

Input: sunny
Output: gloomy

Input: windy
Output: calm

Input: big
Output:

总结:本节介绍Prompt,langchain中提供PromptTemplate和Example Selector两种Prompt。

学习创建简单的模板,聊天模板以及样本选择器事例。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/991511
推荐阅读
相关标签
  

闽ICP备14008679号