Tlola ho ea ho litaba
Diphumo tse hlophisitsoeng

Diphumo tse hlophisitsoeng

Kopa karabo e le JSON: ntho efe kapa efe ea JSON, kapa JSON e latelang schema eo u e romelang. E sebelise ha lenaneo, eseng motho, le bala karabo.

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 ke mongolo oa karabo, ho 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}
  }
}

Mekhoa ea response_format

response_format ke field ea /v1/chat/completions. Ntle le eona karabo ke mongolo o tloaelehileng.

Mokhoa Boleng Seo u se fumanang
Ntho ea JSON {"type": "json_object"} Karabo ke JSON ntle le sebopeho se laetsoeng. Re ho prompt hore u batla li-key life.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Karabo e latela schema eo u e romelang: li-key tsa eona, mefuta le li-field tse hlokahalang.
Strict "strict": true Field ea json_schema, hase mokhoa oa boraro. Ke true ntle le haeba u romela false.
Field Mofuta Default Tlhaloso E sebelisoa ke
type string json_object kapa json_schema. E hlokahala ka hare ho response_format. text e amohelwa mme e fana ka karabo e tloaelehileng ea mongolo. Model e 'ngoe le e 'ngoe e amohelang mokhoa ona
json_schema.schema object JSON Schema eo karabo e e latelang. E romele neng kapa neng ha type e le json_schema. Model e 'ngoe le e 'ngoe e amohelang mokhoa ona
json_schema.name string Lebitso la schema, joalo ka ho sebopeho sa OpenAI. Li-model tse hosted tsa open-weight
json_schema.strict boolean true Hore na karabo e tlameha ho latela schema ka nepo. Li-model tsa Shannon li sebelisa schema e 'ngoe le e 'ngoe e le strict. Li-model tse hosted tsa open-weight

JSON object mode ha e hloke schema. Rea li-key ho prompt:

Body ea kopo
{
  "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šehetso ka model

Model json_object json_schema
Model e 'ngoe le e 'ngoe ea Shannon (shannon-*) Ee Ee
Li-model tse hosted tsa open-weight, ntle le tse rehiloeng ka tlase Ee Ee
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Ee —

GET /v1/models e tlaleha ntho e tšoanang ka id: capabilities.json_schema ho model e 'ngoe le e 'ngoe, le capabilities.response_format ho li-model tse hosted tsa open-weight. Li-model le litheko

Mantsoe a bohlokoa a schema a tšehetsoang

Seo model e se amohelang sa schema ea hau se itšetlehile ka model le endpoint:

Li-model /v1/chat/completions /v1/responses
Li-model tsa Shannon Subset ea mantsoe a bohlokoa e ka tlase, e sebelisoang e le strict. Schema joalo ka ha u e romela, e sebelisoang e le strict.
Li-model tse hosted tsa open-weight Schema joalo ka ha u e romela. strict ke true ntle le haeba u e romela. Schema joalo ka ha u e romela. strict ke true ntle le haeba u e romela.

Subset ea mantsoe a bohlokoa ea li-model tsa Shannon ho /v1/chat/completions:

Lentsoe la bohlokoa Hore na e baloa joang
Mantsoe a baloang type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref E rarolloa ho tsoa ho $defs kapa definitions boemong bo holimo ba schema. Mantsoe a ngotsoeng haufi le $ref, a kang description, a bolokoa. Tšupiso e se nang tlhaloso e lumellanang e baloa e le {"type": "object"}.
allOf Litho li kopanngoa schema e le 'ngoe. Moo litho tse peli li beha lentsoe le le leng, ea morao e a sebetsa.
oneOf E baloa e le anyOf.
Li-schema tse itshupang Li atolosoa ho maemo a 24. Boemo bo tebileng ho feta bo baloa e le {"type": "object"}.
Ka ntle ho subset Lentsoe le leng le le leng, mohlala additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not le if / then / else. Tsena hase karolo ea schema eo model e e amohelang.

Ho /v1/responses: text.format

/v1/responses e ngola kopo e tšoanang e le text.format. Schema e ho format ka ho toba, haufi le name le strict, eseng tlasa key ea json_schema. Bakeng sa JSON object mode romela {"format": {"type": "json_object"}}.

Body ea kopo
{
  "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 ke text ea karolo ea output_text ea ntho ea 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": []
        }
      ]
    }
  ]
}

Tšehetso ka model le liphoso li tšoana le ho /v1/chat/completions. Responses API

Ho /v1/messages: tool e le schema

Sebopeho sa Messages ha se na field ea structured-output. Bakeng sa JSON ho li-model tse tšoanang, letsetsa /v1/chat/completions kapa /v1/responses ka senotlolo se tšoanang.

Ho lula ho /v1/messages, hlalosa ntho e le tool: kenya schema ea hau ho input_schema mme u rehe tool ho tool_choice. input ea block ea tool_use e ntan e ba ntho ea hau, e se e parse-ntsoe. tool_choice e rehiloeng e sebelisoa ke li-model tse hosted tsa open-weight.

Body ea kopo
{
  "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"}
    }
  ]
}

Hlahloba hore stop_reason ke tool_use pele u bala block. Mekhosi ya mesebetsi

Hore na JSON e fihla joang

  • JSON ke string. Karabo ha e na ntho e parse-ntsoeng haufi le eona: sebelisa JSON parser ea hau ho message.content.
  • Ka stream: true JSON e fihla e le likhechana tsa delta.content. Li kopanye mme u parse ka mor'a hore stream e fele; sekhechana se le seng ha se JSON e nepahetseng.
  • Model e hosted ea open-weight e araba kopo ea response_format ntle le mongolo oa reasoning: reasoning_content ke null.

Hlahloba seo u se amohelang

Schema e tataisa hore na model e ngola joang. API ha e bapise karabo e phethiloeng le schema ea hau, kahoo code ea hau e etsa hlahlobo ea ho qetela:

  • Parse ka hare ho error handler, joalo ka ha mohlala o ka holimo o etsa.
  • Netefatsa ntho e parse-ntsoeng ka schema e tšoanang ho code ea hau, mohlala ka JSON Schema library, Pydantic kapa Zod.
  • Karabo e telele e ka fela pele JSON e phethahala. Karabo e se nang streaming e tlaleha finish_reason e le stop kapa tool_calls feela, kahoo phoso ea parse ke letšoao. Botsa hape, kapa u kope ntho e nyane.

Liphoso

Boemo Mofuta Molaetsa Neng
400 invalid_request_error <id> does not accept response_format Model ha e amohele mokhoa ofe kapa ofe.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object Model e amohela json_object mme u rometse json_schema.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … response_format ha e na type, kapa hase ntho ea JSON.

Likarabo tse peli tsa 400 li romeloa pele ho letho le nkoang ho balance ea hau. Diphoso