📦 deps(thirdparty): update snapshots
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
---
|
||||
source: "https://github.com/huggingface/skills/tree/main/skills/transformers-js"
|
||||
name: transformers-js
|
||||
description: Run Hugging Face models in JavaScript or TypeScript with Transformers.js in Node.js or the browser.
|
||||
license: Apache-2.0
|
||||
description: Use Transformers.js to run state-of-the-art machine learning models directly in JavaScript/TypeScript. Supports NLP (text classification, translation, summarization), computer vision (image classification, object detection), audio (speech recognition, audio classification), and...
|
||||
risk: unknown
|
||||
metadata:
|
||||
author: huggingface
|
||||
version: "3.8.1"
|
||||
category: machine-learning
|
||||
repository: https://github.com/huggingface/transformers.js
|
||||
compatibility: Requires Node.js 18+ or modern browser with ES modules support. WebGPU support requires compatible browser/environment. Internet access needed for downloading models from Hugging Face Hub (optional if using local models).
|
||||
source: https://github.com/huggingface/skills/tree/main/skills/transformers-js
|
||||
source_repo: huggingface/skills
|
||||
source_type: official
|
||||
date_added: 2026-07-01
|
||||
license: Apache-2.0
|
||||
license_source: https://github.com/huggingface/skills/blob/main/LICENSE
|
||||
---
|
||||
|
||||
# Transformers.js - Machine Learning for JavaScript
|
||||
|
||||
Transformers.js enables running state-of-the-art machine learning models directly in JavaScript, both in browsers and Node.js environments, with no server required.
|
||||
Transformers.js enables running state-of-the-art machine learning models directly in JavaScript across browsers and server-side runtimes (Node.js, Bun, Deno), with no Python server required.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
@@ -55,7 +53,7 @@ const result = await pipe('I love transformers!');
|
||||
// Output: [{ label: 'POSITIVE', score: 0.999817686 }]
|
||||
|
||||
// IMPORTANT: Always dispose when done to free memory
|
||||
await classifier.dispose();
|
||||
await pipe.dispose();
|
||||
```
|
||||
|
||||
**⚠️ Memory Management:** All pipelines must be disposed with `pipe.dispose()` when finished to prevent memory leaks. See examples in [Code Examples](./references/EXAMPLES.md) for cleanup patterns across different environments.
|
||||
@@ -88,7 +86,7 @@ Choose where to run the model:
|
||||
// Run on CPU (default for WASM)
|
||||
const pipe = await pipeline('sentiment-analysis', 'model-id');
|
||||
|
||||
// Run on GPU (WebGPU - experimental)
|
||||
// Run on GPU (WebGPU)
|
||||
const pipe = await pipeline('sentiment-analysis', 'model-id', {
|
||||
device: 'webgpu',
|
||||
});
|
||||
@@ -361,7 +359,7 @@ await generator.dispose();
|
||||
2. **Check ONNX Support**: Ensure the model has ONNX files (look for `onnx` folder in model repo)
|
||||
3. **Read Model Cards**: Model cards contain usage examples, limitations, and benchmarks
|
||||
4. **Test Locally**: Benchmark inference speed and memory usage in your environment
|
||||
5. **Community Models**: Look for models by `Xenova` (Transformers.js maintainer) or `onnx-community`
|
||||
5. **Filter by Library**: Use `library=transformers.js` to find compatible models: https://huggingface.co/models?library=transformers.js
|
||||
6. **Version Pin**: Use specific git commits in production for stability:
|
||||
```javascript
|
||||
const pipe = await pipeline('task', 'model-id', { revision: 'abc123' });
|
||||
@@ -376,10 +374,10 @@ The `env` object provides comprehensive control over Transformers.js execution,
|
||||
**Quick Overview:**
|
||||
|
||||
```javascript
|
||||
import { env } from '@huggingface/transformers';
|
||||
import { env, LogLevel } from '@huggingface/transformers';
|
||||
|
||||
// View version
|
||||
console.log(env.version); // e.g., '3.8.1'
|
||||
console.log(env.version); // e.g., '4.x'
|
||||
|
||||
// Common settings
|
||||
env.allowRemoteModels = true; // Load from Hugging Face Hub
|
||||
@@ -388,6 +386,18 @@ env.localModelPath = '/models/'; // Local model directory
|
||||
env.useFSCache = true; // Cache models on disk (Node.js)
|
||||
env.useBrowserCache = true; // Cache models in browser
|
||||
env.cacheDir = './.cache'; // Cache directory location
|
||||
// Optional: override logging level (default is LogLevel.WARNING)
|
||||
env.logLevel = LogLevel.INFO;
|
||||
|
||||
// Optional: custom fetch for auth headers, retries, abort signals, etc.
|
||||
env.fetch = (url, options) =>
|
||||
fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
...options?.headers,
|
||||
Authorization: `Bearer ${HF_TOKEN}`,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
**Configuration Patterns:**
|
||||
@@ -414,6 +424,43 @@ For complete documentation on all configuration options, caching strategies, cac
|
||||
|
||||
**→ [Configuration Reference](./references/CONFIGURATION.md)**
|
||||
|
||||
### ModelRegistry (v4)
|
||||
|
||||
`ModelRegistry` gives you visibility and control over model assets before loading a pipeline. Use it to estimate download size, check cache status, inspect available dtypes, and clear cached artifacts for a specific task/model/options tuple.
|
||||
|
||||
```javascript
|
||||
import { ModelRegistry } from '@huggingface/transformers';
|
||||
|
||||
const task = 'feature-extraction';
|
||||
const modelId = 'onnx-community/all-MiniLM-L6-v2-ONNX';
|
||||
const modelOptions = { dtype: 'fp32' };
|
||||
|
||||
// List required files for this pipeline
|
||||
const files = await ModelRegistry.get_pipeline_files(task, modelId, modelOptions);
|
||||
|
||||
// Check if assets are already cached
|
||||
const cached = await ModelRegistry.is_pipeline_cached(task, modelId, modelOptions);
|
||||
|
||||
// Inspect precision formats available for this model
|
||||
const dtypes = await ModelRegistry.get_available_dtypes(modelId);
|
||||
|
||||
console.log({ files: files.length, cached, dtypes });
|
||||
```
|
||||
|
||||
For production patterns and full API coverage, see **[ModelRegistry Reference](./references/MODEL_REGISTRY.md)**.
|
||||
|
||||
### Standalone Tokenization (`@huggingface/tokenizers`)
|
||||
|
||||
For tokenization-only workflows, use `@huggingface/tokenizers`. It is a separate lightweight package useful when you need fast tokenization/encoding without loading full model inference pipelines.
|
||||
|
||||
```bash
|
||||
npm install @huggingface/tokenizers
|
||||
```
|
||||
|
||||
```javascript
|
||||
import { Tokenizer } from '@huggingface/tokenizers';
|
||||
```
|
||||
|
||||
### Working with Tensors
|
||||
|
||||
```javascript
|
||||
@@ -443,10 +490,10 @@ const results = await classifier([
|
||||
]);
|
||||
```
|
||||
|
||||
## Browser-Specific Considerations
|
||||
## Runtime-Specific Considerations
|
||||
|
||||
### WebGPU Usage
|
||||
WebGPU provides GPU acceleration in browsers:
|
||||
WebGPU provides GPU acceleration in browsers and server-side runtimes (when supported):
|
||||
|
||||
```javascript
|
||||
const pipe = await pipeline('text-generation', 'onnx-community/gemma-3-270m-it-ONNX', {
|
||||
@@ -455,10 +502,10 @@ const pipe = await pipeline('text-generation', 'onnx-community/gemma-3-270m-it-O
|
||||
});
|
||||
```
|
||||
|
||||
**Note**: WebGPU is experimental. Check browser compatibility and file issues if problems occur.
|
||||
**Note**: Use `webgpu` when available and fall back to WASM/CPU when not supported in the current runtime.
|
||||
|
||||
### WASM Performance
|
||||
Default browser execution uses WASM:
|
||||
WASM is the most compatible execution backend across runtimes:
|
||||
|
||||
```javascript
|
||||
// Optimized for browsers with quantization
|
||||
@@ -478,13 +525,18 @@ import { pipeline } from '@huggingface/transformers';
|
||||
const fileProgress = {};
|
||||
|
||||
function onProgress(info) {
|
||||
console.log(`${info.status}: ${info.file}`);
|
||||
|
||||
if (info.status === 'progress_total') {
|
||||
console.log(`Total: ${info.progress.toFixed(1)}%`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`${info.status}: ${info.file ?? ''}`);
|
||||
|
||||
if (info.status === 'progress') {
|
||||
fileProgress[info.file] = info.progress;
|
||||
console.log(`${info.file}: ${info.progress.toFixed(1)}%`);
|
||||
}
|
||||
|
||||
|
||||
if (info.status === 'done') {
|
||||
console.log(`✓ ${info.file} complete`);
|
||||
}
|
||||
@@ -500,10 +552,10 @@ const classifier = await pipeline('sentiment-analysis', null, {
|
||||
|
||||
```typescript
|
||||
interface ProgressInfo {
|
||||
status: 'initiate' | 'download' | 'progress' | 'done' | 'ready';
|
||||
status: 'initiate' | 'download' | 'progress' | 'progress_total' | 'done' | 'ready';
|
||||
name: string; // Model id or path
|
||||
file: string; // File being processed
|
||||
progress?: number; // Percentage (0-100, only for 'progress' status)
|
||||
file?: string; // File being processed (per-file events)
|
||||
progress?: number; // Percentage (0-100, for 'progress' and 'progress_total')
|
||||
loaded?: number; // Bytes downloaded (only for 'progress' status)
|
||||
total?: number; // Total bytes (only for 'progress' status)
|
||||
}
|
||||
@@ -581,6 +633,7 @@ For detailed patterns (React cleanup, servers, browser), see **[Code Examples](.
|
||||
### This Skill
|
||||
- **[Pipeline Options](./references/PIPELINE_OPTIONS.md)** - Configure `pipeline()` with `progress_callback`, `device`, `dtype`, etc.
|
||||
- **[Configuration Reference](./references/CONFIGURATION.md)** - Global `env` configuration for caching and model loading
|
||||
- **[ModelRegistry Reference](./references/MODEL_REGISTRY.md)** - Inspect files, cache status, dtypes, and clear cache before loading pipelines
|
||||
- **[Caching Reference](./references/CACHE.md)** - Browser Cache API, Node.js filesystem cache, and custom cache implementations
|
||||
- **[Text Generation Guide](./references/TEXT_GENERATION.md)** - Streaming, chat format, and generation parameters
|
||||
- **[Model Architectures](./references/MODEL_ARCHITECTURES.md)** - Supported models and selection tips
|
||||
@@ -591,7 +644,7 @@ For detailed patterns (React cleanup, servers, browser), see **[Code Examples](.
|
||||
- API reference: https://huggingface.co/docs/transformers.js/api/pipelines
|
||||
- Model hub: https://huggingface.co/models?library=transformers.js
|
||||
- GitHub: https://github.com/huggingface/transformers.js
|
||||
- Examples: https://github.com/huggingface/transformers.js/tree/main/examples
|
||||
- Examples: https://github.com/huggingface/transformers.js-examples
|
||||
|
||||
## Best Practices
|
||||
|
||||
@@ -639,6 +692,7 @@ For detailed patterns (React cleanup, servers, browser), see **[Code Examples](.
|
||||
This skill enables you to integrate state-of-the-art machine learning capabilities directly into JavaScript applications without requiring separate ML servers or Python environments.
|
||||
|
||||
## Limitations
|
||||
- Use this skill only when the task clearly matches the scope described above.
|
||||
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
|
||||
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
|
||||
|
||||
- Use this skill only when the task clearly matches its upstream product or API scope.
|
||||
- Verify commands, API behavior, pricing, quotas, credentials, and deployment effects against current official documentation before making changes.
|
||||
- Do not treat generated examples as a substitute for environment-specific tests, security review, or user approval for destructive or costly actions.
|
||||
|
||||
@@ -9,8 +9,9 @@ Complete guide to configuring Transformers.js behavior using the `env` object.
|
||||
3. [Local Model Configuration](#local-model-configuration)
|
||||
4. [Cache Configuration](#cache-configuration)
|
||||
5. [WASM Configuration](#wasm-configuration)
|
||||
6. [Common Configuration Patterns](#common-configuration-patterns)
|
||||
7. [Environment Best Practices](#environment-best-practices)
|
||||
6. [Network and Logging Controls](#network-and-logging-controls)
|
||||
7. [Common Configuration Patterns](#common-configuration-patterns)
|
||||
8. [Environment Best Practices](#environment-best-practices)
|
||||
|
||||
## Overview
|
||||
|
||||
@@ -20,7 +21,7 @@ The `env` object provides comprehensive control over Transformers.js execution,
|
||||
import { env } from '@huggingface/transformers';
|
||||
|
||||
// View current version
|
||||
console.log(env.version); // e.g., '3.8.1'
|
||||
console.log(env.version); // e.g., '4.x'
|
||||
```
|
||||
|
||||
### Available Properties
|
||||
@@ -29,22 +30,22 @@ console.log(env.version); // e.g., '3.8.1'
|
||||
interface TransformersEnvironment {
|
||||
// Version info
|
||||
version: string;
|
||||
|
||||
|
||||
// Backend configuration
|
||||
backends: {
|
||||
onnx: Partial<ONNXEnv>;
|
||||
};
|
||||
|
||||
|
||||
// Remote model settings
|
||||
allowRemoteModels: boolean;
|
||||
remoteHost: string;
|
||||
remotePathTemplate: string;
|
||||
|
||||
|
||||
// Local model settings
|
||||
allowLocalModels: boolean;
|
||||
localModelPath: string;
|
||||
useFS: boolean;
|
||||
|
||||
|
||||
// Cache settings
|
||||
useBrowserCache: boolean;
|
||||
useFSCache: boolean;
|
||||
@@ -53,6 +54,10 @@ interface TransformersEnvironment {
|
||||
customCache: CacheInterface | null;
|
||||
useWasmCache: boolean;
|
||||
cacheKey: string;
|
||||
|
||||
// Networking and logging (v4)
|
||||
fetch: typeof globalThis.fetch;
|
||||
logLevel: LogLevel;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -243,6 +248,49 @@ env.backends.onnx.wasm.wasmPaths = '/static/wasm/';
|
||||
- `ort-wasm-threaded.wasm` - Multi-threaded WASM binary
|
||||
- `ort-wasm-simd-threaded.wasm` - SIMD + multi-threaded WASM binary
|
||||
|
||||
## Network and Logging Controls
|
||||
|
||||
Transformers.js v4 adds environment controls for authenticated fetching and cleaner runtime logs.
|
||||
|
||||
### Custom Fetch (`env.fetch`)
|
||||
|
||||
Use `env.fetch` to inject auth headers, retries, custom routing, or abort handling.
|
||||
|
||||
```javascript
|
||||
import { env } from '@huggingface/transformers';
|
||||
|
||||
const HF_TOKEN = process.env.HF_TOKEN;
|
||||
|
||||
env.fetch = (url, options) =>
|
||||
fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
...options?.headers,
|
||||
Authorization: `Bearer ${HF_TOKEN}`,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Logging Level (`env.logLevel`)
|
||||
|
||||
Use `env.logLevel` to override runtime verbosity. The default is `LogLevel.WARNING`.
|
||||
|
||||
```javascript
|
||||
import { env, LogLevel } from '@huggingface/transformers';
|
||||
|
||||
// Enable more detailed logs during development
|
||||
env.logLevel = LogLevel.INFO;
|
||||
```
|
||||
|
||||
Common values:
|
||||
- `LogLevel.DEBUG`
|
||||
- `LogLevel.INFO`
|
||||
- `LogLevel.WARNING`
|
||||
- `LogLevel.ERROR`
|
||||
- `LogLevel.NONE`
|
||||
|
||||
For ONNX Runtime session-level logging controls, see `session_options` in **[Pipeline Options](./PIPELINE_OPTIONS.md)**.
|
||||
|
||||
## Common Configuration Patterns
|
||||
|
||||
### Development Setup
|
||||
|
||||
@@ -30,10 +30,10 @@ All examples use the same task and model for consistency:
|
||||
<div id="loading" style="display:none;">Loading model...</div>
|
||||
|
||||
<script type="module">
|
||||
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1';
|
||||
|
||||
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@4';
|
||||
|
||||
let extractor;
|
||||
|
||||
|
||||
// Initialize model on page load
|
||||
document.getElementById('loading').style.display = 'block';
|
||||
extractor = await pipeline(
|
||||
@@ -41,18 +41,18 @@ All examples use the same task and model for consistency:
|
||||
'onnx-community/all-MiniLM-L6-v2-ONNX'
|
||||
);
|
||||
document.getElementById('loading').style.display = 'none';
|
||||
|
||||
|
||||
window.generateEmbedding = async function() {
|
||||
const text = document.getElementById('input').value;
|
||||
const output = await extractor(text, { pooling: 'mean', normalize: true });
|
||||
|
||||
|
||||
document.getElementById('result').innerHTML = `
|
||||
<h3>Embedding Generated:</h3>
|
||||
<p>Dimensions: ${output.data.length}</p>
|
||||
<p>First 5 values: ${Array.from(output.data).slice(0, 5).join(', ')}</p>
|
||||
`;
|
||||
};
|
||||
|
||||
|
||||
// Cleanup on page unload
|
||||
window.addEventListener('beforeunload', () => {
|
||||
if (extractor) extractor.dispose();
|
||||
@@ -104,19 +104,24 @@ All examples use the same task and model for consistency:
|
||||
</div>
|
||||
|
||||
<script type="module">
|
||||
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1';
|
||||
|
||||
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@4';
|
||||
|
||||
let extractor;
|
||||
const fileProgressBars = {};
|
||||
const progressContainer = document.getElementById('progress-container');
|
||||
|
||||
|
||||
extractor = await pipeline(
|
||||
'feature-extraction',
|
||||
'onnx-community/all-MiniLM-L6-v2-ONNX',
|
||||
{
|
||||
progress_callback: (info) => {
|
||||
document.getElementById('status').textContent = `${info.status}: ${info.file}`;
|
||||
|
||||
if (info.status === 'progress_total') {
|
||||
document.getElementById('status').textContent = `Total: ${info.progress.toFixed(1)}%`;
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById('status').textContent = `${info.status}: ${info.file ?? ''}`;
|
||||
|
||||
if (info.status === 'progress') {
|
||||
// Create progress bar for each file
|
||||
if (!fileProgressBars[info.file]) {
|
||||
@@ -131,11 +136,11 @@ All examples use the same task and model for consistency:
|
||||
progressContainer.appendChild(fileDiv);
|
||||
fileProgressBars[info.file] = fileDiv.querySelector('.progress-fill');
|
||||
}
|
||||
|
||||
|
||||
// Update progress
|
||||
fileProgressBars[info.file].style.width = `${info.progress}%`;
|
||||
}
|
||||
|
||||
|
||||
if (info.status === 'ready') {
|
||||
document.getElementById('loading').style.display = 'none';
|
||||
document.getElementById('app').style.display = 'block';
|
||||
@@ -143,16 +148,16 @@ All examples use the same task and model for consistency:
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
window.generateEmbedding = async function() {
|
||||
const text = document.getElementById('input').value;
|
||||
const output = await extractor(text, { pooling: 'mean', normalize: true });
|
||||
|
||||
|
||||
document.getElementById('result').innerHTML = `
|
||||
<p>Embedding: ${output.data.length} dimensions</p>
|
||||
`;
|
||||
};
|
||||
|
||||
|
||||
// Cleanup on page unload
|
||||
window.addEventListener('beforeunload', () => {
|
||||
if (extractor) extractor.dispose();
|
||||
@@ -175,13 +180,13 @@ async function generateEmbedding(text) {
|
||||
'feature-extraction',
|
||||
'onnx-community/all-MiniLM-L6-v2-ONNX'
|
||||
);
|
||||
|
||||
|
||||
const output = await extractor(text, { pooling: 'mean', normalize: true });
|
||||
|
||||
|
||||
console.log('Text:', text);
|
||||
console.log('Embedding dimensions:', output.data.length);
|
||||
console.log('First 5 values:', Array.from(output.data).slice(0, 5));
|
||||
|
||||
|
||||
await extractor.dispose();
|
||||
}
|
||||
|
||||
@@ -200,32 +205,32 @@ async function embedDocuments(documents) {
|
||||
'feature-extraction',
|
||||
'onnx-community/all-MiniLM-L6-v2-ONNX'
|
||||
);
|
||||
|
||||
|
||||
console.log(`Processing ${documents.length} documents...`);
|
||||
|
||||
|
||||
const embeddings = [];
|
||||
|
||||
|
||||
for (let i = 0; i < documents.length; i++) {
|
||||
const output = await extractor(documents[i], {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
const output = await extractor(documents[i], {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
});
|
||||
|
||||
|
||||
embeddings.push({
|
||||
text: documents[i],
|
||||
embedding: Array.from(output.data)
|
||||
});
|
||||
|
||||
|
||||
console.log(`Processed ${i + 1}/${documents.length}`);
|
||||
}
|
||||
|
||||
|
||||
await fs.writeFile(
|
||||
'embeddings.json',
|
||||
JSON.stringify(embeddings, null, 2)
|
||||
);
|
||||
|
||||
|
||||
console.log('Saved to embeddings.json');
|
||||
|
||||
|
||||
await extractor.dispose();
|
||||
}
|
||||
|
||||
@@ -246,45 +251,50 @@ import { pipeline } from '@huggingface/transformers';
|
||||
|
||||
async function main() {
|
||||
const text = process.argv[2] || 'Hello, world!';
|
||||
|
||||
|
||||
console.log('Loading model...');
|
||||
|
||||
|
||||
const fileProgress = {};
|
||||
|
||||
|
||||
const extractor = await pipeline(
|
||||
'feature-extraction',
|
||||
'onnx-community/all-MiniLM-L6-v2-ONNX',
|
||||
{
|
||||
progress_callback: (info) => {
|
||||
if (info.status === 'progress_total') {
|
||||
process.stdout.write(`\r\x1b[KTotal: ${info.progress.toFixed(1)}%`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.status === 'progress') {
|
||||
fileProgress[info.file] = info.progress;
|
||||
|
||||
|
||||
// Show all files progress
|
||||
const progressLines = Object.entries(fileProgress)
|
||||
.map(([file, progress]) => ` ${file}: ${progress.toFixed(1)}%`)
|
||||
.join('\n');
|
||||
|
||||
|
||||
process.stdout.write(`\r\x1b[K${progressLines}`);
|
||||
}
|
||||
|
||||
|
||||
if (info.status === 'done') {
|
||||
console.log(`\n✓ ${info.file} complete`);
|
||||
}
|
||||
|
||||
|
||||
if (info.status === 'ready') {
|
||||
console.log('\nModel ready!');
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
console.log('Generating embedding...');
|
||||
const output = await extractor(text, { pooling: 'mean', normalize: true });
|
||||
|
||||
|
||||
console.log(`\nText: "${text}"`);
|
||||
console.log(`Dimensions: ${output.data.length}`);
|
||||
console.log(`First 5 values: ${Array.from(output.data).slice(0, 5).join(', ')}`);
|
||||
|
||||
|
||||
await extractor.dispose();
|
||||
}
|
||||
|
||||
@@ -308,9 +318,9 @@ export function EmbeddingGenerator() {
|
||||
|
||||
const generate = async () => {
|
||||
if (!text) return;
|
||||
|
||||
|
||||
setLoading(true);
|
||||
|
||||
|
||||
// Load model on first generate
|
||||
if (!extractorRef.current) {
|
||||
extractorRef.current = await pipeline(
|
||||
@@ -318,10 +328,10 @@ export function EmbeddingGenerator() {
|
||||
'onnx-community/all-MiniLM-L6-v2-ONNX'
|
||||
);
|
||||
}
|
||||
|
||||
const output = await extractorRef.current(text, {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
|
||||
const output = await extractorRef.current(text, {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
});
|
||||
setEmbedding(Array.from(output.data));
|
||||
setLoading(false);
|
||||
@@ -339,18 +349,18 @@ export function EmbeddingGenerator() {
|
||||
return (
|
||||
<div>
|
||||
<h2>Text Embedding Generator</h2>
|
||||
|
||||
|
||||
<textarea
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
placeholder="Enter text"
|
||||
disabled={loading}
|
||||
/>
|
||||
|
||||
|
||||
<button onClick={generate} disabled={loading || !text}>
|
||||
{loading ? 'Processing...' : 'Generate Embedding'}
|
||||
</button>
|
||||
|
||||
|
||||
{embedding && (
|
||||
<div>
|
||||
<h3>Result:</h3>
|
||||
@@ -380,27 +390,32 @@ export function EmbeddingGeneratorWithProgress() {
|
||||
|
||||
const generate = async () => {
|
||||
if (!text) return;
|
||||
|
||||
|
||||
setLoading(true);
|
||||
|
||||
|
||||
// Load model on first generate
|
||||
if (!extractorRef.current) {
|
||||
setStatus('Loading model...');
|
||||
|
||||
|
||||
extractorRef.current = await pipeline(
|
||||
'feature-extraction',
|
||||
'onnx-community/all-MiniLM-L6-v2-ONNX',
|
||||
{
|
||||
progress_callback: (info) => {
|
||||
setStatus(`${info.status}: ${info.file}`);
|
||||
|
||||
if (info.status === 'progress_total') {
|
||||
setStatus(`Total: ${info.progress.toFixed(1)}%`);
|
||||
return;
|
||||
}
|
||||
|
||||
setStatus(`${info.status}: ${info.file ?? ''}`);
|
||||
|
||||
if (info.status === 'progress') {
|
||||
setFileProgress(prev => ({
|
||||
...prev,
|
||||
[info.file]: info.progress
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
if (info.status === 'ready') {
|
||||
setStatus('Model ready!');
|
||||
}
|
||||
@@ -408,11 +423,11 @@ export function EmbeddingGeneratorWithProgress() {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
setStatus('Generating embedding...');
|
||||
const output = await extractorRef.current(text, {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
const output = await extractorRef.current(text, {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
});
|
||||
setEmbedding(Array.from(output.data));
|
||||
setStatus('Complete!');
|
||||
@@ -431,7 +446,7 @@ export function EmbeddingGeneratorWithProgress() {
|
||||
return (
|
||||
<div>
|
||||
<h2>Text Embedding Generator</h2>
|
||||
|
||||
|
||||
{loading && Object.keys(fileProgress).length > 0 && (
|
||||
<div>
|
||||
<p>{status}</p>
|
||||
@@ -439,31 +454,31 @@ export function EmbeddingGeneratorWithProgress() {
|
||||
<div key={file} style={{ margin: '10px 0' }}>
|
||||
<div style={{ fontSize: '12px', marginBottom: '5px' }}>{file}</div>
|
||||
<div style={{ width: '100%', height: '20px', background: '#f0f0f0', borderRadius: '5px', overflow: 'hidden' }}>
|
||||
<div
|
||||
style={{
|
||||
width: `${progress}%`,
|
||||
height: '100%',
|
||||
<div
|
||||
style={{
|
||||
width: `${progress}%`,
|
||||
height: '100%',
|
||||
background: '#4CAF50',
|
||||
transition: 'width 0.3s'
|
||||
}}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
<textarea
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
placeholder="Enter text"
|
||||
disabled={loading}
|
||||
/>
|
||||
|
||||
|
||||
<button onClick={generate} disabled={loading || !text}>
|
||||
{loading ? 'Processing...' : 'Generate Embedding'}
|
||||
</button>
|
||||
|
||||
|
||||
{embedding && (
|
||||
<div>
|
||||
<h3>Result:</h3>
|
||||
@@ -502,16 +517,16 @@ let extractor;
|
||||
app.post('/embed', async (req, res) => {
|
||||
try {
|
||||
const { text } = req.body;
|
||||
|
||||
|
||||
if (!text) {
|
||||
return res.status(400).json({ error: 'Text is required' });
|
||||
}
|
||||
|
||||
const output = await extractor(text, {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
|
||||
const output = await extractor(text, {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
});
|
||||
|
||||
|
||||
res.json({
|
||||
text,
|
||||
embedding: Array.from(output.data),
|
||||
@@ -553,16 +568,16 @@ async function initialize() {
|
||||
app.post('/embed', async (req, res) => {
|
||||
try {
|
||||
const { text } = req.body;
|
||||
|
||||
|
||||
if (!text) {
|
||||
return res.status(400).json({ error: 'Text is required' });
|
||||
}
|
||||
|
||||
const output = await extractor(text, {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
|
||||
const output = await extractor(text, {
|
||||
pooling: 'mean',
|
||||
normalize: true
|
||||
});
|
||||
|
||||
|
||||
res.json({
|
||||
embedding: Array.from(output.data),
|
||||
dimensions: output.data.length
|
||||
@@ -574,19 +589,19 @@ app.post('/embed', async (req, res) => {
|
||||
|
||||
async function shutdown(signal) {
|
||||
console.log(`\n${signal} received. Shutting down...`);
|
||||
|
||||
|
||||
if (server) {
|
||||
server.close(() => {
|
||||
console.log('HTTP server closed');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (extractor) {
|
||||
console.log('Disposing model...');
|
||||
await extractor.dispose();
|
||||
console.log('Model disposed');
|
||||
}
|
||||
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
# ModelRegistry Reference
|
||||
|
||||
In Transformers.js v4, `ModelRegistry` provides a preflight API for model assets. You can inspect required files, estimate total download size, check cache state, and clear cached artifacts before calling `pipeline()`.
|
||||
|
||||
This is useful for production UX where you want to:
|
||||
- show accurate download estimates before loading,
|
||||
- support offline-first flows,
|
||||
- avoid surprise bandwidth usage,
|
||||
- and keep cache management explicit.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Overview](#overview)
|
||||
2. [Core APIs](#core-apis)
|
||||
3. [Recommended Workflow](#recommended-workflow)
|
||||
4. [Examples](#examples)
|
||||
5. [Best Practices](#best-practices)
|
||||
|
||||
## Overview
|
||||
|
||||
```javascript
|
||||
import { ModelRegistry } from '@huggingface/transformers';
|
||||
```
|
||||
|
||||
`ModelRegistry` works with the same task/model/options you pass to `pipeline()`.
|
||||
|
||||
Typical tuple:
|
||||
|
||||
```javascript
|
||||
const task = 'feature-extraction';
|
||||
const modelId = 'onnx-community/all-MiniLM-L6-v2-ONNX';
|
||||
const modelOptions = { dtype: 'fp32' };
|
||||
```
|
||||
|
||||
## Core APIs
|
||||
|
||||
### `get_pipeline_files(task, modelId, modelOptions)`
|
||||
|
||||
Returns all files needed to initialize that pipeline configuration.
|
||||
|
||||
```javascript
|
||||
const files = await ModelRegistry.get_pipeline_files(task, modelId, modelOptions);
|
||||
// Example: ['config.json', 'onnx/model.onnx', 'tokenizer.json', ...]
|
||||
```
|
||||
|
||||
Use this to build preflight checks and download manifests.
|
||||
|
||||
### `get_file_metadata(modelId, file)`
|
||||
|
||||
Returns metadata for a single file (including size when available).
|
||||
|
||||
```javascript
|
||||
const metadata = await ModelRegistry.get_file_metadata(modelId, 'onnx/model.onnx');
|
||||
console.log(metadata);
|
||||
```
|
||||
|
||||
Use this to compute total transfer size and identify large artifacts.
|
||||
|
||||
### `is_pipeline_cached(task, modelId, modelOptions)`
|
||||
|
||||
Checks whether required files are already available in cache.
|
||||
|
||||
```javascript
|
||||
const cached = await ModelRegistry.is_pipeline_cached(task, modelId, modelOptions);
|
||||
console.log(cached ? 'Ready offline' : 'Needs download');
|
||||
```
|
||||
|
||||
Use this to gate offline mode and skip unnecessary preload steps.
|
||||
|
||||
### `clear_pipeline_cache(task, modelId, modelOptions)`
|
||||
|
||||
Clears cached assets for a specific pipeline tuple.
|
||||
|
||||
```javascript
|
||||
await ModelRegistry.clear_pipeline_cache(task, modelId, modelOptions);
|
||||
```
|
||||
|
||||
Use this for cache invalidation, testing, or space reclamation.
|
||||
|
||||
### `get_available_dtypes(modelId)`
|
||||
|
||||
Returns precision/quantization formats available for the model.
|
||||
|
||||
```javascript
|
||||
const dtypes = await ModelRegistry.get_available_dtypes(modelId);
|
||||
// Example: ['fp32', 'fp16', 'q4', 'q4f16']
|
||||
```
|
||||
|
||||
Use this to choose the best runtime profile (quality vs. speed vs. memory).
|
||||
|
||||
## Recommended Workflow
|
||||
|
||||
For robust loading UX:
|
||||
|
||||
1. Resolve the exact task/model/options tuple.
|
||||
2. Call `get_pipeline_files(...)`.
|
||||
3. Fetch metadata per file and compute total size.
|
||||
4. Call `is_pipeline_cached(...)`.
|
||||
5. If not cached, show user-facing size/progress expectations.
|
||||
6. Load via `pipeline(...)` and use `progress_total` in `progress_callback`.
|
||||
|
||||
```javascript
|
||||
import { ModelRegistry, pipeline } from '@huggingface/transformers';
|
||||
|
||||
const task = 'feature-extraction';
|
||||
const modelId = 'onnx-community/all-MiniLM-L6-v2-ONNX';
|
||||
const modelOptions = { dtype: 'q8' };
|
||||
|
||||
const files = await ModelRegistry.get_pipeline_files(task, modelId, modelOptions);
|
||||
|
||||
const metadata = await Promise.all(
|
||||
files.map((file) => ModelRegistry.get_file_metadata(modelId, file))
|
||||
);
|
||||
|
||||
const totalBytes = metadata.reduce((sum, item) => sum + (item?.size ?? 0), 0);
|
||||
const totalMB = (totalBytes / 1024 / 1024).toFixed(2);
|
||||
|
||||
const cached = await ModelRegistry.is_pipeline_cached(task, modelId, modelOptions);
|
||||
console.log({ fileCount: files.length, totalMB, cached });
|
||||
|
||||
const pipe = await pipeline(task, modelId, {
|
||||
...modelOptions,
|
||||
progress_callback: (info) => {
|
||||
if (info.status === 'progress_total') {
|
||||
console.log(`Loading: ${info.progress.toFixed(1)}%`);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
await pipe.dispose();
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Example 1: Offer dtype choice dynamically
|
||||
|
||||
```javascript
|
||||
import { ModelRegistry, pipeline } from '@huggingface/transformers';
|
||||
|
||||
const task = 'text-generation';
|
||||
const modelId = 'onnx-community/Qwen2.5-0.5B-Instruct';
|
||||
|
||||
const dtypes = await ModelRegistry.get_available_dtypes(modelId);
|
||||
const preferred = dtypes.includes('q4') ? 'q4' : dtypes[0] ?? 'fp32';
|
||||
|
||||
const generator = await pipeline(task, modelId, { dtype: preferred });
|
||||
// ... inference
|
||||
await generator.dispose();
|
||||
```
|
||||
|
||||
### Example 2: Only clear one pipeline cache entry
|
||||
|
||||
```javascript
|
||||
import { ModelRegistry } from '@huggingface/transformers';
|
||||
|
||||
await ModelRegistry.clear_pipeline_cache(
|
||||
'feature-extraction',
|
||||
'onnx-community/all-MiniLM-L6-v2-ONNX',
|
||||
{ dtype: 'fp32' }
|
||||
);
|
||||
```
|
||||
|
||||
This avoids wiping unrelated model caches.
|
||||
|
||||
### Example 3: Offline gate
|
||||
|
||||
```javascript
|
||||
import { ModelRegistry, env, pipeline } from '@huggingface/transformers';
|
||||
|
||||
const task = 'feature-extraction';
|
||||
const modelId = 'onnx-community/all-MiniLM-L6-v2-ONNX';
|
||||
const modelOptions = { dtype: 'q8' };
|
||||
|
||||
const cached = await ModelRegistry.is_pipeline_cached(task, modelId, modelOptions);
|
||||
|
||||
if (!cached) {
|
||||
throw new Error('Model not cached yet. Connect once to download assets.');
|
||||
}
|
||||
|
||||
env.allowRemoteModels = false;
|
||||
const pipe = await pipeline(task, modelId, { ...modelOptions, local_files_only: true });
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. Use `ModelRegistry` before `pipeline()` when you need predictable download UX.
|
||||
2. Cache decisions should be per task/model/options tuple (dtype and revision matter).
|
||||
3. Use `progress_total` for user-facing progress bars; keep per-file progress optional.
|
||||
4. Prefer selective invalidation with `clear_pipeline_cache(...)` over broad cache deletion.
|
||||
5. In offline mode, combine `is_pipeline_cached(...)` with `local_files_only: true` and `env.allowRemoteModels = false`.
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [Pipeline Options](./PIPELINE_OPTIONS.md) - `pipeline()` options and progress callback
|
||||
- [Configuration Reference](./CONFIGURATION.md) - `env` settings for local/remote loading
|
||||
- [Caching Reference](./CACHE.md) - Browser, filesystem, and custom cache behavior
|
||||
- [Main Skill Guide](../SKILL.md) - Practical usage patterns
|
||||
@@ -32,26 +32,26 @@ The third parameter, `options`, allows you to configure how the model is loaded
|
||||
interface PretrainedModelOptions {
|
||||
// Progress tracking
|
||||
progress_callback?: (info: ProgressInfo) => void;
|
||||
|
||||
|
||||
// Model configuration
|
||||
config?: PretrainedConfig;
|
||||
|
||||
|
||||
// Cache and loading
|
||||
cache_dir?: string;
|
||||
local_files_only?: boolean;
|
||||
revision?: string;
|
||||
|
||||
|
||||
// Model-specific settings
|
||||
subfolder?: string;
|
||||
model_file_name?: string;
|
||||
|
||||
|
||||
// Device and performance
|
||||
device?: DeviceType | Record<string, DeviceType>;
|
||||
dtype?: DataType | Record<string, DataType>;
|
||||
|
||||
|
||||
// External data format (large models)
|
||||
use_external_data_format?: boolean | number | Record<string, boolean | number>;
|
||||
|
||||
|
||||
// ONNX Runtime settings
|
||||
session_options?: InferenceSession.SessionOptions;
|
||||
}
|
||||
@@ -61,18 +61,25 @@ interface PretrainedModelOptions {
|
||||
|
||||
### Progress Callback
|
||||
|
||||
Track model download and loading progress. **Note:** Models consist of multiple files (model weights, config, tokenizer, etc.), and each file reports its own progress:
|
||||
Track model download and loading progress. **Recommended:** use `progress_total` for end-to-end progress, and optionally use `progress` for per-file details.
|
||||
|
||||
```javascript
|
||||
const fileProgress = {};
|
||||
|
||||
const pipe = await pipeline('sentiment-analysis', null, {
|
||||
progress_callback: (info) => {
|
||||
// Recommended: end-to-end loading progress
|
||||
if (info.status === 'progress_total') {
|
||||
console.log(`Total: ${info.progress.toFixed(1)}%`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Optional: per-file progress
|
||||
if (info.status === 'progress') {
|
||||
fileProgress[info.file] = info.progress;
|
||||
console.log(`${info.file}: ${info.progress.toFixed(1)}%`);
|
||||
}
|
||||
|
||||
|
||||
if (info.status === 'done') {
|
||||
console.log(`✓ ${info.file} complete`);
|
||||
}
|
||||
@@ -84,10 +91,10 @@ const pipe = await pipeline('sentiment-analysis', null, {
|
||||
|
||||
```typescript
|
||||
type ProgressInfo = {
|
||||
status: 'initiate' | 'download' | 'progress' | 'done' | 'ready';
|
||||
status: 'initiate' | 'download' | 'progress' | 'progress_total' | 'done' | 'ready';
|
||||
name: string; // Model id or path
|
||||
file: string; // File being processed
|
||||
progress?: number; // Percentage (0-100, only for 'progress' status)
|
||||
file?: string; // File being processed (per-file events)
|
||||
progress?: number; // Percentage (0-100, for 'progress' and 'progress_total')
|
||||
loaded?: number; // Bytes downloaded (only for 'progress' status)
|
||||
total?: number; // Total bytes (only for 'progress' status)
|
||||
};
|
||||
@@ -102,6 +109,11 @@ const fileProgressBars = {};
|
||||
|
||||
const pipe = await pipeline('image-classification', null, {
|
||||
progress_callback: (info) => {
|
||||
if (info.status === 'progress_total') {
|
||||
statusDiv.textContent = `Total: ${info.progress.toFixed(1)}%`;
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.status === 'progress') {
|
||||
// Create progress bar for each file if not exists
|
||||
if (!fileProgressBars[info.file]) {
|
||||
@@ -115,15 +127,15 @@ const pipe = await pipeline('image-classification', null, {
|
||||
progressContainer.appendChild(fileDiv);
|
||||
fileProgressBars[info.file] = fileDiv.querySelector('.progress-fill');
|
||||
}
|
||||
|
||||
|
||||
// Update progress bar
|
||||
fileProgressBars[info.file].style.width = `${info.progress}%`;
|
||||
|
||||
|
||||
const mb = (info.loaded / 1024 / 1024).toFixed(2);
|
||||
const totalMb = (info.total / 1024 / 1024).toFixed(2);
|
||||
statusDiv.textContent = `${info.file}: ${mb}/${totalMb} MB`;
|
||||
}
|
||||
|
||||
|
||||
if (info.status === 'ready') {
|
||||
statusDiv.textContent = 'Model ready!';
|
||||
}
|
||||
@@ -289,7 +301,7 @@ const pipe = await pipeline('sentiment-analysis', 'model-id', {
|
||||
|
||||
**Common devices:**
|
||||
- `'wasm'` - WebAssembly (CPU, most compatible)
|
||||
- `'webgpu'` - WebGPU (GPU, faster in browsers)
|
||||
- `'webgpu'` - WebGPU (GPU acceleration in supported runtimes)
|
||||
- `'cpu'` - CPU
|
||||
- `'gpu'` - Auto-detect GPU
|
||||
- `'cuda'` - NVIDIA CUDA (Node.js with GPU)
|
||||
@@ -310,8 +322,8 @@ const pipe = await pipeline('automatic-speech-recognition', 'model-id', {
|
||||
```
|
||||
|
||||
**WebGPU Requirements:**
|
||||
- Chrome/Edge 113+
|
||||
- Enable chrome://flags/#enable-unsafe-webgpu (if needed)
|
||||
- Runtime with WebGPU support (browser, Node.js, Bun, or Deno)
|
||||
- Compatible hardware/driver stack
|
||||
- Adequate GPU memory
|
||||
|
||||
|
||||
@@ -464,8 +476,8 @@ import { pipeline } from '@huggingface/transformers';
|
||||
|
||||
const pipe = await pipeline('sentiment-analysis', null, {
|
||||
progress_callback: (info) => {
|
||||
if (info.status === 'progress') {
|
||||
console.log(`${info.file}: ${info.progress.toFixed(1)}%`);
|
||||
if (info.status === 'progress_total') {
|
||||
console.log(`Total: ${info.progress.toFixed(1)}%`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -74,19 +74,19 @@ await generator('Tell me a story', {
|
||||
<div id="output"></div>
|
||||
|
||||
<script type="module">
|
||||
import { pipeline, TextStreamer } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.8.1';
|
||||
|
||||
import { pipeline, TextStreamer } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@4';
|
||||
|
||||
const generator = await pipeline(
|
||||
'text-generation',
|
||||
'onnx-community/Qwen2.5-0.5B-Instruct',
|
||||
{ dtype: 'q4' }
|
||||
);
|
||||
|
||||
|
||||
window.generate = async function() {
|
||||
const prompt = document.getElementById('prompt').value;
|
||||
const outputDiv = document.getElementById('output');
|
||||
outputDiv.textContent = '';
|
||||
|
||||
|
||||
const streamer = new TextStreamer(generator.tokenizer, {
|
||||
skip_prompt: true,
|
||||
skip_special_tokens: true,
|
||||
@@ -94,7 +94,7 @@ await generator('Tell me a story', {
|
||||
outputDiv.textContent += token;
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
await generator(prompt, {
|
||||
max_new_tokens: 200,
|
||||
temperature: 0.7,
|
||||
@@ -119,10 +119,10 @@ function StreamingGenerator() {
|
||||
|
||||
const handleGenerate = async (prompt) => {
|
||||
if (!prompt) return;
|
||||
|
||||
|
||||
setLoading(true);
|
||||
setOutput('');
|
||||
|
||||
|
||||
// Load model on first generate
|
||||
if (!generatorRef.current) {
|
||||
generatorRef.current = await pipeline(
|
||||
@@ -131,7 +131,7 @@ function StreamingGenerator() {
|
||||
{ dtype: 'q4' }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
const streamer = new TextStreamer(generatorRef.current.tokenizer, {
|
||||
skip_prompt: true,
|
||||
skip_special_tokens: true,
|
||||
@@ -145,7 +145,7 @@ function StreamingGenerator() {
|
||||
temperature: 0.7,
|
||||
streamer,
|
||||
});
|
||||
|
||||
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
@@ -225,17 +225,17 @@ await generator(prompt, {
|
||||
// Token limits
|
||||
max_new_tokens: 512, // Maximum tokens to generate
|
||||
min_new_tokens: 0, // Minimum tokens to generate
|
||||
|
||||
|
||||
// Sampling
|
||||
temperature: 0.7, // Randomness (0.0-2.0)
|
||||
top_k: 50, // Consider top K tokens
|
||||
top_p: 0.95, // Nucleus sampling
|
||||
do_sample: true, // Use random sampling (false = always pick most likely token)
|
||||
|
||||
|
||||
// Repetition control
|
||||
repetition_penalty: 1.0, // Penalty for repeating (1.0 = no penalty)
|
||||
no_repeat_ngram_size: 0, // Prevent repeating n-grams
|
||||
|
||||
|
||||
// Streaming
|
||||
streamer: streamer, // TextStreamer instance
|
||||
});
|
||||
@@ -260,23 +260,23 @@ await generator(prompt, { temperature: 1.2, max_new_tokens: 100 });
|
||||
|
||||
```javascript
|
||||
// Greedy (deterministic)
|
||||
await generator(prompt, {
|
||||
await generator(prompt, {
|
||||
do_sample: false,
|
||||
max_new_tokens: 100
|
||||
max_new_tokens: 100
|
||||
});
|
||||
|
||||
// Top-k sampling
|
||||
await generator(prompt, {
|
||||
await generator(prompt, {
|
||||
top_k: 50,
|
||||
temperature: 0.7,
|
||||
max_new_tokens: 100
|
||||
max_new_tokens: 100
|
||||
});
|
||||
|
||||
// Top-p (nucleus) sampling
|
||||
await generator(prompt, {
|
||||
await generator(prompt, {
|
||||
top_p: 0.95,
|
||||
temperature: 0.7,
|
||||
max_new_tokens: 100
|
||||
max_new_tokens: 100
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user