Search code examples
ropenai-apigpt-3

OpenAI API: Can I remove the line break from the response with a parameter?


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?


Solution

  • 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
      }
    }