Aschuir Struchtúrtha
Iarr an freagra mar JSON: aon réad JSON, nó JSON a leanann scéim a sheolann tú. Úsáid é nuair a léann clár, ní duine, an freagra.
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"]
}
}
}
}' Is é an JSON téacs an fhreagra, i 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}
}
} Módanna response_format
Is réimse de /v1/chat/completions é response_format. Gan é is gnáth-théacs an freagra.
| Mód | Luach | An rud a gheobhaidh tú |
|---|---|---|
| Réad JSON | {"type": "json_object"} | Is JSON é an freagra gan cruth forordaithe. Abair sa prompt na heochracha atá uait. |
| Scéim JSON | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | Leanann an freagra an scéim a sheolann tú: a heochracha, a cineálacha agus a réimsí riachtanacha. |
| Strict | "strict": true | Réimse de json_schema, ní tríú mód. Is true é mura seolann tú false. |
| Réimse | Cineál | Réamhshocrú | Cur síos | Curtha i bhfeidhm ag |
|---|---|---|---|---|
type | string | json_object nó json_schema. Riachtanach laistigh de response_format. Glactar le text agus tugann sé gnáthfhreagra téacs. | Gach múnla a nglacann leis an mód | |
json_schema.schema | object | An JSON Schema a leanann an freagra. Seol é aon uair is json_schema é type. | Gach múnla a nglacann leis an mód | |
json_schema.name | string | Lipéad don scéim, mar atá i bhformáid OpenAI. | Múnlaí open-weight óstáilte | |
json_schema.strict | boolean | true | An gcaithfidh an freagra an scéim a leanúint go beacht. Cuireann múnlaí Shannon gach scéim i bhfeidhm mar strict. | Múnlaí open-weight óstáilte |
Ní theastaíonn scéim ó mhód réad JSON. Ainmnigh na heochracha sa 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"}
} Tacaíocht do gach múnla
| Múnla | json_object | json_schema |
|---|---|---|
Gach múnla Shannon (shannon-*) | Tá | Tá |
| Múnlaí open-weight óstáilte, seachas na cinn a ainmnítear thíos | Tá | Tá |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Tá | — |
Tuairiscíonn GET /v1/models an rud céanna do gach id: capabilities.json_schema ar gach múnla, agus capabilities.response_format ar na múnlaí open-weight óstáilte. Múnlaí agus praghsáil
Eochairfhocail scéime a dtacaítear leo
An méid de do scéim a fhaigheann múnla, braitheann sé ar an múnla agus ar an gcríochphointe:
| Múnlaí | /v1/chat/completions | /v1/responses |
|---|---|---|
| Múnlaí Shannon | An fhoshraith eochairfhocal thíos, curtha i bhfeidhm mar strict. | An scéim mar a sheolann tú í, curtha i bhfeidhm mar strict. |
| Múnlaí open-weight óstáilte | An scéim mar a sheolann tú í. Is true é strict mura seolann tú é. | An scéim mar a sheolann tú í. Is true é strict mura seolann tú é. |
Foshraith eochairfhocal múnlaí Shannon ar /v1/chat/completions:
| Eochairfhocal | Conas a léitear é |
|---|---|
| Eochairfhocail a léitear | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | Réitithe ó $defs nó definitions ag barrleibhéal na scéime. Coinnítear eochairfhocail a scríobhtar in aice le $ref, mar description. Léitear tagairt gan sainmhíniú comhoiriúnach mar {"type": "object"}. |
allOf | Cumasctar na baill i scéim amháin. Nuair a shocraíonn dhá bhall an t-eochairfhocal céanna, cuirtear an ceann déanach san áireamh. |
oneOf | Léite mar anyOf. |
| Scéimeanna a thagraíonn dóibh féin | Leathnaithe go 24 leibhéal. Léitear leibhéal níos doimhne mar {"type": "object"}. |
| Lasmuigh den fhoshraith | Gach eochairfhocal eile, mar shampla additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not agus if / then / else. Ní chuid den scéim a fhaigheann an múnla iad seo. |
Ar /v1/responses: text.format
Scríobhann /v1/responses an t-iarratas céanna mar text.format. Suíonn an scéim go díreach i format, in aice le name agus strict, ní faoi eochair json_schema. Do mhód réad JSON seol {"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"]
}
}
}
} Is é an JSON text na coda output_text den mhír message:
{
"output": [
{
"id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"annotations": []
}
]
}
]
} Tá an tacaíocht do gach múnla agus na hearráidí mar a chéile le /v1/chat/completions. Responses API
Ar /v1/messages: uirlis mar an scéim
Níl aon réimse aschuir struchtúrtha i bhformáid Messages. Chun JSON a fháil ó na múnlaí céanna, glaoigh ar /v1/chat/completions nó /v1/responses leis an eochair chéanna.
Chun fanacht ar /v1/messages, déan cur síos ar an réad mar uirlis: cuir do scéim in input_schema agus ainmnigh an uirlis i tool_choice. Is é input an bhloic tool_use do réad ansin, parsáilte cheana. Cuireann na múnlaí open-weight óstáilte tool_choice ainmnithe i bhfeidhm.
{
"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"}
}
]
} Seiceáil go bhfuil stop_reason tool_use sula léann tú an bloc. Glaonna Feidhme
Conas a thagann an JSON
- Is sreang é an JSON. Níl aon réad parsáilte in aice leis an bhfreagra: rith do pharsálaí JSON ar
message.content. - Le
stream: truetagann an JSON i bpíosaídelta.content. Ceangail le chéile iad agus parsáil uair amháin tar éis don sruth críochnú; ní JSON bailí é píosa ina aonar. - Freagraíonn múnla open-weight óstáilte iarratas
response_formatgan téacs réasúnaíochta: isnulléreasoning_content.
Seiceáil an méid a fhaigheann tú
Treoraíonn an scéim conas a scríobhann an múnla. Ní chuireann an API an freagra críochnaithe i gcomparáid le do scéim, mar sin déanann do chód an tseiceáil dheiridh:
- Parsáil laistigh de láimhseálaí earráide, mar a dhéanann an sampla ag an mbarr.
- Bailíochtaigh an réad parsáilte leis an scéim chéanna i do chód féin, mar shampla le leabharlann JSON Schema, Pydantic nó Zod.
- Is féidir le freagra fada deireadh a chur sular chríochnaigh an JSON. Ní thuairiscíonn freagra gan sruthú
finish_reasonach marstopnótool_calls, mar sin is comhartha é earráid pharsála. Iarr arís, nó iarr réad níos lú.
Earráidí
| Stádas | Cineál | Teachtaireacht | Cathain |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | Ní ghlacann an múnla le ceachtar den dá mhód. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Glacann an múnla le json_object agus sheol tú json_schema. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Níl type ag response_format, nó ní réad JSON é. |
Seoltar an dá fhreagra 400 sula dtógtar aon rud as d'iarmhéid. Láimhseáil Earráide