node-mlx 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/cli.ts","../src/index.ts"],"sourcesContent":["/**\n * MLX CLI - Interactive chat with LLMs on Apple Silicon\n *\n * Usage:\n * mlx # Interactive mode with default model\n * mlx --model llama-3.2-1b # Use a specific model\n * mlx \"What is 2+2?\" # One-shot query\n * mlx --list # List available models\n */\n\nimport * as readline from \"node:readline\"\nimport {\n loadModel,\n RECOMMENDED_MODELS,\n isSupported,\n isPlatformSupported,\n VERSION,\n type Model,\n type GenerationOptions,\n type RecommendedModelKey\n} from \"./index.js\"\n\n// ANSI colors\nconst colors = {\n reset: \"\\x1b[0m\",\n bold: \"\\x1b[1m\",\n dim: \"\\x1b[2m\",\n cyan: \"\\x1b[36m\",\n green: \"\\x1b[32m\",\n yellow: \"\\x1b[33m\",\n magenta: \"\\x1b[35m\",\n red: \"\\x1b[31m\"\n}\n\nfunction log(msg: string) {\n console.log(msg)\n}\n\nfunction error(msg: string) {\n console.error(`${colors.red}Error:${colors.reset} ${msg}`)\n}\n\nfunction printHeader() {\n log(\"\")\n log(`${colors.bold}${colors.cyan}╔══════════════════════════════════════╗${colors.reset}`)\n log(\n `${colors.bold}${colors.cyan}║${colors.reset} ${colors.bold}MLX CLI${colors.reset} - LLMs on Apple Silicon ${colors.cyan}║${colors.reset}`\n )\n log(`${colors.bold}${colors.cyan}╚══════════════════════════════════════╝${colors.reset}`)\n log(\"\")\n}\n\nfunction printHelp() {\n log(`${colors.bold}Usage:${colors.reset}`)\n log(` mlx Interactive chat`)\n log(` mlx \"prompt\" One-shot generation`)\n log(` mlx --model <name> Use specific model`)\n log(` mlx --image <path> Include image (VLM only)`)\n log(` mlx --repetition-penalty <1-2> Penalize repeated tokens (default: off)`)\n log(` mlx --list List available models`)\n log(` mlx --help Show this help`)\n log(\"\")\n log(`${colors.bold}Vision models (VLM):${colors.reset}`)\n log(` mlx --model gemma-3-4b --image photo.jpg \"What's in this image?\"`)\n log(\"\")\n log(`${colors.bold}Repetition penalty (for models that repeat):${colors.reset}`)\n log(` mlx --model gemma-3n --repetition-penalty 1.2 \"Tell me about AI\"`)\n log(\"\")\n log(`${colors.bold}Interactive commands:${colors.reset}`)\n log(` /model <name> Switch model`)\n log(` /image <path> Set image for next prompt`)\n log(` /temp <0-2> Set temperature`)\n log(` /tokens <n> Set max tokens`)\n log(` /rep <1-2> Set repetition penalty`)\n log(` /clear Clear conversation`)\n log(` /help Show commands`)\n log(` /quit Exit`)\n log(\"\")\n}\n\nfunction printModels() {\n log(`${colors.bold}Available models:${colors.reset}`)\n log(\"\")\n\n // Group models by family, showing unique HuggingFace IDs with all their aliases\n const modelsByHfId = new Map<string, string[]>()\n\n for (const [alias, hfId] of Object.entries(RECOMMENDED_MODELS)) {\n if (!modelsByHfId.has(hfId)) {\n modelsByHfId.set(hfId, [])\n }\n\n modelsByHfId.get(hfId)?.push(alias)\n }\n\n // Organize by family\n const families = [\n {\n name: \"Phi (Microsoft)\",\n prefix: \"Phi\",\n desc: \"Reasoning & coding\"\n },\n {\n name: \"Gemma (Google)\",\n prefix: \"gemma\",\n desc: \"Efficient on-device\"\n },\n {\n name: \"Llama (Meta)\",\n prefix: \"Llama\",\n desc: \"General purpose\"\n },\n {\n name: \"Qwen (Alibaba)\",\n prefix: \"Qwen\",\n desc: \"Multilingual\"\n },\n {\n name: \"Mistral\",\n prefix: \"Mistral\",\n desc: \"Balanced performance\"\n },\n {\n name: \"Ministral\",\n prefix: \"Ministral\",\n desc: \"Fast inference\"\n }\n ]\n\n for (const family of families) {\n const familyModels = Array.from(modelsByHfId.entries()).filter(([hfId]) =>\n hfId.toLowerCase().includes(family.prefix.toLowerCase())\n )\n\n if (familyModels.length === 0) continue\n\n log(`${colors.bold}${family.name}${colors.reset} ${colors.dim}— ${family.desc}${colors.reset}`)\n\n for (const [hfId, aliases] of familyModels) {\n // Sort aliases: shortest first, then alphabetically\n const sortedAliases = aliases.sort((a, b) => a.length - b.length || a.localeCompare(b))\n const primary = sortedAliases[0]\n const others = sortedAliases.slice(1)\n\n const aliasStr =\n others.length > 0\n ? `${colors.green}${primary ?? \"\"}${colors.reset} ${colors.dim}(${others.join(\", \")})${colors.reset}`\n : `${colors.green}${primary ?? \"\"}${colors.reset}`\n\n log(` ${aliasStr.padEnd(45)} ${colors.dim}${hfId}${colors.reset}`)\n }\n\n log(\"\")\n }\n\n log(`${colors.dim}Or use any mlx-community model:${colors.reset}`)\n log(` ${colors.cyan}node-mlx --model mlx-community/YourModel-4bit${colors.reset}`)\n log(\"\")\n}\n\nfunction resolveModel(name: string): string {\n // Check if it's a shortcut\n if (name in RECOMMENDED_MODELS) {\n return RECOMMENDED_MODELS[name as RecommendedModelKey]\n }\n\n // Check if it's already a full model ID\n if (name.includes(\"/\")) {\n return name\n }\n\n // Assume it's from mlx-community\n return `mlx-community/${name}`\n}\n\ninterface ChatState {\n model: Model | null\n modelName: string\n options: GenerationOptions\n history: Array<{ role: \"user\" | \"assistant\"; content: string }>\n imagePath: string | null // For VLM image input\n}\n\nfunction runInteractive(initialModel: string): void {\n const state: ChatState = {\n model: null,\n modelName: initialModel,\n options: {\n maxTokens: 512,\n temperature: 0.7,\n topP: 0.9\n },\n history: [],\n imagePath: null\n }\n\n // Load initial model\n log(`${colors.dim}Loading ${state.modelName}...${colors.reset}`)\n const modelId = resolveModel(state.modelName)\n\n try {\n state.model = loadModel(modelId)\n log(`${colors.green}✓${colors.reset} Model loaded`)\n } catch (err) {\n error(`Failed to load model: ${err instanceof Error ? err.message : String(err)}`)\n process.exit(1)\n }\n\n log(\"\")\n log(`${colors.dim}Type your message or /help for commands${colors.reset}`)\n log(\"\")\n\n const rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout\n })\n\n const promptUser = (): void => {\n rl.question(`${colors.cyan}You:${colors.reset} `, (input) => {\n void handleUserInput(input, state, rl, promptUser)\n })\n }\n\n const handleUserInput = async (\n input: string,\n state: ChatState,\n rl: readline.Interface,\n next: () => void\n ): Promise<void> => {\n const trimmed = input.trim()\n\n if (!trimmed) {\n next()\n\n return\n }\n\n // Handle commands\n if (trimmed.startsWith(\"/\")) {\n await handleCommand(trimmed, state, rl)\n next()\n\n return\n }\n\n // Generate response\n if (!state.model) {\n error(\"No model loaded\")\n next()\n\n return\n }\n\n // Build prompt with history (simple format)\n const fullPrompt = buildPrompt(state.history, trimmed)\n\n state.history.push({ role: \"user\", content: trimmed })\n\n process.stdout.write(`${colors.magenta}AI:${colors.reset} `)\n\n try {\n let result\n\n // Check if we have an image to send\n if (state.imagePath && state.model.isVLM()) {\n result = state.model.generateWithImage(fullPrompt, state.imagePath, state.options)\n state.imagePath = null // Clear after use\n } else {\n // Use streaming - tokens are written directly to stdout\n result = state.model.generateStreaming(fullPrompt, state.options)\n }\n\n // Note: text already streamed, we only have stats\n log(\"\")\n log(\n `${colors.dim}(${String(result.tokenCount)} tokens, ${result.tokensPerSecond.toFixed(1)} tok/s)${colors.reset}`\n )\n log(\"\")\n\n // For history we'd need to capture the text, but streaming writes to stdout\n // For now, history won't track assistant responses in streaming mode\n state.history.push({ role: \"assistant\", content: \"[streamed response]\" })\n } catch (err) {\n log(\"\")\n error(err instanceof Error ? err.message : String(err))\n }\n\n next()\n }\n\n rl.on(\"close\", () => {\n log(\"\")\n log(`${colors.dim}Goodbye!${colors.reset}`)\n\n if (state.model) {\n state.model.unload()\n }\n\n process.exit(0)\n })\n\n promptUser()\n}\n\nfunction buildPrompt(\n history: Array<{ role: \"user\" | \"assistant\"; content: string }>,\n current: string\n): string {\n // Simple chat format\n let prompt = \"\"\n\n for (const msg of history.slice(-6)) {\n // Keep last 3 exchanges\n if (msg.role === \"user\") {\n prompt += `User: ${msg.content}\\n`\n } else {\n prompt += `Assistant: ${msg.content}\\n`\n }\n }\n\n prompt += `User: ${current}\\nAssistant:`\n\n return prompt\n}\n\nasync function handleCommand(input: string, state: ChatState, rl: readline.Interface) {\n const [cmd, ...args] = input.slice(1).split(\" \")\n const arg = args.join(\" \")\n\n switch (cmd) {\n case \"help\":\n case \"h\":\n printHelp()\n break\n\n case \"quit\":\n case \"q\":\n case \"exit\":\n rl.close()\n break\n\n case \"clear\":\n case \"c\":\n state.history = []\n log(`${colors.dim}Conversation cleared${colors.reset}`)\n break\n\n case \"model\":\n case \"m\":\n if (!arg) {\n log(`${colors.dim}Current model: ${state.modelName}${colors.reset}`)\n log(`${colors.dim}Use /model <name> to switch${colors.reset}`)\n } else {\n log(`${colors.dim}Loading ${arg}...${colors.reset}`)\n\n if (state.model) {\n state.model.unload()\n }\n\n try {\n state.model = loadModel(resolveModel(arg))\n state.modelName = arg\n state.history = []\n log(`${colors.green}✓${colors.reset} Switched to ${arg}`)\n } catch (err) {\n error(err instanceof Error ? err.message : String(err))\n }\n }\n\n break\n\n case \"temp\":\n case \"t\":\n if (!arg) {\n log(`${colors.dim}Temperature: ${String(state.options.temperature)}${colors.reset}`)\n } else {\n const temp = parseFloat(arg)\n\n if (isNaN(temp) || temp < 0 || temp > 2) {\n error(\"Temperature must be between 0 and 2\")\n } else {\n state.options.temperature = temp\n log(`${colors.dim}Temperature set to ${String(temp)}${colors.reset}`)\n }\n }\n\n break\n\n case \"tokens\":\n case \"n\":\n if (!arg) {\n log(`${colors.dim}Max tokens: ${String(state.options.maxTokens)}${colors.reset}`)\n } else {\n const tokens = parseInt(arg, 10)\n\n if (isNaN(tokens) || tokens < 1) {\n error(\"Tokens must be a positive number\")\n } else {\n state.options.maxTokens = tokens\n log(`${colors.dim}Max tokens set to ${String(tokens)}${colors.reset}`)\n }\n }\n\n break\n\n case \"rep\":\n case \"r\":\n if (!arg) {\n log(\n `${colors.dim}Repetition penalty: ${state.options.repetitionPenalty != null ? String(state.options.repetitionPenalty) : \"off\"}${colors.reset}`\n )\n } else {\n const penalty = parseFloat(arg)\n\n if (isNaN(penalty) || penalty < 1 || penalty > 2) {\n error(\"Repetition penalty must be between 1 and 2\")\n } else {\n state.options.repetitionPenalty = penalty\n log(`${colors.dim}Repetition penalty set to ${String(penalty)}${colors.reset}`)\n }\n }\n\n break\n\n case \"list\":\n case \"l\":\n printModels()\n break\n\n case \"image\":\n case \"i\":\n if (!arg) {\n if (state.imagePath) {\n log(`${colors.dim}Current image: ${state.imagePath}${colors.reset}`)\n } else {\n log(`${colors.dim}No image set. Use /image <path> to set one.${colors.reset}`)\n }\n } else {\n // Check if file exists\n const fs = await import(\"node:fs\")\n\n if (!fs.existsSync(arg)) {\n error(`Image not found: ${arg}`)\n } else if (!state.model?.isVLM()) {\n error(`Current model doesn't support images. Use a VLM like gemma-3-4b.`)\n } else {\n state.imagePath = arg\n log(`${colors.green}✓${colors.reset} Image set: ${arg}`)\n log(`${colors.dim}The next message will include this image.${colors.reset}`)\n }\n }\n\n break\n\n default:\n error(`Unknown command: /${cmd ?? \"\"}. Type /help for commands.`)\n }\n}\n\nfunction runOneShot(\n modelName: string,\n prompt: string,\n imagePath: string | null,\n options: GenerationOptions\n) {\n log(`${colors.dim}Loading ${modelName}...${colors.reset}`)\n\n const modelId = resolveModel(modelName)\n\n try {\n const model = loadModel(modelId)\n\n let result\n\n // Check if we have an image to process\n if (imagePath) {\n if (!model.isVLM()) {\n error(`Model ${modelName} doesn't support images. Use a VLM like gemma-3-4b.`)\n model.unload()\n process.exit(1)\n }\n\n result = model.generateWithImage(prompt, imagePath, options)\n } else {\n // Use streaming - tokens are written directly to stdout\n result = model.generateStreaming(prompt, options)\n }\n\n // Add newline after streamed output\n log(\"\")\n log(\n `${colors.dim}(${String(result.tokenCount)} tokens, ${result.tokensPerSecond.toFixed(1)} tok/s)${colors.reset}`\n )\n\n model.unload()\n } catch (err) {\n error(err instanceof Error ? err.message : String(err))\n process.exit(1)\n }\n}\n\n// Parse CLI arguments\nfunction parseArgs(): {\n model: string\n prompt: string | null\n imagePath: string | null\n options: GenerationOptions\n command: \"chat\" | \"oneshot\" | \"list\" | \"help\" | \"version\"\n} {\n const args = process.argv.slice(2)\n let model = \"qwen\" // Default to Qwen (no auth required)\n let prompt: string | null = null\n let imagePath: string | null = null\n const options: GenerationOptions = {\n maxTokens: 512,\n temperature: 0.7,\n topP: 0.9\n }\n let command: \"chat\" | \"oneshot\" | \"list\" | \"help\" | \"version\" = \"chat\"\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i]\n\n if (arg === \"--help\" || arg === \"-h\") {\n command = \"help\"\n } else if (arg === \"--version\" || arg === \"-v\") {\n command = \"version\"\n } else if (arg === \"--list\" || arg === \"-l\") {\n command = \"list\"\n } else if (arg === \"--model\" || arg === \"-m\") {\n model = args[++i] || model\n } else if (arg === \"--image\" || arg === \"-i\") {\n imagePath = args[++i] || null\n } else if (arg === \"--temp\" || arg === \"-t\") {\n options.temperature = parseFloat(args[++i] || \"0.7\")\n } else if (arg === \"--tokens\" || arg === \"-n\") {\n options.maxTokens = parseInt(args[++i] || \"512\", 10)\n } else if (arg === \"--repetition-penalty\" || arg === \"-r\") {\n options.repetitionPenalty = parseFloat(args[++i] || \"1.2\")\n } else if (arg && !arg.startsWith(\"-\")) {\n // First positional arg is model, second is prompt\n if (model === \"qwen\") {\n model = arg\n } else if (prompt === null) {\n prompt = arg\n command = \"oneshot\"\n }\n }\n }\n\n return { model, prompt, imagePath, options, command }\n}\n\n// Main\nfunction main(): void {\n const { model, prompt, imagePath, options, command } = parseArgs()\n\n // Commands that don't need Apple Silicon\n switch (command) {\n case \"help\":\n printHeader()\n printHelp()\n\n return\n\n case \"version\":\n log(`node-mlx v${VERSION}`)\n\n return\n\n case \"list\":\n printHeader()\n printModels()\n\n return\n }\n\n // Check platform for commands that need the runtime\n if (!isPlatformSupported()) {\n error(\"node-mlx requires macOS on Apple Silicon (M1/M2/M3/M4)\")\n process.exit(1)\n }\n\n if (!isSupported()) {\n error(\"Native libraries not found. Run 'pnpm build:swift && pnpm build:native' first.\")\n process.exit(1)\n }\n\n switch (command) {\n case \"oneshot\":\n if (prompt) {\n runOneShot(model, prompt, imagePath, options)\n }\n\n break\n\n case \"chat\":\n printHeader()\n runInteractive(model)\n break\n }\n}\n\ntry {\n main()\n} catch (err: unknown) {\n error(err instanceof Error ? err.message : String(err))\n process.exit(1)\n}\n","import { platform, arch } from \"node:os\"\nimport { join, dirname } from \"node:path\"\nimport { fileURLToPath } from \"node:url\"\nimport { existsSync, readFileSync } from \"node:fs\"\nimport { createRequire } from \"node:module\"\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = dirname(__filename)\nconst require = createRequire(import.meta.url)\n\n// Read version from package.json\nconst packageJsonPath = join(__dirname, \"..\", \"package.json\")\nconst packageJson = JSON.parse(readFileSync(packageJsonPath, \"utf-8\")) as { version: string }\n\n/** Package version */\nexport const VERSION = packageJson.version\n\n// Native binding interface\ninterface NativeBinding {\n initialize(dylibPath: string): boolean\n isInitialized(): boolean\n loadModel(modelId: string): number\n unloadModel(handle: number): void\n generate(\n handle: number,\n prompt: string,\n options?: {\n maxTokens?: number\n temperature?: number\n topP?: number\n repetitionPenalty?: number\n repetitionContextSize?: number\n }\n ): string // Returns JSON string\n generateStreaming(\n handle: number,\n prompt: string,\n options?: {\n maxTokens?: number\n temperature?: number\n topP?: number\n repetitionPenalty?: number\n repetitionContextSize?: number\n }\n ): string // Streams to stdout, returns JSON stats\n generateWithImage(\n handle: number,\n prompt: string,\n imagePath: string,\n options?: {\n maxTokens?: number\n temperature?: number\n topP?: number\n repetitionPenalty?: number\n repetitionContextSize?: number\n }\n ): string // VLM: Streams to stdout, returns JSON stats\n isVLM(handle: number): boolean\n isAvailable(): boolean\n getVersion(): string\n}\n\n// JSON response from Swift\ninterface JSONGenerationResult {\n success: boolean\n text?: string\n tokenCount?: number\n tokensPerSecond?: number\n error?: string\n}\n\n// Load the native addon\nlet binding: NativeBinding | null = null\nlet initialized = false\n\n/**\n * Load native addon using node-gyp-build (prebuilds) or fallback to built addon\n */\nfunction loadNativeAddon(): NativeBinding {\n // Try node-gyp-build first (prebuilds)\n try {\n const gypBuild = require(\"node-gyp-build\") as (dir: string) => NativeBinding\n const nativeDir = join(__dirname, \"..\", \"native\")\n\n if (existsSync(join(__dirname, \"..\", \"prebuilds\"))) {\n return gypBuild(join(__dirname, \"..\"))\n }\n\n // Fallback to native/build if no prebuilds\n if (existsSync(join(nativeDir, \"build\"))) {\n return gypBuild(nativeDir)\n }\n } catch {\n // node-gyp-build failed, try manual loading\n }\n\n // Manual fallback: try different paths for the native addon\n const possibleAddonPaths = [\n // From package dist/ (npm installed)\n join(__dirname, \"..\", \"prebuilds\", \"darwin-arm64\", \"node.napi.node\"),\n // From native/build (local development)\n join(__dirname, \"..\", \"native\", \"build\", \"Release\", \"node_mlx.node\"),\n // From project root (monorepo development)\n join(process.cwd(), \"packages\", \"node-mlx\", \"native\", \"build\", \"Release\", \"node_mlx.node\")\n ]\n\n for (const p of possibleAddonPaths) {\n if (existsSync(p)) {\n return require(p) as NativeBinding\n }\n }\n\n throw new Error(\n \"Native addon not found. Run 'pnpm build:native' first.\\n\" +\n `Searched paths:\\n${possibleAddonPaths.join(\"\\n\")}`\n )\n}\n\n/**\n * Find Swift library path\n * Note: The library is expected to be in a directory with mlx.metallib for MLX to find it\n */\nfunction findSwiftLibrary(): string {\n const possibleDylibPaths = [\n // From package swift/ (preferred - has metallib co-located)\n join(__dirname, \"..\", \"swift\", \"libNodeMLX.dylib\"),\n // From project root packages/node-mlx/swift/ (monorepo development)\n join(process.cwd(), \"packages\", \"node-mlx\", \"swift\", \"libNodeMLX.dylib\"),\n // Fallback to packages/swift/.build (monorepo dev)\n join(__dirname, \"..\", \"..\", \"swift\", \".build\", \"release\", \"libNodeMLX.dylib\"),\n join(__dirname, \"..\", \"..\", \"..\", \"swift\", \".build\", \"release\", \"libNodeMLX.dylib\"),\n join(process.cwd(), \"packages\", \"swift\", \".build\", \"release\", \"libNodeMLX.dylib\")\n ]\n\n for (const p of possibleDylibPaths) {\n if (existsSync(p)) {\n return p\n }\n }\n\n throw new Error(\n \"Swift library not found. Run 'pnpm build:swift' first.\\n\" +\n `Searched paths:\\n${possibleDylibPaths.join(\"\\n\")}`\n )\n}\n\nfunction loadBinding(): NativeBinding {\n if (binding && initialized) {\n return binding\n }\n\n if (platform() !== \"darwin\" || arch() !== \"arm64\") {\n throw new Error(\"node-mlx is only supported on macOS Apple Silicon (arm64)\")\n }\n\n binding = loadNativeAddon()\n const dylibPath = findSwiftLibrary()\n const success = binding.initialize(dylibPath)\n\n if (!success) {\n throw new Error(\"Failed to initialize node-mlx native library\")\n }\n\n initialized = true\n\n return binding\n}\n\n// MARK: - Public Types\n\nexport interface GenerationOptions {\n maxTokens?: number\n temperature?: number\n topP?: number\n /** Penalty for repeating tokens (1.0 = no penalty, 1.1-1.2 recommended) */\n repetitionPenalty?: number\n /** Number of recent tokens to consider for penalty (default: 20) */\n repetitionContextSize?: number\n}\n\nexport interface GenerationResult {\n text: string\n tokenCount: number\n tokensPerSecond: number\n}\n\nexport interface StreamingResult {\n tokenCount: number\n tokensPerSecond: number\n}\n\nexport interface Model {\n /** Generate text from a prompt */\n generate(prompt: string, options?: GenerationOptions): GenerationResult\n\n /** Generate text with streaming - tokens are written directly to stdout */\n generateStreaming(prompt: string, options?: GenerationOptions): StreamingResult\n\n /** Generate text from a prompt with an image (VLM only) */\n generateWithImage(prompt: string, imagePath: string, options?: GenerationOptions): StreamingResult\n\n /** Check if this model supports images (is a Vision-Language Model) */\n isVLM(): boolean\n\n /** Unload the model from memory */\n unload(): void\n\n /** Model handle (internal use) */\n readonly handle: number\n}\n\n// MARK: - Recommended Models\n\nexport const RECOMMENDED_MODELS = {\n // Qwen 2.5 (Alibaba) - Working with proper RoPE support\n // Using non-quantized models - quantized models have loading issues\n qwen: \"Qwen/Qwen2.5-1.5B-Instruct\",\n \"qwen-2.5\": \"Qwen/Qwen2.5-1.5B-Instruct\",\n \"qwen-2.5-0.5b\": \"Qwen/Qwen2.5-0.5B-Instruct\",\n \"qwen-2.5-1.5b\": \"Qwen/Qwen2.5-1.5B-Instruct\",\n \"qwen-2.5-3b\": \"Qwen/Qwen2.5-3B-Instruct\",\n\n // Phi (Microsoft) - Working with fused QKV and RoPE\n phi: \"microsoft/phi-4\", // Default to latest\n phi4: \"microsoft/phi-4\",\n \"phi-4\": \"microsoft/phi-4\",\n phi3: \"microsoft/Phi-3-mini-4k-instruct\",\n \"phi-3\": \"microsoft/Phi-3-mini-4k-instruct\",\n \"phi-3-mini\": \"microsoft/Phi-3-mini-4k-instruct\",\n\n // Llama 3.2 (Meta) - Requires HuggingFace authentication\n // Note: meta-llama models require accepting license at huggingface.co\n llama: \"meta-llama/Llama-3.2-1B-Instruct\",\n \"llama-3.2\": \"meta-llama/Llama-3.2-1B-Instruct\",\n \"llama-3.2-1b\": \"meta-llama/Llama-3.2-1B-Instruct\",\n \"llama-3.2-3b\": \"meta-llama/Llama-3.2-3B-Instruct\",\n\n // Gemma 3 (Google) - Standard transformer architecture with sliding window\n gemma: \"mlx-community/gemma-3-1b-it-4bit\",\n \"gemma-3\": \"mlx-community/gemma-3-1b-it-4bit\",\n \"gemma-3-1b\": \"mlx-community/gemma-3-1b-it-4bit\",\n \"gemma-3-1b-bf16\": \"mlx-community/gemma-3-1b-it-bf16\",\n \"gemma-3-4b\": \"mlx-community/gemma-3-4b-it-4bit\",\n \"gemma-3-4b-bf16\": \"mlx-community/gemma-3-4b-it-bf16\",\n \"gemma-3-12b\": \"mlx-community/gemma-3-12b-it-4bit\",\n \"gemma-3-27b\": \"mlx-community/gemma-3-27b-it-4bit\"\n} as const\n\nexport type RecommendedModelKey = keyof typeof RECOMMENDED_MODELS\n\n// MARK: - Public API\n\n/**\n * Check if the platform is Apple Silicon Mac\n */\nexport function isPlatformSupported(): boolean {\n return platform() === \"darwin\" && arch() === \"arm64\"\n}\n\n/**\n * Check if MLX is available on this system\n * (requires macOS 14+ on Apple Silicon with built binaries)\n */\nexport function isSupported(): boolean {\n if (!isPlatformSupported()) {\n return false\n }\n\n try {\n const b = loadBinding()\n\n return b.isAvailable()\n } catch {\n return false\n }\n}\n\n/**\n * Get the library version\n */\nexport function getVersion(): string {\n const b = loadBinding()\n\n return b.getVersion()\n}\n\n/**\n * Load a model from HuggingFace or local path\n *\n * @param modelId - HuggingFace model ID (e.g., \"mlx-community/gemma-3n-E2B-it-4bit\") or local path\n * @returns Model instance\n *\n * @example\n * ```typescript\n * import { loadModel, RECOMMENDED_MODELS } from \"node-mlx\"\n *\n * const model = loadModel(RECOMMENDED_MODELS[\"gemma-3n-2b\"])\n * const result = model.generate(\"Hello, world!\")\n * console.log(result.text)\n * model.unload()\n * ```\n */\nexport function loadModel(modelId: string): Model {\n const b = loadBinding()\n const handle = b.loadModel(modelId)\n\n return {\n handle,\n\n generate(prompt: string, options?: GenerationOptions): GenerationResult {\n const jsonStr = b.generate(handle, prompt, {\n maxTokens: options?.maxTokens ?? 256,\n temperature: options?.temperature ?? 0.7,\n topP: options?.topP ?? 0.9,\n repetitionPenalty: options?.repetitionPenalty ?? 0,\n repetitionContextSize: options?.repetitionContextSize ?? 20\n })\n\n const result = JSON.parse(jsonStr) as JSONGenerationResult\n\n if (!result.success) {\n throw new Error(result.error ?? \"Generation failed\")\n }\n\n return {\n text: result.text ?? \"\",\n tokenCount: result.tokenCount ?? 0,\n tokensPerSecond: result.tokensPerSecond ?? 0\n }\n },\n\n generateStreaming(prompt: string, options?: GenerationOptions): StreamingResult {\n // Tokens are written directly to stdout by Swift\n const jsonStr = b.generateStreaming(handle, prompt, {\n maxTokens: options?.maxTokens ?? 256,\n temperature: options?.temperature ?? 0.7,\n topP: options?.topP ?? 0.9,\n repetitionPenalty: options?.repetitionPenalty ?? 0,\n repetitionContextSize: options?.repetitionContextSize ?? 20\n })\n\n const result = JSON.parse(jsonStr) as JSONGenerationResult\n\n if (!result.success) {\n throw new Error(result.error ?? \"Generation failed\")\n }\n\n return {\n tokenCount: result.tokenCount ?? 0,\n tokensPerSecond: result.tokensPerSecond ?? 0\n }\n },\n\n generateWithImage(\n prompt: string,\n imagePath: string,\n options?: GenerationOptions\n ): StreamingResult {\n // VLM generation with image - tokens are written directly to stdout by Swift\n const jsonStr = b.generateWithImage(handle, prompt, imagePath, {\n maxTokens: options?.maxTokens ?? 256,\n temperature: options?.temperature ?? 0.7,\n topP: options?.topP ?? 0.9,\n repetitionPenalty: options?.repetitionPenalty ?? 0,\n repetitionContextSize: options?.repetitionContextSize ?? 20\n })\n\n const result = JSON.parse(jsonStr) as JSONGenerationResult\n\n if (!result.success) {\n throw new Error(result.error ?? \"Generation failed\")\n }\n\n return {\n tokenCount: result.tokenCount ?? 0,\n tokensPerSecond: result.tokensPerSecond ?? 0\n }\n },\n\n isVLM(): boolean {\n return b.isVLM(handle)\n },\n\n unload(): void {\n b.unloadModel(handle)\n }\n }\n}\n\n/**\n * Generate text using a model (one-shot, loads and unloads model)\n *\n * @param modelId - HuggingFace model ID or local path\n * @param prompt - Input text\n * @param options - Generation options\n * @returns Generation result\n *\n * @example\n * ```typescript\n * import { generate } from \"node-mlx\"\n *\n * const result = generate(\n * \"mlx-community/gemma-3n-E2B-it-4bit\",\n * \"Explain quantum computing\",\n * { maxTokens: 100 }\n * )\n * console.log(result.text)\n * ```\n */\nexport function generate(\n modelId: string,\n prompt: string,\n options?: GenerationOptions\n): GenerationResult {\n const model = loadModel(modelId)\n\n try {\n return model.generate(prompt, options)\n } finally {\n model.unload()\n }\n}\n"],"mappings":";;;AAUA,YAAY,cAAc;;;ACV1B,SAAS,UAAU,YAAY;AAC/B,SAAS,MAAM,eAAe;AAC9B,SAAS,qBAAqB;AAC9B,SAAS,YAAY,oBAAoB;AACzC,SAAS,qBAAqB;AAE9B,IAAM,aAAa,cAAc,YAAY,GAAG;AAChD,IAAM,YAAY,QAAQ,UAAU;AACpC,IAAMA,WAAU,cAAc,YAAY,GAAG;AAG7C,IAAM,kBAAkB,KAAK,WAAW,MAAM,cAAc;AAC5D,IAAM,cAAc,KAAK,MAAM,aAAa,iBAAiB,OAAO,CAAC;AAG9D,IAAM,UAAU,YAAY;AAyDn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getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ RECOMMENDED_MODELS: () => RECOMMENDED_MODELS,
24
+ VERSION: () => VERSION,
25
+ generate: () => generate,
26
+ getVersion: () => getVersion,
27
+ isPlatformSupported: () => isPlatformSupported,
28
+ isSupported: () => isSupported,
29
+ loadModel: () => loadModel
30
+ });
31
+ module.exports = __toCommonJS(src_exports);
32
+
33
+ // ../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.6_tsx@4.21.0_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js
34
+ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
35
+ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
36
+
37
+ // src/index.ts
38
+ var import_node_os = require("os");
39
+ var import_node_path = require("path");
40
+ var import_node_url = require("url");
41
+ var import_node_fs = require("fs");
42
+ var import_node_module = require("module");
43
+ var __filename2 = (0, import_node_url.fileURLToPath)(importMetaUrl);
44
+ var __dirname = (0, import_node_path.dirname)(__filename2);
45
+ var require2 = (0, import_node_module.createRequire)(importMetaUrl);
46
+ var packageJsonPath = (0, import_node_path.join)(__dirname, "..", "package.json");
47
+ var packageJson = JSON.parse((0, import_node_fs.readFileSync)(packageJsonPath, "utf-8"));
48
+ var VERSION = packageJson.version;
49
+ var binding = null;
50
+ var initialized = false;
51
+ function loadNativeAddon() {
52
+ try {
53
+ const gypBuild = require2("node-gyp-build");
54
+ const nativeDir = (0, import_node_path.join)(__dirname, "..", "native");
55
+ if ((0, import_node_fs.existsSync)((0, import_node_path.join)(__dirname, "..", "prebuilds"))) {
56
+ return gypBuild((0, import_node_path.join)(__dirname, ".."));
57
+ }
58
+ if ((0, import_node_fs.existsSync)((0, import_node_path.join)(nativeDir, "build"))) {
59
+ return gypBuild(nativeDir);
60
+ }
61
+ } catch {
62
+ }
63
+ const possibleAddonPaths = [
64
+ // From package dist/ (npm installed)
65
+ (0, import_node_path.join)(__dirname, "..", "prebuilds", "darwin-arm64", "node.napi.node"),
66
+ // From native/build (local development)
67
+ (0, import_node_path.join)(__dirname, "..", "native", "build", "Release", "node_mlx.node"),
68
+ // From project root (monorepo development)
69
+ (0, import_node_path.join)(process.cwd(), "packages", "node-mlx", "native", "build", "Release", "node_mlx.node")
70
+ ];
71
+ for (const p of possibleAddonPaths) {
72
+ if ((0, import_node_fs.existsSync)(p)) {
73
+ return require2(p);
74
+ }
75
+ }
76
+ throw new Error(
77
+ `Native addon not found. Run 'pnpm build:native' first.
78
+ Searched paths:
79
+ ${possibleAddonPaths.join("\n")}`
80
+ );
81
+ }
82
+ function findSwiftLibrary() {
83
+ const possibleDylibPaths = [
84
+ // From package swift/ (preferred - has metallib co-located)
85
+ (0, import_node_path.join)(__dirname, "..", "swift", "libNodeMLX.dylib"),
86
+ // From project root packages/node-mlx/swift/ (monorepo development)
87
+ (0, import_node_path.join)(process.cwd(), "packages", "node-mlx", "swift", "libNodeMLX.dylib"),
88
+ // Fallback to packages/swift/.build (monorepo dev)
89
+ (0, import_node_path.join)(__dirname, "..", "..", "swift", ".build", "release", "libNodeMLX.dylib"),
90
+ (0, import_node_path.join)(__dirname, "..", "..", "..", "swift", ".build", "release", "libNodeMLX.dylib"),
91
+ (0, import_node_path.join)(process.cwd(), "packages", "swift", ".build", "release", "libNodeMLX.dylib")
92
+ ];
93
+ for (const p of possibleDylibPaths) {
94
+ if ((0, import_node_fs.existsSync)(p)) {
95
+ return p;
96
+ }
97
+ }
98
+ throw new Error(
99
+ `Swift library not found. Run 'pnpm build:swift' first.
100
+ Searched paths:
101
+ ${possibleDylibPaths.join("\n")}`
102
+ );
103
+ }
104
+ function loadBinding() {
105
+ if (binding && initialized) {
106
+ return binding;
107
+ }
108
+ if ((0, import_node_os.platform)() !== "darwin" || (0, import_node_os.arch)() !== "arm64") {
109
+ throw new Error("node-mlx is only supported on macOS Apple Silicon (arm64)");
110
+ }
111
+ binding = loadNativeAddon();
112
+ const dylibPath = findSwiftLibrary();
113
+ const success = binding.initialize(dylibPath);
114
+ if (!success) {
115
+ throw new Error("Failed to initialize node-mlx native library");
116
+ }
117
+ initialized = true;
118
+ return binding;
119
+ }
120
+ var RECOMMENDED_MODELS = {
121
+ // Qwen 2.5 (Alibaba) - Working with proper RoPE support
122
+ // Using non-quantized models - quantized models have loading issues
123
+ qwen: "Qwen/Qwen2.5-1.5B-Instruct",
124
+ "qwen-2.5": "Qwen/Qwen2.5-1.5B-Instruct",
125
+ "qwen-2.5-0.5b": "Qwen/Qwen2.5-0.5B-Instruct",
126
+ "qwen-2.5-1.5b": "Qwen/Qwen2.5-1.5B-Instruct",
127
+ "qwen-2.5-3b": "Qwen/Qwen2.5-3B-Instruct",
128
+ // Phi (Microsoft) - Working with fused QKV and RoPE
129
+ phi: "microsoft/phi-4",
130
+ // Default to latest
131
+ phi4: "microsoft/phi-4",
132
+ "phi-4": "microsoft/phi-4",
133
+ phi3: "microsoft/Phi-3-mini-4k-instruct",
134
+ "phi-3": "microsoft/Phi-3-mini-4k-instruct",
135
+ "phi-3-mini": "microsoft/Phi-3-mini-4k-instruct",
136
+ // Llama 3.2 (Meta) - Requires HuggingFace authentication
137
+ // Note: meta-llama models require accepting license at huggingface.co
138
+ llama: "meta-llama/Llama-3.2-1B-Instruct",
139
+ "llama-3.2": "meta-llama/Llama-3.2-1B-Instruct",
140
+ "llama-3.2-1b": "meta-llama/Llama-3.2-1B-Instruct",
141
+ "llama-3.2-3b": "meta-llama/Llama-3.2-3B-Instruct",
142
+ // Gemma 3 (Google) - Standard transformer architecture with sliding window
143
+ gemma: "mlx-community/gemma-3-1b-it-4bit",
144
+ "gemma-3": "mlx-community/gemma-3-1b-it-4bit",
145
+ "gemma-3-1b": "mlx-community/gemma-3-1b-it-4bit",
146
+ "gemma-3-1b-bf16": "mlx-community/gemma-3-1b-it-bf16",
147
+ "gemma-3-4b": "mlx-community/gemma-3-4b-it-4bit",
148
+ "gemma-3-4b-bf16": "mlx-community/gemma-3-4b-it-bf16",
149
+ "gemma-3-12b": "mlx-community/gemma-3-12b-it-4bit",
150
+ "gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit"
151
+ };
152
+ function isPlatformSupported() {
153
+ return (0, import_node_os.platform)() === "darwin" && (0, import_node_os.arch)() === "arm64";
154
+ }
155
+ function isSupported() {
156
+ if (!isPlatformSupported()) {
157
+ return false;
158
+ }
159
+ try {
160
+ const b = loadBinding();
161
+ return b.isAvailable();
162
+ } catch {
163
+ return false;
164
+ }
165
+ }
166
+ function getVersion() {
167
+ const b = loadBinding();
168
+ return b.getVersion();
169
+ }
170
+ function loadModel(modelId) {
171
+ const b = loadBinding();
172
+ const handle = b.loadModel(modelId);
173
+ return {
174
+ handle,
175
+ generate(prompt, options) {
176
+ const jsonStr = b.generate(handle, prompt, {
177
+ maxTokens: options?.maxTokens ?? 256,
178
+ temperature: options?.temperature ?? 0.7,
179
+ topP: options?.topP ?? 0.9,
180
+ repetitionPenalty: options?.repetitionPenalty ?? 0,
181
+ repetitionContextSize: options?.repetitionContextSize ?? 20
182
+ });
183
+ const result = JSON.parse(jsonStr);
184
+ if (!result.success) {
185
+ throw new Error(result.error ?? "Generation failed");
186
+ }
187
+ return {
188
+ text: result.text ?? "",
189
+ tokenCount: result.tokenCount ?? 0,
190
+ tokensPerSecond: result.tokensPerSecond ?? 0
191
+ };
192
+ },
193
+ generateStreaming(prompt, options) {
194
+ const jsonStr = b.generateStreaming(handle, prompt, {
195
+ maxTokens: options?.maxTokens ?? 256,
196
+ temperature: options?.temperature ?? 0.7,
197
+ topP: options?.topP ?? 0.9,
198
+ repetitionPenalty: options?.repetitionPenalty ?? 0,
199
+ repetitionContextSize: options?.repetitionContextSize ?? 20
200
+ });
201
+ const result = JSON.parse(jsonStr);
202
+ if (!result.success) {
203
+ throw new Error(result.error ?? "Generation failed");
204
+ }
205
+ return {
206
+ tokenCount: result.tokenCount ?? 0,
207
+ tokensPerSecond: result.tokensPerSecond ?? 0
208
+ };
209
+ },
210
+ generateWithImage(prompt, imagePath, options) {
211
+ const jsonStr = b.generateWithImage(handle, prompt, imagePath, {
212
+ maxTokens: options?.maxTokens ?? 256,
213
+ temperature: options?.temperature ?? 0.7,
214
+ topP: options?.topP ?? 0.9,
215
+ repetitionPenalty: options?.repetitionPenalty ?? 0,
216
+ repetitionContextSize: options?.repetitionContextSize ?? 20
217
+ });
218
+ const result = JSON.parse(jsonStr);
219
+ if (!result.success) {
220
+ throw new Error(result.error ?? "Generation failed");
221
+ }
222
+ return {
223
+ tokenCount: result.tokenCount ?? 0,
224
+ tokensPerSecond: result.tokensPerSecond ?? 0
225
+ };
226
+ },
227
+ isVLM() {
228
+ return b.isVLM(handle);
229
+ },
230
+ unload() {
231
+ b.unloadModel(handle);
232
+ }
233
+ };
234
+ }
235
+ function generate(modelId, prompt, options) {
236
+ const model = loadModel(modelId);
237
+ try {
238
+ return model.generate(prompt, options);
239
+ } finally {
240
+ model.unload();
241
+ }
242
+ }
243
+ // Annotate the CommonJS export names for ESM import in node:
244
+ 0 && (module.exports = {
245
+ RECOMMENDED_MODELS,
246
+ VERSION,
247
+ generate,
248
+ getVersion,
249
+ isPlatformSupported,
250
+ isSupported,
251
+ loadModel
252
+ });
253
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.6_tsx@4.21.0_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js"],"sourcesContent":["import { platform, arch } from \"node:os\"\nimport { join, dirname } from \"node:path\"\nimport { fileURLToPath } from \"node:url\"\nimport { existsSync, readFileSync } from \"node:fs\"\nimport { createRequire } from \"node:module\"\n\nconst __filename = fileURLToPath(import.meta.url)\nconst __dirname = dirname(__filename)\nconst require = createRequire(import.meta.url)\n\n// Read version from package.json\nconst packageJsonPath = join(__dirname, \"..\", \"package.json\")\nconst packageJson = JSON.parse(readFileSync(packageJsonPath, \"utf-8\")) as { version: string }\n\n/** Package version */\nexport const VERSION = packageJson.version\n\n// Native binding interface\ninterface NativeBinding {\n initialize(dylibPath: string): boolean\n isInitialized(): boolean\n loadModel(modelId: string): number\n unloadModel(handle: number): void\n generate(\n handle: number,\n prompt: string,\n options?: {\n maxTokens?: number\n temperature?: number\n topP?: number\n repetitionPenalty?: number\n repetitionContextSize?: number\n }\n ): string // Returns JSON string\n generateStreaming(\n handle: number,\n prompt: string,\n options?: {\n maxTokens?: number\n temperature?: number\n topP?: number\n repetitionPenalty?: number\n repetitionContextSize?: number\n }\n ): string // Streams to stdout, returns JSON stats\n generateWithImage(\n handle: number,\n prompt: string,\n imagePath: string,\n options?: {\n maxTokens?: number\n temperature?: number\n topP?: number\n repetitionPenalty?: number\n repetitionContextSize?: number\n }\n ): string // VLM: Streams to stdout, returns JSON stats\n isVLM(handle: number): boolean\n isAvailable(): boolean\n getVersion(): string\n}\n\n// JSON response from Swift\ninterface JSONGenerationResult {\n success: boolean\n text?: string\n tokenCount?: number\n tokensPerSecond?: number\n error?: string\n}\n\n// Load the native addon\nlet binding: NativeBinding | null = null\nlet initialized = false\n\n/**\n * Load native addon using node-gyp-build (prebuilds) or fallback to built addon\n */\nfunction loadNativeAddon(): NativeBinding {\n // Try node-gyp-build first (prebuilds)\n try {\n const gypBuild = require(\"node-gyp-build\") as (dir: string) => NativeBinding\n const nativeDir = join(__dirname, \"..\", \"native\")\n\n if (existsSync(join(__dirname, \"..\", \"prebuilds\"))) {\n return gypBuild(join(__dirname, \"..\"))\n }\n\n // Fallback to native/build if no prebuilds\n if (existsSync(join(nativeDir, \"build\"))) {\n return gypBuild(nativeDir)\n }\n } catch {\n // node-gyp-build failed, try manual loading\n }\n\n // Manual fallback: try different paths for the native addon\n const possibleAddonPaths = [\n // From package dist/ (npm installed)\n join(__dirname, \"..\", \"prebuilds\", \"darwin-arm64\", \"node.napi.node\"),\n // From native/build (local development)\n join(__dirname, \"..\", \"native\", \"build\", \"Release\", \"node_mlx.node\"),\n // From project root (monorepo development)\n join(process.cwd(), \"packages\", \"node-mlx\", \"native\", \"build\", \"Release\", \"node_mlx.node\")\n ]\n\n for (const p of possibleAddonPaths) {\n if (existsSync(p)) {\n return require(p) as NativeBinding\n }\n }\n\n throw new Error(\n \"Native addon not found. Run 'pnpm build:native' first.\\n\" +\n `Searched paths:\\n${possibleAddonPaths.join(\"\\n\")}`\n )\n}\n\n/**\n * Find Swift library path\n * Note: The library is expected to be in a directory with mlx.metallib for MLX to find it\n */\nfunction findSwiftLibrary(): string {\n const possibleDylibPaths = [\n // From package swift/ (preferred - has metallib co-located)\n join(__dirname, \"..\", \"swift\", \"libNodeMLX.dylib\"),\n // From project root packages/node-mlx/swift/ (monorepo development)\n join(process.cwd(), \"packages\", \"node-mlx\", \"swift\", \"libNodeMLX.dylib\"),\n // Fallback to packages/swift/.build (monorepo dev)\n join(__dirname, \"..\", \"..\", \"swift\", \".build\", \"release\", \"libNodeMLX.dylib\"),\n join(__dirname, \"..\", \"..\", \"..\", \"swift\", \".build\", \"release\", \"libNodeMLX.dylib\"),\n join(process.cwd(), \"packages\", \"swift\", \".build\", \"release\", \"libNodeMLX.dylib\")\n ]\n\n for (const p of possibleDylibPaths) {\n if (existsSync(p)) {\n return p\n }\n }\n\n throw new Error(\n \"Swift library not found. Run 'pnpm build:swift' first.\\n\" +\n `Searched paths:\\n${possibleDylibPaths.join(\"\\n\")}`\n )\n}\n\nfunction loadBinding(): NativeBinding {\n if (binding && initialized) {\n return binding\n }\n\n if (platform() !== \"darwin\" || arch() !== \"arm64\") {\n throw new Error(\"node-mlx is only supported on macOS Apple Silicon (arm64)\")\n }\n\n binding = loadNativeAddon()\n const dylibPath = findSwiftLibrary()\n const success = binding.initialize(dylibPath)\n\n if (!success) {\n throw new Error(\"Failed to initialize node-mlx native library\")\n }\n\n initialized = true\n\n return binding\n}\n\n// MARK: - Public Types\n\nexport interface GenerationOptions {\n maxTokens?: number\n temperature?: number\n topP?: number\n /** Penalty for repeating tokens (1.0 = no penalty, 1.1-1.2 recommended) */\n repetitionPenalty?: number\n /** Number of recent tokens to consider for penalty (default: 20) */\n repetitionContextSize?: number\n}\n\nexport interface GenerationResult {\n text: string\n tokenCount: number\n tokensPerSecond: number\n}\n\nexport interface StreamingResult {\n tokenCount: number\n tokensPerSecond: number\n}\n\nexport interface Model {\n /** Generate text from a prompt */\n generate(prompt: string, options?: GenerationOptions): GenerationResult\n\n /** Generate text with streaming - tokens are written directly to stdout */\n generateStreaming(prompt: string, options?: GenerationOptions): StreamingResult\n\n /** Generate text from a prompt with an image (VLM only) */\n generateWithImage(prompt: string, imagePath: string, options?: GenerationOptions): StreamingResult\n\n /** Check if this model supports images (is a Vision-Language Model) */\n isVLM(): boolean\n\n /** Unload the model from memory */\n unload(): void\n\n /** Model handle (internal use) */\n readonly handle: number\n}\n\n// MARK: - Recommended Models\n\nexport const RECOMMENDED_MODELS = {\n // Qwen 2.5 (Alibaba) - Working with proper RoPE support\n // Using non-quantized models - quantized models have loading issues\n qwen: \"Qwen/Qwen2.5-1.5B-Instruct\",\n \"qwen-2.5\": \"Qwen/Qwen2.5-1.5B-Instruct\",\n \"qwen-2.5-0.5b\": \"Qwen/Qwen2.5-0.5B-Instruct\",\n \"qwen-2.5-1.5b\": \"Qwen/Qwen2.5-1.5B-Instruct\",\n \"qwen-2.5-3b\": \"Qwen/Qwen2.5-3B-Instruct\",\n\n // Phi (Microsoft) - Working with fused QKV and RoPE\n phi: \"microsoft/phi-4\", // Default to latest\n phi4: \"microsoft/phi-4\",\n \"phi-4\": \"microsoft/phi-4\",\n phi3: \"microsoft/Phi-3-mini-4k-instruct\",\n \"phi-3\": \"microsoft/Phi-3-mini-4k-instruct\",\n \"phi-3-mini\": \"microsoft/Phi-3-mini-4k-instruct\",\n\n // Llama 3.2 (Meta) - Requires HuggingFace authentication\n // Note: meta-llama models require accepting license at huggingface.co\n llama: \"meta-llama/Llama-3.2-1B-Instruct\",\n \"llama-3.2\": \"meta-llama/Llama-3.2-1B-Instruct\",\n \"llama-3.2-1b\": \"meta-llama/Llama-3.2-1B-Instruct\",\n \"llama-3.2-3b\": \"meta-llama/Llama-3.2-3B-Instruct\",\n\n // Gemma 3 (Google) - Standard transformer architecture with sliding window\n gemma: \"mlx-community/gemma-3-1b-it-4bit\",\n \"gemma-3\": \"mlx-community/gemma-3-1b-it-4bit\",\n \"gemma-3-1b\": \"mlx-community/gemma-3-1b-it-4bit\",\n \"gemma-3-1b-bf16\": \"mlx-community/gemma-3-1b-it-bf16\",\n \"gemma-3-4b\": \"mlx-community/gemma-3-4b-it-4bit\",\n \"gemma-3-4b-bf16\": \"mlx-community/gemma-3-4b-it-bf16\",\n \"gemma-3-12b\": \"mlx-community/gemma-3-12b-it-4bit\",\n \"gemma-3-27b\": \"mlx-community/gemma-3-27b-it-4bit\"\n} as const\n\nexport type RecommendedModelKey = keyof typeof RECOMMENDED_MODELS\n\n// MARK: - Public API\n\n/**\n * Check if the platform is Apple Silicon Mac\n */\nexport function isPlatformSupported(): boolean {\n return platform() === \"darwin\" && arch() === \"arm64\"\n}\n\n/**\n * Check if MLX is available on this system\n * (requires macOS 14+ on Apple Silicon with built binaries)\n */\nexport function isSupported(): boolean {\n if (!isPlatformSupported()) {\n return false\n }\n\n try {\n const b = loadBinding()\n\n return b.isAvailable()\n } catch {\n return false\n }\n}\n\n/**\n * Get the library version\n */\nexport function getVersion(): string {\n const b = loadBinding()\n\n return b.getVersion()\n}\n\n/**\n * Load a model from HuggingFace or local path\n *\n * @param modelId - HuggingFace model ID (e.g., \"mlx-community/gemma-3n-E2B-it-4bit\") or local path\n * @returns Model instance\n *\n * @example\n * ```typescript\n * import { loadModel, RECOMMENDED_MODELS } from \"node-mlx\"\n *\n * const model = loadModel(RECOMMENDED_MODELS[\"gemma-3n-2b\"])\n * const result = model.generate(\"Hello, world!\")\n * console.log(result.text)\n * model.unload()\n * ```\n */\nexport function loadModel(modelId: string): Model {\n const b = loadBinding()\n const handle = b.loadModel(modelId)\n\n return {\n handle,\n\n generate(prompt: string, options?: GenerationOptions): GenerationResult {\n const jsonStr = b.generate(handle, prompt, {\n maxTokens: options?.maxTokens ?? 256,\n temperature: options?.temperature ?? 0.7,\n topP: options?.topP ?? 0.9,\n repetitionPenalty: options?.repetitionPenalty ?? 0,\n repetitionContextSize: options?.repetitionContextSize ?? 20\n })\n\n const result = JSON.parse(jsonStr) as JSONGenerationResult\n\n if (!result.success) {\n throw new Error(result.error ?? \"Generation failed\")\n }\n\n return {\n text: result.text ?? \"\",\n tokenCount: result.tokenCount ?? 0,\n tokensPerSecond: result.tokensPerSecond ?? 0\n }\n },\n\n generateStreaming(prompt: string, options?: GenerationOptions): StreamingResult {\n // Tokens are written directly to stdout by Swift\n const jsonStr = b.generateStreaming(handle, prompt, {\n maxTokens: options?.maxTokens ?? 256,\n temperature: options?.temperature ?? 0.7,\n topP: options?.topP ?? 0.9,\n repetitionPenalty: options?.repetitionPenalty ?? 0,\n repetitionContextSize: options?.repetitionContextSize ?? 20\n })\n\n const result = JSON.parse(jsonStr) as JSONGenerationResult\n\n if (!result.success) {\n throw new Error(result.error ?? \"Generation failed\")\n }\n\n return {\n tokenCount: result.tokenCount ?? 0,\n tokensPerSecond: result.tokensPerSecond ?? 0\n }\n },\n\n generateWithImage(\n prompt: string,\n imagePath: string,\n options?: GenerationOptions\n ): StreamingResult {\n // VLM generation with image - tokens are written directly to stdout by Swift\n const jsonStr = b.generateWithImage(handle, prompt, imagePath, {\n maxTokens: options?.maxTokens ?? 256,\n temperature: options?.temperature ?? 0.7,\n topP: options?.topP ?? 0.9,\n repetitionPenalty: options?.repetitionPenalty ?? 0,\n repetitionContextSize: options?.repetitionContextSize ?? 20\n })\n\n const result = JSON.parse(jsonStr) as JSONGenerationResult\n\n if (!result.success) {\n throw new Error(result.error ?? \"Generation failed\")\n }\n\n return {\n tokenCount: result.tokenCount ?? 0,\n tokensPerSecond: result.tokensPerSecond ?? 0\n }\n },\n\n isVLM(): boolean {\n return b.isVLM(handle)\n },\n\n unload(): void {\n b.unloadModel(handle)\n }\n }\n}\n\n/**\n * Generate text using a model (one-shot, loads and unloads model)\n *\n * @param modelId - HuggingFace model ID or local path\n * @param prompt - Input text\n * @param options - Generation options\n * @returns Generation result\n *\n * @example\n * ```typescript\n * import { generate } from \"node-mlx\"\n *\n * const result = generate(\n * \"mlx-community/gemma-3n-E2B-it-4bit\",\n * \"Explain quantum computing\",\n * { maxTokens: 100 }\n * )\n * console.log(result.text)\n * ```\n */\nexport function generate(\n modelId: string,\n prompt: string,\n options?: GenerationOptions\n): GenerationResult {\n const model = loadModel(modelId)\n\n try {\n return model.generate(prompt, options)\n } finally {\n model.unload()\n }\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () => \n typeof document === \"undefined\" \n ? new URL(`file:${__filename}`).href \n : (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') \n ? document.currentScript.src \n : new URL(\"main.js\", document.baseURI).href;\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()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AsBO,SAAS,SACd,SACA,QACA,SACkB;AAClB,QAAM,QAAQ,UAAU,OAAO;AAE/B,MAAI;AACF,WAAO,MAAM,SAAS,QAAQ,OAAO;AAAA,EACvC,UAAE;AACA,UAAM,OAAO;AAAA,EACf;AACF;","names":["__filename","require"]}
@@ -0,0 +1,113 @@
1
+ /** Package version */
2
+ declare const VERSION: string;
3
+ interface GenerationOptions {
4
+ maxTokens?: number;
5
+ temperature?: number;
6
+ topP?: number;
7
+ /** Penalty for repeating tokens (1.0 = no penalty, 1.1-1.2 recommended) */
8
+ repetitionPenalty?: number;
9
+ /** Number of recent tokens to consider for penalty (default: 20) */
10
+ repetitionContextSize?: number;
11
+ }
12
+ interface GenerationResult {
13
+ text: string;
14
+ tokenCount: number;
15
+ tokensPerSecond: number;
16
+ }
17
+ interface StreamingResult {
18
+ tokenCount: number;
19
+ tokensPerSecond: number;
20
+ }
21
+ interface Model {
22
+ /** Generate text from a prompt */
23
+ generate(prompt: string, options?: GenerationOptions): GenerationResult;
24
+ /** Generate text with streaming - tokens are written directly to stdout */
25
+ generateStreaming(prompt: string, options?: GenerationOptions): StreamingResult;
26
+ /** Generate text from a prompt with an image (VLM only) */
27
+ generateWithImage(prompt: string, imagePath: string, options?: GenerationOptions): StreamingResult;
28
+ /** Check if this model supports images (is a Vision-Language Model) */
29
+ isVLM(): boolean;
30
+ /** Unload the model from memory */
31
+ unload(): void;
32
+ /** Model handle (internal use) */
33
+ readonly handle: number;
34
+ }
35
+ declare const RECOMMENDED_MODELS: {
36
+ readonly qwen: "Qwen/Qwen2.5-1.5B-Instruct";
37
+ readonly "qwen-2.5": "Qwen/Qwen2.5-1.5B-Instruct";
38
+ readonly "qwen-2.5-0.5b": "Qwen/Qwen2.5-0.5B-Instruct";
39
+ readonly "qwen-2.5-1.5b": "Qwen/Qwen2.5-1.5B-Instruct";
40
+ readonly "qwen-2.5-3b": "Qwen/Qwen2.5-3B-Instruct";
41
+ readonly phi: "microsoft/phi-4";
42
+ readonly phi4: "microsoft/phi-4";
43
+ readonly "phi-4": "microsoft/phi-4";
44
+ readonly phi3: "microsoft/Phi-3-mini-4k-instruct";
45
+ readonly "phi-3": "microsoft/Phi-3-mini-4k-instruct";
46
+ readonly "phi-3-mini": "microsoft/Phi-3-mini-4k-instruct";
47
+ readonly llama: "meta-llama/Llama-3.2-1B-Instruct";
48
+ readonly "llama-3.2": "meta-llama/Llama-3.2-1B-Instruct";
49
+ readonly "llama-3.2-1b": "meta-llama/Llama-3.2-1B-Instruct";
50
+ readonly "llama-3.2-3b": "meta-llama/Llama-3.2-3B-Instruct";
51
+ readonly gemma: "mlx-community/gemma-3-1b-it-4bit";
52
+ readonly "gemma-3": "mlx-community/gemma-3-1b-it-4bit";
53
+ readonly "gemma-3-1b": "mlx-community/gemma-3-1b-it-4bit";
54
+ readonly "gemma-3-1b-bf16": "mlx-community/gemma-3-1b-it-bf16";
55
+ readonly "gemma-3-4b": "mlx-community/gemma-3-4b-it-4bit";
56
+ readonly "gemma-3-4b-bf16": "mlx-community/gemma-3-4b-it-bf16";
57
+ readonly "gemma-3-12b": "mlx-community/gemma-3-12b-it-4bit";
58
+ readonly "gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit";
59
+ };
60
+ type RecommendedModelKey = keyof typeof RECOMMENDED_MODELS;
61
+ /**
62
+ * Check if the platform is Apple Silicon Mac
63
+ */
64
+ declare function isPlatformSupported(): boolean;
65
+ /**
66
+ * Check if MLX is available on this system
67
+ * (requires macOS 14+ on Apple Silicon with built binaries)
68
+ */
69
+ declare function isSupported(): boolean;
70
+ /**
71
+ * Get the library version
72
+ */
73
+ declare function getVersion(): string;
74
+ /**
75
+ * Load a model from HuggingFace or local path
76
+ *
77
+ * @param modelId - HuggingFace model ID (e.g., "mlx-community/gemma-3n-E2B-it-4bit") or local path
78
+ * @returns Model instance
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { loadModel, RECOMMENDED_MODELS } from "node-mlx"
83
+ *
84
+ * const model = loadModel(RECOMMENDED_MODELS["gemma-3n-2b"])
85
+ * const result = model.generate("Hello, world!")
86
+ * console.log(result.text)
87
+ * model.unload()
88
+ * ```
89
+ */
90
+ declare function loadModel(modelId: string): Model;
91
+ /**
92
+ * Generate text using a model (one-shot, loads and unloads model)
93
+ *
94
+ * @param modelId - HuggingFace model ID or local path
95
+ * @param prompt - Input text
96
+ * @param options - Generation options
97
+ * @returns Generation result
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * import { generate } from "node-mlx"
102
+ *
103
+ * const result = generate(
104
+ * "mlx-community/gemma-3n-E2B-it-4bit",
105
+ * "Explain quantum computing",
106
+ * { maxTokens: 100 }
107
+ * )
108
+ * console.log(result.text)
109
+ * ```
110
+ */
111
+ declare function generate(modelId: string, prompt: string, options?: GenerationOptions): GenerationResult;
112
+
113
+ export { type GenerationOptions, type GenerationResult, type Model, RECOMMENDED_MODELS, type RecommendedModelKey, type StreamingResult, VERSION, generate, getVersion, isPlatformSupported, isSupported, loadModel };
@@ -0,0 +1,113 @@
1
+ /** Package version */
2
+ declare const VERSION: string;
3
+ interface GenerationOptions {
4
+ maxTokens?: number;
5
+ temperature?: number;
6
+ topP?: number;
7
+ /** Penalty for repeating tokens (1.0 = no penalty, 1.1-1.2 recommended) */
8
+ repetitionPenalty?: number;
9
+ /** Number of recent tokens to consider for penalty (default: 20) */
10
+ repetitionContextSize?: number;
11
+ }
12
+ interface GenerationResult {
13
+ text: string;
14
+ tokenCount: number;
15
+ tokensPerSecond: number;
16
+ }
17
+ interface StreamingResult {
18
+ tokenCount: number;
19
+ tokensPerSecond: number;
20
+ }
21
+ interface Model {
22
+ /** Generate text from a prompt */
23
+ generate(prompt: string, options?: GenerationOptions): GenerationResult;
24
+ /** Generate text with streaming - tokens are written directly to stdout */
25
+ generateStreaming(prompt: string, options?: GenerationOptions): StreamingResult;
26
+ /** Generate text from a prompt with an image (VLM only) */
27
+ generateWithImage(prompt: string, imagePath: string, options?: GenerationOptions): StreamingResult;
28
+ /** Check if this model supports images (is a Vision-Language Model) */
29
+ isVLM(): boolean;
30
+ /** Unload the model from memory */
31
+ unload(): void;
32
+ /** Model handle (internal use) */
33
+ readonly handle: number;
34
+ }
35
+ declare const RECOMMENDED_MODELS: {
36
+ readonly qwen: "Qwen/Qwen2.5-1.5B-Instruct";
37
+ readonly "qwen-2.5": "Qwen/Qwen2.5-1.5B-Instruct";
38
+ readonly "qwen-2.5-0.5b": "Qwen/Qwen2.5-0.5B-Instruct";
39
+ readonly "qwen-2.5-1.5b": "Qwen/Qwen2.5-1.5B-Instruct";
40
+ readonly "qwen-2.5-3b": "Qwen/Qwen2.5-3B-Instruct";
41
+ readonly phi: "microsoft/phi-4";
42
+ readonly phi4: "microsoft/phi-4";
43
+ readonly "phi-4": "microsoft/phi-4";
44
+ readonly phi3: "microsoft/Phi-3-mini-4k-instruct";
45
+ readonly "phi-3": "microsoft/Phi-3-mini-4k-instruct";
46
+ readonly "phi-3-mini": "microsoft/Phi-3-mini-4k-instruct";
47
+ readonly llama: "meta-llama/Llama-3.2-1B-Instruct";
48
+ readonly "llama-3.2": "meta-llama/Llama-3.2-1B-Instruct";
49
+ readonly "llama-3.2-1b": "meta-llama/Llama-3.2-1B-Instruct";
50
+ readonly "llama-3.2-3b": "meta-llama/Llama-3.2-3B-Instruct";
51
+ readonly gemma: "mlx-community/gemma-3-1b-it-4bit";
52
+ readonly "gemma-3": "mlx-community/gemma-3-1b-it-4bit";
53
+ readonly "gemma-3-1b": "mlx-community/gemma-3-1b-it-4bit";
54
+ readonly "gemma-3-1b-bf16": "mlx-community/gemma-3-1b-it-bf16";
55
+ readonly "gemma-3-4b": "mlx-community/gemma-3-4b-it-4bit";
56
+ readonly "gemma-3-4b-bf16": "mlx-community/gemma-3-4b-it-bf16";
57
+ readonly "gemma-3-12b": "mlx-community/gemma-3-12b-it-4bit";
58
+ readonly "gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit";
59
+ };
60
+ type RecommendedModelKey = keyof typeof RECOMMENDED_MODELS;
61
+ /**
62
+ * Check if the platform is Apple Silicon Mac
63
+ */
64
+ declare function isPlatformSupported(): boolean;
65
+ /**
66
+ * Check if MLX is available on this system
67
+ * (requires macOS 14+ on Apple Silicon with built binaries)
68
+ */
69
+ declare function isSupported(): boolean;
70
+ /**
71
+ * Get the library version
72
+ */
73
+ declare function getVersion(): string;
74
+ /**
75
+ * Load a model from HuggingFace or local path
76
+ *
77
+ * @param modelId - HuggingFace model ID (e.g., "mlx-community/gemma-3n-E2B-it-4bit") or local path
78
+ * @returns Model instance
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import { loadModel, RECOMMENDED_MODELS } from "node-mlx"
83
+ *
84
+ * const model = loadModel(RECOMMENDED_MODELS["gemma-3n-2b"])
85
+ * const result = model.generate("Hello, world!")
86
+ * console.log(result.text)
87
+ * model.unload()
88
+ * ```
89
+ */
90
+ declare function loadModel(modelId: string): Model;
91
+ /**
92
+ * Generate text using a model (one-shot, loads and unloads model)
93
+ *
94
+ * @param modelId - HuggingFace model ID or local path
95
+ * @param prompt - Input text
96
+ * @param options - Generation options
97
+ * @returns Generation result
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * import { generate } from "node-mlx"
102
+ *
103
+ * const result = generate(
104
+ * "mlx-community/gemma-3n-E2B-it-4bit",
105
+ * "Explain quantum computing",
106
+ * { maxTokens: 100 }
107
+ * )
108
+ * console.log(result.text)
109
+ * ```
110
+ */
111
+ declare function generate(modelId: string, prompt: string, options?: GenerationOptions): GenerationResult;
112
+
113
+ export { type GenerationOptions, type GenerationResult, type Model, RECOMMENDED_MODELS, type RecommendedModelKey, type StreamingResult, VERSION, generate, getVersion, isPlatformSupported, isSupported, loadModel };