createNeuron() โ
Creates a Neuron instance โ the main interface for in-browser LLM chat.
ts
import { createNeuron } from '@agent-layer-zero/dendrite'
const neuron = createNeuron(config)Model loading starts immediately on creation. Use onProgress to track download progress.
Config Options โ
Required โ
| Option | Type | Description |
|---|---|---|
modelId | string | WebLLM model ID (e.g. 'gemma-2-2b-it-q4f16_1-MLC') |
Performance (optional) โ
| Option | Type | Description |
|---|---|---|
worker | Worker | () => Worker | Optional Web Worker for off-thread performance. See Worker Setup. If omitted, runs on main thread. |
Personality (use one) โ
| Option | Type | Description |
|---|---|---|
systemPrompt | string | Simple system prompt string |
personalityDocs | PersonalityDoc[] | Typed personality documents |
Generation โ
| Option | Type | Default | Description |
|---|---|---|---|
temperature | number | 0.2 | Randomness (0 = deterministic, 2 = creative) |
maxTokens | number | 2048 | Max tokens to generate per response |
frequencyPenalty | number | 0.5 | Penalty for repeated tokens (reduces loops) |
maxHistoryTurns | number | 10 | How many conversation turns to include |
Caching โ
| Option | Type | Default | Description |
|---|---|---|---|
useIndexedDBCache | boolean | true | Cache model weights in IndexedDB |
Inference Mode โ
By default Dendrite auto-selects the best engine for the device (WebGPU โ WASM โ API). Override this with inference or engine.
| Option | Type | Default | Description |
|---|---|---|---|
inference | 'auto' | 'fastest' | 'fallback' | 'api' | 'auto' | Selection strategy. 'auto' tries WebGPU then WASM then API. 'fastest' requires WebGPU, throws otherwise. 'fallback' forces Transformers.js (widest compatibility). 'api' requires apiUrl. |
engine | 'webllm' | 'transformers' | 'api' | โ | Force a specific engine, overriding inference. |
Callbacks โ
| Option | Type | Description |
|---|---|---|
onProgress | (percent: number, text: string) => void | Model download progress (0-100) |
onError | (error: Error) => void | Any error (load, generation, etc.) |
onLoadingChange | (loading: boolean) => void | Loading state transitions |
onGeneratingChange | (generating: boolean) => void | Generating state transitions |
onInferenceSelected | (tier: { engine, mode, reason }) => void | Fires when the engine tier is chosen. engine is 'webllm' | 'transformers' | 'api', mode is 'webgpu' | 'wasm' | 'cloud'. |
API Connection (optional) โ
| Option | Type | Description |
|---|---|---|
apiUrl | string | AgentLayerZero API URL |
username | string | Profile username |
instanceSlug | string | Instance slug |
apiToken | string | Auth token for private instances |
Returns โ
A Neuron instance.
Example โ
ts
const neuron = createNeuron({
modelId: 'gemma-2-2b-it-q4f16_1-MLC',
personalityDocs: [
{ type: 'zero-shot', content: 'You are a pirate.' },
{ type: 'knowledge', content: 'You sailed the seven seas.' },
],
temperature: 0.7,
onProgress: (pct, text) => updateProgressBar(pct),
onLoadingChange: (loading) => toggleSpinner(loading),
onError: (err) => showError(err.message),
})