Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenAI GPT-3 API: How to keep the format of the response?

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?

like image 235
Tyler Kim Avatar asked Dec 30 '25 04:12

Tyler Kim


1 Answers

UPDATE

All Edits API endpoints are deprecated.

Screenshot

Recommended replacements:

  • Use gpt-4 instead of text-davinci-edit-001 and code-davinci-edit-001.
  • Use /v1/chat/completions instead of /v1/edits.

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 API endpoint by yourself (i.e., write Python code).

like image 53
Rok Benko Avatar answered Jan 01 '26 23:01

Rok Benko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!