Allbynnau strwythuredig
Gofynnwch am yr ateb fel JSON: unrhyw wrthrych JSON, neu JSON sy'n dilyn sgema a anfonwch. Defnyddiwch ef pan fo rhaglen, nid person, yn darllen yr ateb.
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"]
}
}
}
}' Y JSON yw testun yr ateb, yn 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}
}
} Moddau response_format
Maes o /v1/chat/completions yw response_format. Hebddo mae'r ateb yn destun cyffredin.
| Modd | Gwerth | Beth gewch chi |
|---|---|---|
| Gwrthrych JSON | {"type": "json_object"} | Mae'r ateb yn JSON heb siâp rhagnodedig. Dywedwch yn y prompt pa allweddi rydych eu heisiau. |
| Sgema JSON | {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} | Mae'r ateb yn dilyn y sgema a anfonwch: ei allweddi, ei fathau a'i feysydd gofynnol. |
| Strict | "strict": true | Maes o json_schema, nid trydydd modd. Mae'n true oni anfonwch false. |
| Maes | Math | Rhagosodiad | Disgrifiad | Yn cael ei gymhwyso gan |
|---|---|---|---|---|
type | string | json_object neu json_schema. Gofynnol y tu mewn i response_format. Derbynnir text ac mae'n rhoi ateb testun cyffredin. | Pob model sy'n cymryd y modd | |
json_schema.schema | object | Y sgema JSON y mae'r ateb yn ei ddilyn. Anfonwch ef pryd bynnag y mae type yn json_schema. | Pob model sy'n cymryd y modd | |
json_schema.name | string | Label ar gyfer y sgema, fel yn fformat OpenAI. | Modelau pwysau agored a gynhelir | |
json_schema.strict | boolean | true | A oes rhaid i'r ateb ddilyn y sgema yn union. Mae'r modelau Shannon yn cymhwyso pob sgema fel strict. | Modelau pwysau agored a gynhelir |
Nid oes angen sgema ar y modd gwrthrych JSON. Enwch yr allweddi yn y 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"}
} Cefnogaeth fesul model
| Model | json_object | json_schema |
|---|---|---|
Pob model Shannon (shannon-*) | Ydy | Ydy |
| Modelau pwysau agored a gynhelir, ac eithrio'r rhai a enwir isod | Ydy | Ydy |
Laguna-S-2.1-W4A16-AUTOROUND-REAP | — | — |
inkling-W4A16-AUTOROUND-REAP | Ydy | — |
Mae GET /v1/models yn adrodd yr un peth fesul id: capabilities.json_schema ar bob model, ac capabilities.response_format ar y modelau pwysau agored a gynhelir. Modelau a phrisiau
Allweddeiriau sgema a gefnogir
Mae'r hyn y mae model yn ei dderbyn o'ch sgema yn dibynnu ar y model ac ar y pwynt terfyn:
| Modelau | /v1/chat/completions | /v1/responses |
|---|---|---|
| Modelau Shannon | Yr is-set o allweddeiriau isod, wedi'i chymhwyso fel strict. | Y sgema fel yr anfonwch ef, wedi'i chymhwyso fel strict. |
| Modelau pwysau agored a gynhelir | Y sgema fel yr anfonwch ef. Mae strict yn true oni anfonwch ef. | Y sgema fel yr anfonwch ef. Mae strict yn true oni anfonwch ef. |
Is-set allweddeiriau'r modelau Shannon ar /v1/chat/completions:
| Allweddair | Sut y caiff ei ddarllen |
|---|---|
| Allweddeiriau a ddarllenir | type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default |
$ref | Datrysir o $defs neu definitions ar frig y sgema. Cedwir allweddeiriau a ysgrifennir wrth ymyl y $ref, fel description. Darllenir cyfeiriad heb ddiffiniad cyfatebol fel {"type": "object"}. |
allOf | Cyfunir yr aelodau yn un sgema. Lle mae dau aelod yn gosod yr un allweddair, mae'r un diweddarach yn cyfrif. |
oneOf | Darllenir fel anyOf. |
| Sgemâu sy'n cyfeirio atynt eu hunain | Ehangir hyd at 24 lefel. Darllenir lefel ddyfnach fel {"type": "object"}. |
| Y tu allan i'r is-set | Pob allweddair arall, er enghraifft additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not ac if / then / else. Nid yw'r rhain yn rhan o'r sgema y mae'r model yn ei dderbyn. |
Ar /v1/responses: text.format
Mae /v1/responses yn sillafu'r un cais fel text.format. Mae'r sgema yn eistedd yn uniongyrchol yn format, wrth ymyl name a strict, nid o dan allwedd json_schema. Ar gyfer y modd gwrthrych JSON anfonwch {"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"]
}
}
}
} Y JSON yw text y rhan output_text o'r eitem message:
{
"output": [
{
"id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
"annotations": []
}
]
}
]
} Mae'r gefnogaeth fesul model a'r gwallau yr un fath ag ar /v1/chat/completions. Responses API
Ar /v1/messages: offeryn fel y sgema
Nid oes gan y fformat Messages faes allbwn strwythuredig. I gael JSON o'r un modelau, galwch /v1/chat/completions neu /v1/responses gyda'r un allwedd.
I aros ar /v1/messages, disgrifiwch y gwrthrych fel offeryn: rhowch eich sgema yn input_schema ac enwch yr offeryn yn tool_choice. Yna input y bloc tool_use yw eich gwrthrych, eisoes wedi'i ddadansoddi. Mae'r modelau pwysau agored a gynhelir yn cymhwyso tool_choice a enwir.
{
"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"}
}
]
} Gwiriwch fod stop_reason yn tool_use cyn i chi ddarllen y bloc. Galw swyddogaeth
Sut mae'r JSON yn cyrraedd
- Llinyn yw'r JSON. Nid oes gan yr ateb wrthrych wedi'i ddadansoddi wrth ei ymyl: rhedwch eich dosbarthwr JSON ar
message.content. - Gyda
stream: truemae'r JSON yn cyrraedd mewn darnaudelta.content. Unwch hwy a dadansoddwch unwaith y bydd y ffrwd wedi dod i ben; nid yw darn ar ei ben ei hun yn JSON dilys. - Mae model pwysau agored a gynhelir yn ateb cais
response_formatheb destun rhesymu: maereasoning_contentynnull.
Gwiriwch yr hyn a gewch
Mae'r sgema yn arwain sut mae'r model yn ysgrifennu. Nid yw'r API yn cymharu'r ateb gorffenedig â'ch sgema, felly eich cod chi sy'n gwneud y gwiriad terfynol:
- Dadansoddwch y tu mewn i drinydd gwallau, fel y mae'r enghraifft ar y brig yn ei wneud.
- Dilyswch y gwrthrych a ddadansoddwyd gyda'r un sgema yn eich cod eich hun, er enghraifft gyda llyfrgell JSON Schema, Pydantic neu Zod.
- Gall ateb hir ddod i ben cyn i'r JSON fod yn gyflawn. Mae ateb heb ei ffrydio yn adrodd
finish_reasonfelstopneutool_callsyn unig, felly gwall dadansoddi yw'r arwydd. Gofynnwch eto, neu gofynnwch am wrthrych llai.
Gwallau
| Statws | Math | Neges | Pryd |
|---|---|---|---|
400 | invalid_request_error | <id> does not accept response_format | Nid yw'r model yn cymryd yr un o'r ddau fodd. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | Mae'r model yn cymryd json_object ac anfonoch json_schema. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Nid oes gan response_format type, neu nid yw'n wrthrych JSON. |
Anfonir y ddau ateb 400 cyn tynnu dim o'ch balans. Rheoli gwallau