Կառուցվածքային արդյունքներ
Խնդրեք պատասխանը որպես JSON՝ ցանկացած JSON օբյեկտ կամ JSON, որը հետևում է ձեր ուղարկած սխեմային։ Օգտագործեք, երբ պատասխանը կարդում է ծրագիրը, ոչ թե մարդը։
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 օբյեկտ | {"type": "json_object"} | Պատասխանը JSON է՝ առանց նախանշված կառուցվածքի։ Prompt-ում ասեք, թե որ բանալիներն եք ուզում։ |
| JSON սխեմա | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | Պատասխանը հետևում է ձեր ուղարկած սխեմային՝ նրա բանալիներին, տեսակներին և պարտադիր դաշտերին։ |
| Strict | "strict": true | json_schema-ի դաշտ, ոչ թե երրորդ ռեժիմ։ Այն true է, եթե չեք ուղարկում false։ |
| Դաշտ | Տեսակ | Լռելյայն | Նկարագրություն | Կիրառվում է |
|---|---|---|---|---|
type | string | json_object կամ json_schema։ Պարտադիր է response_format-ի ներսում։ text-ն ընդունվում է և տալիս է սովորական տեքստային պատասխան։ | Յուրաքանչյուր մոդել, որն ընդունում է այդ ռեժիմը | |
json_schema.schema | object | JSON Schema-ն, որին հետևում է պատասխանը։ Ուղարկեք այն, երբ type-ը json_schema է։ | Յուրաքանչյուր մոդել, որն ընդունում է այդ ռեժիմը | |
json_schema.name | string | Սխեմայի պիտակ, ինչպես OpenAI ձևաչափում։ | Hosted open-weight մոդելներ | |
json_schema.strict | boolean | true | Արդյոք պատասխանը պետք է ճշգրիտ հետևի սխեմային։ Shannon մոդելները յուրաքանչյուր սխեմա կիրառում են որպես strict։ | Hosted open-weight մոդելներ |
JSON օբյեկտի ռեժիմը սխեմա չի պահանջում։ Բանալիները նշեք prompt-ում.
{
"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-*) | Այո | Այո |
| Hosted open-weight մոդելներ, բացի ստորև նշվածներից | Այո | Այո |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Այո | — |
GET /v1/models-ը նույնը հայտնում է յուրաքանչյուր id-ի համար՝ capabilities.json_schema յուրաքանչյուր մոդելի վրա և capabilities.response_format hosted open-weight մոդելների վրա։ Մոդելներ և գներ
Աջակցվող սխեմայի հիմնաբառեր
Ձեր սխեմայից մոդելը ինչ է ստանում, կախված է մոդելից և endpoint-ից.
| Մոդելներ | /v1/chat/completions | /v1/responses |
|---|---|---|
| Shannon մոդելներ | Ստորև բերված հիմնաբառերի ենթաբազմությունը՝ կիրառված որպես strict։ | Սխեման՝ ինչպես ուղարկում եք, կիրառված որպես strict։ |
| Hosted open-weight մոդելներ | Սխեման՝ ինչպես ուղարկում եք։ strict-ը true է, եթե չեք ուղարկում։ | Սխեման՝ ինչպես ուղարկում եք։ strict-ը true է, եթե չեք ուղարկում։ |
Shannon մոդելների հիմնաբառերի ենթաբազմությունը /v1/chat/completions-ում.
| Հիմնաբառ | Ինչպես է կարդացվում |
|---|---|
| Կարդացվող հիմնաբառեր | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | Լուծվում է սխեմայի վերին մակարդակի $defs-ից կամ definitions-ից։ $ref-ի կողքին գրված հիմնաբառերը, օրինակ՝ description-ը, պահպանվում են։ Համապատասխան սահմանում չունեցող հղումը կարդացվում է որպես {"type": "object"}։ |
allOf | Անդամները միացվում են մեկ սխեմայի մեջ։ Եթե երկու անդամ սահմանում են նույն հիմնաբառը, հաշվի է առնվում հետինը։ |
oneOf | Կարդացվում է որպես anyOf։ |
| Իրենց վրա հղվող սխեմաներ | Ընդլայնվում են մինչև 24 մակարդակ։ Ավելի խորը մակարդակը կարդացվում է որպես {"type": "object"}։ |
| Ենթաբազմությունից դուրս | Յուրաքանչյուր այլ հիմնաբառ, օրինակ՝ additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not և if / then / else։ Դրանք մոդելի ստացած սխեմայի մաս չեն։ |
/v1/responses-ում. text.format դաշտով
/v1/responses-ը նույն հարցումը գրում է որպես text.format։ Սխեման գտնվում է անմիջապես format-ում՝ name-ի և strict-ի կողքին, ոչ թե json_schema բանալու տակ։ JSON օբյեկտի ռեժիմի համար ուղարկեք {"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-ում. գործիքը որպես սխեմա
Messages ձևաչափը structured-output դաշտ չունի։ Նույն մոդելներից JSON ստանալու համար նույն բանալիով կանչեք /v1/chat/completions կամ /v1/responses։
/v1/messages-ում մնալու համար նկարագրեք օբյեկտը որպես գործիք. դրեք ձեր սխեման input_schema-ում և անվանեք գործիքը tool_choice-ում։ Այդ դեպքում tool_use բլոկի input-ը ձեր օբյեկտն է՝ արդեն վերլուծված։ Անվանված tool_choice-ը կիրառում են hosted 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-ը տող է։ Պատասխանը կողքին վերլուծված օբյեկտ չունի.
message.content-ի վրա կիրառեք ձեր JSON parser-ը։ stream: true-ի դեպքում JSON-ը գալիս էdelta.contentկտորներով։ Միացրեք դրանք և մեկ անգամ վերլուծեք (parse), երբ հոսքն ավարտվի. առանձին կտորը վավեր JSON չէ։- Hosted open-weight մոդելը
response_formatհարցմանը պատասխանում է առանց reasoning տեքստի.reasoning_content-ըnullէ։
Ստուգեք ստացածը
Սխեման ուղղորդում է, թե ինչպես է մոդելը գրում։ API-ն ավարտված պատասխանը ձեր սխեմայի հետ չի համեմատում, ուստի վերջին ստուգումը կատարում է ձեր կոդը.
- Վերլուծեք սխալների մշակիչի ներսում, ինչպես անում է վերևի օրինակը։
- Վերլուծված օբյեկտը վալիդացրեք նույն սխեմայով ձեր կոդում, օրինակ՝ JSON Schema գրադարանով, Pydantic-ով կամ Zod-ով։
- Երկար պատասխանը կարող է ավարտվել՝ նախքան JSON-ի ամբողջանալը։ Streaming չունեցող պատասխանը
finish_reason-ը հայտնում է միայն որպեսstopկամtool_calls, ուստի վերլուծության սխալը ազդանշան է։ Հարցրեք կրկին կամ խնդրեք ավելի փոքր օբյեկտ։
Սխալներ
| Կարգավիճակ | Տեսակ | Հաղորդագրություն | Երբ |
|---|---|---|---|
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 օբյեկտ չէ։ |
Երկու 400 պատասխանները ուղարկվում են՝ նախքան ձեր մնացորդից որևէ բան գանձվելը։ Սխալների մշակում