2

When I use GPT3's playground, I often get results that are formatted with numbered lists and paragraphs like below:

Here's what the above class is doing:

1. It creates a directory for the log file if it doesn't exist.
2. It checks that the log file is newline-terminated.
3. It writes a newline-terminated JSON object to the log file.
4. It reads the log file and returns a dictionary with the following

- list 1
- list 2
- list 3
- list 4

However, when I directly use their API and extract the response from json result, I get the crammed text version that is very hard to read, something like this:

Here's what the above class is doing:1. It creates a directory for the log file if it doesn't exist.2. It checks that the log file is newline-terminated.3. It writes a newline-terminated JSON object to the log file.4. It reads the log file and returns a dictionary with the following-list 1-list 2-list 3- list4

My question is, how do people keep the formats from GPT results so they are displayed in a neater, more readable way?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Tyler Kim
  • 181
  • 1
  • 11

4 Answers4

1

Option 1: Edits endpoint

If you run test.py the OpenAI API will return the following completion:

Python

test.py

import openai

openai.api_key = 'sk-xxxxxxxxxxxxxxxxxxxx'

response = openai.Edit.create(
  model = 'text-davinci-edit-001',
  input = 'I have three items:1. First item.2. Second item.3. Third item.',
  instruction = 'Make numbered list'
)

content = response['choices'][0]['text']

print(content)

Option 2: Processing

Process the completion you get from the Completions endpoint by yourself (i.e., write Python code).

Rok Benko
  • 14,265
  • 2
  • 24
  • 49
1

The best answer is to provide an example of what you want the output to look like. The data in the example doesn't really matter, just show it the structure you want.

Example output:
Here's what the above class is doing:
1. It blah.
2. It blah.
hemp
  • 5,602
  • 29
  • 43
  • I have done a lot more work with OpenAI since providing this answer. While providing an example is still a useful technique, I've learned that the content of the example definitely can and often does affect the output. – hemp Jun 19 '23 at 21:03
1

The problem was on my side's frontend. The openAI API was returning the correct response, and I was rending the result with the wrong whitespace CSS settings

Tyler Kim
  • 181
  • 1
  • 11
0

That would be white-space: pre-line;.

Rok Benko
  • 14,265
  • 2
  • 24
  • 49