Search code examples
rchatbotopenai-apigpt-3

How to avoid word limit in OpenAI API in R?


I registered at this link to get a key for the OpenAI API.

And I used the "chatgpt" package in R.

library(chatgpt)

And set environment:

Sys.setenv(OPENAI_API_KEY = "sk-YOURAPI")

I used this function:

chatgpt::ask_chatgpt("How to make a strawberry pie to donate to my wife? Ingredients, please.")

Sometimes the output is missing words. For example:

*** ChatGPT input:

How to make a strawberry pie to donate to my wife? Ingredients, please.
[1] ... and your wife is"

It does not complete the text after is. Is there any solution for this on R?

Similar question on reddit.

EDIT

I tried increasing the processing time of the function (so that the chat would finish typing all the content, without stopping midway). Like this:

for (i in 35) {

  print(chatgpt::ask_chatgpt("How to make a strawberry pie to donate to my wife? Ingredients, please."))

  Sys.sleep(i)

}

Editors note: Prior March 1st, 2023 there was no official ChatGPT API.


Solution

  • What you are running into is prompt engineering. GPT is a sophisticated autocomplete engine.

    If you are not getting the right response you need to recraft your prompt.

    You can always test your prompts in the OpenAI playground: https://platform.openai.com/playground

    I was able to get a list of ingredients and cooking steps by using the following prompt:

    How can I make a strawberry pie to donate to my wife? Please provide first a numbered list of ingredients, and secondly a numbered lists of steps.

    Here is the output I got in the playground:

    Ingredients:
    1. 2 ½ cups of fresh or frozen strawberries
    2. 1 9-inch pre-made pie crust
    3. ¾ cup of granulated sugar
    4. 2 tablespoons of cornstarch
    5. ¼ teaspoon of salt
    6. 1 tablespoon of fresh lemon juice
    
    Steps:
     1. Preheat oven to 425 degrees F.
     2. Place the pre-made pie crust in a 9-inch pie dish and set aside.
     3. In a medium bowl, combine the strawberries, sugar, cornstarch, salt, and lemon juice. Stir until the mixture is combined.
     4. Pour the strawberry mixture into the pre-made pie crust.
     5. Place the pie dish on a baking sheet and bake for 15 minutes.
     6. Reduce the oven temperature to 375 degrees F and bake for an additional 25 minutes.
     7. Allow the pie to cool completely before serving.
    

    Another thing to note, per the Github repo for the chatgpt R library it says "The {chatgpt} R package provides a set of features to assist in R coding."

    Ref: https://github.com/jcrodriguez1989/chatgpt

    I would use the OpenAI APIs directly, this way you will have a lot more control over your response. I am not an R specialist, but this is how the OpenAI Playground showed me how to do it.

    library(httr)
    
    response <- GET("https://api.openai.com/v1/completions", 
       query = list(
          prompt = "How can I make a strawberry pie to donate to my wife? Please provide first a numbered list of ingredients, and secondly a numbered lists of steps.",
          max_tokens = 200,
          model = 'text-davinci-003'
       ),
       add_headers(Authorization = "bearer YOUR_OPENAI_API_KEY")
    )
    
    content(response)
    

    Ref: OpenAI playground