Kontentga o'tish
Tuzilgan chiqishlar

Tuzilgan chiqishlar

Javobni JSON sifatida so'rang: istalgan JSON obyekti yoki siz yuborgan sxemaga amal qiladigan JSON. Javobni inson emas, dastur o'qiganda foydalaning.

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 javob matni, choices[0].message.contentda:

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 rejimlari

response_format — /v1/chat/completions ning maydoni. Usiz javob oddiy matn bo'ladi.

Rejim Qiymat Nima olasiz
JSON obyekti {"type": "json_object"} Javob oldindan belgilangan shaklsiz JSON. Qaysi kalitlar kerakligini promptda ayting.
JSON sxemasi {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} Javob siz yuborgan sxemaga amal qiladi: uning kalitlari, turlari va majburiy maydonlariga.
Strict "strict": true json_schemaning maydoni, uchinchi rejim emas. false yubormasangiz u true.
Maydon Tur Standart Tavsif Qo'llaydi
type string json_object yoki json_schema. response_format ichida majburiy. text qabul qilinadi va oddiy matnli javob beradi. Rejimni qabul qiladigan har bir model
json_schema.schema object Javob amal qiladigan JSON Schema. type json_schema bo'lganda uni yuboring. Rejimni qabul qiladigan har bir model
json_schema.name string Sxema uchun yorliq, OpenAI formatidagidek. Xostingdagi open-weight modellar
json_schema.strict boolean true Javob sxemaga aynan amal qilishi kerakmi. Shannon modellari har bir sxemani strict sifatida qo'llaydi. Xostingdagi open-weight modellar

JSON obyekti rejimi sxema talab qilmaydi. Kalitlarni promptda nomlang:

So'rov tanasi
{
  "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 bo'yicha qo'llab-quvvatlash

Model json_object json_schema
Har bir Shannon modeli (shannon-*) Ha Ha
Xostingdagi open-weight modellar, quyida nomlanganlardan tashqari Ha Ha
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP Ha —

GET /v1/models har bir id uchun xuddi shuni ko'rsatadi: har bir modelda capabilities.json_schema va xostingdagi open-weight modellarda capabilities.response_format. Modellar va narxlar

Qo'llab-quvvatlanadigan sxema kalit so'zlari

Model sxemangizdan nimani olishi modelga va endpointga bog'liq:

Modellar /v1/chat/completions /v1/responses
Shannon modellari Quyidagi kalit so'zlar qism to'plami, strict sifatida qo'llanadi. Sxema yuborganingizdek, strict sifatida qo'llanadi.
Xostingdagi open-weight modellar Sxema yuborganingizdek. Yubormasangiz strict — true. Sxema yuborganingizdek. Yubormasangiz strict — true.

Shannon modellarining /v1/chat/completions dagi kalit so'zlar qism to'plami:

Kalit so'z Qanday o'qiladi
O'qiladigan kalit so'zlar type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref Sxemaning yuqori darajasidagi $defs yoki definitionsdan hal qilinadi. $ref yonida yozilgan description kabi kalit so'zlar saqlanadi. Mos ta'rifi yo'q havola {"type": "object"} sifatida o'qiladi.
allOf A'zolar bitta sxemaga birlashtiriladi. Ikki a'zo bir xil kalit so'zni o'rnatsa, keyingisi hisobga olinadi.
oneOf anyOf sifatida o'qiladi.
O'ziga murojaat qiladigan sxemalar 24 darajagacha ochiladi. Chuqurroq daraja {"type": "object"} sifatida o'qiladi.
Qism to'plamdan tashqarida Boshqa har bir kalit so'z, masalan additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not va if / then / else. Ular model oladigan sxemaga kirmaydi.

/v1/responses da: text.format

/v1/responses xuddi shu so'rovni text.format deb yozadi. Sxema json_schema kaliti ostida emas, bevosita formatda, name va strict yonida turadi. JSON obyekti rejimi uchun {"format": {"type": "json_object"}} yuboring.

So'rov tanasi
{
  "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 elementining output_text qismining texti:

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 bo'yicha qo'llab-quvvatlash va xatolar /v1/chat/completions dagi bilan bir xil. Responses API

/v1/messages da: sxema sifatida tool

Messages formatida tuzilmali chiqish maydoni yo'q. Xuddi shu modellardan JSON olish uchun /v1/chat/completions yoki /v1/responsesni xuddi shu kalit bilan chaqiring.

/v1/messages da qolish uchun obyektni tool sifatida tasvirlang: sxemangizni input_schemaga qo'ying va tool'ni tool_choiceda nomlang. Shunda tool_use blokining inputi allaqachon parse qilingan obyektingiz bo'ladi. Nomlangan tool_choiceni xostingdagi open-weight modellar qo'llaydi.

So'rov tanasi
{
  "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"}
    }
  ]
}

Blokni o'qishdan oldin stop_reason tool_use ekanini tekshiring. Funksiya chaqirish

JSON qanday keladi

  • JSON — satr. Javobda uning yonida parse qilingan obyekt yo'q: JSON parserni message.contentda ishga tushiring.
  • stream: true bilan JSON delta.content bo'laklarida keladi. Ularni birlashtiring va oqim tugagach bir marta parse qiling; bitta bo'lak o'zi yaroqli JSON emas.
  • Xostingdagi open-weight model response_format so'roviga mulohaza matnisiz javob beradi: reasoning_content — null.

Olganingizni tekshiring

Sxema modelning yozishini yo'naltiradi. API tayyor javobni sxemangiz bilan solishtirmaydi, shuning uchun yakuniy tekshiruvni kodingiz qiladi:

  • Parse'ni xato ishlovchisi ichida bajaring, tepadagi namunadagidek.
  • Parse qilingan obyektni o'z kodingizda xuddi shu sxema bilan tekshiring, masalan JSON Schema kutubxonasi, Pydantic yoki Zod bilan.
  • Uzun javob JSON to'liq bo'lmasdan tugashi mumkin. Oqimsiz javob finish_reasonni faqat stop yoki tool_calls deb ko'rsatadi, shuning uchun parse xatosi signal bo'ladi. Qayta so'rang yoki kichikroq obyekt so'rang.

Xatolar

Holat Tur Xabar Qachon
400 invalid_request_error <id> does not accept response_format Model ikkala rejimni ham qabul qilmaydi.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object Model json_objectni qabul qiladi, siz esa json_schema yubordingiz.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … response_formatda type yo'q yoki u JSON obyekti emas.

Ikkala 400 javobi balansingizdan biror narsa yechilishidan oldin yuboriladi. Xatolarni boshqarish