How do I use the new JSON mode?

Haven’t tested, but give this a try…

Welcome to the community, chickenlord888!

To use the new JSON mode in the OpenAI API with Python, you would modify your API call to specify the response_format parameter with the value { type: "json_object" }. This is how you tell the API that you want the response in JSON mode.

Below is an example of how you might set up your API call in Python:

import openai

openai.api_key = 'your-api-key'

response = openai.Completion.create(
  model="text-davinci-003",
  prompt="Translate the following English text to French: 'Hello, how are you?'",
  response_format={ "type": "json_object" }
)

print(response)

In this snippet:

  1. Replace 'your-api-key' with your actual OpenAI API key.
  2. The response_format parameter is being set to a Python dictionary that represents the JSON object { type: "json_object" }.
  3. model should be set to whichever AI model you’re intending to use (as of my last update, “text-davinci-003” was a current model, but you should check for the latest available).
  4. prompt is whatever you want to send to the model.

When you make this API call, the response you get will be formatted as a JSON object, which is often more structured and easier to parse than plain text, especially if you’re dealing with complex data or you want to integrate the response into other systems that work with JSON.

Be sure to check the latest API documentation or any updates to the API, as parameters and settings can change.

6 Likes