Skip to main content

A lightweight Python SDK for the Kimi API.

Project description

Kimi SDK

Kimi SDK provides a convenient way to access the Kimi API and build agent workflows in Python.

Installation

Kimi SDK requires Python 3.12 or higher. We recommend using uv as the package manager.

uv init --python 3.12  # or higher

Then add Kimi SDK as a dependency:

uv add kimi-sdk

Examples

Simple chat completion

import asyncio

from kimi_sdk import Kimi, Message, generate


async def main() -> None:
    kimi = Kimi(
        base_url="https://api.moonshot.ai/v1",
        api_key="your_kimi_api_key_here",
        model="kimi-k2-turbo-preview",
    )

    history = [
        Message(role="user", content="Who are you?"),
    ]

    result = await generate(
        chat_provider=kimi,
        system_prompt="You are a helpful assistant.",
        tools=[],
        history=history,
    )
    print(result.message)
    print(result.usage)


asyncio.run(main())

Streaming output

import asyncio

from kimi_sdk import Kimi, Message, StreamedMessagePart, generate


async def main() -> None:
    kimi = Kimi(
        base_url="https://api.moonshot.ai/v1",
        api_key="your_kimi_api_key_here",
        model="kimi-k2-turbo-preview",
    )

    history = [
        Message(role="user", content="Who are you?"),
    ]

    def output(message_part: StreamedMessagePart) -> None:
        print(message_part)

    result = await generate(
        chat_provider=kimi,
        system_prompt="You are a helpful assistant.",
        tools=[],
        history=history,
        on_message_part=output,
    )
    print(result.message)
    print(result.usage)


asyncio.run(main())

Upload video

import asyncio
from pathlib import Path
from kimi_sdk import Kimi, Message, TextPart, generate


async def main() -> None:
    kimi = Kimi(
        base_url="https://api.moonshot.ai/v1",
        api_key="your_kimi_api_key_here",
        model="kimi-k2-turbo-preview",
    )

    video_path = Path("demo.mp4")
    video_part = await kimi.files.upload_video(
        data=video_path.read_bytes(),
        mime_type="video/mp4",
    )

    history = [
        Message(
            role="user",
            content=[
                TextPart(text="Please describe this video."),
                video_part,
            ],
        ),
    ]

    result = await generate(
        chat_provider=kimi,
        system_prompt="You are a helpful assistant.",
        tools=[],
        history=history,
    )
    print(result.message)
    print(result.usage)


asyncio.run(main())

Tool calling with step

import asyncio

from pydantic import BaseModel

from kimi_sdk import CallableTool2, Kimi, Message, SimpleToolset, StepResult, ToolOk, ToolReturnValue, step


class AddToolParams(BaseModel):
    a: int
    b: int


class AddTool(CallableTool2[AddToolParams]):
    name: str = "add"
    description: str = "Add two integers."
    params: type[AddToolParams] = AddToolParams

    async def __call__(self, params: AddToolParams) -> ToolReturnValue:
        return ToolOk(output=str(params.a + params.b))


async def main() -> None:
    kimi = Kimi(
        base_url="https://api.moonshot.ai/v1",
        api_key="your_kimi_api_key_here",
        model="kimi-k2-turbo-preview",
    )

    toolset = SimpleToolset()
    toolset += AddTool()

    history = [
        Message(role="user", content="Please add 2 and 3 with the add tool."),
    ]

    result: StepResult = await step(
        chat_provider=kimi,
        system_prompt="You are a precise math tutor.",
        toolset=toolset,
        history=history,
    )
    print(result.message)
    print(await result.tool_results())


asyncio.run(main())

Environment variables

  • KIMI_API_KEY: API key for the Kimi API.
  • KIMI_BASE_URL: Override the API base URL (defaults to https://api.moonshot.ai/v1).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kimi_sdk-0.2.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kimi_sdk-0.2.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file kimi_sdk-0.2.0.tar.gz.

File metadata

  • Download URL: kimi_sdk-0.2.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kimi_sdk-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a3facf41183c52afec9146974d83a136c3a5e7b715ceadcb3cdf331a838892c3
MD5 b617ce7c53cba944aa5e4bc134ec60c6
BLAKE2b-256 1f8486e9a49992bfb127560015da02eb5c3aeda018d0701a6ff3a695a3990e2c

See more details on using hashes here.

File details

Details for the file kimi_sdk-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: kimi_sdk-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kimi_sdk-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f10fe34d6a489322d22fdae56346492030b2b7642033924d3324f97d5d5633bd
MD5 5f0e050d696333b32dcbe8b2c2101739
BLAKE2b-256 bbd16a1946cca7b06e40ba46b62140560f52b14eb0374777665b8fd2b3b87863

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page