I have a goal of making the ChatGPT 4.0 API output something in a consistent format, i.e. in a JSON-format.
desired output example
{
"name": "Jon",
"last_nane": "Doe"
}
actual output example
```json // here it randomly adds backticks and json
{
"name": "Jon",
"last_name": "Doe"
}
``` // here it randomly adds 3 backticks
my actual prompting is
Translate the following text to the languages matching the language codes and output them in JSON format.
Important:
* Do NOT use apostrophes/backticks in the output
* Do NOT start with ```json
* Do NOT end with ```
* Do NOT explain anything in the output
* If there are any exceptions, then do NOT translate them
* If the input is empty, then the output should be empty
Use exactly the same OUTPUT format as the below:
----------------
INPUT: Dette er en Dansk oversættelse
LANGUAGECODES: da-DK, en-US, nl-NL
EXCEPTIONS: Dansk
OUTPUT: [{"da-DK":"Dette er en Dansk oversættelse"},{"en-US":"This is an Dansk translation"},{"nl-NL":"Dit is een Dansk vertaling"}]
INPUT: {{$input}}
LANGUAGECODES: {{$languagecodes}}
EXCEPTIONS: {{$exceptions}}
OUTPUT:
my actual outputs displaying the random stuff it adds
And in my prompt, I tell it to output in a JSON and just JSON format as displayed in my example OUTPUT... But it still sometimes adds random stuff.
How to force it to just output the actual json object?
ChatGPT has JSON Mode.
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
response_format={ "type": "json_object" },
messages=[
{"role": "system", "content": "You are a helpful assistant
designed to output JSON."},
{"role": "user", "content": "Who won the world series in 2020?"}
]
)
print(response.choices[0].message.content)