ChatGPT最佳实践系列第3篇-拆分复杂任务

背景

OpenAI官方详细介绍了ChatGPT使用的最佳实践,不仅适用于使用ChatGPT网站进行直接对话的用户,还适用于通过OpenAI API接入的开发者。

掌握了这些最佳实践,就能更好地利用GPT大模型。

本文是ChatGPT使用最佳实践系列第3篇 - 将复杂任务拆分成更简单的子任务。

软件开发过程中,我们通常会把一个复杂的系统拆分成多个功能模块,这样整个系统更好理解,更容易维护。

这个原理同样适用于GPT大模型,因为复杂任务比简单任务有更高的错误率。如果把一个复杂的任务拆分成多个更为简单的子任务,大模型回答效果通常会更好。

策略1:对用户提问做分类

举个例子,如果你要做一个智能客服,用户的提问可能是非常多样的,有可能问产品信息,有有可能问技术问题,也有可能问账号信息等等。

为了能够精确回答客户的提问,我们可以先让大模型对用户的提问做分类,判断用户是在提问哪方面的问题。然后再根据用户提问的分类来给大模型输入对应的instruction指令,这样回答效果会更好。

参考如下示例:用户买了一个路由器,但是上不了网,于是用户做了如下提问

system You will be provided with customer service queries. Classify each query into a primary category and a secondary category. Provide your output in json format with the keys: primary and secondary. Primary categories: Billing, Technical Support, Account Management, or General Inquiry. Billing secondary categories: - Unsubscribe or upgrade - Add a payment method - Explanation for charge - Dispute a charge Technical Support secondary categories: - Troubleshooting - Device compatibility - Software updates Account Management secondary categories: - Password reset - Update personal information - Close account - Account security General Inquiry secondary categories: - Product information - Pricing - Feedback - Speak to a human
user I need to get my internet working again.

我们可以通过system消息来让大模型先判断用户问题的分类,有了问题分类后,我们就可以把对应问题分类的instruction指令通过system消息告诉大模型,大模型再做回答。

system You will be provided with customer service inquiries that require troubleshooting for technical support. Help the user by: - Ask them to check that all cables to/from the router are connected. Note that it is common for cables to come loose over time. - If all cables are connected and the issue persists, ask them which router model they are using - Now you will advise them how to restart their device: -- If the model number is MTD-327J, advise them to push the red button and hold it for 5 seconds, then wait 5 minutes before testing the connection. -- If the model number is MTD-327S, advise them to unplug and replug it, then wait 5 minutes before testing the connection. - If the customer's issue persists after restarting the device and waiting 5 minutes, connect them to IT support by outputting {"IT support requested"}. - If the user starts asking questions that are unrelated to this topic then confirm if they would like to end the current chat about troubleshooting and classify their request according to the following scheme: Classify their query into a primary category and a secondary category. Provide your output in json format with the keys: primary and secondary. Primary categories: Billing, Technical Support, Account Management, or General Inquiry. Billing secondary categories: - Unsubscribe or upgrade - Add a payment method - Explanation for charge - Dispute a charge Technical Support secondary categories: - Troubleshooting - Device compatibility - Software updates Account Management secondary categories: - Password reset - Update personal information - Close account - Account security General Inquiry secondary categories: - Product information - Pricing - Feedback - Speak to a human
user I need to get my internet working again.
  • 只要大模型输出的结果是分类结果,我们就根据分类结果来重新构造system消息,重新向大模型提问。
  • 如果大模型输出的结果不是问题分类结果,那我们就不用改变system消息,直接把大模型的结果返回给用户即可。

以上技术手段其实构造了一个状态机来回答用户的问题,非常适合于智能客服的场景。

可以通过这个链接地址进行体验:Open in Playground

策略2:总结或者过滤长对话内容

GPT大模型有上下文长度的限制,比如GPT-4最多只支持32k上下文长度,具体每个模型的上下文长度限制参考context length

如果对话的轮次过多,或者对话内容过长(比如你让大模型帮你写论文等),那把所有对话记录都发送给GPT就会超过GPT大模型的context length。

如何解决这个问题呢?有2个推荐的解决方案:

  • 第一种,设定一个token阈值,如果你发现对话记录的长度要超过这个阈值了,就可以把之前的部分对话记录让大模型做一个汇总,然后把汇总内容作为system消息给到大模型,这样就可以解决发送所有对话记录给大模型导致超过大模型上下文长度限制的问题。或者开发者的后台程序可以异步的对对话记录做定期汇总。
  • 第二种,根据用户的提问,从对话记录里筛选出最相关的对话记录,减少要发送的token数量。可以通过向量化检索的方式来实现筛选逻辑,具体可以参考 Use embeddings-based search to implement efficient knowledge retrieval

策略3:递归汇总长文档

假设我们要对长文档(比如一本书)做汇总,由于大模型有上下文长度限制(假设为L),在单次的请求里,假设大模型汇总后的结果completion的长度为A,那发给大模型的prompt的长度不能超过L-N。

completion结果的长度A我们是可以在API层面指定max_token参数来限制的,那怎么能控制prompt的长度呢?

我们可以对长文档的每一个章节分别做summary,然后对多个章节的summary继续递归summary,直到整篇长文档被summary。

要详细了解如何对长文档做summary可以参考OpenAI之前基于GPT-3变种的研究工作research

总结

本文是ChatGPT使用最佳实践系列第3篇 - 将复杂任务拆分成更简单的子任务。

详细讲述了3个策略,以上策略不仅适用于GPT模型,还适用于其它大语言模型。

文章和示例代码开源在GitHub: GPT实战教程,可以看到所有主流的开源LLM。

公众号:coding进阶。

个人网站:Jincheng's Blog

知乎:无忌

References

相关推荐
软件聚导航18 分钟前
从 AI 画马到马年红包封面,我还做了一个小程序
人工智能·chatgpt
bloglin999993 小时前
Qwen3-32B报错Invalid json output:{“type“: “1“}For troubleshooting, visit
llm·json
callJJ5 小时前
Spring AI ImageModel 完全指南:用 OpenAI DALL-E 生成图像
大数据·人工智能·spring·openai·springai·图像模型
七牛云行业应用6 小时前
1M上下文腐烂?实测Opus 4.6 vs GPT-5.3及MoA降本架构源码
人工智能·python·llm·架构设计·gpt-5·claude-opus
蛇皮划水怪12 小时前
深入浅出LangChain4J
java·langchain·llm
组合缺一1 天前
Solon AI (Java) v3.9 正式发布:全能 Skill 爆发,Agent 协作更专业!仍然支持 java8!
java·人工智能·ai·llm·agent·solon·mcp
fanstuck2 天前
从0到提交,如何用 ChatGPT 全流程参与建模比赛的
大数据·数学建模·语言模型·chatgpt·数据挖掘
ASS-ASH2 天前
AI时代之向量数据库概览
数据库·人工智能·python·llm·embedding·向量数据库·vlm
带刺的坐椅2 天前
用 10 行 Java8 代码,开发一个自己的 ClaudeCodeCLI?你信吗?
java·ai·llm·agent·solon·mcp·claudecode·skills
有颜有货2 天前
GEO(生成引擎优化)是什么?GEO的工作流程详解
人工智能·chatgpt·geo