Yiya kumxholo
Iziphumo ezicwangcisiweyo

Iziphumo ezicwangcisiweyo

Cela impendulo njenge-JSON: nayiphi na i-JSON object, okanye i-JSON elandela i-schema oyithumelayo. Yisebenzise xa iprogram, hayi umntu, ifunda impendulo.

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)

I-JSON ngumbhalo wempendulo, kwi-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}
  }
}

Iimowudi ze-response_format

I-response_format yintsimi ye-/v1/chat/completions. Ngaphandle kwayo impendulo ngumbhalo oqhelekileyo.

Imowudi Ixabiso Oko ukufumanayo
I-JSON object {"type": "json_object"} Impendulo yi-JSON ngaphandle kwesimo esimiselweyo. Chaza kwi-prompt ukuba zeziphi izitshixo ozifunayo.
I-JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Impendulo ilandela i-schema oyithumelayo: izitshixo zayo, iintlobo kunye neentsimi ezifunekayo.
Strict "strict": true Intsimi ye-json_schema, hayi imowudi yesithathu. Yi-true ngaphandle kokuba uthumela i-false.
Intsimi Uhlobo Okuqhelekileyo Inkcazelo Isebenza kwi
type string json_object okanye json_schema. Iyafuneka ngaphakathi kwe-response_format. I-text iyamkelwa kwaye inika impendulo eqhelekileyo yombhalo. Yonke imodeli eyamkelayo imowudi
json_schema.schema object I-JSON Schema elandelwa yimpendulo. Yithumele nanini na i-type yi-json_schema. Yonke imodeli eyamkelayo imowudi
json_schema.name string Ileyibhile ye-schema, njengakwifomathi ye-OpenAI. Iimodeli ze-open-weight ezisingathiweyo
json_schema.strict boolean true Ukuba impendulo kufuneka ilandele i-schema ngokuchanekileyo. Iimodeli zeShannon zisebenzisa yonke i-schema njenge-strict. Iimodeli ze-open-weight ezisingathiweyo

Imowudi ye-JSON object ayifuni schema. Dwelisa izitshixo kwi-prompt:

Umzimba wesicelo
{
  "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"}
}

Inkxaso ngemodeli

Imodeli json_object json_schema
Yonke imodeli yeShannon (shannon-*) Ewe Ewe
Iimodeli ze-open-weight ezisingathiweyo, ngaphandle kwezo zikhankanywe ngezantsi Ewe Ewe
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Ewe —

I-GET /v1/models ibika into efanayo nge-id nganye: i-capabilities.json_schema kuyo yonke imodeli, kunye ne-capabilities.response_format kwiimodeli ze-open-weight ezisingathiweyo. Iimodeli namaxabiso

Amagama angundoqo e-schema axhaswayo

Oko imodeli ikufumanayo kwi-schema yakho kuxhomekeke kwimodeli nakwi-endpoint:

Iimodeli /v1/chat/completions /v1/responses
Iimodeli zeShannon Isiqephu samagama angundoqo ngezantsi, sisetyenziswa njenge-strict. I-schema njengoko uyithumela, isetyenziswa njenge-strict.
Iimodeli ze-open-weight ezisingathiweyo I-schema njengoko uyithumela. I-strict yi-true ngaphandle kokuba uyithumele. I-schema njengoko uyithumela. I-strict yi-true ngaphandle kokuba uyithumele.

Isiqephu samagama angundoqo eemodeli zeShannon kwi-/v1/chat/completions:

Igama elingundoqo Indlela efundwa ngayo
Amagama angundoqo afundwayo type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref Isonjululwa ukusuka kwi-$defs okanye kwi-definitions kwinqanaba eliphezulu le-schema. Amagama angundoqo abhalwe ecaleni kwe-$ref, anjenge-description, ayagcinwa. Isalathiso esingenacaciso ehambelanayo sifundwa njenge-{"type": "object"}.
allOf Amalungu adityaniswa abe yi-schema enye. Apho amalungu amabini emisela igama elingundoqo elifanayo, elamva liyabalwa.
oneOf Ifundwa njenge-anyOf.
Ii-schema ezizalathisa kuzo Zandiswa ukuya kumanqanaba angama-24. Inqanaba elinzulu ngakumbi lifundwa njenge-{"type": "object"}.
Ngaphandle kwesiqephu Elinye ilonke igama elingundoqo, umzekelo additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not kunye ne-if / then / else. Ezi azisiyonxalenye ye-schema efunyanwa yimodeli.

Kwi-/v1/responses: text.format

I-/v1/responses ibhala isicelo esifanayo njenge-text.format. I-schema ihlala ngqo kwi-format, ecaleni kwe-name kunye ne-strict, hayi phantsi kwesitshixo se-json_schema. Kwimowudi ye-JSON object thumela i-{"format": {"type": "json_object"}}.

Umzimba wesicelo
{
  "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"]
      }
    }
  }
}

I-JSON yi-text yenxalenye ye-output_text yento ye-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": []
        }
      ]
    }
  ]
}

Inkxaso ngemodeli kunye neempazamo zifana nakwi-/v1/chat/completions. I-Responses API

Kwi-/v1/messages: i-tool njenge-schema

Ifomathi ye-Messages ayinayo intsimi yokuphuma okucwangcisiweyo. Ukufumana i-JSON kwiimodeli ezifanayo, biza i-/v1/chat/completions okanye i-/v1/responses ngesitshixo esifanayo.

Ukuhlala kwi-/v1/messages, chaza i-object njenge-tool: beka i-schema yakho kwi-input_schema kwaye uthiye igama le-tool kwi-tool_choice. I-input yebhloko ye-tool_use ke yi-object yakho, esele ihlalutyiwe. I-tool_choice enegama isetyenziswa ziimodeli ze-open-weight ezisingathiweyo.

Umzimba wesicelo
{
  "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"}
    }
  ]
}

Qinisekisa ukuba i-stop_reason yi-tool_use phambi kokuba ufunde ibhloko. Umsebenzi Ukufowuna

Indlela i-JSON efika ngayo

  • I-JSON ngumtya. Impendulo ayinayo i-object ehlalutyiweyo ecaleni kwayo: qhuba i-JSON parser yakho kwi-message.content.
  • Nge-stream: true i-JSON ifika ngezahlulo ze-delta.content. Zidibanise uze uhlalutye kanye emva kokuba i-stream iphelile; isahlulo esithile asiyiyo i-JSON esebenzayo.
  • Imodeli ye-open-weight esingathiweyo iphendula isicelo se-response_format ngaphandle kombhalo wokuqiqa: i-reasoning_content yi-null.

Hlola oko ukufumanayo

I-schema ikhokela indlela imodeli ebhala ngayo. I-API ayithelekisi mpendulo egqityiweyo ne-schema yakho, ngoko ikhowudi yakho yenza uhlolo lokugqibela:

  • Hlalutya ngaphakathi kwe-error handler, njengomzekelo ophezulu.
  • Qinisekisa i-object ehlalutyiweyo ngale schema ifanayo kwikhowudi yakho, umzekelo ngelayibrari ye-JSON Schema, i-Pydantic okanye i-Zod.
  • Impendulo ende inokuphela phambi kokuba i-JSON igqibelele. Impendulo engekho nge-stream ibika i-finish_reason njenge-stop okanye i-tool_calls kuphela, ngoko impazamo yokuhlalutya ngumqondiso. Cela kwakhona, okanye cela i-object encinci.

Iimpazamo

Isimo Uhlobo Umyalezo Xa
400 invalid_request_error <id> does not accept response_format Imodeli ayiyamkeli nayiphi na imowudi.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object Imodeli yamkela i-json_object kwaye uthumele i-json_schema.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … I-response_format ayinayo i-type, okanye ayiyi-JSON object.

Iimpendulo ezimbini ze-400 zithunyelwa phambi kokuba kuthathwe nantoni na kwibhalansi yakho. Ukuphatha imposiso