Yiya kumxholo
Umsebenzi Ukufowuna

Umsebenzi Ukufowuna

Chaza imisebenzi yakho kwisicelo kwaye imodeli iphendula ngobizo oluyifunayo. Ikhowudi yakho iyaluqhuba kwaye ibuyisele iziphumo: i-API ibuyisa ubizo lwee-tool kwaye ayiyiqhubi i-tool ngokwayo.

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

Imizekelo kweli phepha isebenzisa i-/v1/chat/completions. Iluphu enye iqhuba kwi-/v1/responses nakwi-/v1/messages; apho ifomethi ibhala into ngokwahlukileyo, amacandelo angezantsi abonisa zontathu.

import json
from openai import OpenAI

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

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name, for example Paris"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
            },
            "required": ["city"],
        },
    },
}]

def get_weather(city, unit="celsius"):
    # Your code: ask a weather service here.
    return {"city": city, "temperature": 18, "unit": unit, "conditions": "cloudy"}

messages = [{"role": "user", "content": "What is the weather in Paris right now?"}]
answer = None

for _ in range(5):  # the API sets no limit on rounds: set your own
    response = client.chat.completions.create(
        model="Kimi-K3-3BIT-REAP",
        messages=messages,
        tools=tools,
    )
    choice = response.choices[0]
    if choice.finish_reason != "tool_calls":
        answer = choice.message.content
        break

    messages.append(choice.message)  # the assistant turn with its tool_calls
    for call in choice.message.tool_calls:
        args = json.loads(call.function.arguments)  # arguments is a JSON string
        result = get_weather(**args)
        messages.append({
            "role": "tool",
            "tool_call_id": call.id,
            "content": json.dumps(result),
        })

print(answer)

Impendulo yokuqala ayiphenduli mbuzo. Icela ubizo:

200 JSON
{
  "id": "chatcmpl-5b0c1d8e7f6a4b3c9d2e1f0a8b7c6d5e",
  "object": "chat.completion",
  "created": 1791590400,
  "model": "Kimi-K3-3BIT-REAP",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "reasoning_content": "The user asks for the current weather in Paris. I should call get_weather.",
        "tool_calls": [
          {
            "id": "call_9f1c2ab34d5e4f60a7b8c9d0e1f2a3b4",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"city\":\"Paris\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ],
  "usage": {
    "prompt_tokens": 312,
    "completion_tokens": 58,
    "total_tokens": 370,
    "prompt_tokens_details": {"cached_tokens": 0},
    "completion_tokens_details": {"reasoning_tokens": 31}
  }
}

Emva kokuba uthumele umphumo, impendulo elandelayo yimpendulo eqhelekileyo: i-message.content ibamba umbhalo kwaye i-finish_reason yi-stop.

Iluphu, inyathelo ngenyathelo

Inyathelo Ngubani Okwenzekayo
1 Wena Thumela i-messages kunye ne-tools, uluhlu lwemisebenzi imodeli enokuyicela.
2 Imodeli Impendulo ithwala i-tool_calls kunye ne-finish_reason: "tool_calls". Ubizo ngalunye lubiza umsebenzi kwaye lunika ii-argument zawo.
3 Wena Hlalutya i-arguments yobizo ngalunye uze uqhube umsebenzi wakho.
4 Wena Yongeza umyalezo we-assistant nomyalezo omnye we-tool kubizo ngalunye kwi-messages, uze uthumele isicelo kwakhona nge-tools enye.
5 Imodeli Imodeli iphendula ngombhalo nge-finish_reason: "stop", okanye icela ubizo olungakumbi. Kwelo nyathelo buyela kwinyathelo 3.

Isicelo esinye ngumjikelo omnye wemodeli. I-API ayigcini ncoko phakathi kwezicelo kwaye ayisetha nani lemijikelo: ikhowudi yakho ibamba i-messages kwaye igqiba ukuba iyeke nini.

Ukuchaza ii-tool

I-tools luluhlu olunento enye kumsebenzi ngamnye. Yonke imodeli yekhathalogu iyamkela; i-GET /v1/models ixela i-capabilities.tools: true kwi-id nganye.

Intsimi Uhlobo Inkcazelo
type string Ihlala i-function.
function.name string Igama elisetyenziswa yimodeli xa icela lo msebenzi. Iyafuneka.
function.description string Oko umsebenzi ukwenzayo, ngolwimi olulula.
function.parameters object Ii-argument njenge-JSON Schema ene-type: "object" phezulu. Jonga Amagama angundoqo e-schema ngezantsi.

I-endpoint nganye ine-spelling yayo ye-tool. Thumela leyo ibhekiselele kwi-endpoint oyibizayo:

{
  "type": "function",
  "function": {
    "name": "get_weather",
    "description": "Get the current weather for a city.",
    "parameters": {
      "type": "object",
      "properties": {
        "city": {
          "type": "string",
          "description": "City name, for example Paris"
        },
        "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
      },
      "required": ["city"]
    }
  }
}

tool_choice

I-tool_choice ithi ukuba imodeli inokubiza i-tool, okanye kufuneka ibize. Ifundwa kunye ne-tools kuphela.

Ixabiso Inkcazelo Isebenza kwi
"auto" Imodeli ikhetha ukuba iphendule ngombhalo okanye ibize i-tool. Oku kuyinto eqhelekileyo. Zonke iimodeli
"required" Impendulo ibiza i-tool enye ubuncinane. Iimodeli ze-open-weight ezisingathiweyo
{"type": "function", "function": {"name": "get_weather"}} Impendulo ibiza lo msebenzi. Igama kufuneka libe nguye omnye wemisebenzi ekwi-tools. Ubizo lwemisebenzi emininzi aluboniswa kwimpendulo. Iimodeli ze-open-weight ezisingathiweyo

Iimodeli zeShannon (shannon-*) zizikhethela kwisicelo ngasinye, njenge-auto.

Nge-required okanye nomsebenzi obizwe ngegama, funda i-finish_reason phambi kokuba ufunde i-tool_calls. Xa imodeli ingavelisi bubizo, impendulo ngumbhalo one-finish_reason: "stop".

Intsingiselo /v1/chat/completions /v1/responses /v1/messages
Imodeli iyazikhethela "auto" "auto" {"type": "auto"}
Biza i-tool enye ubuncinane "required" "required" {"type": "any"}
Biza umsebenzi omnye obizwe ngegama {"type": "function", "function": {"name": "get_weather"}} {"type": "function", "name": "get_weather"} {"type": "tool", "name": "get_weather"}

Iyamkelwa ngenxa yokuhambelana: i-tool_choice: "none" kunye ne-parallel_tool_calls. Kumjikelo ongenabubizo lwee-tool, thumela isicelo ngaphandle kwe-tools.

Ubizo lwee-tool kwimpendulo

Intsimi Uhlobo Inkcazelo
finish_reason string I-tool_calls xa impendulo ithwala ubizo olunye ubuncinane.
message.tool_calls array Into enye kubizo ngalunye. Intsimi ikhona kuphela xa imodeli icele ubizo.
tool_calls[].id string call_ ilandelwa ziinhlobo ezingama-32 ze-hexadecimal. Yibuyisele njenge-tool_call_id kunye nomphumo.
tool_calls[].type string Ihlala i-function.
tool_calls[].function.name string Umsebenzi ofanele ukuqhutywa.
tool_calls[].function.arguments string Ii-argument njenge-ntambo ye-JSON, "{}" xa zingekho. Yihlalutye phambi kokuyisebenzisa.
message.content string | null I-null xa impendulo ibamba ubizo kuphela. Iimodeli ze-open-weight ezisingathiweyo zinokubuyisa umbhalo ecaleni kobizo; iimodeli zeShannon zibuyisa i-null.

Ubizo olufanayo kwifomethi nganye. Kwi-/v1/responses yinto ye-function_call ye-output, kunye ne-arguments njengentambo ye-JSON. Kwi-/v1/messages yi-block yomxholo ye-tool_use apho i-input yi-JSON object, i-id yayo iqala nge-toolu_, kwaye i-stop_reason yi-tool_use.

{
  "finish_reason": "tool_calls",
  "message": {
    "role": "assistant",
    "content": null,
    "tool_calls": [
      {
        "id": "call_9f1c2ab34d5e4f60a7b8c9d0e1f2a3b4",
        "type": "function",
        "function": {
          "name": "get_weather",
          "arguments": "{\"city\":\"Paris\"}"
        }
      }
    ]
  }
}

Ubizo oluninzi kumjikelo omnye

Impendulo inokuthwala ubizo oluninzi. Qhuba lonke kwaye uthumele umphumo omnye kubizo ngalunye, nganye ne-tool_call_id yayo, kwisicelo esilandelayo.

Iimodeli ze-open-weight ezisingathiweyo zibuyisa ubizo ngalunye olwahlukileyo kanye: ubizo olubini olunegama elifanayo nee-argument ezifanayo lufika njengolunye.

Ubizo lwee-tool kwi-stream

Nge-stream: true, ubizo lufika lulonke: i-chunk ibamba intambo epheleleyo ye-arguments, ngoko akukho zahlulo zokudibanisa. I-index ibala ubizo lomjikelo ukusuka kwi-0.

data: i-chunk enye, ihlelwe
{
  "id": "chatcmpl-5b0c1d8e7f6a4b3c9d2e1f0a8b7c6d5e",
  "object": "chat.completion.chunk",
  "created": 1791590400,
  "model": "Kimi-K3-3BIT-REAP",
  "choices": [
    {
      "index": 0,
      "delta": {
        "tool_calls": [
          {
            "index": 0,
            "id": "call_9f1c2ab34d5e4f60a7b8c9d0e1f2a3b4",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"city\":\"Paris\"}"
            }
          }
        ]
      },
      "finish_reason": null
    }
  ]
}
Endpoint Okufikayo
/v1/chat/completions I-chat.completion.chunk enye kubizo ngalunye, kunye nobizo kwi-delta.tool_calls[0]. I-chunk yokugqibela inayo i-finish_reason: "tool_calls" kunye ne-usage.
/v1/responses Kubizo ngalunye: i-response.output_item.added (into ye-function_call ene-arguments engenanto), i-response.function_call_arguments.delta (yonke intambo), i-response.function_call_arguments.done, i-response.output_item.done.
/v1/messages Kubizo ngalunye: i-content_block_start (i-block ye-tool_use ene-input engenanto), i-content_block_delta enye apho i-input_json_delta.partial_json ngumbhalo wonke wee-argument, i-content_block_stop. I-message_delta emva koko ithwala i-stop_reason: "tool_use".

Kwi-shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite, shannon-2-pro kunye ne-shannon-coder-1, isicelo se-stream esithwala i-tools sikhupha umbhalo wempendulo kwi-delta enye ye-content ekupheleni komjikelo. Umsinga

Ukubuyisela iziphumo

Isicelo sesibini siphinda incoko kwaye songeza izinto ezimbini: umjikelo we-assistant owacela ubizo, nomphumo omnye kubizo ngalunye.

{
  "messages": [
    {"role": "user", "content": "What is the weather in Paris right now?"},
    {
      "role": "assistant",
      "content": null,
      "tool_calls": [
        {
          "id": "call_9f1c2ab34d5e4f60a7b8c9d0e1f2a3b4",
          "type": "function",
          "function": {
            "name": "get_weather",
            "arguments": "{\"city\":\"Paris\"}"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "call_9f1c2ab34d5e4f60a7b8c9d0e1f2a3b4",
      "content": "{\"city\":\"Paris\",\"temperature\":18,\"unit\":\"celsius\",\"conditions\":\"cloudy\"}"
    }
  ]
}
  • Buyisela umjikelo we-assistant njengoko uyamkele, phambi kweziphumo. Umphumo ngamnye udityaniswa nobizo lwawo nge-id: i-tool_call_id kwi-/v1/chat/completions, i-call_id kwi-/v1/responses, i-tool_use_id kwi-/v1/messages.
  • Umphumo ngumbhalo. Guqula i-object ibe yintambo ye-JSON ye-content (i-output kwi-/v1/responses). Kwi-/v1/messages i-content ye-block ye-tool_result yintambo okanye uluhlu lweeblock zombhalo.
  • Kumjikelo we-assistant, i-function.arguments ihlala iyintambo ye-JSON, kanye njengoko yafika.
  • Incoko inokuphela ngomphumo we-tool. Akukho myalezo mtsha womsebenzisi ofunekayo: imodeli iqhubeka isuka kumphumo.
  • Xa umsebenzi wakho uyaleza, thumela ukusilela njengombhalo womphumo. Imodeli inokuwuxela okanye icele elinye ubizo.

Amagama angundoqo e-schema

I-parameters (i-input_schema kwi-/v1/messages) yi-JSON Schema. Intsapho yeShannon 3 (shannon-3, shannon-3-pro, shannon-3.1, shannon-3.1-pro) neemodeli ze-open-weight ezisingathiweyo ziyamkela njengoko uyithumela.

I-shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite, shannon-2-pro kunye ne-shannon-coder-1 zamkela le nxalenye, kuzo zontathu ii-endpoint:

Unyango Amagama angundoqo
Ifundwa njengoko ithunyelwe Igama ngalinye elingundoqo elingabizwanga kumgca ongezantsi, umzekelo i-type, properties, required, items, enum, description, minimum, maximum, anyOf kunye ne-allOf, kuwo onke amanqanaba e-schema.
format Ifundwa kumaxabiso e-int32, int64, float, double, byte, date, date-time kunye ne-enum.
oneOf Ifundwa njenge-anyOf.
Ayiyonxalenye ye-schema efunyanwa zezi modeli $schema, $id, $ref, $defs, $comment, definitions, additionalProperties, unevaluatedProperties, patternProperties, propertyNames, examples, default, title, const, not, if, then, else, contains, minContains, maxContains, dependentRequired, dependentSchemas, dependencies, readOnly, writeOnly, deprecated, contentEncoding, contentMediaType, contentSchema, multipleOf, uniqueItems; i-exclusiveMinimum ne-exclusiveMaximum xa ziyinombolo; ezinye amaxabiso ze-format.

Ukuziphatha nemida

  • I-API ayisetha mda ophezulu wenani lee-tool kwaye ayisetha mda ophezulu wenani lemijikelo.
  • Umzimba wesicelo unokuba ngu-32 MiB, kwaye incoko neziphumo zayo kufuneka zingene kwifestile yomxholo yemodeli.
  • Xa isicelo sithwala i-tools, i-shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite ne-shannon-2-pro zisebenzisa i-temperature engagqibekanga ye-0.2. Thumela i-temperature ukuseta elinye ixabiso.
  • Umjikelo ngamnye sisicelo esizeleyo: sitsala kwibhalansi yakho kwaye sibalwa kwi-flood protection njengayo nayiphi na enye isicelo. Imida nebhalansi

Iimpazamo

Ubizo lwe-tool yimpendulo eqhelekileyo enesimo 200. Ezi mpazamo zinxulumene nesicelo ngokwaso:

Isimo Uhlobo Umyalezo Xa
400 invalid_request_error unknown model: <id> I-model ayiyiyo i-id yoluhlu olupapashiweyo.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … I-tools ayiloluhlu, okanye enye intsimi inohlobo olungalunganga lwe-JSON.
413 invalid_request_error Failed to buffer the request body: … Umzimba wesicelo mkhulu kuno-32 MiB. Impazamo ithwala i-code: "request_too_large".

Imodeli ibhala ii-argument. Zihlole ngokuchasene ne-schema yakho phambi kokuqhuba umsebenzi, njengoko ubuya kuhlola nayiphi na i-input yangaphandle.

Iimpazamo zokuqinisekisa, zebhalansi nezeserver zifana nakwisicelo ngasinye. Ukuphatha imposiso