使用 OpenAI GPT 模型的最佳实践

发布时间 2023-08-23 16:34:29作者: tuyg

推荐:使用NSDT场景编辑器助你快速搭建可二次编辑的3D应用场景

为了帮助用户获得最佳输出,OpenAI 提供了使用 GPT 模型的最佳实践。这来自体验,因为许多用户不断尝试使用此模型并找到了最有效的方法。

在本文中,我将总结使用 OpenAI GPT 模型应了解的最佳实践。这些做法是什么?让我们进入它。

GPT 最佳做法

GPT 模型输出仅与您的提示一样好。通过对您想要的内容的明确说明,它将提供您期望的结果。提高 GPT 输出的一些提示包括:

  1. 在提示中提供详细信息以获取相关答案。例如,我们可以写“通过 Python 中的代码示例为我提供标准分布计算”,而不是提示“给我代码来计算正态分布”。在每个部分中放置注释,并解释为什么每个代码都以这种方式执行。
  1. 给出一个角色或示例,并添加输出的长度。 我们可以将角色或示例引入模型,以便更清晰。例如,我们可以传递系统角色参数,以教师向学生解释的方式解释某些内容。通过提供角色,GPT 模型将以我们需要的方式带来结果。如果要更改角色,下面是一个示例代码。
import openai

openai.api_key = ""

res = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    max_tokens=100,
    temperature=0.7,
    messages=[
        {
            "role": "system",
            "content": """
          
          When I ask to explain something, bring it in a way that teacher 
          would explain it to students in every paragraph.
          
          """,
        },
        {
            "role": "user",
            "content": """
          
         What is golden globe award and what is the criteria for this award? Summarize them in 2 paragraphs.
             
          """,
        },
    ],
)

提供示例结果来指导 GPT 模型应如何回答您的问题也很棒。例如,在此代码中,我传递了如何解释情绪,GPT 模型应模仿我的风格。

res = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    max_tokens=100,
    temperature=0.7,
    messages=[
        {
            "role": "system",
            "content": "Answer in a consistent style.",
        },
        {
            "role": "user",
            "content": "Teach me about Love",
        },
        {
            "role": "assistant",
            "content": "Love can be sweet, can be sour, can be grand, can be low, and can be anything you want to be",
        },
        {
            "role": "user",
            "content": "Teach me about Fear",
        },
    ],
)
  1. 指定完成任务的步骤。 提供有关如何输出以获得最佳输出的详细步骤。详细说明 GPT 模型应如何操作的说明。例如,我们在此代码中放置了带有前缀和翻译的两步指令。
res = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        max_tokens=100,
        temperature=0.7,
        messages= [
            {
          "role": "system",
          "content": """
          Use the following step-by-step instructions to respond to user inputs.

        step 1 - Explain the question input by the user in 2 paragraphs or less with the prefix "Explanation: ".

        Step 2 - Translate the Step 1 into Indonesian, with a prefix that says "Translation: ".
          
          """,
         },
         {
          "role": "user",
          "content":"What is heaven?",
         },
        ])
  1. 提供参考、链接或引用。如果我们已经有各种问题参考,我们可以将它们用作 GPT 模型的基础来提供输出。提供您认为与您的问题相关的任何参考文献的列表,并将它们传递给系统角色。
  1. 给 GPT 时间“思考”。提供一个查询,允许 GPT 在急于给出不正确的结果之前详细处理提示。如果我们给助理角色传递错误的结果,并且我们希望 GPT 能够自己批判性地思考,则尤其如此。例如,下面的代码显示了我们如何要求 GPT 模型对用户输入更加严格。
res = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        max_tokens=100,
        temperature=0.7,
        messages= [
            {
          "role": "system",
          "content": """

        Work on your solution to the problem, then compare your solution to the user and evaluate
        if the solution is correct or not. Only decide if the solution is correct once you have done the problem yourself.
          
          """,
         }, 
         {
          "role": "user",
          "content":"1 + 1 = 3",
         },
        ])
  1. 使 GPT 使用代码执行来获得精确的结果。 对于更扩展和更复杂的计算,GPT 可能无法按预期工作,因为模型可能会提供不准确的结果。为了缓解这种情况,我们可以要求 GPT 模型编写和运行编码,而不是直接计算它们。这样,GPT 可以依赖代码而不是其计算。例如,我们可以提供如下输入。
res = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        max_tokens=100,
        temperature=0.7,
        messages= [
            {
          "role": "system",
          "content": """

      Write and execute Python code by enclosing it in triple backticks,
       e.g. ```code goes here```. Use this to perform calculations.
          
          """,
         }, 
         {
          "role": "user",
          "content":"""
          
          Find all real-valued roots of the following polynomial equation: 2*x**5 - 3*x**8- 2*x**3 - 9*x + 11.
          
          """,
         },
        ])

结论

GPT 模型是目前最好的模型之一,以下是改进 GPT 模型输出的一些最佳实践:

  1. 在提示中提供详细信息以获取相关答案
  2. 给出一个角色或示例,并添加输出的长度
  3. 指定完成任务的步骤
  4. 提供参考、链接或引用
  5. 给 GPT 时间“思考”
  6. 使 GPT 使用代码执行来获得精确的结果

原文链接:使用 OpenAI GPT 模型的最佳实践 (mvrlink.com)