Best Practices to Use OpenAI GPT Model

The improvement you need to get the best result from GPT.



Best Practices to Use OpenAI GPT Model
Image by rawpixel.com on Freepik

 

Since the release of the GPT model, everyone has been using them constantly. From asking simple questions to developing complex coding, the GPT model can help the user swiftly. That’s why the model would only get bigger over time.

To help users get the best output, OpenAI provides their best practice for using the GPT model. This comes from the experience as many users have experimented with this model constantly and have found what works best.

In this article, I will summarize the best practices you should know for using the OpenAI GPT model. What are these practices? Let’s get into it.

 

GPT Best Practices

 

GPT model output is only as good as your prompt. With definite instructions for what you want, it would provide the result you expected. A few tips to improve your GPT output include:

  1. Have a detail in the prompt to get relevant answers. For example, instead of the prompt “Give me code to calculate normal distribution”, we can write “Provide me with the standard distribution calculation with the code example in Python. Place a comment in each section and explain why every code is executed that way.
  1. Give a persona or example, plus add the length of the output. We can bring a persona or example to the model for better clarity. For example, we can pass the system role parameter to explain something in a way that the teacher would explain things to the students. By providing persona, the GPT model would bring results in a way that we require. Here is a sample code if you want to change the persona.
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.
             
          """,
        },
    ],
)

 

It’s also great to provide example results to direct how the GPT model should answer your questions. For example, in this code, I pass how I would explain emotion, and the GPT model should mimic my style.

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. Specify the steps to complete your tasks. Provide detailed steps on how you want the output for the best output. Give a detailed breakdown of the instruction on how the GPT model should act. For example, we put 2-step instructions with prefixes and translations in this code.
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. Provide references, links or citations. If we already have various references for our questions, we can use them as the basis for the GPT model to provide the output. Give the list of any references you think are relevant to your questions and pass them into the system role. 
  1. Give GPT time to “think”. Provide a query allowing GPT to process the prompt in detail before rushing to give incorrect results. This is especially true if we pass the assistant role a wrong result, and we want the GPT to be able to think critically for themselves. For example, the code below shows how we ask the GPT model to be more critical of the user input.
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. Bring GPT to use Code Execution for precise results. For more extended and more complex calculations, GPT might not work as intended, as the model might provide inaccurate results. To alleviate this, we can ask the GPT model to write and run coding rather than directly calculating them. This way, GPT can rely on the code rather than its calculation. For example, we can provide input like below.
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.
          
          """,
         },
        ])

 

Conclusion

 

The GPT model is one of the best models out there, and here are some best practices to improve the GPT model output:

  1. Have a detail in the prompt to get relevant answers
  2. Give a persona or example, plus add the length of the output
  3. Specify the steps to complete your tasks
  4. Provide references, links or citations
  5. Give GPT time to “think”
  6. Bring GPT to use Code Execution for precise results

 
 
Cornellius Yudha Wijaya is a data science assistant manager and data writer. While working full-time at Allianz Indonesia, he loves to share Python and Data tips via social media and writing media.