Metadata-Version: 2.4
Name: p2d-kimi
Version: 0.1.1
Summary: Unofficial Python client for Kimi AI — reverse-engineered, no API key required
Project-URL: Homepage, https://github.com/pooraddyy/p2d-kimi
Project-URL: Repository, https://github.com/pooraddyy/p2d-kimi
Project-URL: Issues, https://github.com/pooraddyy/p2d-kimi/issues
Author-email: addy <pooraddyy@gmail.com>
License: MIT
License-File: LICENSE
Keywords: ai,chat,kimi,llm,moonshot,unofficial
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Requires-Dist: requests>=2.32.0
Description-Content-Type: text/markdown

<div align="center">

<h1>p2d-kimi</h1>

<p>Unofficial Python client for <strong>Kimi AI</strong> (kimi.moonshot.cn). No API key needed — just your bearer token.</p>

<a href="https://pypi.org/project/p2d-kimi/"><img src="https://img.shields.io/pypi/v/p2d-kimi?color=6c5ce7&label=PyPI&logo=pypi&logoColor=white&style=for-the-badge" alt="PyPI Version"/></a>
<a href="https://pypi.org/project/p2d-kimi/"><img src="https://img.shields.io/pypi/pyversions/p2d-kimi?color=00b894&logo=python&logoColor=white&style=for-the-badge" alt="Python Versions"/></a>
<a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-fdcb6e?style=for-the-badge&logo=opensourceinitiative&logoColor=white" alt="License"/></a>
<a href="https://t.me/pythontodayz"><img src="https://img.shields.io/badge/Telegram-Join%20Channel-2CA5E0?style=for-the-badge&logo=telegram&logoColor=white" alt="Telegram"/></a>

</div>

---

## Features

- No API key required — uses your browser session token
- Persistent sessions — messages stay in the same conversation automatically
- Streaming support — yield reply chunks in real time
- Web search — toggle Kimi's built-in search on any message
- Multi-turn conversations — full context preserved across calls
- Lightweight — only depends on `requests`

---

## Install

```bash
pip install p2d-kimi
```

---

## Quick Start

```python
from p2dkimi import KimiClient

client = KimiClient(token="your-kimi-bearer-token")

reply = client.chat("What is the capital of France?")
print(reply)
```

---

## Multi-Turn Conversation

Messages are automatically grouped into the same session. The client creates one session on the first call and reuses it for all subsequent calls.

```python
client = KimiClient(token="your-token")

reply1 = client.chat("My name is Alex.")
reply2 = client.chat("What is my name?")
print(reply2)
```

---

## Starting a New Session

Call `new_chat()` to explicitly start a fresh conversation.

```python
client.new_chat(name="fresh-start")

reply = client.chat("Let's start over.")
```

---

## Streaming

```python
from p2dkimi import KimiClient

client = KimiClient(token="your-token")

for chunk in client.stream("Explain quantum computing in simple terms"):
    print(chunk, end="", flush=True)
```

---

## With Web Search

```python
reply = client.chat("Latest news on AI today", use_search=True)
```

---

## Manual Chat ID

You can still pass a `chat_id` directly to target a specific conversation.

```python
chat_id = client.new_chat(name="my-session")

reply1 = client.chat("My name is Alex.", chat_id=chat_id)
reply2 = client.chat("What is my name?", chat_id=chat_id)
print(reply2)
```

---

## API Reference

### `KimiClient(token, model="kimi", timeout=60)`

| Parameter | Type | Description |
|-----------|------|-------------|
| `token` | `str` | Bearer JWT from kimi.moonshot.cn |
| `model` | `str` | `"kimi"` (default), `"k1"`, `"ok-computer"`, etc. |
| `timeout` | `int` | HTTP timeout in seconds |

### Methods

| Method | Returns | Description |
|--------|---------|-------------|
| `chat(message, chat_id, history, use_search)` | `str` | Send a message, get full reply |
| `stream(message, chat_id, history, use_search)` | `Generator[str]` | Send a message, yield reply chunks |
| `new_chat(name)` | `str` | Create a new conversation and set it as active |

---

## Getting Your Token

1. Open [kimi.moonshot.cn](https://kimi.moonshot.cn) in your browser
2. Open **DevTools** → **Network** → any request
3. Copy the value of `Authorization: Bearer <token>`

Or from iOS/Android using a MITM proxy (e.g. mitmproxy, Charles).

The token is a JWT that lasts ~30 days.

---

## How It Works

```
KimiClient.chat("hello")
    → new_chat()          POST /api/chat            → chat_id (stored in self.chat_id)
    → stream_completion() POST /api/chat/{id}/completion/stream  → SSE chunks
    → collect chunks      event=cmpl text="Hello!"
    → return full string

KimiClient.chat("follow-up")
    → reuses self.chat_id (no new session created)
    → stream_completion() POST /api/chat/{id}/completion/stream  → SSE chunks
```

---

## REST API Server

The included Flask server exposes HTTP endpoints:

| Endpoint | Method | Description |
|----------|--------|-------------|
| `POST /chat/new` | POST | Create a new session → returns `chat_id` |
| `POST /chat` | POST | Send a message → returns `reply` + `chat_id` |
| `POST /chat/stream` | POST | Stream a reply as plain text |
| `GET /health` | GET | Health check |

Pass your token via `Authorization: Bearer <token>` header. Include `chat_id` in the body to continue an existing session.

---

## Demo

See [DEMO.md](DEMO.md) for a full working Telegram bot built with this library.

---

## Community

<div align="center">

<a href="https://t.me/pythontodayz">
  <img src="https://img.shields.io/badge/Telegram-%40pythontodayz-2CA5E0?style=for-the-badge&logo=telegram&logoColor=white" alt="Telegram Channel"/>
</a>

Join for Python tutorials, projects, and updates → [t.me/pythontodayz](https://t.me/pythontodayz)

</div>

---

## License

MIT — made by [addy](https://t.me/pythontodayz)
