3

I've starting using OpenAI API in R. I downloaded the openai package. I keep getting a double linebreak in the text response. Here's an example of my code:


library(openai)

vector = create_completion(
  model = "text-davinci-003",
  prompt = "Tell me what the weather is like in London, UK, in Celsius in 5 words.",
  max_tokens = 20,
  temperature = 0,
  echo = FALSE
)


vector_2 = vector$choices[1]

vector_2$text


[1] "\n\nRainy, mild, cool, humid."

Is there a way to get rid of this without 'correcting' the response text using other functions?

Rok Benko
  • 14,265
  • 2
  • 24
  • 49
James
  • 463
  • 4
  • 13

3 Answers3

3

No, it's not possible.

The OpenAI API returns the completion with a starting \n\n by default. There's no parameter for the Completions endpoint to control this.

You need to remove the line break manually.

An example response looks like this:

{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "\n\nThis is indeed a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 7,
    "total_tokens": 12
  }
}
Rok Benko
  • 14,265
  • 2
  • 24
  • 49
0

There is away to take out the line breaks in the openai response. Add this param to your function

stop=["\n"]

 completion = openai.Completion.create(
    engine="text-davinci-002",
    prompt=prompt,
    max_tokens=1024,
    n=1,
    stop=["\n"],
    temperature=0.7,
)

This will take out the line breaks that come with the response. Hope that helps!

CryptoDevWill
  • 109
  • 1
  • 2
0

You should add a restart sequence for the language model to know better how to begin with its answer. The restart sequence may one escaped line break (\n) or a white space at the very least.

As the model is completing, it does not want to add the text immediately after the input text, as an answer is not usually appended to the same line of a question.

OBS: Bots, pls stop saying this answer could be improved, of course it could but I don't care. Go and improve it yourself, stupid bot!

  • 3
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 01 '23 at 20:37
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 03 '23 at 10:05