Gafee gaa n'ọdịnaya
Mmepụta ahaziri

Mmepụta ahaziri

Rịọ azịza dị ka JSON: JSON object ọ bụla, ma ọ bụ JSON na-eso schema i zipụrụ. Jiri ya mgbe mmemme, ọ bụghị mmadụ, na-agụ azịza.

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 bụ ederede azịza, na 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}
  }
}

Modes nke response_format

response_format bụ field nke /v1/chat/completions. Na-enweghị ya azịza bụ text nkịtị.

Mode Uru Ihe ị na-enweta
JSON object {"type": "json_object"} Azịza bụ JSON na-enweghị ọdịdị edoziri. Kwuo na prompt keys ndị ịchọrọ.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Azịza na-eso schema i zipụrụ: keys ya, types na fields chọrọ achọrọ.
Strict "strict": true Field nke json_schema, ọ bụghị mode nke atọ. Ọ bụ true belụsọ ị zipụ false.
Field Ụdị Default Nkọwa Ndị na-etinye ya
type string json_object ma ọ bụ json_schema. Achọrọ n'ime response_format. A na-anabata text ma na-enye azịza text nkịtị. Model ọ bụla na-anara mode ahụ
json_schema.schema object JSON Schema azịza na-eso. Zipụ ya mgbe ọ bụla type bụ json_schema. Model ọ bụla na-anara mode ahụ
json_schema.name string Akara maka schema, dị ka n'OpenAI format. Hosted open-weight models
json_schema.strict boolean true Ma azịza ga-eso schema ahụ kpọmkwem. Shannon models na-etinye schema ọ bụla dị ka strict. Hosted open-weight models

JSON object mode achọghị schema. Kpọọ keys aha na prompt:

Request body
{
  "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"}
}

Nkwado kwa model

Model json_object json_schema
Shannon model ọ bụla (shannon-*) Ee Ee
Hosted open-weight models, ewezuga ndị akpọrọ aha n'okpuru Ee Ee
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Ee —

GET /v1/models na-akọ otu ihe ahụ kwa id: capabilities.json_schema na model ọ bụla, na capabilities.response_format na hosted open-weight models. Models & ọnụahịa

Schema keywords akwadoro

Ihe model na-anata n'schema gị na-adabere na model na endpoint:

Models /v1/chat/completions /v1/responses
Shannon models Keyword subset dị n'okpuru, etinyere dị ka strict. Schema dị ka i zipụrụ ya, etinyere dị ka strict.
Hosted open-weight models Schema dị ka i zipụrụ ya. strict bụ true belụsọ ị zipụ ya. Schema dị ka i zipụrụ ya. strict bụ true belụsọ ị zipụ ya.

Keyword subset nke Shannon models na /v1/chat/completions:

Keyword Otu a na-agụ ya
Keywords a na-agụ type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref A na-edozi ya site na $defs ma ọ bụ definitions n'ọkwa elu nke schema. A na-edebe keywords e dere n'akụkụ $ref, dị ka description. A na-agụ ntụaka na-enweghị nkọwa kwekọrọ dị ka {"type": "object"}.
allOf A na-ejikọta members n'otu schema. Ebe members abụọ tọrọ otu keyword, nke ikpeazụ na-agụ.
oneOf A na-agụ ya dị ka anyOf.
Schemas na-ezo aka n'onwe ha Gbasaara ruo ọkwa 24. A na-agụ ọkwa ka omimi dị ka {"type": "object"}.
N'èzí subset Keyword ọ bụla ọzọ, dịka additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not na if / then / else. Ndị a abụghị akụkụ nke schema model na-anata.

Na /v1/responses: text.format

/v1/responses na-ede otu request ahụ dị ka text.format. Schema dị ozugbo na format, n'akụkụ name na strict, ọ bụghị n'okpuru key json_schema. Maka JSON object mode zipụ {"format": {"type": "json_object"}}.

Request body
{
  "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 bụ text nke output_text part nke message item:

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": []
        }
      ]
    }
  ]
}

Nkwado kwa model na njehie bụ otu ihe ahụ dị ka na /v1/chat/completions. Responses API

Na /v1/messages: tool dị ka schema

Messages format enweghị structured-output field. Maka JSON site na otu models, kpọọ /v1/chat/completions ma ọ bụ /v1/responses na otu key.

Iji nọgide na /v1/messages, kọwaa object ahụ dị ka tool: tinye schema gị n'ime input_schema ma kpọọ tool ahụ aha na tool_choice. input nke tool_use block bụ object gị, parsed ugbu a. Hosted open-weight models na-etinye tool_choice akpọrọ aha.

Request body
{
  "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"}
    }
  ]
}

Lelee na stop_reason bụ tool_use tupu ị gụọ block ahụ. Oku ọrụ

Otu JSON si abịa

  • JSON bụ string. Azịza enweghị parsed object n'akụkụ ya: gbaa JSON parser gị na message.content.
  • Site na stream: true JSON na-abịa n'akụkụ delta.content. Jikọta ha ma parse otu ugboro mgbe stream gwụrụ; otu akụkụ naanị abụghị JSON ziri ezi.
  • Hosted open-weight model na-aza response_format request na-enweghị reasoning text: reasoning_content bụ null.

Lelee ihe ị natara

Schema na-eduzi otu model si ede. API anaghị atụnyere azịza gwụchara na schema gị, ya mere koodu gị na-eme nlele ikpeazụ:

  • Parse n'ime error handler, dị ka sample dị n'elu na-eme.
  • Kwado parsed object na otu schema ahụ na koodu gị, dịka site na JSON Schema library, Pydantic ma ọ bụ Zod.
  • Azịza ogologo nwere ike ịgwụ tupu JSON zuo ezu. Azịza abụghị stream na-akọ finish_reason dị ka stop ma ọ bụ tool_calls naanị, ya mere njehie parse bụ ihe ngosi. Jụọ ọzọ, ma ọ bụ rịọ object ka nta.

Njehie

Status Ụdị Ozi Mgbe
400 invalid_request_error <id> does not accept response_format Model anaghị anara mode ọ bụla n'ime abụọ ahụ.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object Model na-anara json_object ma i zipụrụ json_schema.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … response_format enweghị type, ma ọ bụ abụghị JSON object.

A na-ezipu azịza 400 abụọ ahụ tupu e wepụ ihe ọ bụla na balance gị. Njikwa njehie