Skip to main content

Documentation Index

Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt

Use this file to discover all available pages before exploring further.

Mastering the use of tools is a key hallmark of intelligence, and the Kimi large language model is no exception. Tool Use or Function Calling is a crucial feature of the Kimi large language model. When invoking the API to use the model service, you can describe tools or functions in the Messages, and the Kimi large language model will intelligently select and output a JSON object containing the parameters required to call one or more functions, thus enabling the Kimi large language model to link and utilize external tools. Here is a simple example of tool invocation:
{
  "model": "kimi-k2.6",
  "messages": [
    {
      "role": "user",
      "content": "Determine whether 3214567 is a prime number through programming."
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "CodeRunner",
        "description": "A code executor that supports running Python and JavaScript code",
        "parameters": {
          "properties": {
            "language": {
              "type": "string",
              "enum": ["python", "javascript"]
            },
            "code": {
              "type": "string",
              "description": "The code is written here"
            }
          },
          "type": "object"
        }
      }
    }
  ]
}
A diagram of the example above
In the tools field, we can add a list of optional tools. Each tool in the list must include a type. Within the function structure, we need to include a name (which must follow this regular expression: ^[a-zA-Z_][a-zA-Z0-9-_]{2,63}$). A name that is an easily understandable English word is more likely to be accepted by the model. There should also be a description or enum. The description part explains what the tool can do, which helps the model make judgments and selections. Each function also supports a strict parameter (boolean, optional) that controls whether the tool call arguments are strictly constrained according to the parameters JSON Schema:
  • true (default, omitting is equivalent to true): The system strictly constrains output according to the parameters schema, which must conform to the MFJS specification
  • false (must be explicitly passed): Only guarantees output is a valid JSON object, without enforcing internal structure
The function structure must have a parameters field. The root of parameters must be an object, and the content must be a subset of JSON Schema conforming to the MFJS specification. The number of functions in tools currently cannot exceed 128. If you encounter JSON Schema validation issues, please submit feedback at walle GitHub Issues. Like other APIs, we can call it through the Chat API.
from openai import OpenAI

client = OpenAI(
    api_key = "$MOONSHOT_API_KEY",
    base_url = "https://api.moonshot.ai/v1",
)

completion = client.chat.completions.create(
    model = "kimi-k2.6",
    messages = [
        {"role": "system", "content": "You are Kimi, an AI assistant provided by Moonshot AI. You are proficient in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You will reject any questions involving terrorism, racism, or explicit content. Moonshot AI is a proper noun and should not be translated."},
        {"role": "user", "content": "Determine whether 3214567 is a prime number through programming."}
    ],
    tools = [{
        "type": "function",
        "function": {
            "name": "CodeRunner",
            "description": "A code executor that supports running Python and JavaScript code",
            "parameters": {
                "properties": {
                    "language": {
                        "type": "string",
                        "enum": ["python", "javascript"]
                    },
                    "code": {
                        "type": "string",
                        "description": "The code is written here"
                    }
                },
            "type": "object"
            }
        }
    }]
)

print(completion.choices[0].message)

Tools Configuration

You can also use Agent platforms such as Coze, Bisheng, Dify, and LangChain to create and manage these tools, and design more complex workflows in conjunction with the Kimi large language model.