કન્ટેન્ટ પર જાઓ
રચિત આઉટપુટ્સ

રચિત આઉટપુટ્સ

જવાબ JSON તરીકે માગો: કોઈ પણ JSON ઓબ્જેક્ટ, અથવા તમે મોકલેલા 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 નું ફીલ્ડ છે. તેના વિના જવાબ સામાન્ય ટેક્સ્ટ હોય છે.

મોડ મૂલ્ય તમને શું મળે છે
JSON ઓબ્જેક્ટ {"type": "json_object"} જવાબ નિર્ધારિત આકાર વિનાનું JSON છે. તમને કઈ કીઝ જોઈએ છે તે પ્રોમ્પ્ટમાં કહો.
JSON schema {"type": "json_schema", "json_schema": {"name": "…", "schema": {…}}} જવાબ તમે મોકલેલા 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 schema માટેનું લેબલ, OpenAI ફોર્મેટની જેમ. હોસ્ટેડ open-weight મોડેલ્સ
json_schema.strict boolean true જવાબે schema ને બરાબર અનુસરવું પડે કે નહીં. Shannon મોડેલ્સ દરેક schema ને strict તરીકે લાગુ કરે છે. હોસ્ટેડ open-weight મોડેલ્સ

JSON ઓબ્જેક્ટ મોડને schema ની જરૂર નથી. કીઝનાં નામ પ્રોમ્પ્ટમાં આપો:

રિક્વેસ્ટ બોડી
{
  "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, અને હોસ્ટેડ open-weight મોડેલ્સ પર capabilities.response_format. મોડેલ્સ અને કિંમત

સપોર્ટેડ schema કીવર્ડ્સ

તમારા schema નું શું મોડેલને મળે છે તે મોડેલ અને એન્ડપોઇન્ટ પર આધાર રાખે છે:

મોડેલ્સ /v1/chat/completions /v1/responses
Shannon મોડેલ્સ નીચેનો કીવર્ડ સબસેટ, strict તરીકે લાગુ. schema જેવું તમે મોકલો છો તેવું, strict તરીકે લાગુ.
હોસ્ટેડ open-weight મોડેલ્સ schema જેવું તમે મોકલો છો તેવું. તમે મોકલો નહીં ત્યાં સુધી strict true છે. schema જેવું તમે મોકલો છો તેવું. તમે મોકલો નહીં ત્યાં સુધી 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 schema ના ટોચના સ્તરે $defs અથવા definitions માંથી ઉકેલાય છે. $ref ની બાજુમાં લખેલા કીવર્ડ્સ, જેમ કે description, જળવાય છે. મેળ ખાતી વ્યાખ્યા વિનાનો સંદર્ભ {"type": "object"} તરીકે વાંચાય છે.
allOf સભ્યોને એક schema માં ભેળવવામાં આવે છે. જ્યાં બે સભ્યો એક જ કીવર્ડ સેટ કરે ત્યાં પછીનો ગણાય છે.
oneOf anyOf તરીકે વાંચાય છે.
પોતાનો જ સંદર્ભ આપતા schemas 24 સ્તર સુધી વિસ્તારાય છે. વધુ ઊંડા સ્તરને {"type": "object"} તરીકે વાંચવામાં આવે છે.
સબસેટની બહાર બાકીના દરેક કીવર્ડ, ઉદાહરણ તરીકે additionalProperties, const, multipleOf, uniqueItems, exclusiveMinimum, exclusiveMaximum, patternProperties, prefixItems, minProperties, maxProperties, not અને if / then / else. આ મોડેલને મળતા schema નો ભાગ નથી.

/v1/responses પર: text.format વાપરવાની રીત

/v1/responses એ જ રિક્વેસ્ટને text.format તરીકે લખે છે. schema સીધું 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 પર: schema તરીકે ટૂલ

Messages ફોર્મેટમાં સ્ટ્રક્ચર્ડ-આઉટપુટ ફીલ્ડ નથી. એ જ મોડેલ્સમાંથી JSON માટે, એ જ કી સાથે /v1/chat/completions અથવા /v1/responses કોલ કરો.

/v1/messages પર જ રહેવા માટે, ઓબ્જેક્ટને ટૂલ તરીકે વર્ણવો: તમારું schema input_schema માં મૂકો અને tool_choice માં ટૂલનું નામ આપો. ત્યારે tool_use બ્લોકનું input તમારો ઓબ્જેક્ટ છે, પહેલેથી પાર્સ કરેલો. નામ આપેલું tool_choice હોસ્ટેડ open-weight મોડેલ્સ લાગુ કરે છે.

રિક્વેસ્ટ બોડી
{
  "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 નથી.
  • હોસ્ટેડ open-weight મોડેલ response_format રિક્વેસ્ટનો જવાબ રીઝનિંગ ટેક્સ્ટ વિના આપે છે: reasoning_content null છે.

જે મળે તે તપાસો

schema મોડેલ કેવી રીતે લખે છે તેનું માર્ગદર્શન કરે છે. API તૈયાર જવાબને તમારા schema સાથે સરખાવતું નથી, તેથી અંતિમ તપાસ તમારો કોડ કરે છે:

  • એરર હેન્ડલરની અંદર પાર્સ કરો, જેમ ઉપરનું ઉદાહરણ કરે છે.
  • પાર્સ કરેલા ઓબ્જેક્ટને તમારા પોતાના કોડમાં એ જ schema વડે ચકાસો, ઉદાહરણ તરીકે 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 જવાબો તમારા બેલેન્સમાંથી કંઈ પણ કપાય તે પહેલાં મોકલાય છે. ભૂલ સંભાળ