Update: It seems like they made a mistake in the API docs, and fixed it now.
Earlier, it said "when calling gpt-4-vision-preview
or gpt-3.5-turbo
," but now reads "when calling gpt-4-1106-preview
or gpt-3.5-turbo-1106
."
According to Text generation - OpenAI API, "when calling gpt-4-vision-preview
or gpt-3.5-turbo
, you can set response_format to { type: "json_object" }
to enable JSON mode."
However, the following code throws an error:
{'error': {'message': '1 validation error for Request\nbody -> response_format\n extra fields not permitted (type=value_error.extra)', 'type': 'invalid_request_error', 'param': None, 'code': None}}
If I comment "response_format": {"type": "json_object"}
, it works fine.
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"response_format": {"type": "json_object"},
"messages": [
{
"role": "system",
"content": "You are a helpful assistant. Your response should be in JSON format."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 1000,
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
print(response.json())
According to the release notes, those GPT modes don't support the new JSON output format option. Try gpt-3.5-turbo-1106 or gpt-4-1106-preview instead of gpt-3.5-turbo.
https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo