Peke ki ngā ihirangi
Putanga hanganga

Putanga hanganga

Tonoa te whakautu hei JSON: ahakoa tēhea ahanoa JSON, he JSON rānei e whai ana i tētahi schema ka tukuna e koe. Whakamahia ina pānui tētahi hōtaka, kaua tētahi tangata, i te whakautu.

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)

Ko te JSON te kuputuhi o te whakautu, i te 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}
  }
}

Ngā āhua o te response_format

He āpure o te /v1/chat/completions te response_format. Ki te kore ia, he kuputuhi noa te whakautu.

Āhua Uara Te mea ka whiwhi koe
Ahanoa JSON {"type": "json_object"} He JSON te whakautu kāore he āhua kua whakaritea. Whakaaturia i te prompt ngā kī e hiahia ana koe.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Ka whai te whakautu i te schema ka tukuna e koe: ōna kī, momo me āpure e hiahiatia ana.
Strict "strict": true He āpure o te json_schema, ehara i te āhua tuatoru. Ko true mēnā kāore koe e tuku i te false.
Āpure Momo Taunoa Whakaahuatanga Whakamahia e
type string json_object json_schema rānei. E hiahiatia ana i roto i te response_format. E whakaaetia ana te text, ā, ka hoatu he whakautu kuputuhi noa. Ia model e whakaae ana ki te āhua
json_schema.schema object Te JSON Schema e whai ai te whakautu. Tukuna ina he json_schema te type. Ia model e whakaae ana ki te āhua
json_schema.name string He tapanga mō te schema, pēnei i te hōputu OpenAI. Ngā model open-weight hosted
json_schema.strict boolean true Mēnā me tino whai te whakautu i te schema. Ka whakamahi ngā model Shannon i ia schema hei strict. Ngā model open-weight hosted

Kāore te āhua ahanoa JSON e hiahia ki tētahi schema. Whakaingoatia ngā kī i te prompt:

Tinana tono
{
  "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"}
}

Te tautoko mō ia model

Model json_object json_schema
Ia model Shannon (shannon-*) Āe Āe
Ngā model open-weight hosted, haunga ērā e whakaingoatia ana i raro iho Āe Āe
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Āe —

Ka pūrongo te GET /v1/models i te mea ōrite mō ia id: capabilities.json_schema i ia model, me te capabilities.response_format i ngā model open-weight hosted. Ngā model me ngā utu

Ngā kupu matua schema e tautokohia ana

Ko te mea e whiwhi ai tētahi model o tō schema e whakawhirinaki ana ki te model me te endpoint:

Ngā model /v1/chat/completions /v1/responses
Ngā model Shannon Te wāhanga iti o ngā kupu matua i raro iho, ka whakamahia hei strict. Te schema pēnei i tō tuku, ka whakamahia hei strict.
Ngā model open-weight hosted Te schema pēnei i tō tuku. Ko true te strict mēnā kāore koe e tuku. Te schema pēnei i tō tuku. Ko true te strict mēnā kāore koe e tuku.

Te wāhanga iti o ngā kupu matua a ngā model Shannon i runga i te /v1/chat/completions:

Kupu matua Te āhua o te pānui
Ngā kupu matua e pānuitia ana type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref Ka whakatauhia mai i te $defs i te definitions rānei i te taumata o runga o te schema. Ka puritia ngā kupu matua kua tuhia i te taha o te $ref, pēnei i te description. Ka pānuitia tētahi tohutoro kāore he whakamāramatanga e ōrite ana hei {"type": "object"}.
allOf Ka whakakotahitia ngā mema hei schema kotahi. Ina tautuhi ngā mema e rua i te kupu matua ōrite, ka tatau te mea o muri.
oneOf Ka pānuitia hei anyOf.
Ngā schema e tohu ana ki a rātou ake Ka whakawhānuitia ki te 24 taumata. Ka pānuitia tētahi taumata hōhonu ake hei {"type": "object"}.
I waho i te wāhanga iti Ia kupu matua kē atu, hei tauira additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not me if / then / else. Ehara ēnei i te wāhanga o te schema e whiwhi ana te model.

I runga i te /v1/responses: text.format

Ka tuhi te /v1/responses i te tono ōrite hei text.format. Kei te format tonu te schema, i te taha o te name me te strict, kaua i raro i tētahi kī json_schema. Mō te āhua ahanoa JSON tukuna te {"format": {"type": "json_object"}}.

Tinana tono
{
  "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"]
      }
    }
  }
}

Ko te JSON te text o te wāhanga output_text o te tūemi 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": []
        }
      ]
    }
  ]
}

He ōrite te tautoko mō ia model me ngā hapa ki te /v1/chat/completions. Responses API

I runga i te /v1/messages: he tool hei schema

Kāore he āpure output whakaritea i te hōputu Messages. Mō te JSON mai i ngā model ōrite, karangatia te /v1/chat/completions te /v1/responses rānei me te key ōrite.

Kia noho ki te /v1/messages, whakaahuatia te ahanoa hei tool: whakanōhia tō schema ki te input_schema, ā, whakaingoatia te tool i te tool_choice. Kātahi ko te input o te poraka tool_use tō ahanoa, kua parauhia kē. Ka whakamahia tētahi tool_choice whakaingoa e ngā model open-weight hosted.

Tinana tono
{
  "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"}
    }
  ]
}

Tirohia he tool_use te stop_reason i mua i te pānui i te poraka. Karanga ā‑mahi

Te āhua o te taenga mai o te JSON

  • He aho te JSON. Kāore he ahanoa kua parauhia i tōna taha i te whakautu: whakahaerehia tō kaitātari JSON ki te message.content.
  • Ki te stream: true ka tae mai te JSON i ngā wāhanga delta.content. Honoa, ā, parauhia ina mutu te stream; ehara tētahi wāhanga mōna ake i te JSON tika.
  • Ka whakautu tētahi model open-weight hosted ki tētahi tono response_format me te kore kuputuhi reasoning: he null te reasoning_content.

Tirohia te mea ka whiwhi koe

Ka arahi te schema i te āhua o te tuhi a te model. Kāore te API e whakataurite i te whakautu kua oti ki tō schema, nō reira nā tō waehere te tirohanga whakamutunga:

  • Parauhia i roto i tētahi kaiwhakahaere hapa, pēnei i te tauira i te tihi.
  • Manatārautia te ahanoa kua parauhia ki te schema ōrite i tō ake waehere, hei tauira mā tētahi whare pukapuka JSON Schema, Pydantic, Zod rānei.
  • Ka taea te mutu o tētahi whakautu roa i mua i te oti o te JSON. Ka pūrongo anake tētahi whakautu kāore e stream i te finish_reason hei stop hei tool_calls rānei, nō reira ko te hapa parau te tohu. Pātai anō, tono rānei i tētahi ahanoa iti ake.

Ngā hapa

Tūnga Momo Karere Āhea
400 invalid_request_error <id> does not accept response_format Kāore te model e whakaae ki tētahi o ngā āhua e rua.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object Ka whakaae te model ki te json_object, ā, i tukuna e koe te json_schema.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … Kāore he type o te response_format, ehara rānei i te ahanoa JSON.

Ka tukuna ngā whakautu 400 e rua i mua i te tangohanga o tētahi mea i tō toenga. Whakahaere hapa