ഘടനയുള്ള ഔട്ട്പുട്ടുകൾ
ഉത്തരം 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) import OpenAI from "openai";
const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.shannon-ai.com/v1" });
const response = await 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.
let person = null;
try {
person = JSON.parse(response.choices[0].message.content);
} catch {
// ask again, or report the failure
}
console.log(person); curl https://api.shannon-ai.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"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"]
}
}
}
}' JSON എന്നത് choices[0].message.content-ലെ ഉത്തര ടെക്സ്റ്റ് ആണ്:
{
"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 മോഡലുകൾ എല്ലാ സ്കീമയും strict ആയി പ്രയോഗിക്കുന്നു. | ഹോസ്റ്റ് ചെയ്ത ഓപ്പൺ-വെയിറ്റ് മോഡലുകൾ |
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 ഓരോ ഐഡിക്കും ഇതുതന്നെ റിപ്പോർട്ട് ചെയ്യുന്നു: എല്ലാ മോഡലിലും capabilities.json_schema, ഹോസ്റ്റ് ചെയ്ത ഓപ്പൺ-വെയിറ്റ് മോഡലുകളിൽ capabilities.response_format. മോഡലുകളും വിലയും
പിന്തുണയ്ക്കുന്ന സ്കീമ കീവേഡുകൾ
നിങ്ങളുടെ സ്കീമയിൽ നിന്ന് മോഡലിന് എന്ത് ലഭിക്കുന്നു എന്നത് മോഡലിനെയും എൻഡ്പോയിന്റിനെയും ആശ്രയിച്ചിരിക്കും:
| മോഡലുകൾ | /v1/chat/completions | /v1/responses |
|---|---|---|
| Shannon മോഡലുകൾ | താഴെയുള്ള കീവേഡ് ഉപസഞ്ചയം, strict ആയി പ്രയോഗിക്കുന്നു. | നിങ്ങൾ അയയ്ക്കുന്നതുപോലെ തന്നെ സ്കീമ, strict ആയി പ്രയോഗിക്കുന്നു. |
| ഹോസ്റ്റ് ചെയ്ത ഓപ്പൺ-വെയിറ്റ് മോഡലുകൾ | നിങ്ങൾ അയയ്ക്കുന്നതുപോലെ തന്നെ സ്കീമ. നിങ്ങൾ അയച്ചില്ലെങ്കിൽ 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-ൽ നിന്ന് റിസോൾവ് ചെയ്യുന്നു. description പോലെ $ref-ന് അടുത്ത് എഴുതിയ കീവേഡുകൾ നിലനിർത്തും. പൊരുത്തപ്പെടുന്ന നിർവചനമില്ലാത്ത റഫറൻസ് {"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 ആണ്:
{
"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"}
} {
"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ആയാൽ JSONdelta.contentകഷണങ്ങളായി എത്തുന്നു. അവ ചേർത്ത് സ്ട്രീം അവസാനിച്ചതിന് ശേഷം ഒരിക്കൽ പാഴ്സ് ചെയ്യുക; ഒരു കഷണം മാത്രമായി സാധുവായ JSON അല്ല.- ഹോസ്റ്റ് ചെയ്ത ഓപ്പൺ-വെയിറ്റ് മോഡൽ
response_formatറിക്വസ്റ്റിന് റീസണിംഗ് ടെക്സ്റ്റ് ഇല്ലാതെ ഉത്തരം നൽകുന്നു:reasoning_contentnullആണ്.
ലഭിക്കുന്നത് പരിശോധിക്കുക
മോഡൽ എങ്ങനെ എഴുതണമെന്ന് സ്കീമ നയിക്കുന്നു. പൂർത്തിയായ ഉത്തരത്തെ API നിങ്ങളുടെ സ്കീമയുമായി താരതമ്യം ചെയ്യുന്നില്ല, അതിനാൽ അവസാന പരിശോധന നിങ്ങളുടെ കോഡ് നടത്തുന്നു:
- മുകളിലെ സാമ്പിൾ ചെയ്യുന്നതുപോലെ, ഒരു എറർ ഹാൻഡ്ലറിനുള്ളിൽ പാഴ്സ് ചെയ്യുക.
- പാഴ്സ് ചെയ്ത ഒബ്ജക്റ്റ് ഇതേ സ്കീമ ഉപയോഗിച്ച് നിങ്ങളുടെ സ്വന്തം കോഡിൽ വാലിഡേറ്റ് ചെയ്യുക, ഉദാഹരണത്തിന് ഒരു JSON Schema ലൈബ്രറി, Pydantic അല്ലെങ്കിൽ Zod ഉപയോഗിച്ച്.
- ദൈർഘ്യമേറിയ ഉത്തരം JSON പൂർത്തിയാകുന്നതിന് മുമ്പ് അവസാനിക്കാം. സ്ട്രീം ചെയ്യാത്ത മറുപടി
finish_reasonstopഅല്ലെങ്കിൽ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 മറുപടികളും നിങ്ങളുടെ ബാലൻസിൽ നിന്ന് ഒന്നും എടുക്കുന്നതിന് മുമ്പ് അയയ്ക്കുന്നു. എറർ ഹാൻഡ്ലിംഗ്