node-mlx 1.0.1 → 1.0.3
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.
- package/dist/cli.js +5 -1
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +5 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/prebuilds/darwin-arm64/node.node +0 -0
- package/swift/libNodeMLX.dylib +0 -0
package/dist/cli.js
CHANGED
|
@@ -116,7 +116,11 @@ var RECOMMENDED_MODELS = {
|
|
|
116
116
|
"gemma-3-4b": "mlx-community/gemma-3-4b-it-4bit",
|
|
117
117
|
"gemma-3-4b-bf16": "mlx-community/gemma-3-4b-it-bf16",
|
|
118
118
|
"gemma-3-12b": "mlx-community/gemma-3-12b-it-4bit",
|
|
119
|
-
"gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit"
|
|
119
|
+
"gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit",
|
|
120
|
+
// Gemma 3n (Google) - Efficient architecture with AltUp and Laurel
|
|
121
|
+
"gemma-3n": "mlx-community/gemma-3n-E2B-it-4bit",
|
|
122
|
+
"gemma-3n-e2b": "mlx-community/gemma-3n-E2B-it-4bit",
|
|
123
|
+
"gemma-3n-e4b": "mlx-community/gemma-3n-E4B-it-4bit"
|
|
120
124
|
};
|
|
121
125
|
function isPlatformSupported() {
|
|
122
126
|
return platform() === "darwin" && arch() === "arm64";
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +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;AAyDnC,IAAI,UAAgC;AACpC,IAAI,cAAc;AAKlB,SAAS,kBAAiC;AAExC,MAAI;AACF,UAAM,WAAWA,SAAQ,gBAAgB;AACzC,UAAM,YAAY,KAAK,WAAW,MAAM,QAAQ;AAEhD,QAAI,WAAW,KAAK,WAAW,MAAM,WAAW,CAAC,GAAG;AAClD,aAAO,SAAS,KAAK,WAAW,IAAI,CAAC;AAAA,IACvC;AAGA,QAAI,WAAW,KAAK,WAAW,OAAO,CAAC,GAAG;AACxC,aAAO,SAAS,SAAS;AAA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,QAAQ,WAAW,SAAS,QAAQ;AACtD;AAGA,SAAS,OAAa;AACpB,QAAM,EAAE,OAAO,QAAQ,WAAW,SAAS,QAAQ,IAAI,UAAU;AAGjE,UAAQ,SAAS;AAAA,IACf,KAAK;AACH,kBAAY;AACZ,gBAAU;AAEV;AAAA,IAEF,KAAK;AACH,UAAI,aAAa,OAAO,EAAE;AAE1B;AAAA,IAEF,KAAK;AACH,kBAAY;AACZ,kBAAY;AAEZ;AAAA,EACJ;AAGA,MAAI,CAAC,oBAAoB,GAAG;AAC1B,UAAM,wDAAwD;AAC9D,YAAQ,KAAK,CAAC;AAAA,EAChB;AAEA,MAAI,CAAC,YAAY,GAAG;AAClB,UAAM,gFAAgF;AACtF,YAAQ,KAAK,CAAC;AAAA,EAChB;AAEA,UAAQ,SAAS;AAAA,IACf,KAAK;AACH,UAAI,QAAQ;AACV,mBAAW,OAAO,QAAQ,WAAW,OAAO;AAAA,MAC9C;AAEA;AAAA,IAEF,KAAK;AACH,kBAAY;AACZ,qBAAe,KAAK;AACpB;AAAA,EACJ;AACF;AAEA,IAAI;AACF,OAAK;AACP,SAAS,KAAc;AACrB,QAAM,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,CAAC;AACtD,UAAQ,KAAK,CAAC;AAChB;","names":["require","state","rl"]}
|
|
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\n // Gemma 3n (Google) - Efficient architecture with AltUp and Laurel\n \"gemma-3n\": \"mlx-community/gemma-3n-E2B-it-4bit\",\n \"gemma-3n-e2b\": \"mlx-community/gemma-3n-E2B-it-4bit\",\n \"gemma-3n-e4b\": \"mlx-community/gemma-3n-E4B-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\"])\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 * RECOMMENDED_MODELS[\"gemma-3n\"],\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;AAyDnC,IAAI,UAAgC;AACpC,IAAI,cAAc;AAKlB,SAAS,kBAAiC;AAExC,MAAI;AACF,UAAM,WAAWA,SAAQ,gBAAgB;AACzC,UAAM,YAAY,KAAK,WAAW,MAAM,QAAQ;AAEhD,QAAI,WAAW,KAAK,WAAW,MAAM,WAAW,CAAC,GAAG;AAClD,aAAO,SAAS,KAAK,WAAW,IAAI,CAAC;AAAA,IACvC;AAGA,QAAI,WAAW,KAAK,WAAW,OAAO,CAAC,GAAG;AACxC,aAAO,SAAS,SAAS;AAAA,IAC3B;AAAA,EACF,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AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,OAAO,QAAQ,WAAW,SAAS,QAAQ;AACtD;AAGA,SAAS,OAAa;AACpB,QAAM,EAAE,OAAO,QAAQ,WAAW,SAAS,QAAQ,IAAI,UAAU;AAGjE,UAAQ,SAAS;AAAA,IACf,KAAK;AACH,kBAAY;AACZ,gBAAU;AAEV;AAAA,IAEF,KAAK;AACH,UAAI,aAAa,OAAO,EAAE;AAE1B;AAAA,IAEF,KAAK;AACH,kBAAY;AACZ,kBAAY;AAEZ;AAAA,EACJ;AAGA,MAAI,CAAC,oBAAoB,GAAG;AAC1B,UAAM,wDAAwD;AAC9D,YAAQ,KAAK,CAAC;AAAA,EAChB;AAEA,MAAI,CAAC,YAAY,GAAG;AAClB,UAAM,gFAAgF;AACtF,YAAQ,KAAK,CAAC;AAAA,EAChB;AAEA,UAAQ,SAAS;AAAA,IACf,KAAK;AACH,UAAI,QAAQ;AACV,mBAAW,OAAO,QAAQ,WAAW,OAAO;AAAA,MAC9C;AAEA;AAAA,IAEF,KAAK;AACH,kBAAY;AACZ,qBAAe,KAAK;AACpB;AAAA,EACJ;AACF;AAEA,IAAI;AACF,OAAK;AACP,SAAS,KAAc;AACrB,QAAM,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG,CAAC;AACtD,UAAQ,KAAK,CAAC;AAChB;","names":["require","state","rl"]}
|
package/dist/index.cjs
CHANGED
|
@@ -147,7 +147,11 @@ var RECOMMENDED_MODELS = {
|
|
|
147
147
|
"gemma-3-4b": "mlx-community/gemma-3-4b-it-4bit",
|
|
148
148
|
"gemma-3-4b-bf16": "mlx-community/gemma-3-4b-it-bf16",
|
|
149
149
|
"gemma-3-12b": "mlx-community/gemma-3-12b-it-4bit",
|
|
150
|
-
"gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit"
|
|
150
|
+
"gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit",
|
|
151
|
+
// Gemma 3n (Google) - Efficient architecture with AltUp and Laurel
|
|
152
|
+
"gemma-3n": "mlx-community/gemma-3n-E2B-it-4bit",
|
|
153
|
+
"gemma-3n-e2b": "mlx-community/gemma-3n-E2B-it-4bit",
|
|
154
|
+
"gemma-3n-e4b": "mlx-community/gemma-3n-E4B-it-4bit"
|
|
151
155
|
};
|
|
152
156
|
function isPlatformSupported() {
|
|
153
157
|
return (0, import_node_os.platform)() === "darwin" && (0, import_node_os.arch)() === "arm64";
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +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()\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,QAAQ,YAAY,MAAM,WAC1E,SAAS,cAAc,MACvB,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEtC,IAAM,gBAAgC,iCAAiB;;;ADZ9D,qBAA+B;AAC/B,uBAA8B;AAC9B,sBAA8B;AAC9B,qBAAyC;AACzC,yBAA8B;AAE9B,IAAMA,kBAAa,+BAAc,aAAe;AAChD,IAAM,gBAAY,0BAAQA,WAAU;AACpC,IAAMC,eAAU,kCAAc,aAAe;AAG7C,IAAM,sBAAkB,uBAAK,WAAW,MAAM,cAAc;AAC5D,IAAM,cAAc,KAAK,UAAM,6BAAa,iBAAiB,OAAO,CAAC;AAG9D,IAAM,UAAU,YAAY;AAyDnC,IAAI,UAAgC;AACpC,IAAI,cAAc;AAKlB,SAAS,kBAAiC;AAExC,MAAI;AACF,UAAM,WAAWA,SAAQ,gBAAgB;AACzC,UAAM,gBAAY,uBAAK,WAAW,MAAM,QAAQ;AAEhD,YAAI,+BAAW,uBAAK,WAAW,MAAM,WAAW,CAAC,GAAG;AAClD,aAAO,aAAS,uBAAK,WAAW,IAAI,CAAC;AAAA,IACvC;AAGA,YAAI,+BAAW,uBAAK,WAAW,OAAO,CAAC,GAAG;AACxC,aAAO,SAAS,SAAS;AAAA,IAC3B;AAA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D;AAEA,aAAO;AAAA,QACL,YAAY,OAAO,cAAc;AAAA,QACjC,iBAAiB,OAAO,mBAAmB;AAAA,MAC7C;AAAA,IACF;AAAA,IAEA,QAAiB;AACf,aAAO,EAAE,MAAM,MAAM;AAAA,IACvB;AAAA,IAEA,SAAe;AACb,QAAE,YAAY,MAAM;AAAA,IACtB;AAAA,EACF;AACF;AAsBO,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"]}
|
|
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\n // Gemma 3n (Google) - Efficient architecture with AltUp and Laurel\n \"gemma-3n\": \"mlx-community/gemma-3n-E2B-it-4bit\",\n \"gemma-3n-e2b\": \"mlx-community/gemma-3n-E2B-it-4bit\",\n \"gemma-3n-e4b\": \"mlx-community/gemma-3n-E4B-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\"])\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 * RECOMMENDED_MODELS[\"gemma-3n\"],\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()\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,QAAQ,YAAY,MAAM,WAC1E,SAAS,cAAc,MACvB,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEtC,IAAM,gBAAgC,iCAAiB;;;ADZ9D,qBAA+B;AAC/B,uBAA8B;AAC9B,sBAA8B;AAC9B,qBAAyC;AACzC,yBAA8B;AAE9B,IAAMA,kBAAa,+BAAc,aAAe;AAChD,IAAM,gBAAY,0BAAQA,WAAU;AACpC,IAAMC,eAAU,kCAAc,aAAe;AAG7C,IAAM,sBAAkB,uBAAK,WAAW,MAAM,cAAc;AAC5D,IAAM,cAAc,KAAK,UAAM,6BAAa,iBAAiB,OAAO,CAAC;AAG9D,IAAM,UAAU,YAAY;AAyDnC,IAAI,UAAgC;AACpC,IAAI,cAAc;AAKlB,SAAS,kBAAiC;AAExC,MAAI;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YAAM,UAAU,EAAE,kBAAkB,QAAQ,QAAQ,WAAW;AAAA,QAC7D,WAAW,SAAS,aAAa;AAAA,QACjC,aAAa,SAAS,eAAe;AAAA,QACrC,MAAM,SAAS,QAAQ;AAAA,QACvB,mBAAmB,SAAS,qBAAqB;AAAA,QACjD,uBAAuB,SAAS,yBAAyB;AAAA,MAC3D,CAAC;AAED,YAAM,SAAS,KAAK,MAAM,OAAO;AAEjC,UAAI,CAAC,OAAO,SAAS;AACnB,cAAM,IAAI,MAAM,OAAO,SAAS,mBAAmB;AAAA,MACrD;AAEA,aAAO;AAAA,QACL,YAAY,OAAO,cAAc;AAAA,QACjC,iBAAiB,OAAO,mBAAmB;AAAA,MAC7C;AAAA,IACF;AAAA,IAEA,QAAiB;AACf,aAAO,EAAE,MAAM,MAAM;AAAA,IACvB;AAAA,IAEA,SAAe;AACb,QAAE,YAAY,MAAM;AAAA,IACtB;AAAA,EACF;AACF;AAsBO,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"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -56,6 +56,9 @@ declare const RECOMMENDED_MODELS: {
|
|
|
56
56
|
readonly "gemma-3-4b-bf16": "mlx-community/gemma-3-4b-it-bf16";
|
|
57
57
|
readonly "gemma-3-12b": "mlx-community/gemma-3-12b-it-4bit";
|
|
58
58
|
readonly "gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit";
|
|
59
|
+
readonly "gemma-3n": "mlx-community/gemma-3n-E2B-it-4bit";
|
|
60
|
+
readonly "gemma-3n-e2b": "mlx-community/gemma-3n-E2B-it-4bit";
|
|
61
|
+
readonly "gemma-3n-e4b": "mlx-community/gemma-3n-E4B-it-4bit";
|
|
59
62
|
};
|
|
60
63
|
type RecommendedModelKey = keyof typeof RECOMMENDED_MODELS;
|
|
61
64
|
/**
|
|
@@ -81,7 +84,7 @@ declare function getVersion(): string;
|
|
|
81
84
|
* ```typescript
|
|
82
85
|
* import { loadModel, RECOMMENDED_MODELS } from "node-mlx"
|
|
83
86
|
*
|
|
84
|
-
* const model = loadModel(RECOMMENDED_MODELS["gemma-3n
|
|
87
|
+
* const model = loadModel(RECOMMENDED_MODELS["gemma-3n"])
|
|
85
88
|
* const result = model.generate("Hello, world!")
|
|
86
89
|
* console.log(result.text)
|
|
87
90
|
* model.unload()
|
|
@@ -101,7 +104,7 @@ declare function loadModel(modelId: string): Model;
|
|
|
101
104
|
* import { generate } from "node-mlx"
|
|
102
105
|
*
|
|
103
106
|
* const result = generate(
|
|
104
|
-
* "
|
|
107
|
+
* RECOMMENDED_MODELS["gemma-3n"],
|
|
105
108
|
* "Explain quantum computing",
|
|
106
109
|
* { maxTokens: 100 }
|
|
107
110
|
* )
|
package/dist/index.d.ts
CHANGED
|
@@ -56,6 +56,9 @@ declare const RECOMMENDED_MODELS: {
|
|
|
56
56
|
readonly "gemma-3-4b-bf16": "mlx-community/gemma-3-4b-it-bf16";
|
|
57
57
|
readonly "gemma-3-12b": "mlx-community/gemma-3-12b-it-4bit";
|
|
58
58
|
readonly "gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit";
|
|
59
|
+
readonly "gemma-3n": "mlx-community/gemma-3n-E2B-it-4bit";
|
|
60
|
+
readonly "gemma-3n-e2b": "mlx-community/gemma-3n-E2B-it-4bit";
|
|
61
|
+
readonly "gemma-3n-e4b": "mlx-community/gemma-3n-E4B-it-4bit";
|
|
59
62
|
};
|
|
60
63
|
type RecommendedModelKey = keyof typeof RECOMMENDED_MODELS;
|
|
61
64
|
/**
|
|
@@ -81,7 +84,7 @@ declare function getVersion(): string;
|
|
|
81
84
|
* ```typescript
|
|
82
85
|
* import { loadModel, RECOMMENDED_MODELS } from "node-mlx"
|
|
83
86
|
*
|
|
84
|
-
* const model = loadModel(RECOMMENDED_MODELS["gemma-3n
|
|
87
|
+
* const model = loadModel(RECOMMENDED_MODELS["gemma-3n"])
|
|
85
88
|
* const result = model.generate("Hello, world!")
|
|
86
89
|
* console.log(result.text)
|
|
87
90
|
* model.unload()
|
|
@@ -101,7 +104,7 @@ declare function loadModel(modelId: string): Model;
|
|
|
101
104
|
* import { generate } from "node-mlx"
|
|
102
105
|
*
|
|
103
106
|
* const result = generate(
|
|
104
|
-
* "
|
|
107
|
+
* RECOMMENDED_MODELS["gemma-3n"],
|
|
105
108
|
* "Explain quantum computing",
|
|
106
109
|
* { maxTokens: 100 }
|
|
107
110
|
* )
|
package/dist/index.js
CHANGED
|
@@ -111,7 +111,11 @@ var RECOMMENDED_MODELS = {
|
|
|
111
111
|
"gemma-3-4b": "mlx-community/gemma-3-4b-it-4bit",
|
|
112
112
|
"gemma-3-4b-bf16": "mlx-community/gemma-3-4b-it-bf16",
|
|
113
113
|
"gemma-3-12b": "mlx-community/gemma-3-12b-it-4bit",
|
|
114
|
-
"gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit"
|
|
114
|
+
"gemma-3-27b": "mlx-community/gemma-3-27b-it-4bit",
|
|
115
|
+
// Gemma 3n (Google) - Efficient architecture with AltUp and Laurel
|
|
116
|
+
"gemma-3n": "mlx-community/gemma-3n-E2B-it-4bit",
|
|
117
|
+
"gemma-3n-e2b": "mlx-community/gemma-3n-E2B-it-4bit",
|
|
118
|
+
"gemma-3n-e4b": "mlx-community/gemma-3n-E4B-it-4bit"
|
|
115
119
|
};
|
|
116
120
|
function isPlatformSupported() {
|
|
117
121
|
return platform() === "darwin" && arch() === "arm64";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"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"],"mappings":";AAAA,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;AAyDnC,IAAI,UAAgC;AACpC,IAAI,cAAc;AAKlB,SAAS,kBAAiC;AAExC,MAAI;AACF,UAAM,WAAWA,SAAQ,gBAAgB;AACzC,UAAM,YAAY,KAAK,WAAW,MAAM,QAAQ;AAEhD,QAAI,WAAW,KAAK,WAAW,MAAM,WAAW,CAAC,GAAG;AAClD,aAAO,SAAS,KAAK,WAAW,IAAI,CAAC;AAAA,IACvC;AAGA,QAAI,WAAW,KAAK,WAAW,OAAO,CAAC,GAAG;AACxC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BAAmB;AAAA,MACrD;AAEA,aAAO;AAAA,QACL,YAAY,OAAO,cAAc;AAAA,QACjC,iBAAiB,OAAO,mBAAmB;AAAA,MAC7C;AAAA,IACF;AAAA,IAEA,QAAiB;AACf,aAAO,EAAE,MAAM,MAAM;AAAA,IACvB;AAAA,IAEA,SAAe;AACb,QAAE,YAAY,MAAM;AAAA,IACtB;AAAA,EACF;AACF;AAsBO,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":["require"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"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\n // Gemma 3n (Google) - Efficient architecture with AltUp and Laurel\n \"gemma-3n\": \"mlx-community/gemma-3n-E2B-it-4bit\",\n \"gemma-3n-e2b\": \"mlx-community/gemma-3n-E2B-it-4bit\",\n \"gemma-3n-e4b\": \"mlx-community/gemma-3n-E4B-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\"])\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 * RECOMMENDED_MODELS[\"gemma-3n\"],\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":";AAAA,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;AAyDnC,IAAI,UAAgC;AACpC,IAAI,cAAc;AAKlB,SAAS,kBAAiC;AAExC,MAAI;AACF,UAAM,WAAWA,SAAQ,gBAAgB;AACzC,UAAM,YAAY,KAAK,WAAW,MAAM,QAAQ;AAEhD,QAAI,WAAW,KAAK,WAAW,MAAM,WAAW,CAAC,GAAG;AAClD,aAAO,SAAS,KAAK,WAAW,IAAI,CAAC;AAAA,IACvC;AAGA,QAAI,WAAW,KAAK,WAAW,OAAO,CAAC,GAAG;AACxC,aAAO,SAAS,SAAS;AAAA,IAC3B;AAAA,EACF,QAAQ;AAAA,EAER;AAGA,QAAM,qBAAqB;AAAA;AAAA,IAEzB,KAAK,WAAW,MAAM,aAAa,gBAAgB,gBAAgB;AAAA;AAAA,IAEnE,KAAK,WAAW,MAAM,UAAU,SAAS,WAAW,eAAe;AAAA;AAAA,IAEnE,KAAK,QAAQ,IAAI,GAAG,YAAY,YAAY,UAAU,SAAS,WAAW,eAAe;AAAA,EAC3F;AAEA,aAAW,KAAK,oBAAoB;AAClC,QAAI,WAAW,CAAC,GAAG;AACjB,aAAOA,SAAQ,CAAC;AAAA,IAClB;AAAA,EACF;AAEA,QAAM,IAAI;AAAA,IACR;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