#json mode
1 messages · Page 1 of 1 (latest)
Hi @spice plover. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
JSON mode is a feature of OpenAI's Structured Outputs. It's a more basic version that ensures the model's output is valid JSON. However, it doesn't guarantee that the output matches any specific schema. For that, you should use Structured Outputs, which reliably matches the model's output to the schema you specify.
Here are some important points about JSON mode:
- When JSON mode is turned on, the model's output is ensured to be valid JSON, except for in some edge cases that you should detect and handle appropriately.
- To turn on JSON mode with the Chat Completions or Assistants API, you can set the
response_formatto{ "type": "json_object" }. If you are using function calling, JSON mode is always turned on. - When using JSON mode, you must always instruct the model to produce JSON via some message in the conversation, for example via your system message. If you don't include an explicit instruction to generate JSON, the model may generate an unending stream of whitespace and the request may run continually until it reaches the token limit. To help ensure you don't forget, the API will throw an error if the string "JSON" does not appear somewhere in the context.
- JSON mode will not guarantee the output matches any specific schema, only that it is valid and parses without errors. You should use Structured Outputs to ensure it matches your schema, or if that is not possible, you should use a validation library and potentially retries to ensure that the output matches your desired schema.
- Your application must detect and handle the edge cases that can result in the model output not being a complete JSON object.
For more information, you can refer to the OpenAI documentation (https://platform.openai.com/docs/guides/structured-outputs).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@spice plover Kapa.ai is still learning and improving, please let me know how I did by reacting below.
dobra, a jak są te strukturalne outputy z zod, to jak z nich dostac json?
@hard ore a jak sa te strukturalne output from zod, to jak z nich dostac json?
Let me check the knowledge base for you again. One moment! :books:
Aby uzyskać JSON z wyjścia strukturalnego przy użyciu biblioteki zod, możesz skorzystać z funkcji zodResponseFormat dostarczonej przez OpenAI. Ta funkcja konwertuje schemat zod na format, który jest zgodny z API OpenAI.
Oto przykładowy kod:
import { z } from 'zod';
import { zodResponseFormat } from 'openai/helpers/zod';
const BaseResponseSchema = z.object({ /* ... */ });
const UnsuccessfulResponseSchema = z.object({ /* ... */ });
const finalSchema = z.discriminatedUnion('status', [
BaseResponseSchema,
UnsuccessfulResponseSchema,
]);
// Konwersja schematu zod na format odpowiedzi
const json = zodResponseFormat(finalSchema, 'final_schema');
console.log(json);
Warto jednak zauważyć, że obiekt na najwyższym poziomie schematu musi być obiektem, a nie używać anyOf. Wzorzec, który pojawia się w Zod (jako jeden przykład), polega na użyciu związku dyskryminacyjnego, który na najwyższym poziomie generuje anyOf. Kod taki jak powyższy nie zadziała (źródło (https://platform.openai.com/docs/guides/structured-outputs/supported-schemas)).
Jeśli chcesz uzyskać więcej informacji na temat strukturalnych wyjść i jak z nich korzystać, zapoznaj się z dokumentacją OpenAI (https://platform.openai.com/docs/guides/structured-outputs).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@spice plover Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@hard ore continue writing in polish, but when i'm doing the completion, how to get json from completion answer?
Let me check the knowledge base for you again. One moment! :books:
Aby uzyskać odpowiedź w formacie JSON podczas korzystania z API OpenAI, musisz ustawić response_format na { "type": "json_object" } podczas tworzenia zadania uzupełnienia. Oto przykładowy kod:
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=[
{"role": "system", "content": "You are a helpful assistant designed to output JSON."},
{"role": "user", "content": "Who won the world series in 2020? Please respond in the format {winner: ...}"}
],
response_format={"type": "json_object"}
)
W odpowiedzi, model zwróci dane w formacie JSON. Możesz to sprawdzić, analizując pole message w odpowiedzi:
print(response.choices[0].message)