`index` in OpenGPT API output

The following is the code

import os
import openai


openai.api_key = "..."


response = openai.Completion.create(
  model="text-davinci-003",     
  prompt="I am happy!",
  temperature=0, #creativity
  max_tokens=10,
  top_p=1,
  frequency_penalty=0.0,
  presence_penalty=0.0,
  suffix='I am even more happy!'
)

print(response)

Following is the output

{
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "logprobs": null,
      "text": "\n\nI am happy because I am surrounded by"
    }
  ],
  "created": 1674640360,
  "id": "cmpl-6cWkK124234ho8C2134afasdasdnwDKLUMP",
  "model": "text-davinci-003",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 10,
    "prompt_tokens": 10,
    "total_tokens": 20
  }
}

What does the index in above following output represent?

2 Likes

“Index”: 0 refers to the position of the first result of the OpenAI API output.
as follows: [‘choices’][0][‘text’], with this index you take the response of the request precisely.

2 Likes

To elaborate on @guimaraesabri’s response

When you send n in the request (How many completions to generate for each prompt - default is 1) to greater than 1, the response will contain that many (n) choices.

So you send n=2 there will be 2 choices (choice object in the choices array) in the response, and the index represents the position (index) of the current choice in the array

2 Likes

We are going to add to the docs a full description of the response objects, right now we just document the input parameters so I hope this will help others with the same question!

2 Likes

It will help a lot indeed

1 Like