简体   繁体   中英

`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?

The index refers to the index of the prompt the reply is for.

Since prompt can be an string or an array, the documentation refers to it as:

prompt string or array

The prompt(s) to generate completions for, encoded as a string , array of strings , array of tokens, or array of token arrays.


As example, consider calling the /completions endpoint with 2 prompt 's:

{
    "model": "text-davinci-003",
    "prompt": [ 
      "How should I",                            // [ 0 ]
      "Should I"                                 // [ 1 ] 
    ],
    "max_tokens": 7,
    "temperature": 0
}

OpenAI will answer to both those prompts, and the index will refer to what original index in the prompt the reply is for:

{
    "choices": [
        {
            "text": " go about doing this?\n\n",
            "index": 0,                              // Answers prompt[0]
            "logprobs": null,
            "finish_reason": "length"
        },
        {
            "text": " use a VPN on my iPhone?",
            "index": 1,                              // Answers prompt[1]
            "logprobs": null,
            "finish_reason": "length"
        }
    ]
}

In your example, there is only 1 prompt , so the API will only send 1 object in the choices array, so the index will always be 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM