Separate text output from the rest of the output?

Hello, this I assume is a relatively simple question that I just can’t find an answer to. I want to separate the contents of the “text” field in the output from the rest of the output.

Output:

{
  "id": "cmpl-7V3RvHknXFbw3FRXUdyxz9YBdSlBC",
  "object": "text_completion",
  "created": 1687635783,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "lorem ipsum",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 16,
    "total_tokens": 26
  }
}

What I need:

> lorem ipsum

Hey Aidan.
Which programming language are you using?

That’s JSON output. So you need to figure out how to filter that in whatever language you’re using. Search for something like this:

Retrieve nested key value from JSON object in [your programming language]

Here’s how I do it on GNU/Linux using jq.

echo '{ "id": "cmpl-7V3RvHknXFbw3FRXUdyxz9YBdSlBC", "object": "text_completion", "created": 1687635783, "model": "text-davinci-003", "choices": [ { "text": "lorem ipsum", "index": 0, "logprobs": null, "finish_reason": "length" } ], "usage": { "prompt_tokens": 10, "completion_tokens": 16, "total_tokens": 26 } }' \
| jq .

Output:

{
  "id": "cmpl-7V3RvHknXFbw3FRXUdyxz9YBdSlBC",
  "object": "text_completion",
  "created": 1687635783,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "lorem ipsum",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 16,
    "total_tokens": 26
  }
}

And to get the specific object;

| jq '.choices[].text'

Output:

"lorem ipsum"

To get the raw data, IE turning \n into a line break and removing the “” wrapped around the text;

| jq -r '.choices[].text'

Output:

lorem ipsum

Thank you, Justin. I should’ve clarified: I am using Python. I did later find an answer to my question, but I’m still not sure how it works.

I used

def ai(prompt):
    full = openai.Completion.create(prompt=str(prompt), model="text-davinci-003", temperature=1, max_tokens=1700)
    short = full['choices'][0]['text']
    return(short)

The important part being ['choices'][0]['text'].

This is my first Python project, so I’m still new to everything. What’s the purpose of the above snippet? I see that it selects a specific element of the JSON, but what’s the purpose of [0]?

Thank you.

If you take apart the response…

['choices'][0]['text'].

You’re asking for CHOICES then the first element of CHOICES (if there’s more than one it would be [1] or [2], etc. Finally, grab the TEXT

1 Like