0

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.

Rubén
  • 34,714
  • 9
  • 70
  • 166
neves
  • 796
  • 2
  • 10
  • 36
  • 1
    `Sys.sleep()` is not used for increasing processing time! But from [the source codes](https://github.com/jcrodriguez1989/chatgpt) of this package, we cannot say it limits any output. Maybe you could try using `cat()` instead of `print()` just as what the README used. For any more questions, you can also try filing an issue [there](https://github.com/jcrodriguez1989/chatgpt/issues/new). – Liang Zhang Feb 11 '23 at 05:26
  • 1
    I find it a bad faith decision that the authors called this package `chatgpt`. It has nothing to do with that service really. It is simply a wrapper for the openai API using the `text-davinci-003` model. ChatGPT itself has some additional tuning and also keeps old prompts in mind (which this package can not bc the API does not support that). – JBGruber Feb 15 '23 at 12:18
  • Please be more specific about what you mean by "the `chatgpt` package in R". Also consider to change the title as it looks to be misleading for the reasons mentione in the previous comments. – Rubén Feb 15 '23 at 15:55

2 Answers2

4

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

Kane Hooper
  • 1,531
  • 1
  • 9
  • 21
  • Thanks for the answer. But unfortunately an error occurred. Look: `[1] "You did not provide an API key. You need to provide your API key in an authorization header using bearer authentication (i.e. authorization: bearer YOUR_KEY), or as the password field (with username blank) if you are accessing the API from your browser and are asked to provide a username and password. You can obtain an API key at https://platform.openai.com/account/api-keys."` – neves Feb 12 '23 at 02:26
  • 1
    Well, you need to set the API key. The github repo has some instructions on where to get it and how to load it into R: https://github.com/jcrodriguez1989/chatgpt – JBGruber Feb 15 '23 at 12:19
  • The term "prompt engineering" is way too broad (ref https://en.wikipedia.org/wiki/Prompt_engineering) In this specific case it looks to be more appropiate to used "prompt design" ref, https://platform.openai.com/docs/guides/completion/prompt-design – Rubén Feb 15 '23 at 16:03
  • 1
    I have generated the API token but I cant get the code to work for some reason, and get the same error message as the OP – Chamkrai Feb 25 '23 at 10:03
1

Increase max_tokens to get longer answer.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31