U gudub nuxurka
Waxsoosaarka qaabaysan

Waxsoosaarka qaabaysan

Jawaabta codso sida JSON: shay JSON ah oo kasta, ama JSON raacaya schema aad dirto. Isticmaal marka barnaamij, ma aha qof, jawaabta akhriyo.

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)

JSON-ku waa qoraalka jawaabta, choices[0].message.content:

200 JSON
{
  "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}
  }
}

Qaababka response_format

response_format waa field ka mid ah /v1/chat/completions. La'aantiis jawaabtu waa qoraal caadi ah.

Qaab Qiime Waxa aad heshid
Shay JSON {"type": "json_object"} Jawaabtu waa JSON aan lahayn qaab la qoray. Prompt-ka ku sheeg furayaasha aad rabto.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Jawaabtu waxay raacdaa schema aad dirto: furayaasheeda, noocyadeeda iyo field-yada loo baahan yahay.
Strict "strict": true Field ka mid ah json_schema, ma aha qaab saddexaad. Waa true haddii aadan dirin false.
Field Nooc Default Sharaxaad Waxay dabaqaan
type string json_object ama json_schema. Loo baahan yahay response_format dhexdiisa. text waa la aqbalaa wuxuuna siiyaa jawaab qoraal caadi ah. Model kasta oo qaata qaabka
json_schema.schema object JSON Schema-ha jawaabtu raacayso. Dir markasta oo type yahay json_schema. Model kasta oo qaata qaabka
json_schema.name string Calaamad loogu talagalay schema-ha, sida qaabka OpenAI. Model-yada open-weight ee la martigeliyo
json_schema.strict boolean true Haddii jawaabtu tahay inay schema-ha si sax ah u raacdo. Model-yada Shannon schema kasta waxay u dabaqaan sida strict. Model-yada open-weight ee la martigeliyo

Qaabka shay JSON ah schema ma u baahna. Furayaasha ku magacaw prompt-ka:

Jidhka codsiga
{
  "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"}
}

Taageerada model kasta

Model json_object json_schema
Model kasta oo Shannon ah (shannon-*) Haa Haa
Model-yada open-weight ee la martigeliyo, marka laga reebo kuwa hoose lagu magacaabay Haa Haa
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Haa —

GET /v1/models wuxuu sheegaa isla shaygaas id kasta: capabilities.json_schema model kasta, iyo capabilities.response_format model-yada open-weight ee la martigeliyo. Model-yo & qiimo

Erayada schema ee la taageero

Waxa model-ku ka helo schema-gaaga wuxuu ku xiran yahay model-ka iyo endpoint-ka:

Model-yo /v1/chat/completions /v1/responses
Model-yada Shannon Qayb-yarta erayada hoose, oo loo dabaqay sida strict. Schema-ha sida aad u dirto, oo loo dabaqay sida strict.
Model-yada open-weight ee la martigeliyo Schema-ha sida aad u dirto. strict waa true haddii aadan dirin. Schema-ha sida aad u dirto. strict waa true haddii aadan dirin.

Qayb-yarta erayada model-yada Shannon /v1/chat/completions:

Eray muhiim ah Sida loo akhriyo
Erayada la akhriyo type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref Waxaa laga xalliyaa $defs ama definitions heerka sare ee schema-ha. Erayada $ref agtiisa lagu qoray, sida description, waa la hayaa. Tixraac aan lahayn qeexid waafaqsan waxaa loo akhriyaa sida {"type": "object"}.
allOf Xubnaha waxaa lagu daray hal schema. Halka laba xubnood isla eray dejiyaan, mid dambe ayaa tirsan.
oneOf Waxaa loo akhriyaa sida anyOf.
Schemas is tixraacaya Waxaa la balaadhiyaa ilaa 24 heer. Heer ka qoto dheer waxaa loo akhriyaa sida {"type": "object"}.
Qayb-yarta ka baxsan Eray kasta oo kale, tusaale ahaan additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not iyo if / then / else. Kuwani qayb kama aha schema-ha model-ku helo.

/v1/responses: text.format

/v1/responses wuxuu isla codsiga u qoraa text.format. Schema-hu wuxuu si toos ah ugu jiraa format, name iyo strict agtooda, ma aha furaha json_schema hoostiisa. Qaabka shay JSON ah dir {"format": {"type": "json_object"}}.

Jidhka codsiga
{
  "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-ku waa text ee qaybta output_text ee shayga message:

200 JSON
{
  "output": [
    {
      "id": "msg_7e1d3c5b9a2f4d6e8c0b1a3f5e7d9c2b",
      "type": "message",
      "role": "assistant",
      "status": "completed",
      "content": [
        {
          "type": "output_text",
          "text": "{\"name\":\"John Doe\",\"age\":30,\"occupation\":\"engineer\"}",
          "annotations": []
        }
      ]
    }
  ]
}

Taageerada model kasta iyo khaladaadku waxay la mid yihiin /v1/chat/completions. Responses API

/v1/messages: tool sida schema

Qaabka Messages ma laha field structured-output. JSON ka timaada isla model-yada, wac /v1/chat/completions ama /v1/responses isla furahaaga.

Si aad ugu sii jirto /v1/messages, shayga u sharax sida tool: geli schema-gaaga input_schema oo tool-ka magacaw tool_choice. input ee block-ga tool_use kaddib waa shayga, horeba la parse-gareeyay. tool_choice la magacaabay waxaa dabaqa model-yada open-weight ee la martigeliyo.

Jidhka codsiga
{
  "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"}
}
200 JSON
{
  "stop_reason": "tool_use",
  "content": [
    {
      "type": "tool_use",
      "id": "toolu_4b8d2f6a1c3e4a5b9d7f0e2c4a6b8d1f",
      "name": "record_person",
      "input": {"name": "John Doe", "age": 30, "occupation": "engineer"}
    }
  ]
}

Hubi in stop_reason yahay tool_use ka hor inta aadan akhrin block-ga. Wacyigelinta Shaqada

Sida JSON-ku u yimaado

  • JSON-ku waa string. Jawaabtu shay la parse-gareeyay agteeda ma haysato: JSON parser-kaaga ku orodsii message.content.
  • Marka stream: true JSON-ku wuxuu u yimaadaa gabal-gabal delta.content. Isku xir oo parse-garee marka stream-ku dhammaado; gabal keligiis ah ma aha JSON sax ah.
  • Model open-weight oo la martigeliyay wuxuu ku jawaabaa codsi response_format oo qoraal reasoning la'aan ah: reasoning_content waa null.

Hubi waxa aad heshay

Schema-hu wuxuu hagaa sida model-ku u qoro. API-gu jawaabta dhammaystiran schema-gaaga kuma barbar dhigo, sidaas darteed code-kaagu wuxuu sameeyaa hubinta ugu dambeysa:

  • Parse-garee gudaha error handler, sida tusaalaha kore.
  • Shayga la parse-gareeyay ku xaqiiji isla schema code-kaaga, tusaale ahaan JSON Schema library, Pydantic ama Zod.
  • Jawaab dheer waxay ku dhammaan kartaa ka hor inta JSON-ku dhammaystirmin. Jawaab aan stream ahayn waxay sheegtaa finish_reason sida stop ama tool_calls oo keliya, sidaas darteed khalad parse waa calaamadda. Mar kale codso, ama codso shay ka yar.

Khaladaad

Xaalad Nooc Fariin Goorta
400 invalid_request_error <id> does not accept response_format Model-ku labada qaab midna ma qaato.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object Model-ku wuxuu qaataa json_object adiguna waxaad dirtay json_schema.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … response_format ma laha type, ama ma aha shay JSON ah.

Labada jawaab ee 400 waxaa la diraa ka hor inta wax laga qaadin haraaggaaga. Khaladaadka Maareynta