ຂ້າມໄປຫາເນື້ອຫາ
ຜົນອອກມີໂຄງສ້າງ

ຜົນອອກມີໂຄງສ້າງ

ຂໍຄຳຕອບເປັນ JSON: object JSON ໃດກໍໄດ້, ຫຼື JSON ທີ່ເຮັດຕາມ schema ທີ່ທ່ານສົ່ງ. ໃຊ້ເມື່ອໂປຣແກຣມ, ບໍ່ແມ່ນຄົນ, ເປັນຜູ້ອ່ານຄຳຕອບ.

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. ຖ້າບໍ່ມີມັນ ຄຳຕອບເປັນຂໍ້ຄວາມທຳມະດາ.

ໂໝດ ຄ່າ ສິ່ງທີ່ທ່ານໄດ້ຮັບ
JSON object {"type": "json_object"} ຄຳຕອບເປັນ JSON ໂດຍບໍ່ມີຮູບຮ່າງທີ່ກຳນົດ. ບອກໃນ prompt ວ່າທ່ານຕ້ອງການ key ໃດ.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} ຄຳຕອບເຮັດຕາມ schema ທີ່ທ່ານສົ່ງ: key, ປະເພດ ແລະ ຟີລດ໌ທີ່ຈຳເປັນຂອງມັນ.
Strict "strict": true ຟີລດ໌ຂອງ json_schema, ບໍ່ແມ່ນໂໝດທີສາມ. ມັນເປັນ true ເວັ້ນແຕ່ທ່ານສົ່ງ false.
ຟີລດ໌ ປະເພດ ຄ່າເລີ່ມຕົ້ນ ຄຳອະທິບາຍ ໃຊ້ໂດຍ
type string json_object ຫຼື json_schema. ຈຳເປັນພາຍໃນ response_format. text ຖືກຮັບ ແລະ ໃຫ້ຄຳຕອບເປັນຂໍ້ຄວາມທຳມະດາ. ທຸກ model ທີ່ຮັບໂໝດນີ້
json_schema.schema object JSON Schema ທີ່ຄຳຕອບເຮັດຕາມ. ສົ່ງມັນທຸກຄັ້ງທີ່ type ເປັນ json_schema. ທຸກ model ທີ່ຮັບໂໝດນີ້
json_schema.name string ປ້າຍຊື່ສຳລັບ schema, ຄືໃນຮູບແບບ OpenAI. Hosted open-weight models
json_schema.strict boolean true ຄຳຕອບຕ້ອງເຮັດຕາມ schema ຢ່າງແນ່ນອນຫຼືບໍ່. model Shannon ນຳໃຊ້ທຸກ schema ແບບ strict. Hosted open-weight models

ໂໝດ JSON object ບໍ່ຕ້ອງການ schema. ບອກຊື່ key ໃນ prompt:

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
ທຸກ model Shannon (shannon-*) ແມ່ນ ແມ່ນ
Hosted open-weight models, ຍົກເວັ້ນອັນທີ່ລະບຸຊື່ຂ້າງລຸ່ມ ແມ່ນ ແມ່ນ
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP ແມ່ນ —

GET /v1/models ລາຍງານສິ່ງດຽວກັນຕໍ່ id: capabilities.json_schema ໃນທຸກ model, ແລະ capabilities.response_format ໃນ hosted open-weight models. Model ແລະ ລາຄາ

Keyword ຂອງ schema ທີ່ຮອງຮັບ

ສິ່ງທີ່ model ໄດ້ຮັບຈາກ schema ຂອງທ່ານຂຶ້ນກັບ model ແລະ endpoint:

Model /v1/chat/completions /v1/responses
model Shannon ຊຸດຍ່ອຍ keyword ຂ້າງລຸ່ມ, ນຳໃຊ້ແບບ strict. schema ຕາມທີ່ທ່ານສົ່ງ, ນຳໃຊ້ແບບ strict.
Hosted open-weight models schema ຕາມທີ່ທ່ານສົ່ງ. strict ເປັນ true ເວັ້ນແຕ່ທ່ານສົ່ງມັນ. schema ຕາມທີ່ທ່ານສົ່ງ. strict ເປັນ true ເວັ້ນແຕ່ທ່ານສົ່ງມັນ.

ຊຸດຍ່ອຍ keyword ຂອງ model Shannon ໃນ /v1/chat/completions:

Keyword ຖືກອ່ານແນວໃດ
Keyword ທີ່ອ່ານ type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref ຖືກແກ້ຈາກ $defs ຫຼື definitions ທີ່ລະດັບເທິງສຸດຂອງ schema. keyword ທີ່ຂຽນຂ້າງ $ref, ເຊັ່ນ description, ຖືກເກັບໄວ້. ການອ້າງອີງທີ່ບໍ່ມີຄຳນິຍາມຕົງກັນຖືກອ່ານເປັນ {"type": "object"}.
allOf ສະມາຊິກຖືກລວມເປັນ schema ດຽວ. ບ່ອນທີ່ສອງສະມາຊິກຕັ້ງ keyword ດຽວກັນ, ອັນຫຼັງຖືກນຳໃຊ້.
oneOf ອ່ານເປັນ anyOf.
Schema ທີ່ອ້າງອີງຕົນເອງ ຂະຫຍາຍເປັນ 24 ລະດັບ. ລະດັບທີ່ເລິກກວ່ານັ້ນຖືກອ່ານເປັນ {"type": "object"}.
ຢູ່ນອກຊຸດຍ່ອຍ ທຸກ keyword ອື່ນ, ຕົວຢ່າງ additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not ແລະ if / then / else. ສິ່ງເຫຼົ່ານີ້ບໍ່ແມ່ນສ່ວນໜຶ່ງຂອງ schema ທີ່ model ໄດ້ຮັບ.

ການຂໍ JSON ຢູ່ໃນ endpoint ຂອງ /v1/responses ໂດຍໃຊ້ຟີລດ໌ text.format

/v1/responses ຂຽນຄຳຮ້ອງຂໍດຽວກັນເປັນ text.format. schema ຢູ່ໂດຍກົງໃນ format, ຂ້າງ name ແລະ strict, ບໍ່ແມ່ນພາຍໃຕ້ key json_schema. ສຳລັບໂໝດ JSON object ໃຫ້ສົ່ງ {"format": {"type": "json_object"}}.

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 ແມ່ນ text ຂອງສ່ວນ output_text ຂອງລາຍການ 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": []
        }
      ]
    }
  ]
}

ການຮອງຮັບຕໍ່ model ແລະ ຂໍ້ຜິດພາດແມ່ນຄືກັນກັບໃນ /v1/chat/completions. Responses API

ການໃຊ້ tool ເປັນ schema ຢູ່ໃນ endpoint ຂອງ /v1/messages

ຮູບແບບ Messages ບໍ່ມີຟີລດ໌ structured-output. ເພື່ອໃຫ້ໄດ້ JSON ຈາກ model ດຽວກັນ, ເອີ້ນ /v1/chat/completions ຫຼື /v1/responses ດ້ວຍ key ດຽວກັນ.

ເພື່ອຢູ່ໃນ /v1/messages, ໃຫ້ອະທິບາຍ object ເປັນ tool: ໃສ່ schema ຂອງທ່ານໃນ input_schema ແລະ ໃສ່ຊື່ tool ໃນ tool_choice. input ຂອງ block tool_use ຈະເປັນ object ຂອງທ່ານ, ແຕກ (parse) ແລ້ວ. tool_choice ທີ່ລະບຸຊື່ຖືກນຳໃຊ້ໂດຍ hosted open-weight models.

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

ກວດວ່າ stop_reason ເປັນ tool_use ກ່ອນທ່ານອ່ານ block. ການເອີ້ນຟັງຊັນ

JSON ມາຮອດແນວໃດ

  • JSON ເປັນ string. ຄຳຕອບບໍ່ມີ object ທີ່ແຕກແລ້ວຄຽງຂ້າງ: ໃຫ້ຮັນ JSON parser ຂອງທ່ານເທິງ message.content.
  • ດ້ວຍ stream: true JSON ມາເປັນສ່ວນໃນ delta.content. ຕໍ່ພວກມັນ ແລ້ວແຕກ (parse) ເທື່ອດຽວເມື່ອ stream ຈົບ; ສ່ວນໜຶ່ງຕາມລຳພັງບໍ່ແມ່ນ JSON ທີ່ຖືກຕ້ອງ.
  • hosted open-weight model ຕອບຄຳຮ້ອງຂໍ response_format ໂດຍບໍ່ມີຂໍ້ຄວາມ reasoning: reasoning_content ເປັນ null.

ກວດສິ່ງທີ່ທ່ານໄດ້ຮັບ

schema ນຳທາງວິທີທີ່ model ຂຽນ. API ບໍ່ໄດ້ປຽບທຽບຄຳຕອບທີ່ສຳເລັດກັບ schema ຂອງທ່ານ, ດັ່ງນັ້ນໂຄ້ດຂອງທ່ານເປັນຜູ້ກວດຂັ້ນສຸດທ້າຍ:

  • ແຕກ (parse) ພາຍໃນຕົວຈັດການຂໍ້ຜິດພາດ, ຄືກັບຕົວຢ່າງເທິງສຸດ.
  • ກວດສອບ object ທີ່ແຕກແລ້ວດ້ວຍ schema ດຽວກັນໃນໂຄ້ດຂອງທ່ານເອງ, ຕົວຢ່າງດ້ວຍ library JSON Schema, Pydantic ຫຼື Zod.
  • ຄຳຕອບຍາວອາດຈົບກ່ອນ JSON ສົມບູນ. ຄຳຕອບທີ່ບໍ່ stream ລາຍງານ finish_reason ເປັນ stop ຫຼື tool_calls ເທົ່ານັ້ນ, ດັ່ງນັ້ນຂໍ້ຜິດພາດໃນການແຕກ (parse) ຄືສັນຍານ. ຖາມອີກ, ຫຼື ຂໍ object ທີ່ນ້ອຍກວ່າ.

ຂໍ້ຜິດພາດ

ສະຖານະ ປະເພດ ຂໍ້ຄວາມ ເມື່ອໃດ
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, ຫຼື ບໍ່ແມ່ນ object JSON.

ຄຳຕອບ 400 ທັງສອງຖືກສົ່ງກ່ອນທີ່ຈະຫັກຫຍັງຈາກຍອດເງິນຂອງທ່ານ. ການຈັດການຂໍ້ຜິດພາດ