node-mlxnode-mlx
API

API Reference

Complete API documentation for node-mlx

API Reference

Complete documentation for all node-mlx functions and types.

Functions

generate()

One-shot text generation that loads, generates, and unloads automatically.

function generate(model: string, prompt: string, options?: GenerateOptions): GenerateResult

Parameters:

ParameterTypeDescription
modelstringModel name or HuggingFace path
promptstringInput text to generate from
optionsGenerateOptionsOptional generation settings

Returns: GenerateResult object with generated text and metrics.

Example:

import { generate } from "node-mlx"

const result = generate("qwen", "Hello, world!", {
  maxTokens: 100,
  temperature: 0.7
})

console.log(result.text)
console.log(`${result.tokensPerSecond} tok/s`)

loadModel()

Load a model for multiple generations. More efficient when generating multiple responses.

function loadModel(model: string): Model

Parameters:

ParameterTypeDescription
modelstringModel name or HuggingFace path

Returns: Model instance with generate() and unload() methods.

Example:

import { loadModel } from "node-mlx"

const model = loadModel("phi")

const result1 = model.generate("What is 2+2?")
const result2 = model.generate("What is the capital of France?")

model.unload()

Types

GenerateOptions

Configuration options for text generation.

interface GenerateOptions {
  maxTokens?: number // Maximum tokens to generate (default: 256)
  temperature?: number // Sampling temperature 0-2 (default: 0.7)
  topP?: number // Nucleus sampling threshold (default: 0.9)
  repetitionPenalty?: number // Penalty for repeated tokens (default: 1.0)
  systemPrompt?: string // System prompt for chat models
}

GenerateResult

Result object returned from generation.

interface GenerateResult {
  text: string // Generated text
  tokensGenerated: number // Number of tokens generated
  tokensPerSecond: number // Generation speed
  promptTokens: number // Tokens in the prompt
  totalTime: number // Total generation time in ms
}

Model

Loaded model instance for multiple generations.

interface Model {
  generate(prompt: string, options?: GenerateOptions): GenerateResult
  unload(): void
}

Model Names

You can use short aliases or full HuggingFace paths:

AliasFull Path
qwenlmstudio-community/Qwen3-4B-Instruct-2507-MLX-4bit
phimlx-community/phi-4-4bit
gemmamlx-community/gemma-3-1b-it-4bit
llamameta-llama/Llama-4-Scout-17B-16E-Instruct
mistralmlx-community/Mistral-7B-Instruct-v0.3-4bit

Or use any model from the mlx-community on HuggingFace:

const model = loadModel("mlx-community/DeepSeek-R1-Distill-Qwen-1.5B-4bit")

On this page