सामग्रीमा जानुहोस्
संरचित आउटपुट

संरचित आउटपुट

उत्तर JSON मा माग्नुहोस्: कुनै पनि JSON अब्जेक्ट, वा तपाईंले पठाएको स्कीमा पछ्याउने 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 को फिल्ड हो। यसबिना उत्तर सामान्य टेक्स्ट हुन्छ।

मोड मान तपाईंले के पाउनुहुन्छ
JSON अब्जेक्ट {"type": "json_object"} उत्तर तोकिएको आकार बिनाको JSON हो। कुन कुञ्जीहरू चाहिन्छ भन्ने प्रम्प्टमा भन्नुहोस्।
JSON स्कीमा {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} उत्तरले तपाईंले पठाएको स्कीमा पछ्याउँछ: यसका कुञ्जी, प्रकार र आवश्यक फिल्डहरू।
Strict "strict": true json_schema को फिल्ड, तेस्रो मोड होइन। तपाईंले false नपठाएसम्म यो true हुन्छ।
फिल्ड प्रकार पूर्वनिर्धारित विवरण लागू गर्ने
type string json_object वा json_schema। response_format भित्र आवश्यक। text स्वीकार गरिन्छ र सामान्य टेक्स्ट उत्तर दिन्छ। यो मोड लिने हरेक मोडल
json_schema.schema object उत्तरले पछ्याउने JSON Schema। type json_schema हुँदा सधैं पठाउनुहोस्। यो मोड लिने हरेक मोडल
json_schema.name string OpenAI ढाँचामा जस्तै, स्कीमाको लेबल। होस्ट गरिएका ओपन-वेट मोडलहरू
json_schema.strict boolean true उत्तरले स्कीमा ठ्याक्कै पछ्याउनुपर्छ कि पर्दैन। Shannon मोडलहरूले हरेक स्कीमा स्ट्रिक्टका रूपमा लागू गर्छन्। होस्ट गरिएका ओपन-वेट मोडलहरू

JSON अब्जेक्ट मोडलाई स्कीमा चाहिँदैन। प्रम्प्टमा कुञ्जीहरूको नाम दिनुहोस्:

अनुरोध बडी
{
  "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-*) हो हो
होस्ट गरिएका ओपन-वेट मोडलहरू, तल नाम दिइएका बाहेक हो हो
Laguna-S-2.1-W4A16-AUTOROUND-REAP — —
inkling-W4A16-AUTOROUND-REAP हो —

GET /v1/models ले प्रति id उही रिपोर्ट गर्छ: हरेक मोडलमा capabilities.json_schema, र होस्ट गरिएका ओपन-वेट मोडलहरूमा capabilities.response_format। मोडल र मूल्य

समर्थित स्कीमा किवर्डहरू

तपाईंको स्कीमाको मोडलले के पाउँछ भन्ने मोडल र इन्डपोइन्टमा निर्भर हुन्छ:

मोडलहरू /v1/chat/completions /v1/responses
Shannon मोडलहरू तलको किवर्ड सबसेट, स्ट्रिक्टका रूपमा लागू गरिएको। तपाईंले पठाएजस्तै स्कीमा, स्ट्रिक्टका रूपमा लागू गरिएको।
होस्ट गरिएका ओपन-वेट मोडलहरू तपाईंले पठाएजस्तै स्कीमा। नपठाएसम्म strict true हुन्छ। तपाईंले पठाएजस्तै स्कीमा। नपठाएसम्म strict true हुन्छ।

/v1/chat/completions मा Shannon मोडलहरूको किवर्ड सबसेट:

किवर्ड कसरी पढिन्छ
पढिने किवर्डहरू type, format, title, description, nullable, enum, items, properties, required, minItems, maxItems, minimum, maximum, minLength, maxLength, pattern, anyOf, default
$ref स्कीमाको माथिल्लो तहको $defs वा definitions बाट समाधान गरिन्छ। $ref को छेउमा लेखिएका description जस्ता किवर्डहरू राखिन्छन्। मिल्ने परिभाषा नभएको सन्दर्भ {"type": "object"} का रूपमा पढिन्छ।
allOf सदस्यहरू एउटै स्कीमामा मिलाइन्छन्। दुई सदस्यले उही किवर्ड सेट गरेमा पछिल्लो गनिन्छ।
oneOf anyOf का रूपमा पढिन्छ।
आफैंलाई सन्दर्भ गर्ने स्कीमाहरू 24 तहसम्म फैलाइन्छ। त्यसभन्दा गहिरो तह {"type": "object"} का रूपमा पढिन्छ।
सबसेटबाहिर अन्य हरेक किवर्ड, उदाहरणका लागि additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not र if / then / else। यी मोडलले पाउने स्कीमाको भाग होइनन्।

/v1/responses मा: text.format को रूपमा पठाउने

/v1/responses ले उही अनुरोधलाई text.format लेख्छ। स्कीमा format मै, name र strict को छेउमा हुन्छ, json_schema कुञ्जीमुनि होइन। JSON अब्जेक्ट मोडका लागि {"format": {"type": "json_object"}} पठाउनुहोस्।

अनुरोध बडी
{
  "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 आइटमको 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": []
        }
      ]
    }
  ]
}

प्रति मोडल समर्थन र त्रुटिहरू /v1/chat/completions मा जस्तै हुन्। Responses API

/v1/messages मा: स्कीमाका रूपमा टुल

Messages ढाँचामा स्ट्रक्चर्ड-आउटपुट फिल्ड छैन। उही मोडलहरूबाट JSON का लागि उही कीसहित /v1/chat/completions वा /v1/responses कल गर्नुहोस्।

/v1/messages मै रहन, अब्जेक्टलाई टुलका रूपमा वर्णन गर्नुहोस्: आफ्नो स्कीमा input_schema मा राख्नुहोस् र tool_choice मा टुलको नाम दिनुहोस्। त्यसपछि tool_use ब्लकको input पहिल्यै पार्स गरिएको तपाईंको अब्जेक्ट हुन्छ। नाम दिइएको tool_choice होस्ट गरिएका ओपन-वेट मोडलहरूले लागू गर्छन्।

अनुरोध बडी
{
  "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 हो कि जाँच गर्नुहोस्। फङ्क्शन कलिङ

JSON कसरी आइपुग्छ

  • JSON स्ट्रिङ हो। जवाफको छेउमा पार्स गरिएको अब्जेक्ट हुँदैन: message.content मा आफ्नो JSON पार्सर चलाउनुहोस्।
  • stream: true सँग JSON delta.content का टुक्राहरूमा आउँछ। तिनलाई जोड्नुहोस् र स्ट्रिम सकिएपछि एकपटक पार्स गर्नुहोस्; एउटा टुक्रा आफैंमा मान्य JSON हुँदैन।
  • होस्ट गरिएको ओपन-वेट मोडलले response_format अनुरोधको जवाफ रिजनिङ टेक्स्ट बिना दिन्छ: reasoning_content null हुन्छ।

प्राप्त गरेको कुरा जाँच्नुहोस्

स्कीमाले मोडलले कसरी लेख्छ भनेर निर्देशित गर्छ। API ले सकिएको उत्तरलाई तपाईंको स्कीमासँग तुलना गर्दैन, त्यसैले अन्तिम जाँच तपाईंको कोडले गर्छ:

  • माथिको नमूनाले जस्तै, त्रुटि ह्यान्डलरभित्र पार्स गर्नुहोस्।
  • पार्स गरिएको अब्जेक्टलाई आफ्नै कोडमा उही स्कीमासँग प्रमाणित गर्नुहोस्, उदाहरणका लागि JSON Schema लाइब्रेरी, Pydantic वा Zod सँग।
  • लामो उत्तर JSON पूरा हुनु अघि नै सकिन सक्छ। स्ट्रिम नगरिएको जवाफले finish_reason लाई stop वा tool_calls मात्र रिपोर्ट गर्छ, त्यसैले पार्स त्रुटि नै संकेत हो। फेरि सोध्नुहोस्, वा सानो अब्जेक्ट माग्नुहोस्।

त्रुटिहरू

स्थिति प्रकार सन्देश कहिले
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 अब्जेक्ट होइन।

दुवै 400 जवाफ तपाईंको ब्यालेन्सबाट केही घटाउनु अघि पठाइन्छन्। त्रुटि व्यवस्थापन