រំលងទៅមាតិកា
លទ្ធផលមានរចនាសម្ព័ន្ធ

លទ្ធផលមានរចនាសម្ព័ន្ធ

សុំចម្លើយជា JSON៖ JSON object ណាមួយ ឬ 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 គឺជា field របស់ /v1/chat/completions។ បើគ្មានវា ចម្លើយគឺជាអត្ថបទធម្មតា។

របៀប តម្លៃ អ្វីដែលអ្នកទទួលបាន
JSON object {"type": "json_object"} ចម្លើយជា JSON ដោយគ្មានរូបរាងកំណត់។ សូមប្រាប់ក្នុង prompt ថាអ្នកចង់បាន key អ្វីខ្លះ។
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} ចម្លើយធ្វើតាម schema ដែលអ្នកផ្ញើ៖ key ប្រភេទ និង field ចាំបាច់របស់វា។
Strict "strict": true field មួយនៃ json_schema មិនមែនជារបៀបទីបីទេ។ វាគឺ true លុះត្រាតែអ្នកផ្ញើ false។
Field ប្រភេទ លំនាំដើម ការពិពណ៌នា អនុវត្តដោយ
type string json_object ឬ json_schema។ ចាំបាច់ក្នុង response_format។ text ត្រូវបានទទួលយក ហើយផ្តល់ចម្លើយជាអត្ថបទធម្មតា។ គ្រប់ម៉ូដែលដែលទទួលរបៀបនេះ
json_schema.schema object JSON Schema ដែលចម្លើយធ្វើតាម។ សូមផ្ញើវាគ្រប់ពេលដែល type គឺ json_schema។ គ្រប់ម៉ូដែលដែលទទួលរបៀបនេះ
json_schema.name string ស្លាកសម្រាប់ schema ដូចក្នុងទ្រង់ទ្រាយ OpenAI។ ម៉ូដែល open-weight ដែលបង្ហោះ
json_schema.strict boolean true ថាតើចម្លើយត្រូវតែធ្វើតាម schema យ៉ាងពិតប្រាកដឬអត់។ ម៉ូដែល Shannon អនុវត្តរាល់ schema ជា strict។ ម៉ូដែល open-weight ដែលបង្ហោះ

របៀប JSON object មិនត្រូវការ schema ទេ។ សូមដាក់ឈ្មោះ key ក្នុង prompt៖

ខ្លឹមសារ 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"}
}

ការគាំទ្រតាមម៉ូដែល

ម៉ូដែល json_object json_schema
រាល់ម៉ូដែល Shannon (shannon-*) បាទ/ចាស បាទ/ចាស
ម៉ូដែល open-weight ដែលបង្ហោះ លើកលែងតែអ្វីដែលមានឈ្មោះខាងក្រោម បាទ/ចាស បាទ/ចាស
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP បាទ/ចាស —

GET /v1/models រាយការណ៍ដូចគ្នាតាម id៖ capabilities.json_schema លើគ្រប់ម៉ូដែល និង capabilities.response_format លើម៉ូដែល open-weight ដែលបង្ហោះ។ ម៉ូដែល និងតម្លៃ

Keyword របស់ schema ដែលគាំទ្រ

អ្វីដែលម៉ូដែលទទួលពី schema របស់អ្នកអាស្រ័យលើម៉ូដែល និងលើ endpoint៖

ម៉ូដែល /v1/chat/completions /v1/responses
ម៉ូដែល Shannon សំណុំរងនៃ keyword ខាងក្រោម អនុវត្តជា strict។ schema ដូចដែលអ្នកផ្ញើ អនុវត្តជា strict។
ម៉ូដែល open-weight ដែលបង្ហោះ schema ដូចដែលអ្នកផ្ញើ។ strict គឺ true លុះត្រាតែអ្នកផ្ញើ។ schema ដូចដែលអ្នកផ្ញើ។ strict គឺ true លុះត្រាតែអ្នកផ្ញើ។

សំណុំរងនៃ keyword របស់ម៉ូដែល 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 ដែលម៉ូដែលទទួលទេ។

នៅលើ /v1/responses៖ ប្រើវាលមួយឈ្មោះថា text.format

/v1/responses សរសេរ request ដូចគ្នាជា text.format។ schema ស្ថិតដោយផ្ទាល់ក្នុង format ក្បែរ name និង strict មិនមែនក្រោម key json_schema ទេ។ សម្រាប់របៀប JSON object សូមផ្ញើ {"format": {"type": "json_object"}}។

ខ្លឹមសារ 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 គឺជា 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": []
        }
      ]
    }
  ]
}

ការគាំទ្រតាមម៉ូដែល និង error ដូចគ្នានឹងលើ /v1/chat/completions។ Responses API

នៅលើ /v1/messages៖ ប្រើឧបករណ៍ (tool) ជាការកំណត់ schema

ទ្រង់ទ្រាយ Messages គ្មាន field សម្រាប់ output មានរចនាសម្ព័ន្ធទេ។ សម្រាប់ JSON ពីម៉ូដែលដូចគ្នា សូមហៅ /v1/chat/completions ឬ /v1/responses ជាមួយ key ដូចគ្នា។

ដើម្បីនៅលើ /v1/messages សូមពិពណ៌នា object ជា tool៖ ដាក់ schema របស់អ្នកក្នុង input_schema ហើយដាក់ឈ្មោះ tool ក្នុង tool_choice។ ពេលនោះ input នៃ block tool_use គឺជា object របស់អ្នក ដែលបាន parse រួចហើយ។ tool_choice ដែលមានឈ្មោះត្រូវបានអនុវត្តដោយម៉ូដែល open-weight ដែលបង្ហោះ។

ខ្លឹមសារ 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"}
    }
  ]
}

សូមពិនិត្យថា stop_reason គឺ tool_use មុនពេលអ្នកអាន block។ ការហៅមុខងារ

របៀបដែល JSON មកដល់

  • JSON គឺជា string។ ចម្លើយមិនមាន object ដែលបាន parse ក្បែរវាទេ៖ សូមដំណើរការ JSON parser របស់អ្នកលើ message.content។
  • ជាមួយ stream: true JSON មកដល់ជាបំណែក delta.content។ សូមភ្ជាប់ពួកវា ហើយ parse មួយដងបន្ទាប់ពី stream បានបញ្ចប់៖ បំណែកតែមួយមិនមែនជា JSON ត្រឹមត្រូវទេ។
  • ម៉ូដែល open-weight ដែលបង្ហោះឆ្លើយ request response_format ដោយគ្មានអត្ថបទ reasoning៖ reasoning_content គឺ null។

ពិនិត្យអ្វីដែលអ្នកទទួលបាន

schema ណែនាំរបៀបដែលម៉ូដែលសរសេរ។ API មិនប្រៀបធៀបចម្លើយដែលបានបញ្ចប់នឹង schema របស់អ្នកទេ ដូច្នេះកូដរបស់អ្នកធ្វើការពិនិត្យចុងក្រោយ៖

  • Parse ក្នុង error handler ដូចគំរូនៅខាងលើបំផុតធ្វើ។
  • សូម validate object ដែលបាន parse ជាមួយ schema ដូចគ្នាក្នុងកូដរបស់អ្នកផ្ទាល់ ឧទាហរណ៍ជាមួយបណ្ណាល័យ JSON Schema, Pydantic ឬ Zod។
  • ចម្លើយវែងអាចបញ្ចប់មុនពេល JSON ពេញលេញ។ ចម្លើយដែលមិន stream រាយការណ៍ finish_reason ជា stop ឬ tool_calls ប៉ុណ្ណោះ ដូច្នេះ parse error គឺជាសញ្ញា។ សូមសួរម្តងទៀត ឬសុំ object តូចជាង។

កំហុស

ស្ថានភាព ប្រភេទ សារ ពេលណា
400 invalid_request_error <id> does not accept response_format ម៉ូដែលមិនទទួលរបៀបទាំងពីរទេ។
400 invalid_request_error <id> does not accept response_format json_schema; use json_object ម៉ូដែលទទួល json_object ហើយអ្នកបានផ្ញើ json_schema។
422 invalid_request_error Failed to deserialize the JSON body into the target type: … response_format គ្មាន type ឬមិនមែនជា JSON object។

ចម្លើយ 400 ទាំងពីរត្រូវបានផ្ញើមុនពេលមានអ្វីត្រូវបានដកពីសមតុល្យរបស់អ្នក។ ការគ្រប់គ្រងកំហុស