OpenAI API - Practice

文章目录

  • 提示工程
      • 消息和角色
      • 使用响应API
      • 使用 Chat Completions API
      • 为模型提供用于生成的额外数据
    • 获得更好结果的六大策略
      • 写出清晰的说明
      • 提供参考文本
      • 将复杂任务拆分为更简单的子任务
      • 给模型时间“思考”
      • 使用外部工具
      • 系统地测试更改
    • 策略
      • 策略:写出清晰的说明
        • 策略:在您的查询中包含详细信息以获得更相关的答案
        • 策略:要求模型采用角色
        • 策略:使用分隔符清楚地指示输入的不同部分
        • 策略:指定完成任务所需的步骤
        • 策略:提供示例
        • 策略:指定输出的所需长度
      • 策略:提供参考文本
        • 策略:指示模型使用参考文本进行回答
        • 策略:指导模型通过引用参考文本中的段落来回答并添加引用
      • 策略:将复杂任务拆分为更简单的子任务
        • 策略:使用意图分类来识别用户查询中最相关的指令
        • 策略:对于需要非常长对话的对话应用程序,总结或过滤之前的对话
        • 策略:分段总结长文档并递归构建完整摘要
      • 策略:给模型时间“思考”
        • 策略:在匆忙得出结论之前指示模型自己找出解决方案
        • 策略:使用内心独白或一系列查询来隐藏模型的推理过程
        • 策略:询问模型是否在之前的遍历中遗漏了任何内容
      • 策略:使用外部工具
        • 策略:使用基于嵌入的搜索实现高效的知识检索
        • 策略:使用代码执行来执行更准确的计算或调用外部 API
      • 策略:系统地测试更改
        • 战术:将模型输出与黄金标准答案进行比较
    • 优化模型输出
    • 其他资源


提示工程

通过提示工程策略来增强结果。

https://platform.openai.com/docs/guides/prompt-engineering


构建提示词以从模型中获得正确输出的过程被称为提示工程。您可以通过给出精确的指令、示例以及必要的上下文信息(如模型训练数据中未包含的私密或专业信息)来提高输出质量。


消息和角色

通过提供一个包含指令的 messages 数组来创建提示。每个消息都可以有不同的 role,这会影响模型如何解释输入。

角色描述使用示例
user请求模型输出某些内容的指令。类似于你在 ChatGPT 作为最终用户输入的消息。将最终用户的消息传递给模型。写一首关于编程的俳句。
developer优先于用户消息的模型指令,遵循 命令链。之前被称为 system 提示。描述模型应该如何一般行为和响应。1 2 3 4 5 你是一个有帮助的助手,以美国东南部南方美女的风格回答编程问题。现在,对 user 消息的任何响应都应该有南方美女个性和语气。
assistant模型生成的消息,可能是之前一代请求(见下面的“对话”部分)。为模型提供如何响应当前请求的示例。例如,为了让模型正确地回应敲门笑话,你可能会提供一个完整的敲门笑话对话。

消息角色可以帮助你获得更好的响应,特别是如果你想让模型遵循层次指令。它们不是确定的,所以最好的使用方法是尝试不同的方法,看看哪些能给你带来好结果。

以下是一个开发者消息的示例,该消息修改了模型在响应 user 消息时的行为:


使用响应API

const response = await openai.chat.completions.create({model: "gpt-4o",messages: [{"role": "developer","content": [{"type": "text","text": `You are a helpful assistant that answers programming questions in the style of a southern belle from the southeast United States.`}]},{"role": "user","content": [{"type": "text","text": "Are semicolons optional in JavaScript?"}]}],store: true,
});

这个提示返回所需修辞风格的文本输出:

Well, sugar, that's a fine question you've got there! Now, in the 
world of JavaScript, semicolons are indeed a bit like the pearls 
on a necklace – you might slip by without 'em, but you sure do look 
more polished with 'em in place. Technically, JavaScript has this little thing called "automatic 
semicolon insertion" where it kindly adds semicolons for you 
where it thinks they oughta go. However, it's not always perfect, 
bless its heart. Sometimes, it might get a tad confused and cause 
all sorts of unexpected behavior.

使用 Chat Completions API

const response = await openai.chat.completions.create({model: "gpt-4o",messages: [{"role": "developer","content": [{"type": "text","text": `You are a helpful assistant that answers programming questions in the style of a southern belle from the southeast United States.`}]},{"role": "user","content": [{"type": "text","text": "Are semicolons optional in JavaScript?"}]}],store: true,
});

这个提示将返回所请求的修辞风格的文本输出:

Well, sugar, that's a fine question you've got there! Now, in the 
world of JavaScript, semicolons are indeed a bit like the pearls 
on a necklace – you might slip by without 'em, but you sure do look 
more polished with 'em in place. Technically, JavaScript has this little thing called "automatic 
semicolon insertion" where it kindly adds semicolons for you 
where it thinks they oughta go. However, it's not always perfect, 
bless its heart. Sometimes, it might get a tad confused and cause 
all sorts of unexpected behavior.

为模型提供用于生成的额外数据

您还可以使用上述消息类型为模型提供除训练数据之外的其他信息。您可能希望包含数据库查询、文本文档或其他资源的结果,以帮助模型生成相关响应。这种技术通常称为检索增强生成或 RAG。了解有关 RAG 技术的更多信息。

本指南分享了从大型语言模型(有时称为 GPT 模型)如 GPT-4o 获得更好结果的策略和技巧。此处描述的方法有时可以组合使用以获得更好的效果。我们鼓励您进行实验以找到最适合您的方法。

您还可以探索示例提示,展示我们的模型的功能:


提示示例

探索提示示例,了解 GPT 模型可以做什么

https://platform.openai.com/examples


获得更好结果的六大策略


写出清晰的说明

这些模型无法读懂你的想法。如果输出太长,请要求简短回复。如果输出太简单,请要求专家级写作。如果你不喜欢这种格式,请演示你想要看到的格式。模型猜测你想要什么的次数越少,你就越有可能得到它。

策略:

  • Include details in your query to get more relevant answers
  • Ask the model to adopt a persona
  • Use delimiters to clearly indicate distinct parts of the input
  • Specify the steps required to complete a task
  • Provide examples
  • Specify the desired length of the output

提供参考文本

语言模型可以自信地编造虚假答案,尤其是在被问及深奥的话题或引用和 URL 时。就像一张笔记可以帮助学生在考试中取得更好的成绩一样,为这些模型提供参考文本可以帮助他们用更少的编造来回答。

策略:

  • Instruct the model to answer using a reference text
  • Instruct the model to answer with citations from a reference text

将复杂任务拆分为更简单的子任务

正如在软件工程中将复杂系统分解为一组模块化组件是一种很好的做法一样,提交给语言模型的任务也是如此。复杂任务的错误率往往高于简单任务。此外,复杂任务通常可以重新定义为简单任务的工作流程,其中较早任务的输出用于构建后续任务的输入。

策略:

  • Use intent classification to identify the most relevant instructions for a user query
  • For dialogue applications that require very long conversations, summarize or filter previous dialogue
  • Summarize long documents piecewise and construct a full summary recursively

给模型时间“思考”

如果要求将 17 乘以 28,您可能不会立即知道答案,但仍然可以随着时间的推移得出答案。同样,模型在尝试立即回答时会犯更多推理错误,而不是花时间找出答案。在回答之前要求“思路”可以帮助模型更可靠地推理出正确答案。

策略:

  • Instruct the model to work out its own solution before rushing to a conclusion
  • Use inner monologue or a sequence of queries to hide the model’s reasoning process
  • Ask the model if it missed anything on previous passes

使用外部工具

通过向模型提供其他工具的输出来弥补模型的弱点。例如,文本检索系统(有时称为 RAG 或检索增强生成)可以告知模型相关文档。像 OpenAI 的代码解释器这样的代码执行引擎可以帮助模型进行数学运算和运行代码。如果某项任务可以通过工具而不是语言模型更可靠或更高效地完成,则可以卸载它以获得两者的最佳效果。

Tactics:

  • Use embeddings-based search to implement efficient knowledge retrieval
  • Use code execution to perform more accurate calculations or call external APIs
  • Give the model access to specific functions

系统地测试更改

如果可以测量,则提高性能会更容易。在某些情况下,对提示的修改将在几个孤立示例上实现更好的性能,但在更具代表性的示例集上导致整体性能下降。因此,为了确保更改对性能有净积极影响,可能需要定义一个全面的测试套件(也称为“评估”)。

Tactic:

  • Evaluate model outputs with reference to gold-standard answers

策略

上面列出的每种策略都可以用具体的策略来实例化。这些策略旨在提供一些可以尝试的想法。它们绝不是完全全面的,您应该可以自由地尝试这里未介绍的创意。


策略:写出清晰的说明


策略:在您的查询中包含详细信息以获得更相关的答案

为了获得高度相关的响应,请确保请求提供任何重要的细节或背景。否则,您就让模型来猜测您的意思。

更糟更好
如何在 Excel 中添加数字?如何在 Excel 中将一行美元金额相加?我想自动对一整张表的行执行此操作,所有总计都显示在右侧名为“总计”的列中。
谁是总统?2021 年墨西哥总统是谁,选举频率是多少?
编写代码来计算斐波那契数列。编写一个 TypeScript 函数来有效地计算斐波那契数列。对代码进行自由注释,以解释每个部分的作用以及为什么这样写。
总结会议记录。用一段话总结会议记录。然后写下发言者和他们每个要点的 markdown 列表。最后,列出发言者建议的后续步骤或行动项目(如果有)。

策略:要求模型采用角色

系统消息可用于在回复中指定模型使用的角色。


SYSTEMWhen 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.USERWrite 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.

根据内部评估,gpt-4.5-preview 模型具有特定的系统消息,可带来更好的性能。在此之后添加您自己的系统消息内容:


SYSTEMYou are a highly capable, thoughtful, and precise assistant. Your goal is to deeply understand the user's intent, ask clarifying questions when needed, think step-by-step through complex problems, provide clear and accurate answers, and proactively anticipate helpful follow-up information. Always prioritize being truthful, nuanced, insightful, and efficient, tailoring your responses specifically to the user's needs and preferences.

策略:使用分隔符清楚地指示输入的不同部分

三引号、XML 标签、章节标题等分隔符可以帮助划分需要不同处理的文本部分。


USERSummarize the text delimited by triple quotes with a haiku. 
"""insert text here"""

SYSTEMYou 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>
SYSTEMYou 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.USERAbstract: insert abstract here Title: insert title here

对于这些简单的任务,使用分隔符可能不会对输出质量产生影响。但是,任务越复杂,消除任务细节的歧义就越重要。不要让模型努力理解你对他们的要求。


策略:指定完成任务所需的步骤

某些任务最好指定为一系列步骤。明确写出步骤可以让模型更容易遵循它们。


SYSTEMUse 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"""

策略:提供示例

提供适用于所有示例的一般说明通常比通过示例演示任务的所有排列更有效,但在某些情况下,提供示例可能更容易。例如,如果您打算让模型复制一种难以明确描述的特定响应用户查询的风格。这被称为“少量”提示。


SYSTEMAnswer in a consistent style.USERTeach me about patience.ASSISTANTThe 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.USERTeach me about the ocean.

策略:指定输出的所需长度

您可以要求模型生成具有给定目标长度的输出。目标输出长度可以根据单词、句子、段落、要点等的数量来指定。但请注意,指示模型生成特定数量的单词并不能实现高精度。该模型可以更可靠地生成具有特定数量的段落或要点的输出。


USERSummarize the text delimited by triple quotes in about 50 words. """insert text here"""
USERSummarize the text delimited by triple quotes in 2 paragraphs. """insert text here"""
USERSummarize the text delimited by triple quotes in 3 bullet points. """insert text here"""

策略:提供参考文本


策略:指示模型使用参考文本进行回答

如果我们可以为模型提供与当前查询相关的可信信息,那么我们可以指示模型使用提供的信息来撰写答案。


SYSTEMUse the provided articles delimited by triple quotes to answer questions. If the answer cannot be found in the articles, write "I could not find an answer."USER<insert articles, each delimited by triple quotes> 
Question: <insert question here>

鉴于所有模型都具有有限的上下文窗口,我们需要一种方式来动态查找与所提问题相关联的信息。嵌入 可以用于实现高效的知识检索。有关如何实现此功能的更多详细信息,请参阅策略 “使用基于嵌入的搜索来实现高效的知识检索”。


策略:指导模型通过引用参考文本中的段落来回答并添加引用

如果输入已经补充了相关知识,那么要求模型通过引用提供的文档中的段落来添加引用到其答案中是很简单的。注意,输出中的引用可以通过在提供的文档中进行字符串匹配来程序化地进行验证。

SYSTEMYou will be provided with a document delimited by triple quotes and a question. Your task is to answer the question using only the provided document and to cite the passage(s) of the document used to answer the question. If the document does not contain the information needed to answer this question then simply write: "Insufficient information." If an answer to the question is provided, it must be annotated with a citation. Use the following format for to cite relevant passages ({"citation":}).USER"""<insert document here>""" 
Question: <insert question here>

策略:将复杂任务拆分为更简单的子任务


策略:使用意图分类来识别用户查询中最相关的指令

对于需要大量独立指令集来处理不同情况的任务,首先对查询类型进行分类,然后使用该分类确定所需的指令,这可能是有益的。这可以通过定义固定类别并将与处理给定类别中的任务相关的指令硬编码来实现。此过程也可以递归地应用于将任务分解为一系列阶段。这种方法的优势在于,每个查询将仅包含执行任务下一阶段的所需指令,这可以比使用单个查询执行整个任务降低错误率。这也可能导致成本降低,因为更大的提示需要更多的运行成本 (查看定价信息)。

例如,对于一个客户服务应用,查询可以按照以下方式有用地分类:

SYSTEMYou 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 chargeTechnical Support secondary categories:
- Troubleshooting
- Device compatibility
- Software updatesAccount Management secondary categories:
- Password reset
- Update personal information
- Close account
- Account securityGeneral Inquiry secondary categories:
- Product information
- Pricing
- Feedback
- Speak to a humanUSERI need to get my internet working again.

根据客户查询的分类,可以向模型提供一组更具体的指令,以便其处理后续步骤。例如,假设客户需要“故障排除”方面的帮助。


SYSTEMYou will be provided with customer service inquiries that require troubleshooting in a technical support context. 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:<insert primary/secondary classification scheme from above here>USERI need to get my internet working again.

请注意,模型已被指示发出特殊字符串以指示对话状态何时发生变化。这使我们能够将系统变成状态机,其中状态决定注入哪些指令。通过跟踪状态、在该状态下哪些指令相关,以及可选地允许从该状态进行哪些状态转换,我们可以为用户体验设置护栏,而这很难通过不太结构化的方法实现。


策略:对于需要非常长对话的对话应用程序,总结或过滤之前的对话

由于模型具有固定的上下文长度,因此用户和助手之间的对话(其中整个对话都包含在上下文窗口中)不能无限期地继续下去。

有多种解决方法可以解决此问题,其中之一是总结对话中的前几轮。一旦输入的大小达到预定的阈值长度,这可能会触发一个总结部分对话的查询,并且先前对话的摘要可以作为系统消息的一部分包含在内。或者,可以在整个对话过程中在后台异步总结先前的对话。

另一种解决方案是动态选择与当前查询最相关的对话的先前部分。请参阅策略 “使用基于嵌入的搜索实现高效的知识检索”。


策略:分段总结长文档并递归构建完整摘要

由于模型具有固定的上下文长度,因此它们不能用于在单个查询中总结长度超过上下文长度减去生成的摘要长度的文本。

要总结非常长的文档(例如一本书),我们可以使用一系列查询来总结文档的每个部分。可以将部分摘要连接起来并进行总结,从而生成摘要的摘要。此过程可以递归进行,直到总结整个文档。如果为了理解后面的部分而需要使用有关前面章节的信息,那么另一个有用的技巧是在总结书中任何给定点之前的内容时,包含文本的连续摘要。OpenAI 使用 GPT-3 的变体在之前的 研究 中研究了这种总结书籍的程序的有效性。


策略:给模型时间“思考”


策略:在匆忙得出结论之前指示模型自己找出解决方案

有时,当我们明确指示模型在得出结论之前从第一原理推理时,我们会得到更好的结果。例如,假设我们想要一个模型来评估学生对数学问题的解决方案。解决这个问题的最明显方法是简单地询问模型学生的解决方案是否正确。


SYSTEMDetermine if the student's solution is correct or not.USERProblem Statement: I'm building a solar power installation and I need help working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost me a flat $100k per year, and an additional $10 / square foot
What is the total cost for the first year of operations as a function of the number of square feet.Student's Solution: Let x be the size of the installation in square feet.
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000ASSISTANTThe student's solution is correct.

但这位学生的解决方案其实并不正确!我们可以通过提示模型先生成自己的解决方案来让模型成功注意到这一点。


SYSTEMFirst work out your own solution to the problem. Then compare your solution to the student's solution and evaluate if the student's solution is correct or not. Don't decide if the student's solution is correct until you have done the problem yourself.USERProblem Statement: I'm building a solar power installation and I need help working out the financials.
- Land costs $100 / square foot
- I can buy solar panels for $250 / square foot
- I negotiated a contract for maintenance that will cost me a flat $100k per year, and an additional $10 / square foot
What is the total cost for the first year of operations as a function of the number of square feet.Student's Solution: Let x be the size of the installation in square feet.
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 100x
Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000ASSISTANT
Let x be the size of the installation in square feet.
1. Land cost: 100x
2. Solar panel cost: 250x
3. Maintenance cost: 100,000 + 10x
Total cost: 100x + 250x + 100,000 + 10x = 360x + 100,000The student's solution is incorrect. They made an error in the maintenance cost calculation, using 100x instead of 10x. The correct total cost for the first year of operations is 360x + 100,000.

策略:使用内心独白或一系列查询来隐藏模型的推理过程

上一个策略表明,在回答特定问题之前,模型对问题进行详细推理有时很重要。对于某些应用程序,模型用来得出最终答案的推理过程不适合与用户共享。例如,在辅导应用程序中,我们可能希望鼓励学生自己找出答案,但模型对学生解决方案的推理过程可能会向学生透露答案。

内心独白是一种可以用来缓解这种情况的策略。内心独白的理念是指示模型将输出中本应对用户隐藏的部分放入结构化格式中,以便于解析它们。然后在将输出呈现给用户之前,对输出进行解析,并且只显示部分输出。


SYSTEMFollow these steps to answer the user queries.Step 1 - First work out your own solution to the problem. Don't rely on the student's solution since it may be incorrect. Enclose all your work for this step within triple quotes (""").Step 2 - Compare your solution to the student's solution and evaluate if the student's solution is correct or not. Enclose all your work for this step within triple quotes (""").Step 3 - If the student made a mistake, determine what hint you could give the student without giving away the answer. Enclose all your work for this step within triple quotes (""").Step 4 - If the student made a mistake, provide the hint from the previous step to the student (outside of triple quotes). Instead of writing "Step 4 - ..." write "Hint:".USERProblem Statement: <insert problem statement>Student Solution: <insert student solution>

或者,这可以通过一系列查询来实现,其中除最后一个查询之外的所有查询的输出都对最终用户隐藏。

首先,我们可以要求模型自行解决问题。由于这个初始查询不需要学生的解决方案,因此可以省略它。这提供了额外的优势,即模型的解决方案不会受到学生尝试的解决方案的影响。


USER<insert problem statement>

接下来,我们可以让模型使用所有可用的信息来评估学生解决方案的正确性。


SYSTEMCompare your solution to the student's solution and evaluate if the student's solution is correct or not.USERProblem statement: """<insert problem statement>"""Your solution: """<insert model generated solution>"""Student’s solution: """<insert student's solution>"""

最后,我们可以让模型利用自己的分析,以乐于助人的导师的身份构建答复。


SYSTEMYou are a math tutor. If the student made an error, offer a hint to the student in a way that does not reveal the answer. If the student did not make an error, simply offer them an encouraging comment.USERProblem statement: """<insert problem statement>"""Your solution: """<insert model generated solution>"""Student’s solution: """<insert student's solution>"""Analysis: """<insert model generated analysis from previous step>"""

策略:询问模型是否在之前的遍历中遗漏了任何内容

假设我们正在使用一个模型从源文档中列出与特定问题相关的摘录。在列出每个摘录后,模型需要确定是否应该开始编写另一个,还是应该停止。如果源文档很大,模型可能会过早地停止,从而未能列出所有相关的摘录。在这种情况下,通过向模型提出后续查询以找到之前遍历中遗漏的摘录,通常可以获得更好的性能。

SYSTEMYou will be provided with a document delimited by triple quotes. Your task is to select excerpts which pertain to the following question: "What significant paradigm shifts have occurred in the history of artificial intelligence."Ensure that excerpts contain all relevant context needed to interpret them - in other words don't extract small snippets that are missing important context. Provide output in JSON format as follows:[{"excerpt": "..."},
...
{"excerpt": "..."}]USER
"""<insert document here>"""ASSISTANT
[{"excerpt": "the model writes an excerpt here"},
...
{"excerpt": "the model writes another excerpt here"}]USER
Are there more relevant excerpts? Take care not to repeat excerpts. Also ensure that excerpts contain all relevant context needed to interpret them - in other words don't extract small snippets that are missing important context.

策略:使用外部工具


策略:使用基于嵌入的搜索实现高效的知识检索

如果外部信息源作为输入的一部分,模型可以利用这些信息源。这可以帮助模型生成更明智和最新的响应。例如,如果用户询问有关特定电影的问题,将有关该电影的高质量信息(例如演员、导演等)添加到模型的输入中可能会很有用。嵌入可用于实现高效的知识检索,以便可以在运行时动态地将相关信息添加到模型输入中。

文本嵌入是一个可以测量文本字符串之间相关性的向量。相似或相关的字符串将比不相关的字符串更接近。这一事实,加上快速向量搜索算法的存在,意味着嵌入可用于实现高效的知识检索。具体而言,文本语料库可以分成块,每个块都可以嵌入和存储。然后可以嵌入给定的查询,并执行向量搜索以从语料库中找到与查询最相关的嵌入文本块(即在嵌入空间中最接近的文本块)。

可以在 OpenAI Cookbook 中找到示例实现。请参阅策略 “指示模型使用检索到的知识来回答查询”,了解如何使用知识检索来最大限度地降低模型编造错误事实的可能性的示例。


策略:使用代码执行来执行更准确的计算或调用外部 API

语言模型不能依靠自己准确地执行算术或长时间计算。在需要的情况下,可以指示模型编写和运行代码,而不是自己进行计算。具体来说,可以指示模型将要运行的代码放入指定的格式(例如三重反引号)。生成输出后,可以提取并运行代码。最后,如果需要,可以将代码执行引擎(即 Python 解释器)的输出作为模型的输入,以供下一个查询使用。


SYSTEMYou can write and execute Python code by enclosing it in triple backticks, e.g. ```code goes here```. Use this to perform calculations.USERFind all real-valued roots of the following polynomial: 3*x**5 - 5*x**4 - 3*x**3 - 7*x - 10.

代码执行的另一个好用例是调用外部 API。如果模型被指导如何正确使用 API,它就可以编写利用该 API 的代码。可以通过向模型提供文档和/或代码示例来展示如何使用 API,从而指导模型如何使用 API。


SYSTEMYou can write and execute Python code by enclosing it in triple backticks. Also note that you have access to the following module to help users send messages to their friends:\```python
import message
message.write(to="John", message="Hey, want to meetup after work?")```

警告:执行模型生成的代码并非天生安全,任何试图执行此操作的应用程序都应采取预防措施。特别是,需要沙盒代码执行环境来限制不受信任的代码可能造成的危害。


策略:系统地测试更改

有时很难判断更改(例如新指令或新设计)是使您的系统变得更好还是更糟。查看几个示例可能会提示哪个更好,但由于样本量较小,很难区分真正的改进还是随机运气。也许更改有助于提高某些输入的性能,但会损害其他输入的性能。

评估程序(或“评估”)对于优化系统设计很有用。好的评估是:

  • 代表现实世界的使用情况(或至少是多样化的)
  • 包含许多测试用例以获得更大的统计能力(请参阅下表了解指南)
  • 易于自动化或重复
Difference to detectSample size needed for 95% confidence
30%~10
10%~100
3%~1,000
1%~10,000

输出评估可以由计算机、人类或两者混合完成。计算机可以使用客观标准(例如,只有一个正确答案的问题)以及一些主观或模糊标准自动进行评估,其中模型输出由其他模型查询进行评估。OpenAI Evals 是一个开源软件框架,提供用于创建自动评估的工具。

当存在一系列可能的输出被认为质量同样高时(例如,对于答案较长的问题),基于模型的评估会很有用。使用基于模型的评估可以实际评估的内容与需要人工评估的内容之间的界限是模糊的,并且随着模型变得越来越强大而不断变化。我们鼓励进行实验,以了解基于模型的评估对您的用例的效果如何。


战术:将模型输出与黄金标准答案进行比较

假设已知一个问题的正确答案应参考一组已知事实。然后我们可以使用模型查询来统计答案中包含所需事实的数量。

例如,使用以下系统消息:

SYSTEMYou will be provided with text delimited by triple quotes that is supposed to be the answer to a question. Check if the following pieces of information are directly contained in the answer:
- Neil Armstrong was the first person to walk on the moon.
- The date Neil Armstrong first walked on the moon was July 21, 1969.For each of these points perform the following steps:1 - Restate the point.
2 - Provide a citation from the answer which is closest to this point.
3 - Consider if someone reading the citation who doesn't know the topic could directly infer the point. Explain why or why not before making up your mind.
4 - Write "yes" if the answer to 3 was yes, otherwise write "no".Finally, provide a count of how many "yes" answers there are. Provide this count as {"count": <insert count here>}.

以下是一个示例输入,其中两个条件都得到了满足:

SYSTEM<insert system message above>USER"""Neil Armstrong is famous for being the first human to set foot on the Moon. This historic event took place on July 21, 1969, during the Apollo 11 mission."""

以下是一个示例输入,其中只有一个条件得到满足:

SYSTEM<insert system message above>USER"""Neil Armstrong made history when he stepped off the lunar module, becoming the first person to walk on the moon."""

这里是一个示例输入,没有任何一个条件得到满足:

SYSTEM<insert system message above>USER"""In the summer of '69, a voyage grand,
Apollo 11, bold as legend's hand.
Armstrong took a step, history unfurled,
"One small step," he said, for a new world."""

有许多可能的变体可以用于这种基于模型的评估。考虑以下变体,它跟踪候选答案和黄金标准答案之间的重叠程度,并跟踪候选答案是否与黄金标准答案的任何部分相矛盾。

SYSTEMUse the following steps to respond to user inputs. Fully restate each step before proceeding. i.e. "Step 1: Reason...".Step 1: Reason step-by-step about whether the information in the submitted answer compared to the expert answer is either: disjoint, equal, a subset, a superset, or overlapping (i.e. some intersection but not subset/superset).Step 2: Reason step-by-step about whether the submitted answer contradicts any aspect of the expert answer.Step 3: Output a JSON object structured like: {"type_of_overlap": "disjoint" or "equal" or "subset" or "superset" or "overlapping", "contradiction": true or false}

以下是示例输入,其中包含了一个不合格的答案,但这个答案并没有与专家答案相矛盾:

SYSTEM<insert system message above>USERQuestion: """What event is Neil Armstrong most famous for and on what date did it occur? Assume UTC time."""Submitted Answer: """Didn't he walk on the moon or something?"""Expert Answer: """Neil Armstrong is most famous for being the first person to walk on the moon. This historic event occurred on July 21, 1969."""

以下是一个示例输入,其中包含与专家答案直接矛盾的答案:

SYSTEM<insert system message above>USERQuestion: """What event is Neil Armstrong most famous for and on what date did it occur? Assume UTC time."""Submitted Answer: """On the 21st of July 1969, Neil Armstrong became the second person to walk on the moon, following after Buzz Aldrin."""Expert Answer: """Neil Armstrong is most famous for being the first person to walk on the moon. This historic event occurred on July 21, 1969."""

以下是一个示例输入,其中包含了一个正确的答案,并且还提供了一些不必要的细节:

SYSTEM<insert system message above>USERQuestion: """What event is Neil Armstrong most famous for and on what date did it occur? Assume UTC time."""Submitted Answer: """At approximately 02:56 UTC on July 21st 1969, Neil Armstrong became the first human to set foot on the lunar surface, marking a monumental achievement in human history."""Expert Answer: """Neil Armstrong is most famous for being the first person to walk on the moon. This historic event occurred on July 21, 1969."""

优化模型输出

随着您对提示的迭代,您将不断致力于提高 准确性成本延迟。以下是一些优化每个目标的技巧。

目标可用技巧
准确性确保模型能够对您的提示产生准确和有用的响应。准确的响应需要模型拥有生成响应所需的所有信息,并且知道如何进行响应的创建(从解释输入到格式化和样式)。通常,这需要结合 提示工程、RAG 和 模型微调 的方法。了解更多关于优化准确性的信息。
成本通过减少令牌使用量和使用可能的情况下更便宜的模式来降低使用模型的总成本。为了控制成本,您可以尝试使用更少的令牌或更小、更便宜的模型。了解更多关于优化成本的信息。
延迟减少对您的提示生成响应所需的时间。优化低延迟是一个多方面的过程,包括提示工程和您自己的代码中的并行性。了解更多关于优化延迟的信息。

其他资源

如需更多灵感,请访问 OpenAI Cookbook,其中包含示例代码以及第三方资源的链接,例如:

  • Prompting libraries & tools
  • Prompting guides
  • Video courses
  • Papers on advanced prompting to improve reasoning

2025-03-29(六)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/43301.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[MySQL] 库的操作 表的操作

1.库的操作 1.创建数据库 这里就是一个创建数据库的例子&#xff0c;框内的东西可以不填&#xff0c;因为有默认设置&#xff0c;而这些东西是什么呢&#xff1f; 2.字符集和校验规则 2.1查看字符集校验规则 show variables like ‘character_set_database’; show variable…

Let’s Encrypt 宣布推出短期证书与 IP 地址支持,推动 Web 安全迈向新高度

2025 年 1 月 16 日&#xff0c;全球领先的免费 SSL/TLS 证书颁发机构 Let’s Encrypt 正式宣布两项重大功能更新计划&#xff1a;推出六天有效期证书&#xff08;Short-Lived Certificates&#xff09;及支持以 IP 地址为主体的证书申请。两项功能将于 2025 年起陆续开放&…

十二、Cluster集群

目录 一、集群简介1、现状问题2、集群作用 二、集群结构设计1、集群存储设2、消息通信设计 三、Cluster集群三主三从结构搭建1、redis.conf配置文件可配置项2、配置集群3、链接集群4、命令客户端连接集群并使用 四、集群扩容1、添加节点2、槽位分配3、添加从节点 五、集群缩容1…

Linux进程管理之子进程的创建(fork函数)、子进程与线程的区别、fork函数的简单使用例子、子进程的典型应用场景、父进程等待子进程结束后自己再结束

收尾 进程终止&#xff1a;子进程通过exit()或_exit()终止&#xff0c;父进程通过wait()或waitpid()等待子进程终止&#xff0c;并获取其退出状态。&#xff1f;其实可以考虑在另一篇博文中来写 fork函数讲解 fork函数概述 fork() 是 Linux 中用于创建新进程的系统调用。当…

【AI论文】挑战推理的边界:大型语言模型的数学基准测试

摘要&#xff1a;近年来&#xff0c;大型推理模型的迅猛发展导致现有用于评估数学推理能力的基准测试趋于饱和&#xff0c;这凸显出迫切需要更具挑战性和严谨性的评估框架。为填补这一空白&#xff0c;我们推出了OlymMATH&#xff0c;这是一项全新的奥林匹克级数学基准测试&…

典范硬币系统(Canonical Coin System)→ 贪心算法

【典范硬币系统】 ● 典范硬币系统&#xff08;Canonical Coin System&#xff09;是指使用贪心算法总能得到最少硬币数量解‌的货币面值组合‌。 ● 给定一个硬币系统 &#xff0c;若使其为典范硬币系统&#xff0c;则要求其各相邻面值比例 &#xff0c;及各开区间 内各金额…

Android7 Input(二)Linux 驱动层输入事件管理

概述 在Linux系统中&#xff0c;将键盘&#xff0c;鼠标&#xff0c;触摸屏等这类交互设备交由Linux Input子系统进行管理&#xff0c;Linux Input驱动子系统由于具有良好的和用户空间交互的接口。因此Linux Input驱动子系统&#xff0c;不止于只管理输入类型的设备。也可以将其…

高清壁纸一站式获取:海量分类,免费无弹窗

软件介绍 在如今这个追求个性化与高品质视觉体验的时代&#xff0c;一款出色的壁纸应用无疑能为我们的电子设备增添别样魅力。此刻&#xff0c;要给大家重磅推荐的便是Wallpaper这款应用&#xff0c;它犹如一个绚丽多彩的壁纸宝库&#xff0c;全方位满足你的审美需求。 海量壁…

Linux安装Cmake (Centos 7.9)

cmake安装 这个虽然已经更新到了4.0.0版本了&#xff0c;但是我们要用3.5版本的&#xff0c;因为这个比较稳定 官方地址&#xff1a;https://github.com/Kitware/CMake/releases/tag/v3.5.0&#xff0c;选择那个cmake-3.5.0-Linux-x86_64.tar.gz下载&#xff0c; 首先解压文…

Centos7,tar包方式部署rabbitmq-3.7.6

1. 环境准备 安装编译工具和依赖包 yum -y install make gcc gcc-c glibc-devel m4 perl openssl openssl-devel ncurses-devel ncurses-devel xz xmlto perl 2. Erlang环境搭建 版本对应&#xff1a;https://www.rabbitmq.com/docs/which-erlang 解压到指定目录 tar -xv…

【MySQL篇】事务管理,事务的特性及深入理解隔离级别

目录 一&#xff0c;什么是事务 二&#xff0c;事务的版本支持 三&#xff0c;事务的提交方式 四&#xff0c;事务常见操作方式 五&#xff0c;隔离级别 1&#xff0c;理解隔离性 2&#xff0c;查看与设置隔离级别 3&#xff0c;读未提交&#xff08;read uncommitted&a…

C++Primer学习(14.1 基本概念)

当运算符作用于类类型的运算对象时&#xff0c;可以通过运算符重载重新定义该运算符的含义。明智地使用运算符重载能令我们的程序更易于编写和阅读。举个例子&#xff0c;因为在Sales_item类中定义了输入、输出和加法运算符&#xff0c;所以可以通过下述形式输出两个Sales_item…

循相似之迹:解锁协同过滤的核心推荐逻辑

目录 一、引言二、协同过滤的基本原理三、协同过滤的算法类型&#xff08;一&#xff09;基于用户的协同过滤&#xff08;二&#xff09;基于物品的协同过滤 四、协同过滤的应用案例&#xff08;一&#xff09;电商平台的商品推荐&#xff08;二&#xff09;音乐平台的歌曲推荐…

RuoYi基础学习

1 若依搭建 前后端分离版本&#xff1a;RuoYi-Vue利用SpringBoot作为后端开发框架&#xff0c;与Vue.js结合&#xff0c;实现了前后端分离的开发模式。这种架构有助于提高开发效率&#xff0c;前后端可以独立开发和部署&#xff0c;更适合现代化的Web应用开发。 RuoYi-Vue3&a…

Docker 安装部署Harbor 私有仓库

Docker 安装部署Harbor 私有仓库 系统环境:redhat x86_64 一、首先部署docker 环境 定制软件源 wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repoyum install -y yum-utils device-mapper-persistent-data lvm2…

【Basys3】外设-灯和数码管

灯 约束文件 set_property PACKAGE_PIN W5 [get_ports CLK] set_property PACKAGE_PIN U18 [get_ports rst] set_property PACKAGE_PIN U16 [get_ports {led[0]}] set_property PACKAGE_PIN E19 [get_ports {led[1]}] set_property PACKAGE_PIN U19 [get_ports {led[2]}] set…

【Django】教程-1-安装+创建项目+目录结构介绍

欢迎关注我&#xff01;后续会更新django教程。一周2-3更&#xff0c;欢迎跟进&#xff0c;本周会更新第一个Demo的单独一个模块的增删改查【Django】教程-4-一个增删改查的Demo【Django】教程-2-前端-目录结构介绍【Django】教程-3-数据库相关介绍 1.项目创建 1.1 安装 Djan…

蓝桥杯 之 二分

文章目录 习题肖恩的n次根分巧克力2.卡牌 二分是十分重要的一个算法&#xff0c;常常用于求解一定范围内&#xff0c;找到满足条件的边界值的情况主要分为浮点数二分和整数二分二分问题&#xff0c;最主要是写出这个check函数&#xff0c;这个check函数最主要就是使用模拟的方法…

SpringBoot集成腾讯云OCR实现身份证识别

OCR身份证识别 官网地址&#xff1a;https://cloud.tencent.com/document/product/866/33524 身份信息认证&#xff08;二要素核验&#xff09; 官网地址&#xff1a;https://cloud.tencent.com/document/product/1007/33188 代码实现 引入依赖 <dependency><…