2.3 策略

本节主要介绍了编写 Prompt 常用的指导原则和策略。

参考资料:

  1. OpenAI GPT Best Practices
  2. ChatGPT Prompt Engineering for Developers Course
  3. ChatGPT Prompt Engineering for Developers Github
  4. Best practices for prompt engineering with OpenAI API

2.3.1 Write clear and specific instructions

  • 策略 1:使用分隔符(如 ```, ### 或 """ 等)分隔 Prompt 的不同部分。

❌ Not good:

Summarize the text below.

{text input here}

✅ Better:

Summarize the text delimited by triple quotes.

"""{text input here}"""
  • 策略 2: Instruction 要具体,尽量不要使用模糊的词语。

❌ Not good:

Summarize the text delimited by triple quotes in only a few sentences.

"""{text input here}"""

✅ Better:

Summarize the text delimited by triple quotes in 3 to 5 sentences.

"""{text input here}"""
  • 策略 3: 输出形式和输出长度等可以通过描述或示例等方式明确具体说明。

✅ Recommend 1:

Summarize the text delimited by triple quotes in 3 bullet points.

"""{text input here}"""

✅ Recommend 2:

Summarize the text delimited by triple quotes.

Output should less than 100 words, and in the following format:
- xxxx
- yyyy
- zzzz

"""{text input here}"""

✅ Recommend 3:

write a fairy tale based on the following text delimited by triple quotes.

output in JSON format, include following fields: story_title, story_content. 

"""{text input here}"""
  • 策略 3: 在 Instruction 中指明 LLM 的角色。

✅ Recommend 1:

You a Rust programing language developer.

Write a function to calculate fibonacci number.

✅ Recommend 2:

You are a primary school teacher who can explain complex content to a level that a 6 year old child can understand. 

Rewrite the following text delimited by triple quotes to make them easier to understand.

"""{text input here}"""
  • 策略 4:提供示例,而不是仅仅描述 - “Few-Shot Prompting”。

✅ Recommend 1:

Stable Diffusion is an AI art generation model similar to DALLE-2.
You need to generate Stable Diffusion Prompts based on the given content.

There are one example of generating Stable Diffusion Prompts based on the given content.

Content:
[
    {
        "page_id": 1,
        "page_content": "Once upon a time, there was a prince named Arthur. He loved exploring the enchanted forest.",
    },
    {
        "page_id": 2,
        "page_content": "One day, as Prince Arthur walked deep into the forest, he discovered a magical talking rabbit",
    },
]

Stable Diffusion Prompt:
[
    {
        "page_id": 1,
        "page_content": "Once upon a time, there was a prince named Arthur. He loved exploring the enchanted forest.",
        "sd_prompt": "A little boy in the enchanted forest."
    },
    {
        "page_id": 2,
        "page_content": "One day, as Prince Arthur walked deep into the forest, he discovered a magical talking rabbit",
        "sd_prompt": "A little boy, a rabbit, in the deep forest."
    },
]

generate Stable Diffusion Prompt for the following content:
[
    {
        "page_id": 3,
        "page_content": "The rabbit told Prince Arthur about a hidden treasure guarded by a friendly dragon."
    },
    {
        "page_id": 4,
        "page_content": "Prince Arthur decided to find the treasure. He followed the rabbit to the dragon's cave."
    },
]

2.3.2 Provide reference text

  • 策略 1:在 Prompt 中提供参考文本,指示 LLM 根据参考文本生成结果,甚至可以要求生成的结果需要注明引用的参考文本中哪部分内容,防止 “幻觉”。

✅ Recommend 1:

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."

"""{reference text input here}"""

Question: {question input here}

2.3.3 Give the model time to “think”

  • 策略 1:明确指出完成任务的步骤。

✅ Recommend 1:

Perform the following actions: 
1 - Summarize the following text delimited by triple quotes with 1 sentence.
2 - Translate the summary into French.
3 - List each name in the French summary.
4 - Output a json object that contains the following keys: french_summary, num_names.

Separate your answers with line breaks.

Text:
"""{text input here}"""
  • 策略 2:让模型先自行推导得出解决方案,而不是直接对结果加以评判。

❌ Not good:

Determine if the student's solution is correct or not.

Problem Statement: I'm building a solar power installation and I need help working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost me a flat $100k per year, and an additional $10 / square foot
What is the total cost for the first year of operations as a function of the number of square feet.

Student's Solution: Let x be the size of the installation in square feet.
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000

:::caution 目前该 Prompt 在 GPT-4 模型上也得不出正确的解答。 :::

✅ Better:

First work out your own solution to the problem. Then compare your solution to the student's solution and evaluate if the student's solution is correct or not. Don't decide if the student's solution is correct until you have done the problem yourself.

Problem Statement: I'm building a solar power installation and I need help working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost me a flat $100k per year, and an additional $10 / square foot
What is the total cost for the first year of operations as a function of the number of square feet.

Student's Solution: Let x be the size of the installation in square feet.
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000

2.3.4 start simple and gradually interative

Prompt 的设计是一个逐步迭代的过程。

因为 LLM 模型也在逐步迭代优化,以后使用简单的 Prompt 就能完成目前需要进行复杂 Prompt 设计的任务。

所以我们在编写 Prompt 时,首先应该从简单的 Zero-Shot Prompting 开始,逐步增加 Prompt 构成部分,逐步增加复杂度,直至满足我们的需求。

最后更新于