Awọn igbejade ti a ṣeto
Béèrè fún ìdáhùn bí JSON: object JSON èyíkéyìí, tàbí JSON tó tẹ̀lé schema tí o fi ránṣẹ́. Lò ó nígbà tí ètò kọ̀ǹpútà, kì í ṣe ènìyàn, bá ń ka èsì náà.
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 jẹ́ ọ̀rọ̀ ìdáhùn, nínú 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}
}
} Àwọn mode ti response_format
response_format jẹ́ field ti /v1/chat/completions. Láìsí i, ìdáhùn jẹ́ ọ̀rọ̀ àtọwọ́dọ́wọ́.
| Mode | Iye | Ohun tí o gbà |
|---|---|---|
| JSON object | {"type": "json_object"} | Ìdáhùn jẹ́ JSON láìsí ìrísí tí a pinnu. Sọ nínú prompt àwọn key tí o fẹ́. |
| JSON schema | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | Ìdáhùn tẹ̀lé schema tí o fi ránṣẹ́: àwọn key rẹ̀, àwọn irú àti àwọn field dandan. |
| Strict | "strict": true | Field ti json_schema, kì í ṣe mode kẹta. Ó jẹ́ true àfi tí o bá fi false ránṣẹ́. |
| Field | Irú | Default | Àpèjúwe | Ó ń ṣiṣẹ́ lórí |
|---|---|---|---|---|
type | string | json_object tàbí json_schema. Dandan nínú response_format. A gba text, ó sì ń fúnni ní ìdáhùn ọ̀rọ̀ àtọwọ́dọ́wọ́. | Gbogbo model tó ń gba mode náà | |
json_schema.schema | object | JSON Schema tí ìdáhùn ń tẹ̀lé. Fi ránṣẹ́ nígbà gbogbo tí type bá jẹ́ json_schema. | Gbogbo model tó ń gba mode náà | |
json_schema.name | string | Àmì fún schema, bí ní ọ̀nà OpenAI. | Àwọn model open-weight tí a gbàlejò | |
json_schema.strict | boolean | true | Bóyá ìdáhùn gbọdọ̀ tẹ̀lé schema ní pípé. Àwọn model Shannon ń lo gbogbo schema bí strict. | Àwọn model open-weight tí a gbàlejò |
Mode JSON object kò nílò schema. Dárúkọ àwọn key nínú 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"}
} Ìtìlẹ́yìn fún model kọ̀ọ̀kan
| Model | json_object | json_schema |
|---|---|---|
Gbogbo model Shannon (shannon-*) | Bẹ́ẹ̀ ni | Bẹ́ẹ̀ ni |
| Àwọn model open-weight tí a gbàlejò, àfi àwọn tí a dárúkọ ní ìsàlẹ̀ | Bẹ́ẹ̀ ni | Bẹ́ẹ̀ ni |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Bẹ́ẹ̀ ni | — |
GET /v1/models ń ròyìn bákan náà fún id kọ̀ọ̀kan: capabilities.json_schema lórí gbogbo model, àti capabilities.response_format lórí àwọn model open-weight tí a gbàlejò. Àwọn model àti iye owó
Àwọn keyword schema tí a ń gbà
Ohun tí model ń gbà nínú schema rẹ dá lórí model àti lórí endpoint:
| Àwọn model | /v1/chat/completions | /v1/responses |
|---|---|---|
| Àwọn model Shannon | Ìpín keyword ìsàlẹ̀, tí a lò bí strict. | Schema bí o ṣe fi ránṣẹ́, tí a lò bí strict. |
| Àwọn model open-weight tí a gbàlejò | Schema bí o ṣe fi ránṣẹ́. strict jẹ́ true àfi tí o bá fi ránṣẹ́. | Schema bí o ṣe fi ránṣẹ́. strict jẹ́ true àfi tí o bá fi ránṣẹ́. |
Ìpín keyword ti àwọn model Shannon lórí /v1/chat/completions:
| Keyword | Bí a ṣe ń kà á |
|---|---|
| Àwọn keyword tí a ń kà | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | A ń yanjú rẹ̀ láti $defs tàbí definitions ní ipele òkè schema. A ń pa àwọn keyword tí a kọ lẹ́gbẹ̀ẹ́ $ref mọ́, bíi description. A ń ka ìtọ́kasí tí kò ní ìtumọ̀ tó bá a mu bí {"type": "object"}. |
allOf | A ń da àwọn ẹ̀yà pọ̀ sí schema kan. Níbi tí ẹ̀yà méjì bá ṣètò keyword kan náà, èyí tó kẹ́yìn ni a ń kà. |
oneOf | A ń kà á bí anyOf. |
| Àwọn schema tó ń tọ́ka sí ara wọn | A ń sọ wọ́n di ipele 24. A ń ka ipele tó jinlẹ̀ jù bí {"type": "object"}. |
| Lẹ́yìn ìpín náà | Gbogbo keyword mìíràn, fún àpẹẹrẹ additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not àti if / then / else. Wọn kì í ṣe apá schema tí model ń gbà. |
Lórí /v1/responses: text.format
/v1/responses ń kọ ìbéèrè kan náà bí text.format. Schema wà tààrà nínú format, lẹ́gbẹ̀ẹ́ name àti strict, kì í ṣe lábẹ́ key json_schema. Fún mode JSON object, fi {"format": {"type": "json_object"}} ránṣẹ́.
{
"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 jẹ́ text ti apá output_text ti ohun 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ìlẹ́yìn fún model kọ̀ọ̀kan àti àwọn àṣìṣe jẹ́ bákan náà bí lórí /v1/chat/completions. Responses API
Lórí /v1/messages: tool bí schema
Ọ̀nà Messages kò ní field structured-output. Fún JSON láti àwọn model kan náà, pe /v1/chat/completions tàbí /v1/responses pẹ̀lú key kan náà.
Láti dúró lórí /v1/messages, ṣàpèjúwe object náà bí tool: fi schema rẹ sínú input_schema kí o sì dárúkọ tool náà nínú tool_choice. input ti block tool_use jẹ́ object rẹ lẹ́yìn náà, tí a ti parse. Àwọn model open-weight tí a gbàlejò ń lo tool_choice tí a dárúkọ.
{
"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"}
}
]
} Ṣàyẹ̀wò pé stop_reason jẹ́ tool_use kí o tó ka block náà. Ipe iṣẹ
Bí JSON ṣe ń dé
- JSON jẹ́ string. Èsì kò ní object tí a ti parse lẹ́gbẹ̀ẹ́ rẹ̀: lo JSON parser rẹ lórí
message.content. - Pẹ̀lú
stream: true, JSON ń dé ní àwọn ègédelta.content. So wọ́n pọ̀ kí o sì parse lẹ́ẹ̀kan nígbà tí stream bá ti parí; ègé kan nìkan kì í ṣe JSON tó tọ̀. - Model open-weight tí a gbàlejò ń dáhùn ìbéèrè
response_formatláìsí ọ̀rọ̀ ìrònú:reasoning_contentjẹ́null.
Ṣàyẹ̀wò ohun tí o gbà
Schema ń darí bí model ṣe ń kọ̀wé. API kì í fi ìdáhùn tó ti parí wé schema rẹ, nítorí náà code rẹ ni ó ń ṣe ìṣàyẹ̀wò ìkẹyìn:
- Parse nínú error handler, bí àpẹẹrẹ ní òkè ṣe ń ṣe.
- Fìdí object tí a ti parse múlẹ̀ pẹ̀lú schema kan náà nínú code tìrẹ, fún àpẹẹrẹ pẹ̀lú ibi ìkówèé JSON Schema, Pydantic tàbí Zod.
- Ìdáhùn gígùn lè parí kí JSON tó pé. Èsì tí kì í ṣe streaming ń ròyìn
finish_reasonbístoptàbítool_callsnìkan, nítorí náà àṣìṣe parse ni àmì náà. Tún béèrè, tàbí béèrè fún object tó kéré jù.
Àwọn àṣìṣe
| Ipò | Irú | Ìfiránṣẹ́ | Nígbà wo |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | Model náà kò gba èyíkéyìí nínú àwọn mode méjèèjì. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Model náà ń gba json_object o sì fi json_schema ránṣẹ́. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | response_format kò ní type, tàbí kì í ṣe object JSON. |
A ń fi àwọn èsì 400 méjèèjì ránṣẹ́ kí a tó yọ nǹkankan láti balance rẹ. Mimu Aṣiṣe