链载Ai

标题: 给开发者使用的ChatGPT Prompt Engineering之指导原则 [打印本页]

作者: 链载Ai    时间: 6 小时前
标题: 给开发者使用的ChatGPT Prompt Engineering之指导原则

昨天晚上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

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

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

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

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

text=f"""
Youshouldexpresswhatyouwantamodeltodoby\
providinginstructionsthatareasclearand\
specificasyoucanpossiblymakethem.\
Thiswillguidethemodeltowardsthedesiredoutput,\
andreducethechancesofreceivingirrelevant\
orincorrectresponses.Don'tconfusewritinga\
clearpromptwithwritingashortprompt.\
Inmanycases,longerpromptsprovidemoreclarity\
andcontextforthemodel,whichcanleadto\
moredetailedandrelevantoutputs.
"""
prompt=f"""
Summarizethetextdelimitedbytriplebackticks\
intoasinglesentence.
```{text}```
"""
  1. 要求模型进行结构化的格式输出,比如HTML和JSON

具体的一个提示词如下:

prompt=f"""
Generatealistofthreemade-upbooktitlesalong\
withtheirauthorsandgenres.
ProvidetheminJSONformatwiththefollowingkeys:
book_id,title,author,genre.
"""

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

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

具体样例如下:

prompt=f"""
Youwillbeprovidedwithtextdelimitedbytriplequotes.
Ifitcontainsasequenceofinstructions,\
re-writethoseinstructionsinthefollowingformat:

Step1-...
Step2-…

StepN-…

Ifthetextdoesnotcontainasequenceofinstructions,\
thensimplywrite\"Nostepsprovided.\"

\"\"\"{text_1}\"\"\"
"""

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

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

具体样例如下:

prompt=f"""
Yourtaskistoanswerinaconsistentstyle.

<child>:Teachmeaboutpatience.

<grandparent>:Theriverthatcarvesthedeepest\
valleyflowsfromamodestspring;the\
grandestsymphonyoriginatesfromasinglenote;\
themostintricatetapestrybeginswithasolitarythread.

<child>:Teachmeaboutresilience.
"""

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

Give the model time to think

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

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

一个具体的样例如下:

prompt_2=f"""
Yourtaskistoperformthefollowingactions:
1-Summarizethefollowingtextdelimitedby
<>with1sentence.
2-TranslatethesummaryintoFrench.
3-ListeachnameintheFrenchsummary.
4-Outputajsonobjectthatcontainsthe
followingkeys:french_summary,num_names.

Usethefollowingformat:
Text:<texttosummarize>
Summary:<summary>
Translation:<summarytranslation>
Names:<listofnamesinItaliansummary>
OutputJSON:<jsonwithsummaryandnum_names>

Text:<{text}>
"""

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

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

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

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

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

To solve the problem do the following:

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

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






欢迎光临 链载Ai (https://www.lianzai.com/) Powered by Discuz! X3.5