ਸਟ੍ਰਕਚਰਡ ਆਉਟਪੁੱਟਸ
ਜਵਾਬ JSON ਵਜੋਂ ਮੰਗੋ: ਕੋਈ ਵੀ JSON object, ਜਾਂ ਉਹ JSON ਜੋ ਤੁਹਾਡੇ ਭੇਜੇ schema ਦੀ ਪਾਲਣਾ ਕਰੇ। ਇਸਨੂੰ ਉਦੋਂ ਵਰਤੋ ਜਦੋਂ ਜਵਾਬ ਕੋਈ ਵਿਅਕਤੀ ਨਹੀਂ ਸਗੋਂ ਪ੍ਰੋਗਰਾਮ ਪੜ੍ਹਦਾ ਹੈ।
POST https://api.shannon-ai.com/v1/chat/completions
import json
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.shannon-ai.com/v1")
response = client.chat.completions.create(
model="Kimi-K3-3BIT-REAP",
messages=[
{"role": "user", "content": "Extract the person: John Doe, 30 years old, engineer."}
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": True,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"occupation": {"type": "string"},
},
"required": ["name", "age", "occupation"],
},
},
},
)
# The JSON arrives as text in message.content: parse it, then check it.
try:
person = json.loads(response.choices[0].message.content)
except json.JSONDecodeError:
person = None # ask again, or report the failure
print(person) import OpenAI from "openai";
const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.shannon-ai.com/v1" });
const response = await client.chat.completions.create({
model: "Kimi-K3-3BIT-REAP",
messages: [
{ role: "user", content: "Extract the person: John Doe, 30 years old, engineer." },
],
response_format: {
type: "json_schema",
json_schema: {
name: "person",
strict: true,
schema: {
type: "object",
properties: {
name: { type: "string" },
age: { type: "integer" },
occupation: { type: "string" },
},
required: ["name", "age", "occupation"],
},
},
},
});
// The JSON arrives as text in message.content: parse it, then check it.
let person = null;
try {
person = JSON.parse(response.choices[0].message.content);
} catch {
// ask again, or report the failure
}
console.log(person); curl https://api.shannon-ai.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "Kimi-K3-3BIT-REAP",
"messages": [
{"role": "user", "content": "Extract the person: John Doe, 30 years old, engineer."}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"occupation": {"type": "string"}
},
"required": ["name", "age", "occupation"]
}
}
}
}' JSON ਜਵਾਬ ਦਾ ਟੈਕਸਟ ਹੈ, choices[0].message.content ਵਿੱਚ:
{
"id": "chatcmpl-0c4e7a1b9d2f4e6a8b3c5d7e9f1a2b3c",
"object": "chat.completion",
"created": 1791590400,
"model": "Kimi-K3-3BIT-REAP",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"reasoning_content": null
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 96,
"completion_tokens": 19,
"total_tokens": 115,
"prompt_tokens_details": {"cached_tokens": 0},
"completion_tokens_details": {"reasoning_tokens": 0}
}
} response_format ਦੇ ਮੋਡਾਂ ਦੀ ਸੂਚੀ
response_format /v1/chat/completions ਦਾ ਇੱਕ ਫੀਲਡ ਹੈ। ਇਸ ਤੋਂ ਬਿਨਾਂ ਜਵਾਬ ਆਮ ਟੈਕਸਟ ਹੁੰਦਾ ਹੈ।
| ਮੋਡ | ਮੁੱਲ | ਤੁਹਾਨੂੰ ਕੀ ਮਿਲਦਾ ਹੈ |
|---|---|---|
| JSON object | {"type": "json_object"} | ਜਵਾਬ ਬਿਨਾਂ ਕਿਸੇ ਨਿਰਧਾਰਤ ਸ਼ਕਲ ਦਾ JSON ਹੁੰਦਾ ਹੈ। ਪ੍ਰੋਂਪਟ ਵਿੱਚ ਦੱਸੋ ਕਿ ਤੁਹਾਨੂੰ ਕਿਹੜੀਆਂ keys ਚਾਹੀਦੀਆਂ ਹਨ। |
| JSON schema | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | ਜਵਾਬ ਤੁਹਾਡੇ ਭੇਜੇ schema ਦੀ ਪਾਲਣਾ ਕਰਦਾ ਹੈ: ਇਸਦੀਆਂ keys, ਕਿਸਮਾਂ ਅਤੇ ਲਾਜ਼ਮੀ ਫੀਲਡ। |
| Strict | "strict": true | json_schema ਦਾ ਇੱਕ ਫੀਲਡ, ਤੀਜਾ ਮੋਡ ਨਹੀਂ। ਜਦੋਂ ਤੱਕ ਤੁਸੀਂ false ਨਹੀਂ ਭੇਜਦੇ, ਇਹ true ਹੁੰਦਾ ਹੈ। |
| ਫੀਲਡ | ਕਿਸਮ | ਡਿਫੌਲਟ | ਵੇਰਵਾ | ਲਾਗੂ ਕਰਨ ਵਾਲੇ |
|---|---|---|---|---|
type | string | json_object ਜਾਂ json_schema। response_format ਦੇ ਅੰਦਰ ਲਾਜ਼ਮੀ। text ਸਵੀਕਾਰ ਕੀਤਾ ਜਾਂਦਾ ਹੈ ਅਤੇ ਆਮ ਟੈਕਸਟ ਜਵਾਬ ਦਿੰਦਾ ਹੈ। | ਹਰ ਮਾਡਲ ਜੋ ਇਸ ਮੋਡ ਨੂੰ ਲੈਂਦਾ ਹੈ | |
json_schema.schema | object | ਉਹ JSON Schema ਜਿਸਦੀ ਜਵਾਬ ਪਾਲਣਾ ਕਰਦਾ ਹੈ। ਜਦੋਂ ਵੀ type json_schema ਹੋਵੇ, ਇਸਨੂੰ ਭੇਜੋ। | ਹਰ ਮਾਡਲ ਜੋ ਇਸ ਮੋਡ ਨੂੰ ਲੈਂਦਾ ਹੈ | |
json_schema.name | string | schema ਲਈ ਇੱਕ ਲੇਬਲ, ਜਿਵੇਂ OpenAI ਫਾਰਮੈਟ ਵਿੱਚ। | ਹੋਸਟ ਕੀਤੇ open-weight ਮਾਡਲ | |
json_schema.strict | boolean | true | ਕੀ ਜਵਾਬ ਨੂੰ schema ਦੀ ਠੀਕ-ਠੀਕ ਪਾਲਣਾ ਕਰਨੀ ਪਵੇਗੀ। Shannon ਮਾਡਲ ਹਰ schema ਨੂੰ strict ਵਜੋਂ ਲਾਗੂ ਕਰਦੇ ਹਨ। | ਹੋਸਟ ਕੀਤੇ open-weight ਮਾਡਲ |
JSON object ਮੋਡ ਨੂੰ schema ਦੀ ਲੋੜ ਨਹੀਂ। ਪ੍ਰੋਂਪਟ ਵਿੱਚ keys ਦੇ ਨਾਮ ਦਿਓ:
{
"model": "shannon-3",
"messages": [
{
"role": "user",
"content": "Name the capital of France. Answer as JSON with the keys city and country."
}
],
"response_format": {"type": "json_object"}
} ਹਰ ਮਾਡਲ ਲਈ ਸਹਾਇਤਾ
| ਮਾਡਲ | json_object | json_schema |
|---|---|---|
ਹਰ Shannon ਮਾਡਲ (shannon-*) | ਹਾਂ | ਹਾਂ |
| ਹੋਸਟ ਕੀਤੇ open-weight ਮਾਡਲ, ਹੇਠਾਂ ਦੱਸੇ ਮਾਡਲਾਂ ਨੂੰ ਛੱਡ ਕੇ | ਹਾਂ | ਹਾਂ |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | ਹਾਂ | — |
GET /v1/models ਹਰ id ਲਈ ਇਹੀ ਦੱਸਦਾ ਹੈ: ਹਰ ਮਾਡਲ ਉੱਤੇ capabilities.json_schema, ਅਤੇ ਹੋਸਟ ਕੀਤੇ open-weight ਮਾਡਲਾਂ ਉੱਤੇ capabilities.response_format। ਮਾਡਲ ਅਤੇ ਕੀਮਤਾਂ
ਸਮਰਥਿਤ schema ਕੀਵਰਡ
ਮਾਡਲ ਨੂੰ ਤੁਹਾਡੀ schema ਦਾ ਕਿੰਨਾ ਹਿੱਸਾ ਮਿਲਦਾ ਹੈ, ਇਹ ਮਾਡਲ ਅਤੇ endpoint ਉੱਤੇ ਨਿਰਭਰ ਕਰਦਾ ਹੈ:
| ਮਾਡਲ | /v1/chat/completions | /v1/responses |
|---|---|---|
| Shannon ਮਾਡਲ | ਹੇਠਾਂ ਦਿੱਤਾ ਕੀਵਰਡ ਉਪ-ਸਮੂਹ, strict ਵਜੋਂ ਲਾਗੂ। | schema ਜਿਵੇਂ ਤੁਸੀਂ ਭੇਜਦੇ ਹੋ, strict ਵਜੋਂ ਲਾਗੂ। |
| ਹੋਸਟ ਕੀਤੇ open-weight ਮਾਡਲ | schema ਜਿਵੇਂ ਤੁਸੀਂ ਭੇਜਦੇ ਹੋ। ਜਦੋਂ ਤੱਕ ਤੁਸੀਂ strict ਨਹੀਂ ਭੇਜਦੇ, ਇਹ true ਹੁੰਦਾ ਹੈ। | schema ਜਿਵੇਂ ਤੁਸੀਂ ਭੇਜਦੇ ਹੋ। ਜਦੋਂ ਤੱਕ ਤੁਸੀਂ strict ਨਹੀਂ ਭੇਜਦੇ, ਇਹ true ਹੁੰਦਾ ਹੈ। |
/v1/chat/completions ਉੱਤੇ Shannon ਮਾਡਲਾਂ ਦਾ ਕੀਵਰਡ ਉਪ-ਸਮੂਹ:
| ਕੀਵਰਡ | ਇਸਨੂੰ ਕਿਵੇਂ ਪੜ੍ਹਿਆ ਜਾਂਦਾ ਹੈ |
|---|---|
| ਪੜ੍ਹੇ ਜਾਣ ਵਾਲੇ ਕੀਵਰਡ | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | schema ਦੇ ਸਿਖਰਲੇ ਪੱਧਰ ਉੱਤੇ $defs ਜਾਂ definitions ਤੋਂ ਹੱਲ ਕੀਤਾ ਜਾਂਦਾ ਹੈ। $ref ਦੇ ਨਾਲ ਲਿਖੇ ਕੀਵਰਡ, ਜਿਵੇਂ description, ਰੱਖੇ ਜਾਂਦੇ ਹਨ। ਜਿਸ ਹਵਾਲੇ ਦੀ ਕੋਈ ਮੇਲ ਖਾਂਦੀ ਪਰਿਭਾਸ਼ਾ ਨਾ ਹੋਵੇ, ਉਹ {"type": "object"} ਵਜੋਂ ਪੜ੍ਹਿਆ ਜਾਂਦਾ ਹੈ। |
allOf | ਮੈਂਬਰਾਂ ਨੂੰ ਇੱਕ schema ਵਿੱਚ ਮਿਲਾ ਦਿੱਤਾ ਜਾਂਦਾ ਹੈ। ਜਿੱਥੇ ਦੋ ਮੈਂਬਰ ਇੱਕੋ ਕੀਵਰਡ ਸੈੱਟ ਕਰਦੇ ਹਨ, ਉੱਥੇ ਬਾਅਦ ਵਾਲਾ ਗਿਣਿਆ ਜਾਂਦਾ ਹੈ। |
oneOf | anyOf ਵਜੋਂ ਪੜ੍ਹਿਆ ਜਾਂਦਾ ਹੈ। |
| ਆਪਣੇ ਆਪ ਦਾ ਹਵਾਲਾ ਦੇਣ ਵਾਲੀਆਂ schemas | 24 ਪੱਧਰਾਂ ਤੱਕ ਫੈਲਾਈਆਂ ਜਾਂਦੀਆਂ ਹਨ। ਇਸ ਤੋਂ ਡੂੰਘਾ ਪੱਧਰ {"type": "object"} ਵਜੋਂ ਪੜ੍ਹਿਆ ਜਾਂਦਾ ਹੈ। |
| ਉਪ-ਸਮੂਹ ਤੋਂ ਬਾਹਰ | ਹਰ ਹੋਰ ਕੀਵਰਡ, ਉਦਾਹਰਨ ਲਈ additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not ਅਤੇ if / then / else। ਇਹ ਉਸ schema ਦਾ ਹਿੱਸਾ ਨਹੀਂ ਹਨ ਜੋ ਮਾਡਲ ਨੂੰ ਮਿਲਦੀ ਹੈ। |
/v1/responses ਉੱਤੇ ਫੀਲਡ text.format ਰਾਹੀਂ ਮੰਗ ਕਰਨਾ
/v1/responses ਉਸੇ ਰਿਕੁਐਸਟ ਨੂੰ text.format ਵਜੋਂ ਲਿਖਦਾ ਹੈ। schema ਸਿੱਧੀ format ਵਿੱਚ, name ਅਤੇ strict ਦੇ ਕੋਲ ਹੁੰਦੀ ਹੈ, json_schema key ਹੇਠ ਨਹੀਂ। JSON object ਮੋਡ ਲਈ {"format": {"type": "json_object"}} ਭੇਜੋ।
{
"model": "Kimi-K3-3BIT-REAP",
"input": "Extract the person: John Doe, 30 years old, engineer.",
"text": {
"format": {
"type": "json_schema",
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"occupation": {"type": "string"}
},
"required": ["name", "age", "occupation"]
}
}
}
} JSON message ਆਈਟਮ ਦੇ output_text ਹਿੱਸੇ ਦਾ text ਹੈ:
{
"output": [
{
"id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"annotations": []
}
]
}
]
} ਹਰ ਮਾਡਲ ਲਈ ਸਹਾਇਤਾ ਅਤੇ ਗਲਤੀਆਂ /v1/chat/completions ਵਾਂਗ ਹੀ ਹਨ। Responses API
/v1/messages ਉੱਤੇ: schema ਵਜੋਂ ਇੱਕ ਟੂਲ
Messages ਫਾਰਮੈਟ ਵਿੱਚ structured-output ਫੀਲਡ ਨਹੀਂ ਹੈ। ਉਨ੍ਹਾਂ ਹੀ ਮਾਡਲਾਂ ਤੋਂ JSON ਲਈ, ਉਸੇ key ਨਾਲ /v1/chat/completions ਜਾਂ /v1/responses ਨੂੰ ਕਾਲ ਕਰੋ।
/v1/messages ਉੱਤੇ ਰਹਿਣ ਲਈ, object ਨੂੰ ਟੂਲ ਵਜੋਂ ਦੱਸੋ: ਆਪਣੀ schema ਨੂੰ input_schema ਵਿੱਚ ਰੱਖੋ ਅਤੇ ਟੂਲ ਦਾ ਨਾਮ tool_choice ਵਿੱਚ ਦਿਓ। ਫਿਰ tool_use ਬਲਾਕ ਦਾ input ਤੁਹਾਡਾ object ਹੁੰਦਾ ਹੈ, ਪਹਿਲਾਂ ਹੀ parse ਕੀਤਾ ਹੋਇਆ। ਨਾਮ ਦੇ ਕੇ ਚੁਣਿਆ tool_choice ਹੋਸਟ ਕੀਤੇ open-weight ਮਾਡਲ ਲਾਗੂ ਕਰਦੇ ਹਨ।
{
"model": "Kimi-K3-3BIT-REAP",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Extract the person: John Doe, 30 years old, engineer."
}
],
"tools": [
{
"name": "record_person",
"description": "Record the extracted person.",
"input_schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"occupation": {"type": "string"}
},
"required": ["name", "age", "occupation"]
}
}
],
"tool_choice": {"type": "tool", "name": "record_person"}
} {
"stop_reason": "tool_use",
"content": [
{
"type": "tool_use",
"id": "toolu_4b8d2f6a1c3e4a5b9d7f0e2c4a6b8d1f",
"name": "record_person",
"input": {"name": "John Doe", "age": 30, "occupation": "engineer"}
}
]
} ਬਲਾਕ ਪੜ੍ਹਨ ਤੋਂ ਪਹਿਲਾਂ ਜਾਂਚ ਕਰੋ ਕਿ stop_reason tool_use ਹੈ। ਫੰਕਸ਼ਨ ਕਾਲਿੰਗ
JSON ਕਿਵੇਂ ਆਉਂਦਾ ਹੈ
- JSON ਇੱਕ string ਹੈ। ਜਵਾਬ ਦੇ ਨਾਲ ਕੋਈ parsed object ਨਹੀਂ ਹੁੰਦਾ:
message.contentਉੱਤੇ ਆਪਣਾ JSON parser ਚਲਾਓ। stream: trueਨਾਲ JSONdelta.contentਦੇ ਟੁਕੜਿਆਂ ਵਿੱਚ ਆਉਂਦਾ ਹੈ। ਉਨ੍ਹਾਂ ਨੂੰ ਜੋੜੋ ਅਤੇ ਸਟ੍ਰੀਮ ਖਤਮ ਹੋਣ ਤੋਂ ਬਾਅਦ ਇੱਕ ਵਾਰ parse ਕਰੋ; ਇਕੱਲਾ ਟੁਕੜਾ ਵੈਲਿਡ JSON ਨਹੀਂ ਹੁੰਦਾ।- ਹੋਸਟ ਕੀਤਾ open-weight ਮਾਡਲ
response_formatਵਾਲੀ ਰਿਕੁਐਸਟ ਦਾ ਜਵਾਬ ਰੀਜ਼ਨਿੰਗ ਟੈਕਸਟ ਤੋਂ ਬਿਨਾਂ ਦਿੰਦਾ ਹੈ:reasoning_contentnullਹੁੰਦਾ ਹੈ।
ਜੋ ਮਿਲੇ ਉਸਦੀ ਜਾਂਚ ਕਰੋ
schema ਮਾਡਲ ਦੇ ਲਿਖਣ ਦੇ ਢੰਗ ਨੂੰ ਸੇਧ ਦਿੰਦੀ ਹੈ। API ਮੁਕੰਮਲ ਜਵਾਬ ਦੀ ਤੁਹਾਡੀ schema ਨਾਲ ਤੁਲਨਾ ਨਹੀਂ ਕਰਦਾ, ਇਸ ਲਈ ਆਖਰੀ ਜਾਂਚ ਤੁਹਾਡਾ ਕੋਡ ਕਰਦਾ ਹੈ:
- ਗਲਤੀ ਹੈਂਡਲਰ ਦੇ ਅੰਦਰ parse ਕਰੋ, ਜਿਵੇਂ ਸਿਖਰ ਦਾ ਨਮੂਨਾ ਕਰਦਾ ਹੈ।
- parse ਕੀਤੇ object ਨੂੰ ਆਪਣੇ ਕੋਡ ਵਿੱਚ ਉਸੇ schema ਨਾਲ ਵੈਲਿਡੇਟ ਕਰੋ, ਉਦਾਹਰਨ ਲਈ JSON Schema ਲਾਇਬ੍ਰੇਰੀ, Pydantic ਜਾਂ Zod ਨਾਲ।
- ਲੰਬਾ ਜਵਾਬ JSON ਦੇ ਪੂਰਾ ਹੋਣ ਤੋਂ ਪਹਿਲਾਂ ਹੀ ਖਤਮ ਹੋ ਸਕਦਾ ਹੈ। ਜੋ ਜਵਾਬ ਸਟ੍ਰੀਮ ਨਹੀਂ ਹੁੰਦਾ, ਉਹ
finish_reasonਸਿਰਫ਼stopਜਾਂtool_callsਦੱਸਦਾ ਹੈ, ਇਸ ਲਈ parse ਗਲਤੀ ਹੀ ਸੰਕੇਤ ਹੈ। ਦੁਬਾਰਾ ਮੰਗੋ, ਜਾਂ ਛੋਟਾ object ਮੰਗੋ।
ਗਲਤੀਆਂ
| ਸਟੇਟਸ | ਕਿਸਮ | ਸੰਦੇਸ਼ | ਕਦੋਂ |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | ਮਾਡਲ ਕੋਈ ਵੀ ਮੋਡ ਨਹੀਂ ਲੈਂਦਾ। |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | ਮਾਡਲ json_object ਲੈਂਦਾ ਹੈ ਅਤੇ ਤੁਸੀਂ json_schema ਭੇਜਿਆ। |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | response_format ਵਿੱਚ type ਨਹੀਂ ਹੈ, ਜਾਂ ਉਹ JSON object ਨਹੀਂ ਹੈ। |
ਦੋਵੇਂ 400 ਜਵਾਬ ਤੁਹਾਡੇ ਬੈਲੇਂਸ ਵਿੱਚੋਂ ਕੁਝ ਕੱਟੇ ਜਾਣ ਤੋਂ ਪਹਿਲਾਂ ਭੇਜੇ ਜਾਂਦੇ ਹਨ। ਗਲਤੀਆਂ