-1

Hi all I was facing trouble when trying to read JSON data in python. Here is the JSON format result that I have. The following includes the error that I had occured in the console. The data that i would like to retrieve is response['choices']['text']

Error

print(response['choices']['text'])
TypeError: list indices must be integers or slices, not str

Command Code:

print(response['choices']['text'])

JSON Format

{
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "logprobs": null,
      "text": "\n\nThis report discusses work performed during the period January-March. A total of 2,002 evaporation me were performed under contract 12, completed during the second quarter, yielding a total of film samples ranging in size from 1-inch to 1-inch. Processing data showed that the average conversion efficiency was raised by 2 percent. The principal increase was believed to be due to the curious oxide slurry technique developed in the laboratory. A single crystal cell processed in this manner exhibited a nominal efficiency of 9 percent, nearly twice that of a crystal cell prepared prior to electroplating. The spectral transmission curve of the evaporated Al film showed the effect of an optical transmission barrier layer formed by the curious oxide slurry. Thermodynamic optical measurements were discussed, including transmission and UV behavior. A synthetic substrate material was used for solar simulator measurements in order to determine conversion efficiency. Electric measurements metioned briefly includedHall effect and conductivity measurement, added to the usual determine tions of efficiency and rect"
    }
  ],
  "created": 1664503532,
  "id": "cmpl-5vzh6uFm3SZl9UdtXRRJaU2ji39y5",
  "model": "text-davinci-002",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 200,
    "prompt_tokens": 195,
    "total_tokens": 395
  }
}
Wesley
  • 65
  • 9
  • It's hard to read this formatting. If you are trying commands at the interactive prompt, then copy and paste the entire block, including the `>>>` and `...` symbols, and format the entire thing as a single chunk of multi-line code. – Karl Knechtel Sep 30 '22 at 02:15
  • 1
    `print(response['choices'][0]['text'])`? – blhsing Sep 30 '22 at 02:17
  • Anyway, the reason that you cannot get `response['choices']['text']` is exactly as it says in the first error message: `response['choices']` is a list, and therefore does not have a `['text']` index, nor any index that is a string. Please also read [mre], and show [complete](https://meta.stackoverflow.com/questions/359146) error messages. Right now it is not clear how you are parsing the JSON - normally one would use the standard library `json` module, not anything to do with OpenAI. – Karl Knechtel Sep 30 '22 at 02:18
  • Hi thanks for the suggestions given! I had reformatting my questions really appreciate your comments given. I had manage to solve the problem and thanks for the resources provide for me to understand the concept! – Wesley Sep 30 '22 at 02:25

1 Answers1

1

@blhsing is correct: print(response['choices'][0]['text'])

rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
spamsink
  • 291
  • 1
  • 6