20220709 - ChatGPT Prompt Engineering for Developers


Why Note

开发过程中如果能够使用大规模语言模型(Large Language Model, LLM,如 ChatGPT)来快速构建应用是一件很棒的事!所以就有了这篇与 DeepLearning 和 OpenAI 一起学习如何使用 LLM 为开发过程助力的笔记!

What

Content

Introduction

  • 重要的是使用 gpt api 来快速构建应用
  • 两种类型的 LLM
    • Base LLM
      • 基于文本训练的数据来预测下一个词
    • Instruction Tuned LLM (course focus)
      • 被训练用于跟随指示
      • 如果觉得其回答得不好,可能是因为给的问题不够准确

Guidelines for Prompting

两个原则

1. Write clear and specific instructions

Tactics 1 - Use delimiters to clearly indicate distinct parts of the input
Delimiters can be anything like: ```, """, < >, `<tag> </tag>`, `:`
text = f"""
You should express what you want a model to do by \ 
providing instructions that are as clear and \ 
specific as you can possibly make them. \ 
This will guide the model towards the desired output, \ 
and reduce the chances of receiving irrelevant \ 
or incorrect responses. Don't confuse writing a \ 
clear prompt with writing a short prompt. \ 
In many cases, longer prompts provide more clarity \ 
and context for the model, which can lead to \ 
more detailed and relevant outputs.
"""
prompt = f"""
Summarize the text delimited by triple backticks \ 
into a single sentence.
```{text}```
"""
response = get_completion(prompt)
print(response)
To guide a model towards the desired output and reduce irrelevant or incorrect responses, it is important to provide clear and specific instructions, which can be achieved through longer prompts that offer more clarity and context.

Tactic 2: Ask for a structured output

HTML/JSON

prompt = f"""
Generate a list of three made-up book titles along \ 
with their authors and genres. 
Provide them in JSON format with the following keys: 
book_id, title, author, genre.
"""
response = get_completion(prompt)
print(response)
{
  "books": [
    {
      "book_id": 1,
      "title": "The Enigma of Elysium",
      "author": "Evelyn Sinclair",
      "genre": "Mystery"
    },
    {
      "book_id": 2,
      "title": "Whispers in the Wind",
      "author": "Nathaniel Blackwood",
      "genre": "Fantasy"
    },
    {
      "book_id": 3,
      "title": "Echoes of the Past",
      "author": "Amelia Hart",
      "genre": "Romance"
    }
  ]
}
Tactic 3: Ask the model to check whether conditions are satisfied
text_1 = f"""
Making a cup of tea is easy! First, you need to get some \ 
water boiling. While that's happening, \ 
grab a cup and put a tea bag in it. Once the water is \ 
hot enough, just pour it over the tea bag. \ 
Let it sit for a bit so the tea can steep. After a \ 
few minutes, take out the tea bag. If you \ 
like, you can add some sugar or milk to taste. \ 
And that's it! You've got yourself a delicious \ 
cup of tea to enjoy.
"""
prompt = f"""
You will be provided with text delimited by triple quotes. 
If it contains a sequence of instructions, \ 
re-write those instructions in the following format:

Step 1 - ...
Step 2 - …
…
Step N - …

If the text does not contain a sequence of instructions, \ 
then simply write \"No steps provided.\"

\"\"\"{text_1}\"\"\"
"""
response = get_completion(prompt)
print("Completion for Text 1:")
print(response)
Completion for Text 1:
Step 1 - Get some water boiling.
Step 2 - Grab a cup and put a tea bag in it.
Step 3 - Once the water is hot enough, pour it over the tea bag.
Step 4 - Let the tea sit for a bit to steep.
Step 5 - After a few minutes, take out the tea bag.
Step 6 - Add sugar or milk to taste, if desired.
Step 7 - Enjoy your delicious cup of tea.
Tactic 4: “Few-shot” prompting

Give successful examples of completing tasks then ask model to perform the task

prompt = f"""
Your task is to answer in a consistent style.

<child>: Teach me about patience.

<grandparent>: 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.

<child>: Teach me about resilience.
"""
response = get_completion(prompt)
print(response)
<grandparent>: Resilience is like a mighty oak tree that withstands the strongest storms, bending but never breaking. It is the unwavering determination to rise again after every fall, and the ability to find strength in the face of adversity. Just as a diamond is formed under immense pressure, resilience is forged through challenges and hardships, making us stronger and more resilient in the process.

2. Give the model time to think

Tactic 1: Specify the steps required to complete a task
text = f"""
In a charming village, siblings Jack and Jill set out on \ 
a quest to fetch water from a hilltop \ 
well. As they climbed, singing joyfully, misfortune \ 
struck—Jack tripped on a stone and tumbled \ 
down the hill, with Jill following suit. \ 
Though slightly battered, the pair returned home to \ 
comforting embraces. Despite the mishap, \ 
their adventurous spirits remained undimmed, and they \ 
continued exploring with delight.
"""
# example 1
prompt_1 = f"""
Perform the following actions: 
1 - Summarize the following text delimited by triple \
backticks 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}```
"""
response = get_completion(prompt_1)
print("Completion for prompt 1:")
print(response)
Completion for prompt 1:
1 - Jack and Jill, siblings, go on a quest to fetch water from a hilltop well, but encounter misfortune when Jack trips on a stone and tumbles down the hill, with Jill following suit, yet they return home and remain undeterred in their adventurous spirits.

2 - Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi de Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.

3 - Jack, Jill

4 - {
  "french_summary": "Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline, mais rencontrent un malheur lorsque Jack trébuche sur une pierre et dévale la colline, suivi de Jill, pourtant ils rentrent chez eux et restent déterminés dans leur esprit d'aventure.",
  "num_names": 2
}

Ask for output in a specified format

prompt_2 = f"""
Your task is to perform the following actions: 
1 - Summarize the following text delimited by 
  <> 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.

Use the following format:
Text: <text to summarize>
Summary: <summary>
Translation: <summary translation>
Names: <list of names in Italian summary>
Output JSON: <json with summary and num_names>

Text: <{text}>
"""
response = get_completion(prompt_2)
print("\nCompletion for prompt 2:")
print(response)
Completion for prompt 2:
Summary: Jack and Jill, siblings, go on a quest to fetch water from a hilltop well but encounter misfortune along the way. 
Translation: Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline mais rencontrent des malheurs en chemin.
Names: Jack, Jill
Output JSON: {"french_summary": "Jack et Jill, frère et sœur, partent en quête d'eau d'un puits au sommet d'une colline mais rencontrent des malheurs en chemin.", "num_names": 2}
Tactic 2: Instruct the model to work out its own solution before rushing to a conclusion
prompt = f"""
Determine if the student's solution is correct or not.

Question:
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.
Costs:
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
"""
response = get_completion(prompt)
print(response)
The student's solution is correct. They correctly identified the costs for land, solar panels, and maintenance, and calculated the total cost for the first year of operations as a function of the number of square feet.

Note that the student’s solution is actually not correct.
We can fix this by instructing the model to work out its own solution first.

这里模型的输出出错了哈哈… 应该是 360x + 100000 来着。

Model Limitations: Hallucinations

This will Makes statements that sound plausible But are not true
也就是说模型可能会胡乱编造。

为了减少类似情况的发生的 tactics:
First find relevant information,
Then answer the question based on the relevant information.

Iterative Prompt Development

  • 第一次可能总不会得到理想结果,这不重要,重要的是分析为什么没有,然后基于此不断迭代

  • LLM 对于字符(character)统计不是很擅长,所以可能会给出不符合字数要求的答案

  • 如果答案文本过长

    • 就让其限制答案字数
  • 如果错误关注了不应该关注的细节

    • 需要指明需要其关注的细节
  • 需要的话可以让其提供表格形式的输出

Summarizing

  • 可以让其根据受众来 summarize
    • 会根据内容来总结
  • 如果是 extract
    • 则会是第三方的客观视角

Inferring

  • 比如可以用于推断评论的情绪效价
  • 也可以让其辨别一段话中包含的情绪类型
  • 可以让其从评论中 extract 产品信息
  • 可以一次做好多不同的任务
  • Inferring topics
    • 根据文本推测 topic
    • 也可以根据 topic 看文本是否包含了相应 topic(舆情监测) zero-shot training algorithm

Transforming

  • 翻译
  • 转译
  • 语气转换
  • 形式/格式转换
  • 语法检查
    • 甚至可以列出更改的部分

      Expanding

  • 扩写
  • 提醒模型在扩写的同时使用已知的细节
  • 不应该使用该功能来写垃圾邮件
  • 使用 temperature 参数来控制 variety 和 creativity
    • 如果 temperature = 0 基本可以保证每次结果都一致

Chatbot / Chat Workflow

Chatbot

  • OpenAI 的 chat model 使用 user message 作为 input 使用 assistant message 作为 output

  • 在此基础之上,system message 其实会影响 assistant 的回答逻辑

大概这也是为什么许多 prompt 教程中提到的,先预设身份是有效的。这相当于把“预设身份”这一信息作为 system message 来告知模型,让其调整 assistant 的输出模式,从而更好地输出结果。

OrderBot

  • 有时候模型无法回答是因为其不知道上下文情景(context),需要为其提供

Conclusion

  • Use it responsibly!

Author:
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source !
 Previous
20230716 - That was Then, This Is Now 20230716 - That was Then, This Is Now
观看 Masahiro Sakurai on Creating Games 的视频,了解多多尝试是更好地享受现在的秘诀!
Next 
20230709 - Distinguishing Between Major and Minor Elements 20230709 - Distinguishing Between Major and Minor Elements
观看 Masahiro Sakurai on Creating Games 的视频,了解通过区分元素的主次进行优化的方法!
  TOC