Gestruktureerde uitvoer
Vra vir die antwoord as JSON: enige JSON-objek, of JSON wat 'n skema volg wat jy stuur. Gebruik dit wanneer 'n program, nie 'n mens nie, die antwoord lees.
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"]
}
}
}
}' Die JSON is die teks van die antwoord, in 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}
}
} Die modusse van response_format
response_format is 'n veld van /v1/chat/completions. Daarsonder is die antwoord gewone teks.
| Modus | Waarde | Wat jy kry |
|---|---|---|
| JSON-objek | {"type": "json_object"} | Die antwoord is JSON sonder 'n voorgeskrewe vorm. Sê in die prompt watter sleutels jy wil hê. |
| JSON-skema | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | Die antwoord volg die skema wat jy stuur: sy sleutels, tipes en vereiste velde. |
| Streng | "strict": true | 'n Veld van json_schema, nie 'n derde modus nie. Dit is true tensy jy false stuur. |
| Veld | Tipe | Verstek | Beskrywing | Toegepas deur |
|---|---|---|---|---|
type | string | json_object of json_schema. Vereis binne response_format. text word aanvaar en gee 'n gewone teksantwoord. | Elke model wat die modus neem | |
json_schema.schema | object | Die JSON Schema wat die antwoord volg. Stuur dit wanneer type json_schema is. | Elke model wat die modus neem | |
json_schema.name | string | 'n Etiket vir die skema, soos in die OpenAI-formaat. | Gehuisveste oopgewig-modelle | |
json_schema.strict | boolean | true | Of die antwoord die skema presies moet volg. Die Shannon-modelle pas elke skema as streng toe. | Gehuisveste oopgewig-modelle |
JSON-objekmodus het geen skema nodig nie. Noem die sleutels in die 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"}
} Ondersteuning per model
| Model | json_object | json_schema |
|---|---|---|
Elke Shannon-model (shannon-*) | Ja | Ja |
| Gehuisveste oopgewig-modelle, behalwe dié hieronder genoem | Ja | Ja |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Ja | — |
GET /v1/models meld dieselfde per id: capabilities.json_schema op elke model, en capabilities.response_format op die gehuisveste oopgewig-modelle. Modelle en pryse
Ondersteunde skema-sleutelwoorde
Wat 'n model van jou skema ontvang, hang van die model en van die eindpunt af:
| Modelle | /v1/chat/completions | /v1/responses |
|---|---|---|
| Shannon-modelle | Die sleutelwoord-substel hieronder, as streng toegepas. | Die skema soos jy dit stuur, as streng toegepas. |
| Gehuisveste oopgewig-modelle | Die skema soos jy dit stuur. strict is true tensy jy dit stuur. | Die skema soos jy dit stuur. strict is true tensy jy dit stuur. |
Die sleutelwoord-substel van die Shannon-modelle op /v1/chat/completions:
| Sleutelwoord | Hoe dit gelees word |
|---|---|
| Sleutelwoorde gelees | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | Opgelos uit $defs of definitions op die boonste vlak van die skema. Sleutelwoorde wat langs die $ref geskryf is, soos description, word behou. 'n Verwysing sonder 'n passende definisie word as {"type": "object"} gelees. |
allOf | Die lede word in een skema saamgevoeg. Waar twee lede dieselfde sleutelwoord stel, tel die latere een. |
oneOf | Gelees as anyOf. |
| Skemas wat na hulself verwys | Uitgebrei tot 24 vlakke. 'n Dieper vlak word as {"type": "object"} gelees. |
| Buite die substel | Elke ander sleutelwoord, byvoorbeeld additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not en if / then / else. Hulle is nie deel van die skema wat die model ontvang nie. |
Op /v1/responses: text.format
/v1/responses skryf dieselfde versoek as text.format. Die skema sit direk in format, langs name en strict, nie onder 'n json_schema-sleutel nie. Vir JSON-objekmodus stuur {"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"]
}
}
}
} Die JSON is die text van die output_text-deel van die message-item:
{
"output": [
{
"id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"annotations": []
}
]
}
]
} Ondersteuning per model en die foute is dieselfde as op /v1/chat/completions. Responses API
Op /v1/messages: 'n gereedskap as die skema
Die Messages-formaat het geen veld vir gestruktureerde uitvoer nie. Vir JSON van dieselfde modelle, roep /v1/chat/completions of /v1/responses met dieselfde sleutel op.
Om op /v1/messages te bly, beskryf die objek as 'n gereedskap: sit jou skema in input_schema en noem die gereedskap in tool_choice. Die input van die tool_use-blok is dan jou objek, reeds ontleed. 'n Benoemde tool_choice word deur die gehuisveste oopgewig-modelle toegepas.
{
"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"}
}
]
} Toets dat stop_reason tool_use is voordat jy die blok lees. Funksie-oproepe
Hoe die JSON aankom
- Die JSON is 'n string. Die antwoord het nie 'n ontlede objek daarnaas nie: laat jou JSON-ontleder oor
message.contentloop. - Met
stream: truekom die JSON indelta.content-stukke aan. Voeg hulle saam en ontleed een keer nadat die stroom geëindig het; 'n stuk op sy eie is nie geldige JSON nie. - 'n Gehuisveste oopgewig-model beantwoord 'n
response_format-versoek sonder redeneerteks:reasoning_contentisnull.
Toets wat jy ontvang
Die skema lei hoe die model skryf. Die API vergelyk nie die voltooide antwoord met jou skema nie, so jou kode doen die finale toets:
- Ontleed binne 'n foutafhandelaar, soos die voorbeeld heel bo doen.
- Valideer die ontlede objek met dieselfde skema in jou eie kode, byvoorbeeld met 'n JSON Schema-biblioteek, Pydantic of Zod.
- 'n Lang antwoord kan eindig voordat die JSON volledig is. 'n Antwoord wat nie gestroom is nie, meld
finish_reasonnet asstopoftool_calls, so 'n ontlede-fout is die sein. Vra weer, of vra vir 'n kleiner objek.
Foute
| Status | Tipe | Boodskap | Wanneer |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | Die model neem nie een van die twee modusse nie. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Die model neem json_object en jy het json_schema gestuur. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | response_format het geen type nie, of is nie 'n JSON-objek nie. |
Die twee 400-antwoorde word gestuur voordat enigiets van jou balans afgetrek word. Fouthantering