ChatGPT最佳实践系列-第1篇

背景

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来告诉模型需要扮演的角色。

bash 复制代码
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

相关推荐
小何code2 分钟前
人工智能【第11篇】K近邻算法KNN:简单有效的分类方法(长文+代码实现)
人工智能·机器学习·knn
测试员周周2 分钟前
【AI测试系统】第5篇:AI 编码工具抛硬币?我们用 LangGraph 做了个“确定性+AI”的测试系统(附自愈架构)
人工智能·python·功能测试·测试工具·架构·langchain·单元测试
初学大模型3 分钟前
与机器心智的对话:论人机交互中提问的精确性与描述的详尽性
人工智能
AI木马人4 分钟前
18.人工智能实战:LoRA 微调后效果不升反降?从数据清洗到训练参数的完整排查方案
人工智能·深度学习·机器学习
davedeveloper4 分钟前
ReAct 论文深度解读:让大模型学会“边想边做“
人工智能
2601_956414145 分钟前
2026年5月PCB厂家推荐:TOP5榜产品应对5G基站散热挑战
大数据·人工智能·5g
生成论实验室6 分钟前
《源·觉·知·行·事·物:生成论视域下的统一认知语法》导论:在破碎的世界寻找统一语法
人工智能·科技·算法·架构·创业创新
zhutier-8 分钟前
国科大交叉学院二次选拔笔试2025 AI方向基础知识整理存档
人工智能
嵌入式老牛13 分钟前
液晶段码(米/日字格)识别—预处理
人工智能·opencv·计算机视觉
comcoo14 分钟前
本地 AI 智能体 OpenClaw 部署实操教程
人工智能·openclaw安装包·龙虾ai·open claw部署