#json output

1 messages · Page 1 of 1 (latest)

slate spear
#

how can I constrain a gpt-4 call to output json in a specific format i tell it in a prompt

tawdry yewBOT
#

Hi @slate spear. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To constrain a GPT-4 model to output JSON in a specific format, you can use the "JSON mode" feature. This feature allows the model to generate strings that parse into valid JSON. You can enable this mode by setting response_format: { type: "json_object" } when making a call to the model.

Here's an example of how you might structure your API call:

openai.ChatCompletion.create(
  model="gpt-4-visual-preview",
  messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": 'Generate a JSON response in the format {"key": "value"}.'}
    ],
  response_format={"type": "json_object"}
)

Please note that the model will try to generate a response that matches the format you specify in your prompt, but it's not guaranteed to be perfect every time. The model can't read your mind, so the clearer and more specific your instructions, the better the results will likely be.

You can also use various tactics to improve the relevance and format of the model's responses. These tactics include including details in your query, asking the model to adopt a persona, using delimiters to clearly indicate distinct parts of the input, specifying the steps required to complete a task, providing examples, and specifying the desired length of the output. You can find more information about these tactics in the OpenAI Prompt Engineering Guide (https://platform.openai.com/docs/guides/prompt-engineering).

Please note that the gpt-4-visual-preview model does not currently support the response_format field. This information is based on the extracts from the OpenAI Node API Library Github Issue (https://github.com/openai/openai-node/issues/469) and the OpenAI Community Forum (https://community.openai.com/t/484377).