Peke ki ngā ihirangi
Whakahaere hapa

Whakahaere hapa

Ka whakahoki te tono rahua i tētahi waehere tūnga HTTP me tētahi tinana JSON me tētahi momo hapa me tētahi karere. Ka rārangi tēnei whārangi i ia tūnga, ka whakaatu i te tinana hapa o ia hōputu API, ā, ka kī ko ēhea hapa e tika ana kia tukuna anō.

Ka whakaara ngā SDK i tētahi ahanoa hapa e kawe ana i te waehere tūnga me ngā āpure o te tinana. Ka whakaingoa tēnei tono i tētahi model kāore e noho ana:

import openai
from openai import OpenAI

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

try:
    client.chat.completions.create(
        model="no-such-model",
        messages=[{"role": "user", "content": "Hello"}],
    )
except openai.APIStatusError as error:
    print(error.status_code)                           # 400
    print(error.type)                                  # invalid_request_error
    print(error.body["message"])                       # unknown model: no-such-model
    print(error.response.headers.get("x-request-id"))  # the id of this request

Ka whakahoki i te tūnga 400 me tēnei tinana:

400 JSON
{
  "error": {
    "type": "invalid_request_error",
    "message": "unknown model: no-such-model"
  }
}

Tinana hapa

E rua ngā tinana. Ka whakautu te /v1/messages i te hōputu Anthropic; ka whakautu ia endpoint atu i te hōputu OpenAI. I ngā mea e rua, kei te error.type me te error.message ngā taipitopito, nō reira kotahi te kaipānui mō ngā endpoint katoa.

401 Hōputu OpenAI
{
  "error": {
    "type": "authentication_error",
    "message": "Invalid API key",
    "code": "invalid_api_key",
    "param": null
  }
}
401 Hōputu Anthropic
{
  "type": "error",
  "error": {
    "type": "authentication_error",
    "message": "Invalid API key"
  }
}
Āpure Momo Whakaahuatanga
error.type string Te momo hapa. Tirohia Ngā momo hapa i raro iho.
error.message string He rerenga mō te tangata. Ka tūturu ngā kuputuhi i ngā ripanga i raro iho, haunga ōna wāhanga rerekē, e whakaatuhia ana hei <id>, <n> he … rānei.
error.code string | null Kei ētahi hapa o te hōputu OpenAI: invalid_api_key me te 401, request_too_large me te 413, context_length_exceeded ina roa rawa tētahi kōrero, ki te kore he null he ngaro rānei. Whakatauhia i runga i te tūnga me te error.type i te tuatahi, ā, whakaarohia te code hei mea tāpiri.
error.param null Kei runga i ngā hapa ōrite ki te code. Ko null i ngā wā katoa.
type string Hōputu Anthropic anake. Ko error i ngā wā katoa.

Ngā momo hapa

Momo Ngā tūnga Whakaahuatanga
invalid_request_error 400, 413, 415, 422 He hē te tono ake: he āpure, te tinana, te id model, he input rānei kāore e whakaaetia e te model. Huria te tono i mua i te tuku anō.
authentication_error 401 Kei te ngaro te API key, kāore e mōhiotia, kua whakakorea rānei.
not_found_error 404 Kāore te ara e noho ana.
rate_limit_error 429 Kāore te toenga e kapi i te tono, kua aukati te Flood protection, he pukumahi rānei te model. Ka kī te karere ko tēhea.
api_error 500, 502, 503, 504 I pai te tono, ā, kei tō mātou taha te rahua. Tukuna anō.
server_error 500 I pai te tono, ā, kei tō mātou taha te rahua. Tukuna anō.

Ngā waehere tūnga

Tūnga Momo Karere Āhea
400 invalid_request_error Failed to parse the request body as JSON: … Ehara te tinana i te JSON tika.
400 invalid_request_error unknown model: <id> Ehara te model i tētahi id kua whakaputaina. I runga i te /v1/responses ina ngaro hoki te model.
400 invalid_request_error No user message provided Ngā taumata Shannon: kāore he kuputuhi kaiwhakamahi i roto i te tono.
400 invalid_request_error <id> does not accept image input I tukuna tētahi whakaahua ki tētahi model open-weight hosted kāore he input whakaahua.
400 invalid_request_error audio and video input are not supported by any hosted model I tukuna tētahi wāhanga oro, ataata rānei ki tētahi model open-weight hosted.
400 invalid_request_error <id> does not accept response_format I tonoa te output whakaritea mai i tētahi model open-weight hosted kāore e whai.
400 invalid_request_error <id> does not accept response_format json_schema; use json_object I tukuna tētahi JSON schema ki tētahi model open-weight hosted e whakaae ana ki te json_object anake.
400 invalid_request_error unknown reasoning effort '<value>'; expected off, low, medium or high Ehara te reasoning effort i tētahi o ngā uara kua rārangihia. Ngā model open-weight hosted.
400 invalid_request_error count_tokens is available for the hosted open models; unknown model: <id> I tonoa te tatau token mō tētahi model i waho i ngā model open-weight hosted.
401 authentication_error Missing authentication Kāore he API key i tukuna.
401 authentication_error Invalid API key Kāore e mōhiotia te key, kua whakakapia e tētahi hou ake, kua whakakorea rānei.
404 not_found_error Not Found Kāore te ara e noho ana, hei tauira /v1/embeddings.
405 api_error Method Not Allowed Kei te noho te ara engari ehara ki tēnei tikanga HTTP.
413 invalid_request_error Failed to buffer the request body: … Nui ake te tinana tono i te 32 MiB.
415 invalid_request_error Expected request with `Content-Type: application/json` Kei te ngaro te header Content-Type, ehara rānei i te application/json.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … Kei te ngaro tētahi āpure e hiahiatia ana, pērā i te messages, he hē rānei te momo JSON o tētahi āpure.
429 rate_limit_error Quota exceeded. Upgrade your plan at shannon-ai.com/plan Nui ake te tepe output o te tono i te toenga o tō toenga.
429 rate_limit_error Too many requests. Retry in <n>s. Flood protection: neke atu i te 120 tono i roto i te meneti kotahi i tō pūkete. Ko <n> te maha o ngā hēkona tae noa ki te mutunga o te meneti.
429 rate_limit_error Shannon Coder call quota reached. Upgrade your plan at shannon-ai.com/plan shannon-coder-1 i runga i te /v1/chat/completions me te /v1/messages: kua pau ngā karanga Shannon Coder o tō mahere mō te matapihi 4 hāora o nāianei.
429 rate_limit_error Shannon routes are temporarily busy. Please retry. He pukumahi te model i tēnei wā.
500 server_error The model backend failed to answer. Please retry. Kāore te model i whakaputa i tētahi whakautu.
500 server_error An internal error occurred. Please retry. He rahua o roto i te wā e whakaritea ana te tono.
502 api_error The model backend failed to answer. Please retry. Kāore te model i whakaputa i tētahi whakautu. Te whānau Shannon 3 me ngā model open-weight hosted.
503 api_error Could not verify your quota right now. Please retry. Kāore i taea te pānui i tō toenga i tēnei wā.
503 api_error token counting is temporarily unavailable for this model Kāore e watea te tatau token mō tēnei model i tēnei wā.
504 api_error upstream timed out I roa ake te wā o tētahi model open-weight hosted i te tepe wā o tētahi tono kāore e stream.

Ka mahi ngā tirohanga i tēnei raupapa: API key, tinana tono, id model, Flood protection, toenga. Nō reira ka whakahoki te tono me tētahi key hē me tētahi tinana hē i te 401. Kāore e utua tētahi tono e whakakāhoretia ana me tētahi tūnga 4xx.

Toenga me Flood protection

Ka ratoa ia tono i te ōrite: kāore he taumata rate, kāore hoki he quota API motuhake. E rua ngā mea ka taea tonu te whakautu ki te 429, ā, he rate_limit_error te momo o ngā mea e rua. Ka wehewehe te karere i a rāua.

Toenga

Ko tō toenga ko te whakaaetanga o te mahere mō tēnei rā, ka tautuhi anō i te 00:00 UTC, ā, i muri i tērā ko tō nama kua hokona. Ka tango te Chat me te API i te toenga kotahi.

Ka wāhi atu ia tono i tōna tepe output i te tuatahi: max_tokens, te max_output_tokens rānei i runga i te /v1/responses, 4,096 ina kāore koe e tuku i tētahi. Ina oti te whakautu, ka whakakapia te nui kua wāhi atu e te mea i whakamahia e te tono. Mēnā kāore te nui e uru ki te toenga e toe ana, ka whakahoki te tono i te Quota exceeded i mua i te tīmatanga o te model.

Ka wāhi atu ia tono e rere tahi ana i tōna ake nui. Nō reira ka taea e te maha o ngā tono whakarara me te max_tokens teitei te whakapau i te toenga mō tētahi wā poto, ahakoa he poto ia whakautu. Ka iti ake te wāhi atu i te max_tokens iti ake.

Flood protection

Ka whakaae te Flood protection ki te 120 tono i ia meneti mō ia pūkete. Ka tatau i te pūkete, kaua i te key, i runga i te /v1/chat/completions, /v1/responses me te /v1/messages tahi. Ka tīmata te meneti i tō tono tuatahi.

Ka whakahoki te tono tau 121 i roto i taua meneti i te Too many requests. Retry in <n>s. ā, kāore he utu. Ka tatau hoki ngā tono kua whakakāhoretia, nō reira ka noho whakakāhoretia tētahi kiritaki e tuku anō ana me te kore tatari tae noa ki te mutunga o te meneti. Tatari i ngā hēkona <n> mai i te karere.

Kāore e whakakāhoretia ngā tono e tukuna whakarara ana mō te whakarara. Ka tatari rātou i te rārangi, ā, ka whakautua i te raupapa.

Kāore e tatauria te GET /v1/models me ngā endpoint tatau token.

Ētahi atu mō ngā tepe me te toenga, me te wāhi hei tiro i tō ake toenga me tō whakamahinga: Ngā tepe me te toenga Keys & usage

Ngā mea hei whakamātau anō

Tukuna anō tētahi tono ina taea anake e te tono ōrite te angitu. Kua whakamātau anō kē ngā SDK OpenAI me Anthropic i ngā whakautu 429 me 5xx e rua ngā wā mā rātou anō; tautuhia te max_retries (maxRetries i JavaScript) hei huri i tērā.

Hapa Tukuna anō? Te tikanga
400, 404, 405, 413, 415, 422 Kāo. Whakatikaina te tono i te tuatahi. Ka whakaingoa te karere i te āpure, i te model, i te input rānei e hē ana.
401 Kāo. Whakamahia he key tika. Waihangatia, whakakapia rānei te key i te whārangi Keys & usage.
429 Too many requests Āe, i muri i te tatari. Tatari i te maha o ngā hēkona i te karere, kātahi ka tuku i te tono ōrite.
429 Quota exceeded Kāo, pērā i tēnei. Whakaitia te max_tokens, tatari ki te tautuhi anō ia rā i te 00:00 UTC, tāpiri rānei i tētahi nama.
429 Shannon Coder call quota reached Āe, i te matapihi e whai ake nei. Ka tīmata ngā matapihi ia 4 hāora mai i te 00:00 UTC. Ka whakautu tētahi atu model i taua wā tonu.
429 … temporarily busy Āe. Tatari kia rua toru hēkona, kātahi kia roa ake i ia whakamātau e whai ake nei.
500, 502, 503 Āe. Tatari kia 1 hēkona, kātahi kia 2, 4 me te 8. Kati i muri i ētahi whakamātau, ā, pūrongotia te id tono.
504 Āe. Tukuna me te stream kua tautuhia ki te true: ka pā te tepe wā ki ngā whakautu kāore e stream.

He retry loop e whai ana i tēnei ripanga:

import re
import time

import openai
from openai import OpenAI

# max_retries=0: this loop does the retrying, the SDK does not add its own.
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.shannon-ai.com/v1", max_retries=0)


def create_with_retry(attempts=5, **request):
    for attempt in range(attempts):
        try:
            return client.chat.completions.create(**request)
        except openai.RateLimitError as error:
            text = str(error)
            flood = re.search(r"Retry in (\d+)s", text)
            if flood:
                time.sleep(int(flood.group(1)))   # flood protection: wait out the minute
            elif "temporarily busy" in text:
                time.sleep(2 ** attempt)
            else:
                raise                             # balance: a wait of seconds does not help
        except (openai.InternalServerError, openai.APIConnectionError):
            time.sleep(2 ** attempt)              # 1, 2, 4, 8 seconds
    raise RuntimeError("the request failed after all attempts")


response = create_with_retry(
    model="shannon-3",
    messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)

Ngā hapa i roto i tētahi stream

He tūnga 200 tō tētahi stream mai i tōna paita tuatahi. Ka tae mai tētahi rahua i muri i tērā hei kaupapa i roto i te stream: he chunk me tētahi mema error i runga i te /v1/chat/completions, he kaupapa error i runga i te /v1/messages, he kaupapa response.failed he error rānei i runga i te /v1/responses. Ka whai tonu ngā kaupapa kati o te stream. Whakaarohia taua whakautu hei mea kua rahua, ā, tukuna anō te tono, pērā i tētahi tūnga 5xx.

Ka pūrongotia i tēnei tikanga tētahi kōrero e roa ake ana i te matapihi horopaki o te model, me te momo invalid_request_error me te waehere context_length_exceeded. Whakapotoa te kōrero i mua i te tuku anō.

Kua whakaahuatia ngā frame tika i raro i te streaming. Rere

Ngā id tono

He header x-request-id tō ia whakautu, tae atu ki ngā hapa. Ko te uara i tukuna e koe i tō ake header x-request-id, 12 tohu hekihekimara rānei ina kāore koe i tuku. Tuhia ki te log, ā, whakahuatia ina tono āwhina koe mō tētahi tono.