Peke ki ngā ihirangi
Karanga ā‑mahi

Karanga ā‑mahi

Whakaahuatia ō function i te tono, ā, ka whakautu te model me ngā karanga e hiahia ana ia kia mahia. Ka whakahaere tō waehere i a rātou, ā, ka tuku i ngā hua whakamuri: ka whakahoki te API i ngā karanga tool, ā, kāore ia e whakahaere i tētahi tool.

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

Ka whakamahi ngā tauira o tēnei whārangi i te /v1/chat/completions. Ka rere te loop ōrite i runga i te /v1/responses me te /v1/messages; ina rerekē te tuhi a tētahi hōputu, ka whakaatu ngā wāhanga i raro iho i ngā mea e toru.

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)

Kāore te whakautu tuatahi e whakautu i te pātai. Ka tono ia i tētahi karanga:

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}
  }
}

Whai muri i tō tuku i te hua, he whakautu noa te whakautu e whai ake nei: kei te message.content te kuputuhi, ā, ko stop te finish_reason.

Te loop, hikoi ki te hikoi

Hikoi Wai Ka aha
1 Koe Tukuna te messages me te tools, te rārangi function e taea ai e te model te tono.
2 Model Ka kawe te whakautu i te tool_calls me te finish_reason: "tool_calls". Ka whakaingoa ia karanga i tētahi function, ā, ka hoatu ōna argument.
3 Koe Parauhia ngā arguments o ia karanga, ā, whakahaerehia tō function.
4 Koe Tāpiria te karere assistant me tētahi karere tool mō ia karanga ki te messages, kātahi ka tuku anō i te tono me te tools ōrite.
5 Model Ka whakautu te model ki te kuputuhi me te finish_reason: "stop", ka tono rānei i ētahi atu karanga. Mēnā ko tērā, hoki ki te hikoi 3.

Kotahi tono, kotahi hurihanga a te model. Kāore te API e pupuri i tētahi kōrero i waenga i ngā tono, ā, kāore ia e tautuhi i te maha o ngā rauna: ka pupuri tō waehere i te messages, ā, ka whakatau ia āhea kati ai.

Te tautuhi i ngā tool

He array te tools me tētahi urunga mō ia function. Ka whakaae ia model o te rārangi ki a ia; ka pūrongo te GET /v1/models i te capabilities.tools: true mō ia id.

Āpure Momo Whakaahuatanga
type string Ko function i ngā wā katoa.
function.name string Te ingoa e whakamahia ana e te model ina tono ia i tēnei function. E hiahiatia ana.
function.description string Te mahi a te function, i roto i te reo noa.
function.parameters object Ngā argument hei JSON Schema me te type: "object" i runga. Tirohia Ngā kupu matua schema i raro iho.

He tuhinga motuhake tō ia endpoint mō tētahi tool. Tukuna te mea o te endpoint e karangatia ana e koe:

{
  "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

Ka kī te tool_choice ka taea e te model, me whai rānei, te karanga i tētahi tool. Ka pānuitia anake me te tools.

Uara Whakaahuatanga Whakamahia e
"auto" Ka whakatau te model me whakautu ia ki te kuputuhi, me karanga rānei i tētahi tool. Koinei te taunoa. Ngā model katoa
"required" Ka karanga te whakautu i tētahi tool neke atu rānei. Ngā model open-weight hosted
{"type": "function", "function": {"name": "get_weather"}} Ka karanga te whakautu i tēnei function. Me tētahi o ngā function i roto i te tools te ingoa. Ka waihohia ngā karanga ki ētahi atu function i te whakautu. Ngā model open-weight hosted

Ka whakatau ngā model Shannon (shannon-*) i ia tono, pērā i te auto.

Ki te required ki tētahi function whakaingoa rānei, pānuitia te finish_reason i mua i te pānui i te tool_calls. Ina kāore te model e whakaputa i tētahi karanga, he whakautu kuputuhi te whakautu me te finish_reason: "stop".

Tikanga /v1/chat/completions /v1/responses /v1/messages
Ka whakatau te model "auto" "auto" {"type": "auto"}
Karanga i tētahi tool neke atu rānei "required" "required" {"type": "any"}
Karanga i tētahi function whakaingoa {"type": "function", "function": {"name": "get_weather"}} {"type": "function", "name": "get_weather"} {"type": "tool", "name": "get_weather"}

E whakaaetia ana mō te hototahi: tool_choice: "none" me te parallel_tool_calls. Mō tētahi hurihanga kāore he karanga tool, tukuna te tono kāore he tools.

Ngā karanga tool i te whakautu

Āpure Momo Whakaahuatanga
finish_reason string tool_calls ina kawea e te whakautu tētahi karanga neke atu rānei.
message.tool_calls array Kotahi urunga mō ia karanga. Kei reira anake te āpure ina i tono te model i tētahi karanga.
tool_calls[].id string call_ ka whai ko ngā tohu hekihekimara e 32. Whakahokia hei tool_call_id me te hua.
tool_calls[].type string Ko function i ngā wā katoa.
tool_calls[].function.name string Te function hei whakahaere.
tool_calls[].function.arguments string Ngā argument hei aho JSON, "{}" ina kāore he. Parauhia i mua i te whakamahi.
message.content string | null null ina he karanga anake kei te whakautu. Ka taea e ngā model open-weight hosted te whakahoki kuputuhi i te taha o ngā karanga; ka whakahoki ngā model Shannon i te null.

Te karanga ōrite i ia hōputu. I runga i te /v1/responses he tūemi function_call o te output, me te arguments hei aho JSON. I runga i te /v1/messages he poraka content tool_use tōna input he ahanoa JSON, ka tīmata tōna id ki te toolu_, ā, ko tool_use te stop_reason.

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

He maha ngā karanga i te hurihanga kotahi

Ka taea e tētahi whakautu te kawe i te maha o ngā karanga. Whakahaerehia ia karanga, ā, tukuna tētahi hua mō ia karanga, ia hua me tōna ake tool_call_id, i te tono e whai ake nei.

Ka whakahoki ngā model open-weight hosted i ia karanga rerekē kia kotahi anake: ka tae mai ngā karanga e rua me te ingoa ōrite me ngā argument ōrite hei kotahi.

Ngā karanga tool stream

Ki te stream: true, ka tae mai te karanga hei wāhanga kotahi: kei te chunk te aho arguments katoa, nō reira kāore he kongakonga hei hono. Ka tatau te index i ngā karanga o te hurihanga mai i te 0.

data: kotahi chunk, kua whakahōputu
{
  "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 Ngā mea ka tae mai
/v1/chat/completions Kotahi chat.completion.chunk mō ia karanga, me te karanga i te delta.tool_calls[0]. Kei te chunk whakamutunga te finish_reason: "tool_calls" me te usage.
/v1/responses Mō ia karanga: response.output_item.added (he tūemi function_call me te arguments kau), response.function_call_arguments.delta (te aho katoa), response.function_call_arguments.done, response.output_item.done.
/v1/messages Mō ia karanga: content_block_start (he poraka tool_use me te input kau), kotahi content_block_delta ko tōna input_json_delta.partial_json te kuputuhi argument katoa, content_block_stop. Kātahi ka kawe te message_delta i te stop_reason: "tool_use".

I runga i te shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite, shannon-2-pro me te shannon-coder-1, ka tuku tētahi tono stream e kawe ana i te tools i tana kuputuhi whakautu i roto i te delta content kotahi i te mutunga o te hurihanga. Rere

Te tuku hua whakamuri

Ka tuaruatia e te tono tuarua te kōrero, ā, ka tāpiri e rua ngā mea: te hurihanga assistant i tono i ngā karanga, me tētahi hua mō ia karanga.

{
  "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\"}"
    }
  ]
}
  • Tukuna whakamuri te hurihanga assistant pēnei i tō whiwhinga, i mua i ngā hua. Ka tūhonoa ia hua ki tana karanga mā te id: tool_call_id i runga i te /v1/chat/completions, call_id i runga i te /v1/responses, tool_use_id i runga i te /v1/messages.
  • He kuputuhi te hua. Whakarārangitia tētahi ahanoa ki tētahi aho JSON mō te content (te output i runga i te /v1/responses). I runga i te /v1/messages he aho, he rārangi poraka kuputuhi rānei te content o tētahi poraka tool_result.
  • I te hurihanga assistant, ka noho te function.arguments hei aho JSON, pēnei i tana taenga mai.
  • Ka taea te mutu o te kōrero ki tētahi hua tool. Kāore he karere kaiwhakamahi hou e hiahiatia: ka haere tonu te model mai i te hua.
  • Ina rahua tō function, tukuna te rahua hei kuputuhi hua. Ka taea e te model te pūrongo i a ia, te tono rānei i tētahi atu karanga.

Ngā kupu matua schema

He JSON Schema te parameters (te input_schema i runga i te /v1/messages). Ka whiwhi te whānau Shannon 3 (shannon-3, shannon-3-pro, shannon-3.1, shannon-3.1-pro) me ngā model open-weight hosted i a ia pēnei i tō tuku.

Ka whiwhi te shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite, shannon-2-pro me te shannon-coder-1 i tēnei wāhanga iti, i runga i ngā endpoint e toru:

Te whakahaere Kupu matua
Ka pānuitia pēnei i te tukunga Ia kupu matua kāore i whakaingoatia i ngā rārangi i raro iho, hei tauira type, properties, required, items, enum, description, minimum, maximum, anyOf me allOf, i ia taumata o te schema.
format Ka pānuitia mō ngā uara int32, int64, float, double, byte, date, date-time me enum.
oneOf Ka pānuitia hei anyOf.
Ehara i te wāhanga o te schema e whiwhi ana ēnei model $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; exclusiveMinimum me exclusiveMaximum ina he tau rātou; ētahi atu uara o te format.

Te whanonga me ngā tepe

  • Kāore te API e tautuhi i te tino maha o ngā tool, i te tino maha rānei o ngā rauna.
  • Ka taea e te tinana tono te tae ki te 32 MiB, ā, me uru te kōrero me ōna hua ki te matapihi horopaki o te model.
  • Ina kawea e tētahi tono te tools, ka whakamahi te shannon-1.6-lite, shannon-1.6-pro, shannon-2-lite me te shannon-2-pro i te temperature taunoa o 0.2. Tukuna te temperature hei tautuhi i tētahi atu uara.
  • He tono motuhake ia rauna: ka tango i tō toenga, ā, ka tatau ki te Flood protection pērā i tētahi atu tono. Ngā tepe me te toenga

Ngā hapa

He whakautu noa te karanga tool me te tūnga 200. Ko ēnei hapa e pā ana ki te tono ake:

Tūnga Momo Karere Āhea
400 invalid_request_error unknown model: <id> Ehara te model i tētahi id o te rārangi kua whakaputaina.
422 invalid_request_error Failed to deserialize the JSON body into the target type: … Ehara te tools i te array, he hē rānei te momo JSON o tētahi atu āpure.
413 invalid_request_error Failed to buffer the request body: … Nui ake te tinana tono i te 32 MiB. Ka kawe te hapa i te code: "request_too_large".

Ka tuhi te model i ngā argument. Tirohia ki tō schema i mua i te whakahaere i te function, pērā i te tirotiro i tētahi input o waho.

He ōrite ngā hapa whakamotuhēhē, toenga me te tūmau ki ia tono. Whakahaere hapa