Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompts 入门 #95

Open
QingyaFan opened this issue Dec 11, 2024 · 0 comments
Open

Prompts 入门 #95

QingyaFan opened this issue Dec 11, 2024 · 0 comments

Comments

@QingyaFan
Copy link
Owner

QingyaFan commented Dec 11, 2024

LLM 的训练过程,是让其大规模参数适配训练语料的模式。说对这种模式,才能打开LLM 知识的大门。与 LLM 对话时对模型的输入称为 Prompt,Prompt工程是一些说对prompt的最佳工程实践。

这实际是很容易理解:我们使用Google搜索时,一些搜索技巧可以提高搜索效率;同样的,使用LLM,也需要一些交互技巧。问对问题很关键。

Image

Prompt 中的 Message

按照角色对 Message 分类

image

按照角色,openai 将 Message 分为了 developer、system、user、function、assistant、tool 六类,其中前四个是输入 LLM的,后两个则是LLM输出的。它们分别对 交互 有不同的作用和效果:

  • developer: 据说是专门给 o-series 模型设计的,如 o1。为了专门向有推理能力的模型说明指令来自于开发者。对于 o1 来说,system 类型的 message 会自动转换成 developer 类型 message;而对于 GPT-4o 来说,developer类型的message 则会自动转换成 system 类型的 message。 1
  • system:设置模型行为和对话context的指令。为模型提供明确的角色设定和指令,定义它该如何回应用户。
  • user:代表用户的输入,用于传递问题或指令。模型会基于user message 来生成回应。
  • function:当模型需要调用外部工具时,输出的工具建议和参数。表示模型想要触发某个工具来完成任务,通常用于增强模型能力。除了role和content字段外,还有一个 name字段,标识要调用的外部函数名字。2
  • assistant:表示模型的响应,用于返回给用户的内容。输出格式可以是文本、代码或其他内容。3
  • tool:外部工具或函数的实际响应,用于返回调用结果。表示工具的返回值或函数执行后的结果,辅助最终的 assistant 响应 4

使用prompt的6个最佳实践

  1. Clear Instructions
  2. Adopt a persona
  3. Specify the format
  4. Avoid leading the answer
  5. Limit the scope

1. 清晰明确的指令

1.1 要包含尽可能多的细节信息。例如:

Worse Better
How do I add numbers in Excel? How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows with all the totals ending up on the right in a column called "Total".
Summarize the meeting notes. Summarize the meeting notes in a single paragraph. Then write a markdown list of the speakers and each of their key points. Finally, list the next steps or action items suggested by the speakers, if any.

1.2 在SYSTEM Message 中要求LLM 扮演一个角色。例如:

SYSTEM
When I ask for help to write something, you will reply with a document that contains at least one joke or playful comment in every paragraph.
USER
Write a thank you note to my steel bolt vendor for getting the delivery in on time and in short notice. This made it possible for us to deliver an important order.

1.3 使用分隔符来包围特定含义的文本。例如:

USER
Summarize the text delimited by triple quotes with a haiku.

"""insert text here"""

这里就指示LLM 总结 """ 包围的文字。

1.4 指定完成任务的步骤。例如:

SYSTEM
Use the following step-by-step instructions to respond to user inputs.

Step 1 - The user will provide you with text in triple quotes. Summarize this text in one sentence with a prefix that says "Summary: ".

Step 2 - Translate the summary from Step 1 into Spanish, with a prefix that says "Translation: ".
USER
"""insert text here"""

这类信息适合在 SYSTEM Message 中指定。

1.5 提供示例。

通常提供通用的行为指导更高效,但是有时很难准确描述你要表达的意思,这个时候提供几个示例就容易一点,让模型模拟这种行为,当然,这种行为如果刚好是某几维特征,那么就能准确模拟。这种方法叫做 少样本提示(few-shot prompting)

SYSTEM
Answer in a consistent style.
USER
Teach me about patience.
ASSISTANT
The river that carves the deepest valley flows from a modest spring; the grandest symphony originates from a single note; the most intricate tapestry begins with a solitary thread.
USER
Teach me about the ocean.

1.6 指定响应的长度。例如:

USER
Summarize the text delimited by triple quotes in about 50 words.

"""insert text here"""

2. 提供参考文本

LLM 可能会自信地编造答案,提供参考示例文本可以减少虚假信息。例如:5

SYSTEM       Use the provided articles delimited by triple quotes to answer questions. If the answer cannot be found in the articles, write "I could not find an answer."
USER         <insert articles, each delimited by triple quotes>

             Question: <insert question here>

SYSTEM 类型的 Message 指导 LLM 如何使用提供的参考文本,USER <> 中的内容是参考文本,这些文本可以从外部的知识库检索而来,例如外部的向量知识库。Question 则是真正用户的 query。

3. 将复杂任务分解成多个简单的子任务

执行复杂任务出错的概率一般更高,复杂任务一般也能重新定义成由工作流串/并联起来的一些简单子任务,上一个工作流节点的执行结果作为下一个节点的输入。

todo

Prompt 中一些常见技术

Zero-shot Prompting

Zero-shot prompting means that the prompt used to interact with the model won't contain examples or demonstrations. The zero-shot prompt directly instructs the model to perform a task without any additional examples to steer it. 6

Few-shot Prompting

Few-shot prompting can be used as a technique to enable in-context learning where we provide demonstrations in the prompt to steer the model to better performance. The demonstrations serve as conditioning for subsequent examples where we would like the model to generate a response. 7

Chain-of-Thought Prompting

Introduced in Wei et al. (2022), chain-of-thought (CoT) prompting enables complex reasoning capabilities through intermediate reasoning steps. You can combine it with few-shot prompting to get better results on more complex tasks that require reasoning before responding. 8

LangChain中的prompt

在langchain中,Prompt 对象负责生成和格式化模型输入,还对每一轮交互定义了一个表示,称为 Messages。Message是Prompt的一部分,它们都包含 role 和 content 两个字段,对于不同类别的 message,还会包含其他定制的额外字段。 9

  1. ToolMessage 代表工具调用返回的结果,role 是 tool,除了 role 和 content外,还有 tool_call_idartifact 两个字段。tool_call_id 就是字面意思,artifact 的内容是传递给 tool 的信息,不会传给 Model。 10
  2. HumanMessage 代表 User input:todo
  3. AIMessage 代表通过tools 查询 vector store:too
  4. ToolMessage 表示检索到的文档(retrieved documents):todo

在对话型应用中,Message 对象常常作为上下文的一部分,传递给 ChatPromptTemplate,帮助构建更复杂的对话上下文。

Footnotes

  1. https://community.openai.com/t/how-is-developer-message-better-than-system-prompt/1062784

  2. https://python.langchain.com/v0.1/docs/modules/model_io/chat/message_types/

  3. https://community.make.com/t/what-is-the-difference-between-system-user-and-assistant-roles-in-chatgpt/36160/3

  4. https://community.openai.com/t/message-possible-type-role-tool-how-to-use-that/722442/5

  5. https://platform.openai.com/docs/guides/prompt-engineering

  6. https://www.promptingguide.ai/techniques/zeroshot

  7. https://www.promptingguide.ai/techniques/fewshot

  8. https://www.promptingguide.ai/techniques/cot

  9. https://python.langchain.com/docs/concepts/messages/

  10. https://python.langchain.com/docs/concepts/messages/#toolmessage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant