අන්තර්ගතයට යන්න
ව්‍යුහගත ප්‍රතිදාන

ව්‍යුහගත ප්‍රතිදාන

පිළිතුර JSON ලෙස ඉල්ලන්න: ඕනෑම JSON object එකක්, හෝ ඔබ යවන schema එකක් අනුගමනය කරන JSON. පිළිතුර කියවන්නේ පුද්ගලයෙකු නොව වැඩසටහනක් නම් එය භාවිතා කරන්න.

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 එකකි. එය නොමැතිව පිළිතුර සාමාන්‍ය පෙළ වේ.

Mode අගය ඔබට ලැබෙන දේ
JSON object {"type": "json_object"} පිළිතුර නියමිත හැඩයක් නැති JSON වේ. ඔබට අවශ්‍ය keys prompt එකේ කියන්න.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} පිළිතුර ඔබ යවන schema එක අනුගමනය කරයි: එහි keys, types සහ අවශ්‍ය fields.
Strict "strict": true json_schema හි field එකක්, තුන්වන mode එකක් නොවේ. ඔබ false නොයවන්නේ නම් එය true වේ.
Field වර්ගය පෙරනිමිය විස්තරය යොදන්නේ
type string json_object හෝ json_schema. response_format තුළ අවශ්‍යයි. text පිළිගන්නා අතර සාමාන්‍ය පෙළ පිළිතුරක් ලබා දෙයි. මෙම mode එක පිළිගන්නා සෑම model එකක්ම
json_schema.schema object පිළිතුර අනුගමනය කරන JSON Schema එක. type json_schema වන සෑම විටම එය යවන්න. මෙම mode එක පිළිගන්නා සෑම model එකක්ම
json_schema.name string OpenAI format එකේ මෙන් schema එක සඳහා ලේබලයක්. අපගේ සේවාදායකවල ධාවනය වන open-weight models
json_schema.strict boolean true පිළිතුර schema එක හරියටම අනුගමනය කළ යුතුද යන්න. Shannon models සෑම schema එකක්ම strict ලෙස ක්‍රියාත්මක කරයි. අපගේ සේවාදායකවල ධාවනය වන open-weight models

JSON object mode එකට schema එකක් අවශ්‍ය නැත. Keys 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
සෑම Shannon model එකක්ම (shannon-*) ඔව් ඔව්
පහත නම් කළ ඒවා හැර අපගේ සේවාදායකවල ධාවනය වන open-weight models ඔව් ඔව්
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP ඔව් —

GET /v1/models එයම සෑම id එකකටම වාර්තා කරයි: සෑම model එකකම capabilities.json_schema, සහ අපගේ සේවාදායකවල ධාවනය වන open-weight models වල capabilities.response_format. Models සහ මිල ගණන්

සහාය දක්වන schema මූල පද

Model එකකට ඔබේ schema එකෙන් ලැබෙන දේ model එක සහ endpoint එක මත රඳා පවතී:

Models /v1/chat/completions /v1/responses
Shannon models පහත මූල පද උප කුලකය, strict ලෙස ක්‍රියාත්මක කෙරේ. ඔබ යවන ආකාරයටම schema එක, strict ලෙස ක්‍රියාත්මක කෙරේ.
අපගේ සේවාදායකවල ධාවනය වන open-weight models ඔබ යවන ආකාරයටම schema එක. ඔබ strict නොයවන්නේ නම් එය true වේ. ඔබ යවන ආකාරයටම schema එක. ඔබ strict නොයවන්නේ නම් එය true වේ.

/v1/chat/completions හි Shannon models වල මූල පද උප කුලකය:

මූල පදය කියවන ආකාරය
කියවන මූල පද type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref Schema එකේ ඉහළ මට්ටමේ $defs හෝ definitions වලින් විසඳයි. description වැනි $ref අසල ලියූ මූල පද තබා ගනී. ගැළපෙන අර්ථ දැක්වීමක් නැති යොමුවක් {"type": "object"} ලෙස කියවයි.
allOf සාමාජිකයන් එක schema එකකට ඒකාබද්ධ කෙරේ. සාමාජිකයන් දෙදෙනෙකු එකම මූල පදය සකසන තැන, පසුව එන එක ගණන් ගැනේ.
oneOf anyOf ලෙස කියවයි.
තමන්වම යොමු කරන schemas මට්ටම් 24ක් දක්වා පුළුල් කෙරේ. ඊට ගැඹුරු මට්ටමක් {"type": "object"} ලෙස කියවයි.
උප කුලකයෙන් පිටත අනෙක් සෑම මූල පදයක්ම, උදාහරණයක් ලෙස additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not සහ if / then / else. මේවා model එකට ලැබෙන schema එකේ කොටසක් නොවේ.

/v1/responses හිදී text.format භාවිතා කරන ආකාරය

/v1/responses එම ඉල්ලීමම text.format ලෙස ලියයි. Schema එක json_schema key එක යටතේ නොව, name සහ strict අසල, format තුළම කෙළින්ම තබා ඇත. JSON object mode සඳහා {"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 යනු message item එකේ output_text කොටසේ 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 හිදී: schema එක ලෙස tool එකක් භාවිතා කිරීම

Messages format එකට ව්‍යුහගත-output field එකක් නැත. එම models වලින්ම JSON ලබා ගැනීමට, එම key එකෙන්ම /v1/chat/completions හෝ /v1/responses call කරන්න.

/v1/messages හිම රැඳී සිටීමට, object එක tool එකක් ලෙස විස්තර කරන්න: ඔබේ schema එක input_schema හි තබා tool_choice හි tool එක නම් කරන්න. එවිට tool_use block එකේ input යනු දැනටමත් parse කළ ඔබේ object එකයි. නම් කළ tool_choice ක්‍රියාත්මක කරන්නේ අපගේ සේවාදායකවල ධාවනය වන 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"}
    }
  ]
}

Block එක කියවීමට පෙර stop_reason එක tool_use දැයි පරීක්ෂා කරන්න. කාර්ය කැඳවීම

JSON පැමිණෙන ආකාරය

  • JSON යනු string එකකි. පිළිතුර අසල parse කළ object එකක් නැත: message.content මත ඔබේ JSON parser එක ධාවනය කරන්න.
  • stream: true සමඟ JSON delta.content කෑලි වලින් පැමිණේ. ඒවා සම්බන්ධ කර stream එක අවසන් වූ පසු parse කරන්න; තනි කෑල්ලක් වලංගු JSON නොවේ.
  • අපගේ සේවාදායකවල ධාවනය වන open-weight model එකක් response_format ඉල්ලීමකට තර්කන පෙළ නොමැතිව පිළිතුරු දෙයි: reasoning_content null වේ.

ඔබට ලැබෙන දේ පරීක්ෂා කරන්න

Schema එක model එක ලියන ආකාරය මෙහෙයවයි. API එක අවසන් පිළිතුර ඔබේ schema එක සමඟ සසඳන්නේ නැත, එබැවින් අවසන් පරීක්ෂාව කරන්නේ ඔබේ code එකයි:

  • ඉහළම නියැදිය කරන ආකාරයට, දෝෂ හසුරුවනයක් තුළ parse කරන්න.
  • Parse කළ object එක ඔබේම code එකේ එම schema එකෙන්ම validate කරන්න, උදාහරණයක් ලෙස JSON Schema library එකකින්, Pydantic හෝ Zod සමඟ.
  • දිගු පිළිතුරක් JSON සම්පූර්ණ වීමට පෙර අවසන් විය හැක. Stream නොකළ පිළිතුරක් finish_reason වාර්තා කරන්නේ stop හෝ tool_calls ලෙස පමණි, එබැවින් parse දෝෂයක් ඒ බව පෙන්වන සංඥාවයි. නැවත ඉල්ලන්න, නැතහොත් කුඩා object එකක් ඉල්ලන්න.

දෝෂ

තත්ත්වය වර්ගය පණිවිඩය කවදාද
400 invalid_request_error <id> does not accept response_format Model එක mode දෙකම පිළිගන්නේ නැත.
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 පිළිතුරු දෙක ඔබේ ශේෂයෙන් කිසිවක් අඩු කිරීමට පෙර යවනු ලැබේ. දෝෂ