Eqa uye okuqukethwe
Imiphumela ehlanganisiwe

Imiphumela ehlanganisiwe

Cela impendulo njenge-JSON: noma iyiphi i-JSON object, noma i-JSON elandela i-schema oyithumelayo. Yisebenzise lapho uhlelo, hhayi umuntu, lufunda 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 ingumbhalo wempendulo, ku-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}
  }
}

Izindlela ze-response_format

I-response_format iyi-field ye-/v1/chat/completions. Ngaphandle kwayo impendulo iyombhalo ojwayelekile.

Indlela Inani Okutholayo
I-JSON object {"type": "json_object"} Impendulo iyi-JSON engenasimo esimisiwe. Chaza ku-prompt ukuthi ufuna yiziphi izikhiye.
I-JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Impendulo ilandela i-schema oyithumelayo: izikhiye zayo, izinhlobo nama-field adingekayo.
I-strict "strict": true I-field ye-json_schema, hhayi indlela yesithathu. Iyi-true ngaphandle kokuba uthumela i-false.
I-field Uhlobo Okuzenzakalelayo Incazelo Kusebenza ku-
type string json_object noma json_schema. Iyadingeka ngaphakathi kwe-response_format. I-text iyamukelwa futhi inikeza impendulo yombhalo ejwayelekile. Wonke ama-model amukela le ndlela
json_schema.schema object I-JSON Schema impendulo elandela yona. Yithumele noma nini lapho i-type iyi-json_schema. Wonke ama-model amukela le ndlela
json_schema.name string Ilebuli ye-schema, njengasesimeni se-OpenAI. Ama-model e-open-weight ahlinzekiwe
json_schema.strict boolean true Ukuthi impendulo kufanele ilandele i-schema ngqo yini. Ama-model e-Shannon asebenzisa yonke i-schema njenge-strict. Ama-model e-open-weight ahlinzekiwe

Indlela ye-JSON object ayidingi schema. Qamba izikhiye ku-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"}
}

Ukusekelwa ngama-model

I-model json_object json_schema
Wonke ama-model e-Shannon (shannon-*) Yebo Yebo
Ama-model e-open-weight ahlinzekiwe, ngaphandle kwalawo abhalwe ngezansi Yebo Yebo
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Yebo —

I-GET /v1/models ibika okufanayo nge-id ngayinye: i-capabilities.json_schema kuwo wonke ama-model, ne-capabilities.response_format kuma-model e-open-weight ahlinzekiwe. Ama-model namanani

Amagama abalulekile e-schema asekelwayo

Lokho i-model eyitholayo ku-schema yakho kuncike ku-model nakuma-endpoint:

Ama-model /v1/chat/completions /v1/responses
Ama-model e-Shannon Isethi engaphansi yamagama abalulekile engezansi, isetshenziswe njenge-strict. I-schema njengoba uyithumela, isetshenziswe njenge-strict.
Ama-model e-open-weight ahlinzekiwe I-schema njengoba uyithumela. I-strict iyi-true ngaphandle kokuba uyithumela. I-schema njengoba uyithumela. I-strict iyi-true ngaphandle kokuba uyithumela.

Isethi engaphansi yamagama abalulekile yama-model e-Shannon ku-/v1/chat/completions:

Igama elibalulekile Ukuthi ifundwa kanjani
Amagama abalulekile afundwayo type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref Ixazululwa ku-$defs noma ku-definitions esezingeni eliphezulu le-schema. Amagama abalulekile abhalwe eduze kwe-$ref, afana ne-description, agcinwa. Inkomba engenayo incazelo ehambisanayo ifundwa njenge-{"type": "object"}.
allOf Amalungu ahlanganiswa abe yi-schema eyodwa. Lapho amalungu amabili esetha igama elifanayo, lelo lamuva liyabalwa.
oneOf Ifundwa njenge-anyOf.
Ama-schema azibhekisela kuwo uqobo Anwetshwa aze afike emazingeni angu-24. Izinga elijule kakhulu lifundwa njenge-{"type": "object"}.
Ngaphandle kwesethi engaphansi Wonke amanye amagama abalulekile, isibonelo additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not ne-if / then / else. Lawa akayona ingxenye ye-schema i-model eyitholayo.

Ku-/v1/responses: text.format

I-/v1/responses ibhala isicelo esifanayo njenge-text.format. I-schema ihlala ngqo ku-format, eduze kwe-name ne-strict, hhayi ngaphansi kokhiye ongu-json_schema. Ngendlela 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 iyi-text yengxenye 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": []
        }
      ]
    }
  ]
}

Ukusekelwa ngama-model namaphutha kufana nakuleyo ye-/v1/chat/completions. I-Responses API

Ku-/v1/messages: ithuluzi njenge-schema

Isimo se-Messages asinayo i-field yokuphumayo okuhlelekile. Ukuthola i-JSON kuma-model afanayo, biza i-/v1/chat/completions noma i-/v1/responses nge-key efanayo.

Ukuhlala ku-/v1/messages, chaza i-object njengethuluzi: faka i-schema yakho ku-input_schema bese uqamba ithuluzi ku-tool_choice. I-input yebhlokhi ye-tool_use ibe yi-object yakho, esivele ihlaziyiwe. I-tool_choice enegama isetshenziswa ama-model e-open-weight ahlinzekiwe.

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 ukuthi i-stop_reason iyi-tool_use ngaphambi kokufunda ibhlokhi. Ukubiza umsebenzi

Ukuthi i-JSON ifika kanjani

  • I-JSON iyi-string. Impendulo ayinayo i-object ehlaziyiwe eceleni kwayo: qhuba i-JSON parser yakho ku-message.content.
  • Nge-stream: true i-JSON ifika ngezingcezu ze-delta.content. Zihlanganise bese uyihlaziya i-stream isiphelile; ucezu olodwa ayiyona i-JSON esebenzayo.
  • I-model e-open-weight ehlinzekiwe iphendula isicelo se-response_format ngaphandle kombhalo wokucabanga: i-reasoning_content iyi-null.

Hlola lokho okutholayo

I-schema iqondisa ukuthi i-model ibhala kanjani. I-API ayiqhathanisi impendulo eqediwe ne-schema yakho, ngakho ikhodi yakho yenza ukuhlola kokugcina:

  • Hlaziya ngaphakathi kwe-error handler, njengesibonelo esiphezulu senza.
  • Qinisekisa i-object ehlaziyiwe nge-schema efanayo ekhodini yakho, isibonelo nge-JSON Schema library, i-Pydantic noma i-Zod.
  • Impendulo ende ingaphela ngaphambi kokuba i-JSON iphelele. Impendulo engeyona i-stream ibika i-finish_reason njenge-stop noma tool_calls kuphela, ngakho iphutha lokuhlaziya liyisiginali. Cela futhi, noma ucele i-object encane.

Amaphutha

Isimo Uhlobo Umlayezo Nini
400 invalid_request_error <id> does not accept response_format I-model ayamukeli neyodwa yezindlela.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object I-model yamukela i-json_object futhi uthumele i-json_schema.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … I-response_format ayinayo i-type, noma ayiyona i-JSON object.

Izimpendulo ezimbili ze-400 zithunyelwa ngaphambi kokuba kuthathwe okuthile ku-balance yakho. Ukuphathwa kwamaphutha