背景
OpenAI官方具体介绍了ChatGPT运用的最佳实践,不只适用于运用ChatGPT网站进行直接对话的用户,还适用于经过OpenAI API接入的开发者。
把握了这些最佳实践,就能更好地运用GPT大模型。
本文是ChatGPT运用最佳实践系列第1篇 – 供给明晰且明晰的指令(write clear instructions)。
GPT大模型并不会读心术,需要你在提示词(prompt)里明晰你的具体诉求,大模型才会供给最佳的答复。
- 假如大模型给的答复过长,你能够在prompt里告知它你想要更简略的答复。
- 假如大模型给的答复过于简单,你能够在prompt里要求它供给专家水准一般的输出。
- 假如大模型给的答复格式你不喜欢,你能够在prompt里告知大模型你想要的输出格式。
简而言之,GPT需要猜的东西越少,答复的作用越好。
接下来具体讲述6个具体的操作指引。
战略1:在prompt里供给细节
假如要让GPT给出你想要的成果,需要保证你的prompt里包含重要的细节,不然GPT模型需要猜测你想要的答案,那给出的成果就未必好了。
以下是一些具体示例,第一列为bad prompt,第二列为good prompt,大家能够比照感受下。
Worse | Better |
---|---|
How do I add numbers in Excel? | How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows with all the totals ending up on the right in a column called “Total”. |
Who’s president? | Who was the president of Mexico in 2021, and how frequently are elections held? |
Write code to calculate the Fibonacci sequence. | Write a TypeScript function to efficiently calculate the Fibonacci sequence. Comment the code liberally to explain what each piece does and why it’s written that way. |
Summarize the meeting notes. | Summarize the meeting notes in a single paragraph. Then write a markdown list of the speakers and each of their key points. Finally, list the next steps or action items suggested by the speakers, if any. |
战略2:指定模型需要扮演的人物
OpenAI的Chat Completions API的messages参数能够经过指定role为system来告知模型需要扮演的人物。
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a math genius."
},
{
"role": "user",
"content": "Hello!"
}
]
}'
例如,你希望GPT帮你做内容创作,创作的每段内容里包含至少一个笑话或俏皮的评论。
那system能够按如下示例规划:
SYSTEM | When I ask for help to write something, you will reply with a document that contains at least one joke or playful comment in every paragraph. |
---|---|
USER | Write a thank you note to my steel bolt vendor for getting the delivery in on time and in short notice. This made it possible for us to deliver an important order. |
能够在这个链接里看作用:Open in Playground
战略3:用分隔符来明晰prompt的结构
分隔符能够便利大模型更准确识别prompt里的不同组成部分,答复作用更好。
参阅示例:
比方1:
USER | Summarize the text delimited by triple quotes with a haiku. “””insert text here””” |
---|
比方2:
SYSTEM | You will be provided with a pair of articles (delimited with XML tags) about the same topic. First summarize the arguments of each article. Then indicate which of them makes a better argument and explain why. |
---|---|
USER | <article> insert first article here </article> <article> insert second article here </article> |
比方3:
SYSTEM | You will be provided with a thesis abstract and a suggested title for it. The thesis title should give the reader a good idea of the topic of the thesis but should also be eye-catching. If the title does not meet these criteria, suggest 5 alternatives. |
---|---|
USER | Abstract: insert abstract here Title: insert title here |
关于一些很简单的使命,加分隔符前后作用可能不明显。
可是关于一些杂乱的使命,比方很长的的prompt,加分隔符能够让GPT准确识别到每部分的结构,答复作用会更好。
战略4:指定完结本项使命需要的过程
有些使命是能够分步拆解的,明晰告知GPT要执行的每个过程能够让答复作用更好。
SYSTEM | Use the following step-by-step instructions to respond to user inputs.
Step 1 – The user will provide you with text in triple quotes. Summarize this text in one sentence with a prefix that says “Summary: “. Step 2 – Translate the summary from Step 1 into Spanish, with a prefix that says “Translation: “. |
---|---|
USER | “””insert text here””” |
比方上面的比方,GPT的答复就会根据你的要求,第一步先输出summary,第二步再把summary翻译为西班牙语。
能够在这个链接里看作用:Open in Playground
战略5:供给示例
有时候你希望GPT依照你想要的风格答复问题,可是这个风格又很难用明晰的语言表述出来,就能够经过供给样例的方法给GPT,这种就叫 few-shot learning/prompting。
参阅示例如下,你供给了一组<user, assistant>样例,system里指定了模型要扮演的人物。
后续user的问题,模型就会依照你供给的样例的风格进行答复。
SYSTEM | Answer in a consistent style. |
---|---|
USER | Teach me about patience. |
ASSISTANT | 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. |
USER | Teach me about the ocean. |
能够在这个链接里看作用:Open in Playground
战略6:明晰答复的长度
你能够告知模型答复内容的长度,这个长度能够是字数,能够是句子数量,也能够是段落数量等。关于字数,模型不会特别精准。
参阅示例如下:
USER | Summarize the text delimited by triple quotes in about 50 words. “””insert text here””” |
---|
USER | Summarize the text delimited by triple quotes in 2 paragraphs. “””insert text here””” |
---|
USER | Summarize the text delimited by triple quotes in 3 bullet points. “””insert text here””” |
---|
总结
本文是ChatGPT运用最佳实践系列第1篇 – 供给明晰且明晰的指令(write clear instructions)。
具体讲述了6个战略,以上战略不只适用于GPT模型,还适用于其它大语言模型。
文章和示例代码开源在GitHub: GPT实战教程,能够看到一切干流的开源LLM。
大众号:coding进阶。
个人网站:Jincheng’s Blog。
知乎:无忌。
References
- platform.openai.com/docs/guides…