AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


给开发者使用的ChatGPT Prompt Engineering之指导原则
发布日期:2024-06-24 14:44:44 浏览次数: 1728


昨天晚上11点多,吴恩达@AndrewYNg 在社交媒体上发布了一项与OpenAI的工程师Isabella Fulford@Isabella Fulford 合作的一门新课程——ChatGPT Prompt Engineering for Developers

这门课程主要是讲如何更好地使用prompt来挖掘大语言模型(LLM)潜力的。课程官方网站为ChatGPT Prompt Engineering for Developers[1],目前可以免费学习

课程除了视频,还有对应的jupyter notebook可以进行实践,当然,还需要有可用的OpenAI Key和网络环境。关于网络环境,之前的一篇文章中介绍过如何使用cloudflare构建可访问的网络环境,详见——搭建稳定的私有ChatGPT服务,你都需要什么

课程短小精悍(总共1.5小时左右),但目前只有英文字幕,里面的样例也是英文。不过这对于使用没有什么影响,毕竟目前好用的大语言模型在英文上的效果要好于中文。

接下来,我打算写一个系列的文章,来记录下这门课程的主要内容。

第一篇文章,主要记录课程对应的第二讲——Guidelines(指导原则)

Guidelines(指导原则)

其实主要遵循的原则就两条:

  • Write clear and specific instructions(清晰和特定的提示语句)
  • Give the model time to think(给模型时间去思考)

下面具体展开。

Write clear and specific instructions

写出清晰和特定的提示语,主要包含如下策略:

  1. 使用特定的分隔符,比如"""、```、---、<>、以及一些XML标记

这么做主要是可以让模型区分提示词 和要处理的内容,防止prompt injection(提示词注入)

下面这段代码中的prompt变量,包含的```{text}```就是将处理的问题描述放到了特定分隔符中。

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}```
"""

  1. 要求模型进行结构化的格式输出,比如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.
"
""

这里的Provide them in JSON format with the following keys: 就是告诉模型,要以json的格式输出结果。

  1. 检查一些条件是否满足,当不满足一些条件时,可以给模型指定的回答

具体样例如下:

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}\"\"\"
"""

这里的 If the text does not contain a sequence of instructions, then simply write "No steps provided." ,就是告诉模型,当不满足条件时,不要强行输出

  1. few-shot prompting,即提示词中包含一些可以参考的任务样例

具体样例如下:

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

这里的child和grandparent的第一回合对话,就是给出了一个样例,这样模型就可以按照样例中的风格输出。

Give the model time to think

给模型时间思考的含义就类似那句经典的提示词——Let's think step by step。只不过为了获得更好的性能,作者给出了下面两条策略:

  1. 指定完成任务的步骤,即告诉模型完成一个任务需要有哪些中间步骤,每一步来具体做什么

一个具体的样例如下:

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

这里following actions中的1、2、3、4就是人为将任务进行分解,最后的following format,就像上面讲到的格式化输出的策略了。

  1. 在模型给出结论之前,引导模型来写出具体的解题步骤

这个样例比较长,可以参考原始的jupyter notebook文件。讲的是一个让模型判断某个问题的解答是否正确的任务。

如果直接让模型回答是否正确,模型很容易给出错误结果

如果在提示词中添加如下的语句,则更可能得到正确结果

To solve the problem do the following:

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

当然,最后也要注意模型目前存在的局限性,即一本正经地胡说八道。为了减少这种情况的出现,作者建议先引导模型找到相关信息,然后基于相关信息来回答问题。

以上就是第二讲的主要内容——构造prompt的两个原则以及对应的若干策略,总结如下:

  • Write clear and specific instructions(清晰和特定的提示语句)

    • 使用特定的分隔符,比如"""、```、---、<>、以及一些XML标记
    • 要求模型进行结构化的格式输出,比如HTML和JSON
    • 检查一些条件是否满足,当不满足一些条件时,可以给模型指定的回答
    • few-shot prompting,即提示词中包含一些可以参考的任务样例
  • Give the model time to think(给模型时间去思考)

    指定完成任务的步骤
    在模型给出结论之前,引导模型来写出具体的解题步骤


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询