返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

大模型Prompt提示设计简介(2):有效的建议

[复制链接]
链载Ai 显示全部楼层 发表于 1 小时前 |阅读模式 打印 上一主题 下一主题

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;background-color: rgb(255, 255, 255);visibility: visible;line-height: 1.6em;margin-top: 16px;margin-bottom: 16px;">本文将介绍在设计Prompt时可以采用的一些常规设计策略。虽然Prompt设计方法没有正确或错误一说,但可以使用常见的策略来影响模型的回答。严格地测试和评估对于优化模型性能仍然至关重要。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;background-color: rgb(255, 255, 255);visibility: visible;line-height: 1.6em;margin-top: 16px;margin-bottom: 16px;">大语言模型 (LLM) 使用大量文本数据进行训练,以学习语言单元之间的模式和关系。在获得一些文本(Prompt)后,语言模型可以预测接下的内容,就像一个复杂的自动补全工具。因此,在设计Prompt时,请考虑可能影响模型预测结果的不同因素。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;border-left: 4px solid rgb(248, 57, 41);">提供清晰而具体的说明

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;text-wrap: wrap;background-color: rgb(255, 255, 255);text-align: left;line-height: 1.6em;margin-top: 8px;margin-bottom: 8px;">通过向模型提供关于要做什么的指令,可以有效且高效地自定义模型行为。确保提供的指令清晰而具体。指令可以很简单,比如是一个分步说明列表;也可以很复杂,例如反映用户的思考过程和思维模式。

划重点:
  • 明确向模型提供有关要做什么的指令。
  • 说明应清晰而具体。
  • 明确指定限制条件或格式要求。

定义要执行的任务

详细描述希望模型执行的任务。以下提示告诉模型提取一段文本的摘要:

Summarizethistext.Text:Aquantumcomputerexploitsquantummechanicalphenomenatoperformcalculationsexponentiallyfasterthananymoderntraditionalcomputer.Atverytinyscales,physicalmatteractsasbothparticlesandaswaves,andquantumcomputingusesspecializedhardwaretoleveragethisbehavior.Theoperatingprinciplesofquantumdevicesisbeyondthescopeofclassicalphysics.Whendeployedatscale,quantumcomputerscouldbeusedinawidevarietyofapplicationssuchas:incybersecuritytobreakexistingencryptionmethodswhilehelpingresearcherscreatenewones,inmeteorologytodevelopbetterweatherforecastingetc.However,thecurrentstateoftheartquantumcomputersarestilllargelyexperimentalandimpractical.

模型结果:

Quantumcomputersutilizequantummechanicalphenomenatoperformcomputationssignificantlyfasterthantraditionalcomputers.Byexploitingtheparticle-wavedualityofmatterattinyscales,quantumcomputersleveragespecializedhardwaretosolveproblemsbeyondclassicalphysics.Potentialapplicationsincludebreakingencryption,developingbetterweatherforecasting,andmore.However,currentquantumcomputersarestilllargelyexperimentalandimpractical.
指定限制条件

明确指定有关读取Prompt或生成回答的限制条件。可以告诉模型要做什么和不应该做什么。在上一个示例中,摘要比原始文本短,但您可能想要更短的摘要。在这种情况下,可以在Prompt中指定一个限制条件,以限制摘要长度(只输出两句话):
Summarizethistextintwosentences.Text:Aquantumcomputerexploitsquantummechanicalphenomenatoperformcalculationsexponentiallyfasterthananymoderntraditionalcomputer.Atverytinyscales,physicalmatteractsasbothparticlesandaswaves,andquantumcomputingusesspecializedhardwaretoleveragethisbehavior.Theoperatingprinciplesofquantumdevicesisbeyondthescopeofclassicalphysics.Whendeployedatscale,quantumcomputerscouldbeusedinawidevarietyofapplicationssuchas:incybersecuritytobreakexistingencryptionmethodswhilehelpingresearcherscreatenewones,inmeteorologytodevelopbetterweatherforecastingetc.However,thecurrentstateoftheartquantumcomputersarestilllargelyexperimentalandimpractical.

模型结果:

Quantumcomputersusequantummechanicalphenomenatoperformcalculationsmuchfasterthantraditionalcomputers.Theyhavethepotentialtorevolutionizevariousfields,butcurrentquantumcomputersarestillexperimentalandimpractical.


定义回答格式

可以提供指定回答格式的说明。例如,可以要求将回答格式设置为表格、项目符号列表、电梯间推销、关键字、句子或段落。以下Prompt指示模型使用项目符号列出文本中的关键信息:

Summarizethistextasbulletspointsofkeyinformation.Text:Aquantumcomputerexploitsquantummechanicalphenomenatoperformcalculationsexponentiallyfasterthananymoderntraditionalcomputer.Atverytinyscales,physicalmatteractsasbothparticlesandaswaves,andquantumcomputingusesspecializedhardwaretoleveragethisbehavior.Theoperatingprinciplesofquantumdevicesisbeyondthescopeofclassicalphysics.Whendeployedatscale,quantumcomputerscouldbeusedinawidevarietyofapplicationssuchas:incybersecuritytobreakexistingencryptionmethodswhilehelpingresearcherscreatenewones,inmeteorologytodevelopbetterweatherforecastingetc.However,thecurrentstateoftheartquantumcomputersarestilllargelyexperimentalandimpractical.

模型结果:

-Quantumcomputersleveragequantummechanicalphenomenaforexponentiallyfastercalculationsthantraditionalcomputers.-Quantumdevicesutilizespecializedhardwaretoexploittiny-scalematterbehaviorasbothparticlesandwaves.-Theiroperatingprinciplestranscendclassicalphysics.-Potentialapplicationsincludecybersecurity,weatherforecasting,andmore.-Currentquantumcomputersarelargelyexperimentalandimpractical.

指定格式在实际项目中是非常有用的操作,这样可以便于我们对结果进行提取和加工,满足下一步的业务需求。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;border-left: 4px solid rgb(248, 57, 41);">添加少样本示例

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;">可以在Prompt中添加示例,以向模型示范想要的结果。模型会尝试识别示例中的模式和关系,并在生成回答时应用它们。包含少量示例的提示称为少样本提示,而不提供示例的提示称为零样本提示。少样本提示通常用于规定模型回答的格式、措辞、范围界定或一般模式。使用具体而多样的示例来帮助模型缩小其重点并生成更准确的结果。

建议始终在Prompt中包含少样本示例。不含少样本示例的Prompt可能效果不大,因为它们向模型展示了如何应用指令。事实上,如果示例足够清晰地显示当前任务,则可以从提示中移除指令。

划重点:

  • 在Prompt中包含提示回答示例有助于模型学习如何回答。

  • 向模型提供要遵循的模式示例,而不是应避免的模式示例。

  • 尝试在提示中添加不同数量的示例。具体取决于模型,示例太少会导致无法有效改变模型行为,示例太多会导致模型过拟合。

  • 在各个示例之间使用一致的格式

零样本提示与少样本提示

以下零样本提示要求模型选择最佳解释。

Please choose the best explanation to the question:
Question: How is snow formed?Explanation1: Snow is formed when water vapor in the air freezes into ice crystals in theatmosphere, which can combine and grow into snowflakes as they fall through the atmosphere andaccumulate on the ground.Explanation2: Water vapor freezes into ice crystals forming snow.Answer:

模型结果:

Explanation1

如果您的用例要求模型生成简洁的回答,您可以在提示中添加偏好简洁回答的示例。

以下提示提供了两个偏好较短解释的示例。在回答中,您可以看到这些示例引导模型选择较短的解释(解释 2),而不是像之前那样较长的解释(解释 1)。

Please choose the best explanation to the question:
Question: Why is sky blue?Explanation1: The sky appears blue because of Rayleigh scattering, which causes shorter bluewavelengths of light to be scattered more easily than longer red wavelengths, making the sky lookblue.Explanation2: Due to Rayleigh scattering effect.Answer: Explanation2
Question: What is the cause of earthquakes?Explanation1: Sudden release of energy in the Earth's crust.Explanation2: Earthquakes happen when tectonic plates suddenly slip or break apart, causing arelease of energy that creates seismic waves that can shake the ground and cause damage.Answer: Explanation1
Question: How is snow formed?Explanation1: Snow is formed when water vapor in the air freezes into ice crystals in theatmosphere, which can combine and grow into snowflakes as they fall through the atmosphere andaccumulate on the ground.Explanation2: Water vapor freezes into ice crystals forming snow.Answer:

模型结果:

Explanation2

找到最佳示例数量

您可以改变提示中提供的示例数量来进行实验,以获得最想要的结果。PaLM 和 Gemini 等模型通常可以通过少量示例来学习模式,但您可能需要进行多次实验,以找到可产生所需结果的示例数量。对于 BERT 等较简单的模型,您可能需要更多示例。但是,如果您添加太多示例,模型的回答可能会与示例过拟合。

使用示例肯定模式而不是否定模式

使用示例向模型展示要遵循的模式比使用示例向模型展示要避免的否定模式更有效。

否定模式

Don'tendhaikuswithaquestion:HaikuarefunAshortandsimplepoemDon'tyouenjoythem?

肯定模式

Alwaysendhaikuswithanassertion:HaikuarefunAshortandsimplepoemAjoytowrite

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 0.544px;background-color: rgb(255, 255, 255);text-align: left;">

在Prompt中,我们使用肯定语句的效果比否定语句更好,应该告诉模型做什么,而不是要求他不要做什么。

在各个示例之间使用一致的格式

确保少样本示例的结构和格式相同,以避免回答具有不需要的格式。在Prompt中添加少样本示例的主要目标之一是向模型展示回答格式。因此,必须确保在所有示例中使用一致的格式,尤其是注意 XML 标记、空格、换行符和示例分隔符。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;border-left: 4px solid rgb(248, 57, 41);">添加上下文信息

可以在Prompt中添加指令和模型解决问题所需的信息,而不是假设模型具备所有必要信息。这些上下文信息可帮助模型了解您要求其执行的操作的限制和详细信息。

划重点:

  • 在希望模型在生成回答时使用的Prompt中添加信息(上下文)。

  • 为模型提供有关如何使用上下文信息的指令。

以下示例要求模型提供路由器的问题排查指导:

WhatshouldIdotofixmydisconnectedwifi?ThelightonmyGoogleWifirouterisyellowandblinkingslowly.

模型结果:

1. **Check the Cables:**- Make sure that all cables are securely connected to your Google Wifi router and modem.- Try replacing the cables with new ones to ensure they are not faulty.

2. **Reboot the Router:**- Unplug your Google Wifi router and modem from the power outlet.- Wait for at least 30 seconds.- Plug the modem and router back in and wait for them to start up.

3. **Factory Reset the Router:**- Resetting the router will restore it to its default settings.- Find the small reset button on the back of the router.- Press and hold the reset button with a paper clip or similar object for 10 seconds.- Release the button and wait for the router to restart.- If you've tried the above steps and the issue persists, contact Google support for further assistance.
这个回答看起来是通用的问题排查信息,并非特定于路由器或 LED 指示灯的状态。
如需针对特定路由器自定义回答,可以在Prompt中添加该路由器的问题排查指南作为上下文,供模型在回答时参考。
Answer the question using the text below. Respond with only the text provided.Question: What should I do to fix my disconnected wifi? The light on my Google Wifi router is yellow and blinking slowly.
Text:Color: Slowly pulsing yellowWhat it means: There is a network error.What to do:Check that the Ethernet cable is connected to both your router and your modem and both devices are turned on. You might need to unplug and plug in each device again.
Color: Fast blinking yellowWhat it means: You are holding down the reset button and are factory resetting this device.What to do:If you keep holding down the reset button, after about 12 seconds, the light will turn solid yellow. Once it is solid yellow, let go of the factory reset button.
Color: Solid yellowWhat it means: Router is factory resetting.What to do:This can take up to 10 minutes. When it's done, the device will reset itself and start pulsing white, letting you know it's ready for setup.
Color: Solid redWhat it means: Something is wrong.What to do:Critical failure. Factory reset the router. If the light stays red, contact Wifi customer support.响应:

模型回答:

CheckthattheEthernetcableisconnectedtobothyourrouterandyourmodemandbothdevicesareturnedon.Youmightneedtounplugandplugineachdeviceagain.

由于大模型的能力限制,使用上下文是目前比较火的应用领域,可以用来加入领域知识回答专有领域的问题;也可以加入上下文,限制回答的范围等。

ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;border-left: 4px solid rgb(248, 57, 41);">添加前缀

前缀是添加到Prompt内容中的字词或短语,有多种用途,具体取决于前缀的位置:

  • 输入前缀:为输入添加前缀可向模型指示输入中在语义上有意义的部分。例如,前缀“英语:”和“法语:”区分两种不同的语言。

  • 输出前缀:虽然输出由模型生成,也可以在Prompt中为输出添加前缀。输出前缀可为模型提供有关预期回答的信息。例如,输出前缀“JSON:”告诉模型输出应采用 JSON 格式。

  • 示例前缀:在少样本Prompt中,为示例添加前缀提供了模型在生成输出时可以使用的标签,使解析输出内容变得更加容易。

在以下示例中,“Text:”是输入前缀,“The answer is:”是输出前缀。

Classifythetextasoneofthefollowingcategories.-large-smallText:RhinoTheansweris:largeText:MouseTheansweris:smallText:SnailTheansweris:smallText:ElephantTheansweris:

模型回答:

large

使用前缀可以明确的告诉模型,后面内容块作用,在实践中尽量要提供出来。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ