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())
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 tohttps://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.1.0.tar.gz
(2.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kimi_sdk-0.1.0.tar.gz.
File metadata
- Download URL: kimi_sdk-0.1.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b66a309afb1e771c15cf48a548da520366f456002dc89cdd1a86d1a2778135bf
|
|
| MD5 |
292e3e19dc295c7cc0c51f6f7673fea6
|
|
| BLAKE2b-256 |
d640f78f827828df2190de87f1dc48480db41bd9f84a85575adff8d991eac7c7
|
File details
Details for the file kimi_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: kimi_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd4c9236f57dde15212ab7a5bc57b561ac7a1c11fb9c56ac327167c592f7e466
|
|
| MD5 |
aaeff40eb985275fcd727b65b7a5b0ae
|
|
| BLAKE2b-256 |
cdb9c9f2051aed930e0375bb1f4317add44f5a1df2b0817ca180d1932b59d965
|