The official OpenAI Chat Completion API JSON schema specification is not publicly available in a standalone JSON Schema format. However, OpenAI provides detailed documentation and examples for the API, which can be used to understand the structure of the requests and responses.
Here’s a breakdown of the key components of the Chat Completion API based on the official documentation:
Request Structure
The request to the Chat Completion API is a JSON object with the following key fields:
{
"model": "string", // The model to use (e.g., "gpt-4", "gpt-3.5-turbo")
"messages": [ // Array of message objects
{
"role": "string", // "system", "user", or "assistant"
"content": "string" // The content of the message
}
],
"temperature": 0.0, // Optional: Controls randomness (0.0 to 2.0)
"max_tokens": 0, // Optional: Maximum number of tokens in the response
"top_p": 0.0, // Optional: Nucleus sampling parameter (0.0 to 1.0)
"frequency_penalty": 0.0, // Optional: Penalizes repeated tokens
"presence_penalty": 0.0, // Optional: Penalizes new tokens
"stop": ["string"] // Optional: Up to 4 sequences to stop generation
}
Response Structure
The response from the Chat Completion API is a JSON object with the following key fields:
{
"id": "string", // Unique ID for the completion
"object": "string", // Always "chat.completion"
"created": 0, // Timestamp of creation
"model": "string", // The model used
"choices": [ // Array of choice objects
{
"index": 0, // Index of the choice
"message": { // The message object
"role": "string", // "assistant"
"content": "string" // The generated content
},
"finish_reason": "string" // Reason for completion (e.g., "stop", "length")
}
],
"usage": { // Token usage statistics
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}
}
Example Request
{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
],
"max_tokens": 50
}
Example Response
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1680534567,
"model": "gpt-3.5-turbo",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The capital of France is Paris."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 10,
"total_tokens": 25
}
}
Where to Find Official Documentation
For the most accurate and up-to-date information, refer to the OpenAI API documentation. While there isn't a formal JSON Schema file provided, the documentation includes detailed descriptions and examples of the request and response formats.
If you need a JSON Schema for validation purposes, you can manually create one based on the documentation or use tools like QuickType to generate a schema from example JSON data.