Peke ki ngā ihirangi
Tīmatanga Tere

Tīmatanga Tere

Hangaia tō tono tuatahi: tikina tō key, tukuna he karere kotahi, ā, pānuitia te whakautu. Ka kōrero te API i ngā hōputu OpenAI me Anthropic, nō reira ka mahi te SDK e whakamahi ana koe i muri i tō huri i tōna base URL me tōna key.

1. Tikina tō key

Kotahi te API key a ia pūkete. Takiurutia, huakina Keys & usage, ā, tāruatia. Ka mahi te key i ia endpoint me ia model. Keys & usage

2. Tohua tō kiritaki ki te API

Kei raro i te base URL kotahi ngā endpoint katoa. Ka tango tētahi SDK OpenAI i a ia me te /v1 i te pito; ka tango tētahi SDK Anthropic i a ia me te kore, nā te mea ka tāpiri ia i te /v1/messages mōna ake.

Kiritaki Base URL
Ngā SDK OpenAI me ngā kiritaki hototahi ki a OpenAI https://api.shannon-ai.com/v1
Ngā SDK Anthropic https://api.shannon-ai.com

E toru ngā endpoint e whakaputa kuputuhi ana. Ka tae rātou ki ngā model ōrite; kōwhiria te mea ka whakamahi tō waehere i tōna hōputu kē.

Endpoint Hōputu
POST /v1/chat/completions OpenAI Chat Completions
POST /v1/messages Anthropic Messages
POST /v1/responses OpenAI Responses

3. Tukuna he tono tuatahi

Tukuna he karere user kotahi ki te shannon-3 i te endpoint Chat Completions. Whakakapia te YOUR_API_KEY ki tō key.

POST https://api.shannon-ai.com/v1/chat/completions

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="shannon-3",
    messages=[
        {"role": "user", "content": "Hello, Shannon!"}
    ],
    max_tokens=1024
)

print(response.choices[0].message.content)
Āpure Momo Taunoa Whakaahuatanga
model string Te id o te model e whakautu ana. Kua rārangihia ngā id i Models & pricing.
messages array Te kōrero tae noa ki nāianei: ngā ahanoa me te role me te content. Ko te karere user whakamutunga te prompt. E hiahiatia ana.
max_tokens integer 4096 Te tahua output o te tono, mai i te 1 ki te 65,536. Ka pupuri te tono i tēnei nui i tō toenga, ā, ka utua ia mō te mea i whakamahia tūturutia e ia.
stream boolean false Ka tuku te true i te whakautu wāhanga ki te wāhanga i a ia e tuhia ana.

He JSON te tinana tono, ā, e hiahia ana ki te header Content-Type: application/json. Kei te whārangi endpoint te rārangi katoa o ngā āpure. Chat Completions

Te whakautu

Ka whakahoki tētahi karanga kāore he stream i tētahi ahanoa JSON kotahi ina oti te whakautu. He tauira ngā kuputuhi me ngā tau i raro iho.

200 JSON
{
  "id": "chatcmpl-9b2f6c1e4a7d4f0f8a3c5d2e1b6a7c90",
  "object": "chat.completion",
  "created": 1791590400,
  "model": "shannon-3",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! What would you like to work on?",
        "reasoning_content": "A greeting. A short, friendly reply fits."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 1386,
    "completion_tokens": 74,
    "total_tokens": 1460
  }
}
Āpure Momo Whakaahuatanga
id string chatcmpl- ka whai ko ngā tohu hekihekimara e 32.
object string Ko chat.completion i ngā wā katoa.
created integer Te wā o te whakautu i ngā hēkona Unix.
model string Te id kua whakaputaina o te model i whakautu, ahakoa i tukuna e koe tētahi alias, tētahi atu tuhinga rānei.
choices array Kotahi te urunga i ngā wā katoa, me te index 0.
choices[0].message.role string Ko assistant i ngā wā katoa.
choices[0].message.content string | null Te whakautu. Ka taea te null ina kawea e te whakautu te tool_calls.
choices[0].message.reasoning_content string | null Te reasoning a te model i mua i tana whakautu, null rānei ina kāore he.
choices[0].message.tool_calls array Kei reira anake ina karanga te model i tētahi o ngā tool i whakapuakina e koe.
choices[0].finish_reason string stop mō tētahi whakautu, tool_calls ina karanga te model i tētahi tool.
usage.prompt_tokens integer Ngā token i pānuitia e te model mō tēnei whakautu. I runga i te shannon-2-* me te whānau Shannon 3 ka nui ake pea i te kuputuhi i tukuna e koe.
usage.completion_tokens integer Ngā token i tuhia e te model.
usage.total_tokens integer Te tapeke. Ka tātaihia te utu o tētahi model Shannon mai i tēnei tau.
  • Kei te usage ngā tatau token, kaua te moni. Ko te mea i tangohia e te karanga i tō toenga ka whakaatuhia mō ia tono i Keys & usage; kei Ngā tepe me te toenga te tikanga o tana tātai. Ngā tepe me te toenga
  • Kei ia whakautu, tae atu ki tētahi hapa me tētahi stream, te header x-request-id. Mēnā e kawe ana tō tono i tētahi header x-request-id, ka hoki mai tōna uara; ki te kore, ko te uara he 12 tohu hekihekimara. Pupuritia ki ō log: ka whakaingoa i tēnei tono kotahi.

Te karanga ōrite hei stream

Tautuhia te stream ki te true kia pānuitia te whakautu i a ia e tuhia ana. Ka noho tonu ētahi atu mea katoa o te tono.

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.shannon-ai.com/v1"
)

stream = client.chat.completions.create(
    model="shannon-3",
    messages=[
        {"role": "user", "content": "Hello, Shannon!"}
    ],
    max_tokens=1024,
    stream=True
)

for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

He rārangi kotahi ia frame o te stream ka tīmata ki te data: ā, kei roto he ahanoa JSON. Kotahi frame, kua tāngia ki ētahi rārangi:

Kotahi frame
{
  "id": "chatcmpl-9b2f6c1e4a7d4f0f8a3c5d2e1b6a7c90",
  "object": "chat.completion.chunk",
  "created": 1791590400,
  "model": "shannon-3",
  "choices": [
    {
      "index": 0,
      "delta": {
        "content": "Hello! "
      },
      "finish_reason": null
    }
  ]
}
Ngā frame, i te raupapa delta finish_reason
I a te model e whakaaro ana. Kore, neke atu rānei ngā frame. {"reasoning_content": "…"} null
Te whakautu, wāhanga ki te wāhanga. Kore, neke atu rānei ngā frame. {"content": "…"} null
Te frame JSON whakamutunga. Ka kawe hoki i te usage; kāore koe e tono. {} "stop"
Te mutunga o te stream: te rārangi data: [DONE].
  • He tūnga 200 me te content-type: text/event-stream tō te whakautu. He rārangi data: <json> ia frame, ā, ka whai ko tētahi rārangi kau.
  • Ka tae mai tētahi rārangi : ping ia 15 hēkona. He kōrero tērā e pupuri ana i te hononga tuwhera; ka whakarere ngā kiritaki SSE i a ia.
  • Ka tae mai tētahi rahua i muri i te whakatuwheratanga o te stream hei frame data: {"error": {…}}. Ka noho tonu te tūnga ki te 200, nō reira tirohia ngā frame.

Rere

Ina rahua te karanga

He ahanoa JSON te hapa me tētahi error e kawe ana i te type me te message. Koinei ngā whakautu e tūtaki pinepine ai te karanga tuatahi.

Tūnga Momo Karere Te tikanga
401 authentication_error Missing authentication Kāore he key i tae ki te API. Tukuna te Authorization: Bearer ka whai ko tō key.
401 authentication_error Invalid API key Ehara te key i te key o nāianei o tētahi pūkete. Tāruatia anō mai i Keys & usage.
400 invalid_request_error unknown model: <id> Ehara te model i tētahi id kua whakaputaina. Whakamahia tētahi id mai i Models & pricing.
422 invalid_request_error Failed to deserialize the JSON body into the target type… He hē te momo JSON o tētahi āpure, kei te ngaro rānei te messages.
429 rate_limit_error Quota exceeded. Upgrade your plan at shannon-ai.com/plan Kāore te tahua output o te tono e uru ki tō toenga.

Whakahaere hapa

Te wāhi hei haere i muri mai

  • Ngā model me ngā utu — Kōwhiria tētahi model: ngā id, ngā matapihi horopaki me ngā utu.
  • Chat Completions — Ia āpure tono, āpure whakautu me kaupapa stream o te endpoint i karangatia e koe.
  • Rere — Ngā frame o tētahi stream, ngā rārangi keep-alive me ngā hapa i roto i tētahi stream.
  • Karanga ā‑mahi — Tukuna te model ki te karanga i ō function.
  • Ngā tepe me te toenga — Te mea e pupuri ai, e utu ai hoki tētahi tono, me te maha o ngā tono ka taea e koe te tuku.
  • API Playground — Whakamātauria ngā tono i te browser me tō key.