技巧 6:通过格式词阐述需要输出的格式

这个技巧是技巧 2 的变种,比较常用于生成文本场景。其实在场景 7 中,你也可以用到这个技巧。

假设你想让 AI 总结一篇非常非常长的文章,并且按照特定格式给你总结,那你可以在文章前面明确输出的格式(为了不占用太多显示空间,我省略了正文内容,你可以在 OpenAI 的 Playground 看到完整案例)。它的意思其实是让 ChatGPT 按 Topic 总结,每个 Topic 里按照无序列表(就是里面那个 -)将每个 Topic 的主要观点罗列出来。

  1. Summarize the main points of the following speech
  2. Use the following format:
  3. Topic 1: <topic_name_1>
  4. - <point_1>
  5. ..
  6. Topic 2: <topic_name_2>
  7. - <point_1>
  8. ..
  9. Topic 10: ..
  10. Text: """
  11. Thank you so much, Fred, for that lovely introduction. And thanks to the Atlantic Council for hosting me today.
  12. The course of the global economy over the past two years has been shaped by COVID-19 and our efforts to fight the pandemic. It’s now evident, though, that the war between Russia and Ukraine has redrawn the contours of the world economic outlook. Vladimir Putin’s unprovoked attack on Ukraine and its people is taking a devastating human toll, with lives tragically lost, families internally displaced or becoming refugees, and communities and cities destroyed.
  13. ...
  14. """

Output 是这样的:

  1. Topic 1: The war in Ukraine
  2. - The war is taking a human toll with lives lost, families displaced, and communities destroyed
  3. - The Biden administration is committed to holding Russia accountable
  4. - The war has violated international law and is a challenge to the international order
  5. Topic 2: The global economy
  6. - The war is having negative impacts on the global economy, including higher commodity prices and inflation
  7. - The IMF and World Bank will be focused on helping developing countries weather the impacts of the war
  8. - The ultimate outcome for the global economy depends on the path of the war

关于这个场景和技巧,我想再解释一下为什么潜力很大。

根据我使用各种 Summary 或者信息提取的产品,我发现 AI 并不知道什么是重点,所以在总结的过程中,会丢失很多内容。如何引导 AI 进行总结,就变得非常重要,且具有一定的可玩性。


感谢 AlexZhangji 的分享,TA 提到一个使用此技巧的场景:

使用此技巧可以使输出更结构化。比如针对一篇文章进行问答,你不仅想要得到一个答案,也希望 ChatGPT 的答案符合特定的格式,方便你下一步进行自动化。

比如问 “这里的债券 duration 是多少?”. 正常 GPT 模型的答案可能是 “债券 duration 是 4 年” 或 “duration 4 年”。 ChatGPT 的回答不稳定,且不方便继续处理。

解法: 我们可以通过这个技巧,让模型理解我们预期的格式。并在此基础上,为了方便自动化,让模型输出特定的结构化答案 (比如 JSON/Markdown 等)。 也可以方便集成更多的额外要求,比如增加一个”confidence level”, 并通过 prompt 的形式指定这些数值的格式。

比如:

  1. {context}
  2. Question: What is bond duration mentioned here.
  3. Answer template (Valid JSON format):
  4. {{
  5. "duration": $duration_numeric_value_in_year,
  6. "confidence_level": $answer_confidence_level_high_modrate_or_low,
  7. }}
  8. Answer:

在吴恩达的 ChatGPT Prompt Engineering 课程中,有提到一个这个技巧的高级用法,在让 AI 按照特定格式输出内容的同时,还让 AI 根据内容是否满足特定条件,来判断应该输出什么结果,下面课程中的案例的 prompt(注意这个是 python 代码,有一些转义字符,可以不用管):

  1. You will be provided with text delimited by triple quotes.
  2. If it contains a sequence of instructions, \
  3. re-write those instructions in the following format:
  4. Step 1 - ...
  5. Step 2 -
  6. Step N -
  7. If the text does not contain a sequence of instructions, \
  8. then simply write \"No steps provided.\"
  9. \"\"\"{text}\"\"\"

简单解释下,这个 prompt 分成两步:

让 AI 将输入的 text 转为步骤(就是 prompt 里的 Step 1、2)

然后还增加了一个判断,如果输入的 text 里没有 step 的内容,那么就输出 No Step

假如输入的 text 是一个泡茶的步骤介绍:

  1. Making a cup of tea is easy! First, you need to get some \
  2. water boiling. While that's happening, \
  3. grab a cup and put a tea bag in it. Once the water is \
  4. hot enough, just pour it over the tea bag. \
  5. Let it sit for a bit so the tea can steep. After a \
  6. few minutes, take out the tea bag. If you \
  7. like, you can add some sugar or milk to taste. \
  8. And that's it! You've got yourself a delicious \
  9. cup of tea to enjoy.

那么 AI 输出的内容是这样的(因为内容中包含了步骤式的内容):

  1. Step 1 - Get some water boiling.
  2. Step 2 - Grab a cup and put a tea bag in it.
  3. Step 3 - Once the water is hot enough, pour it over the tea bag.
  4. Step 4 - Let it sit for a bit so the tea can steep.
  5. Step 5 - After a few minutes, take out the tea bag.
  6. Step 6 - Add some sugar or milk to taste.
  7. Step 7 - Enjoy your delicious cup of tea!

但如果我们输入的是这样的 text:

  1. The sun is shining brightly today, and the birds are \
  2. singing. It's a beautiful day to go for a \
  3. walk in the park. The flowers are blooming, and the \
  4. trees are swaying gently in the breeze. People \
  5. are out and about, enjoying the lovely weather. \
  6. Some are having picnics, while others are playing \
  7. games or simply relaxing on the grass. It's a \
  8. perfect day to spend time outdoors and appreciate the \
  9. beauty of nature.

从内容上看,这段话,没有任何步骤式的内容,所以 AI 的输出是这样的:

  1. No steps provided.