Tsallake zuwa abun ciki
Abubuwan da aka Tsara

Abubuwan da aka Tsara

Ku nemi amsar a matsayin JSON: kowane object na JSON, ko JSON da ke bin schema da kuka aika. Ku yi amfani da shi idan shirin kwamfuta, ba mutum ba, ne ke karanta amsar.

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 shi ne rubutun amsar, a cikin 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}
  }
}

Yanayin response_format

response_format field ne na /v1/chat/completions. Ba tare da shi ba amsar rubutu ce ta yau da kullun.

Yanayi Ƙima Abin da kuke samu
JSON object {"type": "json_object"} Amsar JSON ce ba tare da tsari da aka sanya ba. Ku faɗa a cikin prompt wane keys kuke so.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Amsar tana bin schema da kuka aika: keys ɗinsa, nau'o'insa da fields da ake buƙata.
Strict "strict": true Field na json_schema, ba yanayi na uku ba. Yana true sai dai idan kun aika false.
Field Nau'i Na asali Bayani Wanda ke aiwatarwa
type string json_object ko json_schema. Ana buƙata a cikin response_format. Ana karɓar text kuma yana ba da amsar rubutu ta yau da kullun. Kowane model da ke ɗaukar wannan yanayin
json_schema.schema object JSON Schema da amsar ke bi. Ku aika shi duk lokacin da type shi ne json_schema. Kowane model da ke ɗaukar wannan yanayin
json_schema.name string Suna na schema, kamar a tsarin OpenAI. Hosted open-weight models
json_schema.strict boolean true Ko amsar dole ta bi schema daidai. Models na Shannon suna aiwatar da kowane schema a matsayin strict. Hosted open-weight models

Yanayin JSON object ba ya buƙatar schema. Ku ambaci keys a cikin prompt:

Body na request
{
  "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"}
}

Tallafi ga kowane model

Model json_object json_schema
Kowane model na Shannon (shannon-*) Ee Ee
Hosted open-weight models, banda waɗanda aka ambata a ƙasa Ee Ee
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Ee —

GET /v1/models yana bayar da rahoton iri ɗaya ga kowane id: capabilities.json_schema a kowane model, da capabilities.response_format a hosted open-weight models. Models & farashi

Keywords na schema da ake tallafawa

Abin da model ke karɓa daga schema ɗinku ya dogara da model da kuma endpoint:

Models /v1/chat/completions /v1/responses
Models na Shannon Ƙaramin saitin keywords da ke ƙasa, ana aiwatar da shi a matsayin strict. Schema yadda kuka aika shi, ana aiwatar da shi a matsayin strict.
Hosted open-weight models Schema yadda kuka aika shi. strict shi ne true sai dai idan kun aika shi. Schema yadda kuka aika shi. strict shi ne true sai dai idan kun aika shi.

Ƙaramin saitin keywords na models na Shannon a /v1/chat/completions:

Keyword Yadda ake karanta shi
Keywords da ake karantawa type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref Ana warware shi daga $defs ko definitions a matakin sama na schema. Ana riƙe keywords da aka rubuta kusa da $ref, kamar description. Ana karanta nuni da babu ma'ana da ya dace a matsayin {"type": "object"}.
allOf Ana haɗa members zuwa schema ɗaya. Idan members biyu sun saita keyword iri ɗaya, na ƙarshe ne ke ƙidayawa.
oneOf Ana karanta shi a matsayin anyOf.
Schemas da ke nuni da kansu Ana faɗaɗa su zuwa matakai 24. Ana karanta mataki mai zurfi fiye da haka a matsayin {"type": "object"}.
Wajen ƙaramin saitin Kowane sauran keyword, misali additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not da if / then / else. Waɗannan ba sa cikin schema da model ke karɓa.

A /v1/responses: text.format

/v1/responses yana rubuta request iri ɗaya a matsayin text.format. Schema yana kai tsaye a cikin format, kusa da name da strict, ba a ƙarƙashin key na json_schema ba. Don yanayin JSON object ku aika {"format": {"type": "json_object"}}.

Body na request
{
  "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 shi ne text na ɓangaren output_text na item ɗin 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": []
        }
      ]
    }
  ]
}

Tallafi ga kowane model da kurakuran iri ɗaya ne da na /v1/chat/completions. Responses API

A /v1/messages: tool a matsayin schema

Tsarin Messages ba shi da field na structured-output. Don JSON daga models iri ɗaya, ku kira /v1/chat/completions ko /v1/responses da maɓalli ɗaya.

Don ku tsaya a /v1/messages, ku bayyana object ɗin a matsayin tool: ku sa schema ɗinku a cikin input_schema kuma ku ambaci sunan tool a cikin tool_choice. input na block ɗin tool_use sai ya zama object ɗinku, an riga an yi parse. Hosted open-weight models suna aiwatar da tool_choice mai suna.

Body na request
{
  "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"}
    }
  ]
}

Ku tabbatar stop_reason shi ne tool_use kafin ku karanta block ɗin. Kiran Aiki

Yadda JSON yake zuwa

  • JSON string ne. Amsar ba ta da object da aka yi parse kusa da ita: ku gudanar da JSON parser ɗinku a kan message.content.
  • Da stream: true JSON yana zuwa a guntayen delta.content. Ku haɗa su kuma ku yi parse bayan stream ya ƙare; guntu shi kaɗai ba JSON mai inganci ba ne.
  • Hosted open-weight model yana amsa request na response_format ba tare da rubutun reasoning ba: reasoning_content shi ne null.

Ku duba abin da kuka karɓa

Schema yana jagorantar yadda model ke rubutu. API ba ya kwatanta amsar da ta kammala da schema ɗinku, don haka code ɗinku ne ke yin binciken ƙarshe:

  • Ku yi parse a cikin error handler, kamar yadda samfurin da ke sama ya yi.
  • Ku tabbatar da ingancin object da aka yi parse da schema iri ɗaya a cikin code ɗinku, misali da JSON Schema library, Pydantic ko Zod.
  • Amsa mai tsawo na iya ƙarewa kafin JSON ya cika. Amsar da ba streaming ba tana bayar da finish_reason a matsayin stop ko tool_calls kawai, don haka kuskuren parse shi ne alamar. Ku sake nema, ko ku nemi ƙaramin object.

Kurakurai

Matsayi Nau'i Saƙo Yaushe
400 invalid_request_error <id> does not accept response_format Model ba ya ɗaukar ko ɗaya daga cikin yanayin biyu.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object Model yana ɗaukar json_object kuma kun aika json_schema.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … response_format ba shi da type, ko kuma ba object na JSON ba ne.

Ana aika amsoshin 400 biyun kafin a cire komai daga balance ɗinku. Kuskuren Gudanarwa