跳转至

MaaS_Kimi

请求协议

http

参数名 类型 必填 描述
Content-Type string 固定为 application/json
Authorization string Bearer {your_api_key}

请求URL

POST https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}

请求参数

参数 类型 必需 默认值 描述
model string - 模型标识符。
messages array

- 输入消息。每条消息包含 role 和 content
temperature number 0.6 采样温度,范围 0 到 2。较低的值使输出更确定
max_tokens integer 1024 生成的最大 token 数。模型最大值为 128000
top_p number 1.0 核采样阈值。temperature 的替代方案
frequency_penalty number 0 惩罚重复的 token。范围:-2.0 到 2.0
presence_penalty number 0 基于出现情况惩罚 token。范围:-2.0 到 2.0
stream boolean false 增量流式传输响应
n integer 1 生成的补全数量
stop string/array null 停止序列(最多 4 个)
user string null 用于跟踪最终用户的唯一标识符

流式

Curl 请求示例

    curl 'https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions' \
    --header 'Authorization: Bearer ${your AK}' \
    --header 'Content-Type: application/json' \
    --data '{
      "model": "moonshot-v1-8k",
      "stream":"True",
      "messages": [
        {
          "role": "system",
          "content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
        },
        {
          "role": "user",
          "content": "你好?"
        },
        {
          "partial": true,
          "role": "assistant",
          "content": "尊敬的用户您好,"
        }
      ],
      "temperature": 0.3
    }'

Python 请求示例

import requests
import json

url = "https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions"

headers = {
    "Authorization": "Bearer ${your AK}",
    "Content-Type": "application/json"
}

data = {
    "model": "moonshot-v1-8k",
    "stream": "True",
    "messages": [
        {
            "role": "system",
            "content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
        },
        {
            "role": "user",
            "content": "你好?"
        },
        {
            "partial": True,
            "role": "assistant",
            "content": "尊敬的用户您好,"
        }
    ],
    "temperature": 0.3
}

response = requests.post(url, headers=headers, json=data)

# 处理响应
if response.status_code == 200:
    print(response.json())
else:
    print(f"请求失败,状态码: {response.status_code}")
    print(response.text)

Tool call

Curl 请求示例

curl 'https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions' \
--header 'Authorization: Bearer ${your AK}' \
--header 'Content-Type: application/json' \
--data '{
    "model": "grok-4-0709",
    "messages": [{"role": "user", "content": "计算北京和上海的直线距离"}],
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_distance",
        "description": "计算两个地点之间的直线距离",
        "parameters": {
          "type": "object",
          "properties": {
            "location1": {"type": "string", "description": "起始城市"},
            "location2": {"type": "string", "description": "目标城市"}
          },
          "required": ["location1", "location2"]
        }
      }
    }],
    "tool_choice": "auto"
  }'

Python 请求

import requests
import json

url = "https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions"

headers = {
    "Authorization": "Bearer ${your AK}",
    "Content-Type": "application/json"
}

data = {
    "model": "grok-4-0709",
    "messages": [
        {
            "role": "user",
            "content": "计算北京和上海的直线距离"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_distance",
                "description": "计算两个地点之间的直线距离",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location1": {
                            "type": "string",
                            "description": "起始城市"
                        },
                        "location2": {
                            "type": "string",
                            "description": "目标城市"
                        }
                    },
                    "required": ["location1", "location2"]
                }
            }
        }
    ],
    "tool_choice": "auto"
}

# 发送请求
response = requests.post(url, headers=headers, json=data)

# 处理响应
if response.status_code == 200:
    result = response.json()
    print(json.dumps(result, indent=2, ensure_ascii=False))

    # 检查是否有工具调用
    message = result.get("choices", [{}])[0].get("message", {})
    if message.get("tool_calls"):
        print("\n模型请求调用工具:")
        for tool_call in message["tool_calls"]:
            print(f"  函数名: {tool_call['function']['name']}")
            print(f"  参数: {tool_call['function']['arguments']}")
else:
    print(f"请求失败,状态码: {response.status_code}")
    print(response.text)

响应示例

{
    "id": "chatcmpl-68770593a58dcb3e99966225",
    "choices": [
        {
            "index": 0,
            "logprobs": null,
            "message": {
                "role": "assistant",
                "content": "我来帮您计算北京和上海之间的直线距离。",
                "reasoning_content": null,
                "function_call": null,
                "tool_calls": [
                    {
                        "id": "get_distance:0",
                        "type": "function",
                        "function": {
                            "arguments": "{\"location1\": \"北京\", \"location2\": \"上海\"}",
                            "name": "get_distance"
                        }
                    }
                ],
                "reasoning_details": null
            },
            "finish_reason": "tool_calls",
            "native_finish_reason": null
        }
    ],
    "created": 1752630675,
    "model": "kimi-k2-0711-preview",
    "object": "chat.completion",
    "system_fingerprint": null,
    "usage": {
        "prompt_tokens": 83,
        "completion_tokens": 33,
        "total_tokens": 116,
        "completion_tokens_details": null,
        "prompt_tokens_details": null
    }
}

⚠️注意:携带第一次请求响应内容的中的id

  "tool_calls": [
                    {
                        "id": "get_distance:0",
                        "type": "function",
                        "function": {
                            "arguments": "{\"location1\": \"北京\", \"location2\": \"上海\"}",
                            "name": "get_distance"
                        }
                    }
                ],

到第二次请求中的tool_call_id

curl --location 'https://genaiapi.cloudsway.net/v1/ai/xxx/chat/completions' \
--header 'Authorization: Bearer ${your AK}' \
--header 'Content-Type: application/json' \
--data '{
    "max_tokens": 4096,
    "messages": [
        {
            "role": "user",
            "content": "计算北京和上海的直线距离"
        },
        {
            "role": "assistant",
            "content": null,
            "tool_calls": [
                {
                    "id": "functions.get_distance:0",
                    "type": "function",
                    "function": {
                        "name": "get_distance",
                        "arguments": "{\"location1\":\"北京\",\"location2\":\"上海\"}"
                    }
                }
            ]
        },
        {
            "role": "tool",
            "tool_call_id": "functions.get_distance:0",
            "name": "get_distance",
            "content": "1000"
        }
    ]
}'

响应示例

{
    "id": "90b265eb9fec4fdfb42275bf020deb47",
    "choices": [
        {
            "index": 0,
            "logprobs": null,
            "message": {
                "role": "assistant",
                "content": "根据计算,**北京到上海的直线距离约为 1,000 公里**(即大圆距离)。\n\n补充说明:\n- 这是两地之间的空中直线距离,不考虑地形和路线\n- 实际的交通距离会更长:\n  - 高铁线路距离:约 1,318 公里\n  - 驾车距离:约 1,200-1,300 公里\n  - 飞行航线距离:约 1,100 公里左右(受航路规划影响)\n\n北京位于北纬 39°54′,东经 116°23′;上海位于北纬 31°14′,东经 121°29′,两地经度相近,主要纬度差约为 8.7°,这对应了约 1,000 公里的直线距离。",
                "refusal": null,
                "annotations": null,
                "images": null,
                "reasoning_content": "用户询问北京和上海的直线距离。我需要使用get_distance函数来计算这两个城市之间的距离。我将\"北京\"作为location1,\"上海\"作为location2传入函数。\n\n函数返回了1000,这看起来是公里数。北京和上海的直线距离(大圆距离)大约在1000-1100公里之间,所以这个结果是合理的。\n\n我应该以清晰的方式向用户展示这个结果,并说明这是直线距离(大圆距离),不是实际的交通距离。 ",
                "function_call": null,
                "tool_calls": null,
                "reasoning_details": null
            },
            "finish_reason": "stop",
            "native_finish_reason": null
        }
    ],
    "logprobs": null,
    "created": 1776050977,
    "model": "MaaS_Kimi_K2.5_20260127",
    "object": "chat.completion",
    "system_fingerprint": null,
    "service_tier": null,
    "usage": {
        "prompt_tokens": 59,
        "completion_tokens": 258,
        "total_tokens": 317,
        "completion_tokens_details": null,
        "prompt_tokens_details": {
            "audio_tokens": 0,
            "cached_tokens": 0
        },
        "cache_creation_input_tokens": null,
        "cache_creation": null,
        "gemini_cache_tokens_details": null
    }
}

Json scheme (json model)

Curl 请求示例

curl 'https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions' \
--header 'Authorization: Bearer ${your AK}' \
--header 'Content-Type: application/json' \
--data '{
  "model": "moonshot-v1-8k",
  "messages": [
    {
      "role": "system",
      "content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
    },
    {
      "role": "system",
      "content": "你是月之暗面(Kimi)的智能客服,你负责回答用户提出的各种问题。请参考文档内容回复用户的问题,你的回答可以是文字、图片、链接,在一次回复中可以同时包含文字、图片、链接。\n\n请使用如下 JSON 格式输出你的回复:\n\n{\n    \"text\": \"文字信息\",\n    \"image\": \"图片地址\",\n    \"url\": \"链接地址\"\n}\n\n注意,请将文字信息放置在 `text` 字段中,将图片以 `oss://` 开头的链接形式放在 `image` 字段中,将普通链接放置在 `url` 字段中。"
    },
    {
      "role": "user",
      "content": "你好,我叫李雷,1+1等于多少?"
    }
  ],
  "temperature": 0.3,
  "response_format": {
    "type": "json_object"
  }
}'

Python 请求示例

import requests
import json

url = "https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions"

headers = {
    "Authorization": "Bearer ${your AK}",
    "Content-Type": "application/json"
}

data = {
    "model": "moonshot-v1-8k",
    "messages": [
        {
            "role": "system",
            "content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
        },
        {
            "role": "system",
            "content": "你是月之暗面(Kimi)的智能客服,你负责回答用户提出的各种问题。请参考文档内容回复用户的问题,你的回答可以是文字、图片、链接,在一次回复中可以同时包含文字、图片、链接。\n\n请使用如下 JSON 格式输出你的回复:\n\n{\n    \"text\": \"文字信息\",\n    \"image\": \"图片地址\",\n    \"url\": \"链接地址\"\n}\n\n注意,请将文字信息放置在 `text` 字段中,将图片以 `oss://` 开头的链接形式放在 `image` 字段中,将普通链接放置在 `url` 字段中。"
        },
        {
            "role": "user",
            "content": "你好,我叫李雷,1+1等于多少?"
        }
    ],
    "temperature": 0.3,
    "response_format": {
        "type": "json_object"
    }
}

# 发送请求
response = requests.post(url, headers=headers, json=data)

# 处理响应
if response.status_code == 200:
    result = response.json()
    print("完整响应:")
    print(json.dumps(result, indent=2, ensure_ascii=False))

    # 解析模型返回的 JSON 格式回复
    try:
        message_content = result.get("choices", [{}])[0].get("message", {}).get("content", "")
        if message_content:
            # 模型应该返回 JSON 格式的字符串
            parsed_response = json.loads(message_content)
            print("\n解析后的回复:")
            print(f"文本: {parsed_response.get('text', '')}")
            print(f"图片: {parsed_response.get('image', '')}")
            print(f"链接: {parsed_response.get('url', '')}")
    except json.JSONDecodeError:
        print(f"\n模型返回的内容不是有效的 JSON: {message_content}")
else:
    print(f"请求失败,状态码: {response.status_code}")
    print(response.text)

响应示例

{
    "id": "chatcmpl-687715097b6d3f9aad6f416a",
    "choices": [
        {
            "index": 0,
            "logprobs": null,
            "message": {
                "role": "assistant",
                "content": "{\n    \"text\": \"你好,李雷!1 + 1 = 2。\",\n    \"image\": \"\",\n    \"url\": \"\"\n}",
                "reasoning_content": null,
                "function_call": null,
                "tool_calls": null,
                "reasoning_details": null
            },
            "finish_reason": "stop",
            "native_finish_reason": null
        }
    ],
    "created": 1752634634,
    "model": "kimi-k2-0711-preview",
    "object": "chat.completion",
    "system_fingerprint": null,
    "usage": {
        "prompt_tokens": 212,
        "completion_tokens": 32,
        "total_tokens": 244,
        "completion_tokens_details": null,
        "prompt_tokens_details": null
    }
}

Partial Mode

Partial Mode是 Kimi API 提供的一项高级功能,它允许开发者预填(Prefill)模型回复的开头部分,让模型精确地从这个预置内容开始继续生成。

你可以把它理解为一种“给 AI 递话头”的技术,强制模型的回复以指定的内容开头

要点:

  1. 在 messages 列表尾部添加一条额外的 message,设置 role=assistantpartial=True

  2. 将需要作为开头的内容放置在 content 字段中,Kimi 大模型会强制以 content 的内容开头开始生成回复;

  3. 将步骤 2 中的 content 拼接到 Kimi 大模型生成的内容之前,组成完整的回复

Curl 请求示例

curl 'https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions' \
--header 'Authorization: Bearer ${your AK}' \
--header 'Content-Type: application/json' \
--data '{
  "model": "moonshot-v1-8k",
  "messages": [
    {
      "role": "system",
      "content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
    },
    {
      "role": "user",
      "content": "你好?"
    },
    {
      "partial": true,
      "role": "assistant",
      "content": "尊敬的用户您好,介绍一下kimi"
    }
  ],
  "temperature": 0.3
}'

Python 请求

import requests
import json

url = "https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions"

headers = {
    "Authorization": "Bearer ${your AK}",
    "Content-Type": "application/json"
}

data = {
    "model": "moonshot-v1-8k",
    "messages": [
        {
            "role": "system",
            "content": "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言。"
        },
        {
            "role": "user",
            "content": "你好?"
        },
        {
            "partial": True,
            "role": "assistant",
            "content": "尊敬的用户您好,介绍一下kimi"
        }
    ],
    "temperature": 0.3
}

# 发送请求
response = requests.post(url, headers=headers, json=data)

# 处理响应
if response.status_code == 200:
    result = response.json()
    print(json.dumps(result, indent=2, ensure_ascii=False))

    # 提取并打印助手的回复内容
    try:
        assistant_message = result.get("choices", [{}])[0].get("message", {}).get("content", "")
        if assistant_message:
            print("\n" + "="*50)
            print("Kimi 的回复:")
            print("="*50)
            print(assistant_message)
    except Exception as e:
        print(f"\n解析回复时出错: {e}")
else:
    print(f"请求失败,状态码: {response.status_code}")
    print(response.text)

响应示例

{
    "id": "4052eed298b04f6abba93680034825c2",
    "choices": [
        {
            "index": 0,
            "logprobs": null,
            "message": {
                "role": "assistant",
                "content": "你好!我是 **Kimi**,由 **Moonshot AI**(月之暗面)开发的人工智能助手。\n\n我擅长中文和英文对话,可以帮你解答问题、撰写文章、分析资料、编写代码、翻译文本,或者只是聊聊天。\n\n我会努力为你提供**安全、有帮助且准确**的回答,同时严格遵守内容规范,拒绝涉及恐怖主义、种族歧视、黄色暴力等不当内容的请求。\n\n有什么我可以帮你的吗?无论是学习、工作还是日常生活中的问题,都欢迎随时告诉我!",
                "refusal": null,
                "annotations": null,
                "images": null,
                "reasoning_content": "用户用中文问候“你好?”,这是一个简单的打招呼。作为Kimi,我应该用中文友好地回应,并询问用户有什么可以帮助他们的。\n\n用户说“介绍一下kimi”,但在这个上下文中,用户可能是在测试我,或者真的想了解关于Kimi(我自己)的信息。不过根据系统提示,我是Kimi,由Moonshot AI提供的人工智能助手。\n\n我需要:\n1. 友好地回应问候\n2. 简要介绍自己(Kimi)的身份和能力\n3. 询问用户有什么具体需求\n\n自我介绍应该包括:\n- 我是Kimi,由Moonshot AI开发\n- 擅长中英文对话\n- 提供安全、有帮助、准确的回答\n- 拒绝不当内容\n\n保持简洁、友好、专业。 ",
                "function_call": null,
                "tool_calls": null,
                "reasoning_details": null
            },
            "finish_reason": "stop",
            "native_finish_reason": null
        }
    ],
    "logprobs": null,
    "created": 1776052369,
    "model": "MaaS_Kimi_K2.5_20260127",
    "object": "chat.completion",
    "system_fingerprint": null,
    "service_tier": null,
    "usage": {
        "prompt_tokens": 87,
        "completion_tokens": 257,
        "total_tokens": 344,
        "completion_tokens_details": null,
        "prompt_tokens_details": {
            "audio_tokens": 0,
            "cached_tokens": 0
        },
        "cache_creation_input_tokens": null,
        "cache_creation": null,
        "gemini_cache_tokens_details": null
    }
}

Kimi 2.5 支持 thinking

默认开启状态

curl --location --request POST 'https://genaiapi.cloudsway.net/v1/ai/{endpoinPath}/chat/completions' \
--header 'Authorization: Bearer ${your AK}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "你是谁"
        }
    ],
    "thinking": {
        "type": "false",
        "budget_tokens": 2000
    }
}'