အကြောင်းအရာသို့ ကျော်သွားရန်
ဖွဲ့စည်းထားသော အထွက်

ဖွဲ့စည်းထားသော အထွက်

အဖြေကို JSON အဖြစ် တောင်းဆိုပါ- JSON object မည်သည့်အရာမဆို၊ သို့မဟုတ် သင်ပို့သော schema ကို လိုက်နာသည့် JSON။ ပြန်ကြားချက်ကို လူမဟုတ်ဘဲ program က ဖတ်သည့်အခါ အသုံးပြုပါ။

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 သည် 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}
  }
}

response_format ၏ မုဒ်များအကြောင်း

response_format သည် /v1/chat/completions ၏ field တစ်ခုဖြစ်သည်။ ၎င်းမပါလျှင် အဖြေသည် သာမန်စာသား ဖြစ်သည်။

မုဒ် တန်ဖိုး ရရှိသည့်အရာ
JSON object {"type": "json_object"} အဖြေသည် သတ်မှတ်ထားသော ပုံစံမရှိသော JSON ဖြစ်သည်။ မည်သည့် key များ လိုချင်သည်ကို prompt တွင် ပြောပါ။
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} အဖြေသည် သင်ပို့သော schema ကို လိုက်နာသည်- ၎င်း၏ key များ၊ type များနှင့် လိုအပ်သော field များ။
Strict "strict": true json_schema ၏ field တစ်ခုဖြစ်ပြီး တတိယမုဒ် မဟုတ်ပါ။ false ကို မပို့လျှင် true ဖြစ်သည်။
Field Type Default ဖော်ပြချက် အသုံးပြုသော model
type string json_object သို့မဟုတ် json_schema။ response_format အတွင်း လိုအပ်သည်။ text ကို လက်ခံပြီး သာမန်စာသားအဖြေ ပေးသည်။ ထိုမုဒ်ကို လက်ခံသော model တိုင်း
json_schema.schema object အဖြေ လိုက်နာရမည့် JSON Schema။ type သည် json_schema ဖြစ်သည့်အခါတိုင်း ပို့ပါ။ ထိုမုဒ်ကို လက်ခံသော model တိုင်း
json_schema.name string OpenAI format ကဲ့သို့ schema အတွက် အညွှန်း။ Host လုပ်ထားသော open-weight မော်ဒယ်များ
json_schema.strict boolean true အဖြေသည် schema ကို တိတိကျကျ လိုက်နာရမလား။ Shannon models များသည် schema တိုင်းကို strict အဖြစ် အသုံးပြုသည်။ Host လုပ်ထားသော open-weight မော်ဒယ်များ

JSON object မုဒ်သည် schema မလိုအပ်ပါ။ prompt တွင် key များကို အမည်ပေးပါ-

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"}
}

Model တစ်ခုစီအလိုက် ပံ့ပိုးမှု

Model json_object json_schema
Shannon model တိုင်း (shannon-*) ဟုတ်သည် ဟုတ်သည်
အောက်တွင် အမည်ဖော်ပြထားသည်များမှလွဲ၍ host လုပ်ထားသော open-weight models ဟုတ်သည် ဟုတ်သည်
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP ဟုတ်သည် —

GET /v1/models သည် id တစ်ခုစီအလိုက် တူညီသည့်အရာကို ဖော်ပြသည်- model တိုင်းတွင် capabilities.json_schema နှင့် host လုပ်ထားသော open-weight models များတွင် capabilities.response_format။ Model များနှင့် ဈေးနှုန်း

ပံ့ပိုးထားသော schema keyword များ

သင့် schema မှ model လက်ခံရရှိသည့်အရာသည် model နှင့် endpoint ပေါ်မူတည်သည်-

Models /v1/chat/completions /v1/responses
Shannon models အောက်ပါ keyword subset ကို strict အဖြစ် အသုံးပြုသည်။ သင်ပို့သည့်အတိုင်း schema ကို strict အဖြစ် အသုံးပြုသည်။
Host လုပ်ထားသော open-weight မော်ဒယ်များ သင်ပို့သည့်အတိုင်း schema။ မပို့လျှင် strict သည် true ဖြစ်သည်။ သင်ပို့သည့်အတိုင်း schema။ မပို့လျှင် strict သည် true ဖြစ်သည်။

/v1/chat/completions တွင် Shannon models များက ပံ့ပိုးသော keyword အစုခွဲ-

Keyword ဖတ်ပုံ
ဖတ်သော keyword များ type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref schema ၏ အပေါ်ဆုံးအဆင့်ရှိ $defs သို့မဟုတ် definitions မှ ဖြေရှင်းသည်။ $ref ဘေးတွင် ရေးထားသော description ကဲ့သို့ keyword များကို ထိန်းထားသည်။ ကိုက်ညီသော အဓိပ္ပာယ်ဖွင့်ဆိုချက် မရှိသည့် reference ကို {"type": "object"} အဖြစ် ဖတ်သည်။
allOf အဖွဲ့ဝင်များကို schema တစ်ခုတည်းအဖြစ် ပေါင်းစည်းသည်။ အဖွဲ့ဝင်နှစ်ခုက keyword တူကို သတ်မှတ်ထားလျှင် နောက်တစ်ခုကို ယူသည်။
oneOf anyOf အဖြစ် ဖတ်သည်။
ကိုယ်ပြန်ကိုးကားသော schema များ အဆင့် 24 အထိ ချဲ့ပြသည်။ ပိုနက်သောအဆင့်ကို {"type": "object"} အဖြစ် ဖတ်သည်။
Subset ပြင်ပ အခြား keyword အားလုံး၊ ဥပမာ additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not နှင့် if / then / else။ ၎င်းတို့သည် model လက်ခံရရှိသော schema ၏ အစိတ်အပိုင်း မဟုတ်ပါ။

/v1/responses တွင် text.format ကို အသုံးပြုပုံ အကြောင်း

/v1/responses သည် တူညီသော request ကို text.format အဖြစ် ရေးသည်။ schema သည် json_schema key အောက်တွင် မဟုတ်ဘဲ name နှင့် strict ဘေးရှိ format ထဲတွင် တိုက်ရိုက်ရှိသည်။ JSON object မုဒ်အတွက် {"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 သည် message item ၏ output_text part ၏ text ဖြစ်သည်-

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

Model တစ်ခုစီအလိုက် ပံ့ပိုးမှုနှင့် အမှားများသည် /v1/chat/completions နှင့် တူညီသည်။ Responses API

/v1/messages တွင် tool တစ်ခုကို schema အဖြစ် သုံးခြင်း

Messages format တွင် structured-output field မရှိပါ။ တူညီသော model များမှ JSON ရရန် တူညီသော key ဖြင့် /v1/chat/completions သို့မဟုတ် /v1/responses ကို ခေါ်ပါ။

/v1/messages တွင်ပင် ဆက်နေလိုလျှင် object ကို tool တစ်ခုအဖြစ် ဖော်ပြပါ- သင့် schema ကို input_schema တွင်ထည့်ပြီး tool ကို tool_choice တွင် အမည်ပေးပါ။ ထို့နောက် tool_use block ၏ input သည် parse လုပ်ပြီးသား သင့် object ဖြစ်သည်။ အမည်ပေးထားသော tool_choice ကို host လုပ်ထားသော open-weight models များက အသုံးပြုသည်။

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

block ကို မဖတ်မီ stop_reason သည် tool_use ဖြစ်ကြောင်း စစ်ဆေးပါ။ လုပ်ဆောင်ချက် ခေါ်ဆိုမှု

JSON ရောက်လာပုံ

  • JSON သည် string တစ်ခုဖြစ်သည်။ ပြန်ကြားချက်တွင် parse လုပ်ပြီးသား object မပါပါ- message.content ပေါ်တွင် သင့် JSON parser ကို run ပါ။
  • stream: true ဖြင့် JSON သည် delta.content အပိုင်းများအဖြစ် ရောက်လာသည်။ ၎င်းတို့ကို ပေါင်းပြီး stream ပြီးဆုံးမှ parse လုပ်ပါ၊ အပိုင်းတစ်ခုတည်းသည် မှန်ကန်သော JSON မဟုတ်ပါ။
  • host လုပ်ထားသော open-weight model သည် response_format request ကို reasoning စာသားမပါဘဲ ဖြေသည်- reasoning_content သည် null ဖြစ်သည်။

လက်ခံရရှိသည့်အရာကို စစ်ဆေးပါ

schema သည် model ရေးပုံကို လမ်းညွှန်သည်။ API သည် ပြီးဆုံးသော အဖြေကို သင့် schema နှင့် မနှိုင်းယှဉ်သဖြင့် နောက်ဆုံးစစ်ဆေးမှုကို သင့် code က လုပ်သည်-

  • အပေါ်ဆုံးနမူနာကဲ့သို့ error handler အတွင်း parse လုပ်ပါ။
  • parse လုပ်ပြီးသော object ကို သင့်ကိုယ်ပိုင် code တွင် တူညီသော schema ဖြင့် စစ်ဆေးပါ၊ ဥပမာ JSON Schema library၊ Pydantic သို့မဟုတ် Zod ဖြင့်။
  • ရှည်လျားသော အဖြေသည် JSON မပြည့်စုံမီ အဆုံးသတ်နိုင်သည်။ stream မလုပ်သော ပြန်ကြားချက်သည် finish_reason ကို stop သို့မဟုတ် tool_calls သာ ဖော်ပြသဖြင့် parse error သည် အချက်ပြမှု ဖြစ်သည်။ ထပ်မေးပါ၊ သို့မဟုတ် ပိုသေးသော object ကို တောင်းပါ။

အမှားများ

Status Type မက်ဆေ့ချ် ဖြစ်ပေါ်ချိန်
400 invalid_request_error <id> does not accept response_format model သည် မုဒ်နှစ်ခုလုံးကို မလက်ခံပါ။
400 invalid_request_error <id> does not accept response_format json_schema; use json_object model သည် json_object ကို လက်ခံပြီး သင်က json_schema ကို ပို့ခဲ့သည်။
422 invalid_request_error Failed to deserialize the JSON body into the target type: … response_format တွင် type မပါ၊ သို့မဟုတ် JSON object မဟုတ်ပါ။

400 ပြန်ကြားချက်နှစ်ခုကို သင့်လက်ကျန်ငွေမှ ဘာမျှ မနုတ်မီ ပို့သည်။ အမှားကိုင်တွယ်မှု