@bgicli/bgicli 2.2.8 → 2.2.10

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.
Files changed (113) hide show
  1. package/data/skills/anthropic-algorithmic-art/SKILL.md +405 -0
  2. package/data/skills/anthropic-canvas-design/SKILL.md +130 -0
  3. package/data/skills/anthropic-claude-api/SKILL.md +243 -0
  4. package/data/skills/anthropic-doc-coauthoring/SKILL.md +375 -0
  5. package/data/skills/anthropic-docx/SKILL.md +590 -0
  6. package/data/skills/anthropic-frontend-design/SKILL.md +42 -0
  7. package/data/skills/anthropic-internal-comms/SKILL.md +32 -0
  8. package/data/skills/anthropic-mcp-builder/SKILL.md +236 -0
  9. package/data/skills/anthropic-pdf/SKILL.md +314 -0
  10. package/data/skills/anthropic-pptx/SKILL.md +232 -0
  11. package/data/skills/anthropic-skill-creator/SKILL.md +485 -0
  12. package/data/skills/anthropic-webapp-testing/SKILL.md +96 -0
  13. package/data/skills/anthropic-xlsx/SKILL.md +292 -0
  14. package/data/skills/arxiv-database/SKILL.md +362 -0
  15. package/data/skills/astropy/SKILL.md +329 -0
  16. package/data/skills/ctx-advanced-evaluation/SKILL.md +402 -0
  17. package/data/skills/ctx-bdi-mental-states/SKILL.md +311 -0
  18. package/data/skills/ctx-context-compression/SKILL.md +272 -0
  19. package/data/skills/ctx-context-degradation/SKILL.md +206 -0
  20. package/data/skills/ctx-context-fundamentals/SKILL.md +201 -0
  21. package/data/skills/ctx-context-optimization/SKILL.md +195 -0
  22. package/data/skills/ctx-evaluation/SKILL.md +251 -0
  23. package/data/skills/ctx-filesystem-context/SKILL.md +287 -0
  24. package/data/skills/ctx-hosted-agents/SKILL.md +260 -0
  25. package/data/skills/ctx-memory-systems/SKILL.md +225 -0
  26. package/data/skills/ctx-multi-agent-patterns/SKILL.md +257 -0
  27. package/data/skills/ctx-project-development/SKILL.md +291 -0
  28. package/data/skills/ctx-tool-design/SKILL.md +271 -0
  29. package/data/skills/dhdna-profiler/SKILL.md +162 -0
  30. package/data/skills/generate-image/SKILL.md +183 -0
  31. package/data/skills/geomaster/SKILL.md +365 -0
  32. package/data/skills/get-available-resources/SKILL.md +275 -0
  33. package/data/skills/hamelsmu-build-review-interface/SKILL.md +96 -0
  34. package/data/skills/hamelsmu-error-analysis/SKILL.md +164 -0
  35. package/data/skills/hamelsmu-eval-audit/SKILL.md +183 -0
  36. package/data/skills/hamelsmu-evaluate-rag/SKILL.md +177 -0
  37. package/data/skills/hamelsmu-generate-synthetic-data/SKILL.md +131 -0
  38. package/data/skills/hamelsmu-validate-evaluator/SKILL.md +212 -0
  39. package/data/skills/hamelsmu-write-judge-prompt/SKILL.md +144 -0
  40. package/data/skills/hf-cli/SKILL.md +174 -0
  41. package/data/skills/hf-mcp/SKILL.md +178 -0
  42. package/data/skills/hugging-face-dataset-viewer/SKILL.md +121 -0
  43. package/data/skills/hugging-face-datasets/SKILL.md +542 -0
  44. package/data/skills/hugging-face-evaluation/SKILL.md +651 -0
  45. package/data/skills/hugging-face-jobs/SKILL.md +1042 -0
  46. package/data/skills/hugging-face-model-trainer/SKILL.md +717 -0
  47. package/data/skills/hugging-face-paper-pages/SKILL.md +239 -0
  48. package/data/skills/hugging-face-paper-publisher/SKILL.md +624 -0
  49. package/data/skills/hugging-face-tool-builder/SKILL.md +110 -0
  50. package/data/skills/hugging-face-trackio/SKILL.md +115 -0
  51. package/data/skills/hugging-face-vision-trainer/SKILL.md +593 -0
  52. package/data/skills/huggingface-gradio/SKILL.md +245 -0
  53. package/data/skills/matlab/SKILL.md +376 -0
  54. package/data/skills/modal/SKILL.md +381 -0
  55. package/data/skills/openai-cloudflare-deploy/SKILL.md +224 -0
  56. package/data/skills/openai-develop-web-game/SKILL.md +149 -0
  57. package/data/skills/openai-doc/SKILL.md +80 -0
  58. package/data/skills/openai-figma/SKILL.md +42 -0
  59. package/data/skills/openai-figma-implement-design/SKILL.md +264 -0
  60. package/data/skills/openai-gh-address-comments/SKILL.md +25 -0
  61. package/data/skills/openai-gh-fix-ci/SKILL.md +69 -0
  62. package/data/skills/openai-imagegen/SKILL.md +174 -0
  63. package/data/skills/openai-jupyter-notebook/SKILL.md +107 -0
  64. package/data/skills/openai-linear/SKILL.md +87 -0
  65. package/data/skills/openai-netlify-deploy/SKILL.md +247 -0
  66. package/data/skills/openai-notion-knowledge-capture/SKILL.md +56 -0
  67. package/data/skills/openai-notion-meeting-intelligence/SKILL.md +60 -0
  68. package/data/skills/openai-notion-research-documentation/SKILL.md +59 -0
  69. package/data/skills/openai-notion-spec-to-implementation/SKILL.md +58 -0
  70. package/data/skills/openai-openai-docs/SKILL.md +69 -0
  71. package/data/skills/openai-pdf/SKILL.md +67 -0
  72. package/data/skills/openai-playwright/SKILL.md +147 -0
  73. package/data/skills/openai-render-deploy/SKILL.md +479 -0
  74. package/data/skills/openai-screenshot/SKILL.md +267 -0
  75. package/data/skills/openai-security-best-practices/SKILL.md +86 -0
  76. package/data/skills/openai-security-ownership-map/SKILL.md +206 -0
  77. package/data/skills/openai-security-threat-model/SKILL.md +81 -0
  78. package/data/skills/openai-sentry/SKILL.md +123 -0
  79. package/data/skills/openai-sora/SKILL.md +178 -0
  80. package/data/skills/openai-speech/SKILL.md +144 -0
  81. package/data/skills/openai-spreadsheet/SKILL.md +145 -0
  82. package/data/skills/openai-transcribe/SKILL.md +81 -0
  83. package/data/skills/openai-vercel-deploy/SKILL.md +77 -0
  84. package/data/skills/openai-yeet/SKILL.md +28 -0
  85. package/data/skills/pennylane/SKILL.md +224 -0
  86. package/data/skills/polars-bio/SKILL.md +374 -0
  87. package/data/skills/primekg/SKILL.md +97 -0
  88. package/data/skills/pymatgen/SKILL.md +689 -0
  89. package/data/skills/qiskit/SKILL.md +273 -0
  90. package/data/skills/qutip/SKILL.md +316 -0
  91. package/data/skills/recursive-decomposition/SKILL.md +185 -0
  92. package/data/skills/rowan/SKILL.md +427 -0
  93. package/data/skills/scholar-evaluation/SKILL.md +298 -0
  94. package/data/skills/sentry-create-alert/SKILL.md +210 -0
  95. package/data/skills/sentry-fix-issues/SKILL.md +126 -0
  96. package/data/skills/sentry-pr-code-review/SKILL.md +105 -0
  97. package/data/skills/sentry-python-sdk/SKILL.md +317 -0
  98. package/data/skills/sentry-setup-ai-monitoring/SKILL.md +217 -0
  99. package/data/skills/stable-baselines3/SKILL.md +297 -0
  100. package/data/skills/sympy/SKILL.md +498 -0
  101. package/data/skills/trailofbits-ask-questions-if-underspecified/SKILL.md +85 -0
  102. package/data/skills/trailofbits-audit-context-building/SKILL.md +302 -0
  103. package/data/skills/trailofbits-differential-review/SKILL.md +220 -0
  104. package/data/skills/trailofbits-insecure-defaults/SKILL.md +117 -0
  105. package/data/skills/trailofbits-modern-python/SKILL.md +333 -0
  106. package/data/skills/trailofbits-property-based-testing/SKILL.md +123 -0
  107. package/data/skills/trailofbits-semgrep-rule-creator/SKILL.md +172 -0
  108. package/data/skills/trailofbits-sharp-edges/SKILL.md +292 -0
  109. package/data/skills/trailofbits-variant-analysis/SKILL.md +142 -0
  110. package/data/skills/transformers.js/SKILL.md +637 -0
  111. package/data/skills/writing/SKILL.md +419 -0
  112. package/dist/bgi.js +66 -2
  113. package/package.json +1 -1
@@ -0,0 +1,637 @@
1
+ ---
2
+ name: transformers-js
3
+ 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 multimodal tasks. Works in Node.js and browsers (with WebGPU/WASM) using pre-trained models from Hugging Face Hub.
4
+ license: Apache-2.0
5
+ metadata:
6
+ author: huggingface
7
+ version: "3.8.1"
8
+ category: machine-learning
9
+ repository: https://github.com/huggingface/transformers.js
10
+ 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).
11
+ ---
12
+
13
+ # Transformers.js - Machine Learning for JavaScript
14
+
15
+ 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.
16
+
17
+ ## When to Use This Skill
18
+
19
+ Use this skill when you need to:
20
+ - Run ML models for text analysis, generation, or translation in JavaScript
21
+ - Perform image classification, object detection, or segmentation
22
+ - Implement speech recognition or audio processing
23
+ - Build multimodal AI applications (text-to-image, image-to-text, etc.)
24
+ - Run models client-side in the browser without a backend
25
+
26
+ ## Installation
27
+
28
+ ### NPM Installation
29
+ ```bash
30
+ npm install @huggingface/transformers
31
+ ```
32
+
33
+ ### Browser Usage (CDN)
34
+ ```javascript
35
+ <script type="module">
36
+ import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers';
37
+ </script>
38
+ ```
39
+
40
+ ## Core Concepts
41
+
42
+ ### 1. Pipeline API
43
+ The pipeline API is the easiest way to use models. It groups together preprocessing, model inference, and postprocessing:
44
+
45
+ ```javascript
46
+ import { pipeline } from '@huggingface/transformers';
47
+
48
+ // Create a pipeline for a specific task
49
+ const pipe = await pipeline('sentiment-analysis');
50
+
51
+ // Use the pipeline
52
+ const result = await pipe('I love transformers!');
53
+ // Output: [{ label: 'POSITIVE', score: 0.999817686 }]
54
+
55
+ // IMPORTANT: Always dispose when done to free memory
56
+ await classifier.dispose();
57
+ ```
58
+
59
+ **⚠️ 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.
60
+
61
+ ### 2. Model Selection
62
+ You can specify a custom model as the second argument:
63
+
64
+ ```javascript
65
+ const pipe = await pipeline(
66
+ 'sentiment-analysis',
67
+ 'Xenova/bert-base-multilingual-uncased-sentiment'
68
+ );
69
+ ```
70
+
71
+ **Finding Models:**
72
+
73
+ Browse available Transformers.js models on Hugging Face Hub:
74
+ - **All models**: https://huggingface.co/models?library=transformers.js&sort=trending
75
+ - **By task**: Add `pipeline_tag` parameter
76
+ - Text generation: https://huggingface.co/models?pipeline_tag=text-generation&library=transformers.js&sort=trending
77
+ - Image classification: https://huggingface.co/models?pipeline_tag=image-classification&library=transformers.js&sort=trending
78
+ - Speech recognition: https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&library=transformers.js&sort=trending
79
+
80
+ **Tip:** Filter by task type, sort by trending/downloads, and check model cards for performance metrics and usage examples.
81
+
82
+ ### 3. Device Selection
83
+ Choose where to run the model:
84
+
85
+ ```javascript
86
+ // Run on CPU (default for WASM)
87
+ const pipe = await pipeline('sentiment-analysis', 'model-id');
88
+
89
+ // Run on GPU (WebGPU - experimental)
90
+ const pipe = await pipeline('sentiment-analysis', 'model-id', {
91
+ device: 'webgpu',
92
+ });
93
+ ```
94
+
95
+ ### 4. Quantization Options
96
+ Control model precision vs. performance:
97
+
98
+ ```javascript
99
+ // Use quantized model (faster, smaller)
100
+ const pipe = await pipeline('sentiment-analysis', 'model-id', {
101
+ dtype: 'q4', // Options: 'fp32', 'fp16', 'q8', 'q4'
102
+ });
103
+ ```
104
+
105
+ ## Supported Tasks
106
+
107
+ **Note:** All examples below show basic usage.
108
+
109
+ ### Natural Language Processing
110
+
111
+ #### Text Classification
112
+ ```javascript
113
+ const classifier = await pipeline('text-classification');
114
+ const result = await classifier('This movie was amazing!');
115
+ ```
116
+
117
+ #### Named Entity Recognition (NER)
118
+ ```javascript
119
+ const ner = await pipeline('token-classification');
120
+ const entities = await ner('My name is John and I live in New York.');
121
+ ```
122
+
123
+ #### Question Answering
124
+ ```javascript
125
+ const qa = await pipeline('question-answering');
126
+ const answer = await qa({
127
+ question: 'What is the capital of France?',
128
+ context: 'Paris is the capital and largest city of France.'
129
+ });
130
+ ```
131
+
132
+ #### Text Generation
133
+ ```javascript
134
+ const generator = await pipeline('text-generation', 'onnx-community/gemma-3-270m-it-ONNX');
135
+ const text = await generator('Once upon a time', {
136
+ max_new_tokens: 100,
137
+ temperature: 0.7
138
+ });
139
+ ```
140
+
141
+ **For streaming and chat:** See **[Text Generation Guide](./references/TEXT_GENERATION.md)** for:
142
+ - Streaming token-by-token output with `TextStreamer`
143
+ - Chat/conversation format with system/user/assistant roles
144
+ - Generation parameters (temperature, top_k, top_p)
145
+ - Browser and Node.js examples
146
+ - React components and API endpoints
147
+
148
+ #### Translation
149
+ ```javascript
150
+ const translator = await pipeline('translation', 'Xenova/nllb-200-distilled-600M');
151
+ const output = await translator('Hello, how are you?', {
152
+ src_lang: 'eng_Latn',
153
+ tgt_lang: 'fra_Latn'
154
+ });
155
+ ```
156
+
157
+ #### Summarization
158
+ ```javascript
159
+ const summarizer = await pipeline('summarization');
160
+ const summary = await summarizer(longText, {
161
+ max_length: 100,
162
+ min_length: 30
163
+ });
164
+ ```
165
+
166
+ #### Zero-Shot Classification
167
+ ```javascript
168
+ const classifier = await pipeline('zero-shot-classification');
169
+ const result = await classifier('This is a story about sports.', ['politics', 'sports', 'technology']);
170
+ ```
171
+
172
+ ### Computer Vision
173
+
174
+ #### Image Classification
175
+ ```javascript
176
+ const classifier = await pipeline('image-classification');
177
+ const result = await classifier('https://example.com/image.jpg');
178
+ // Or with local file
179
+ const result = await classifier(imageUrl);
180
+ ```
181
+
182
+ #### Object Detection
183
+ ```javascript
184
+ const detector = await pipeline('object-detection');
185
+ const objects = await detector('https://example.com/image.jpg');
186
+ // Returns: [{ label: 'person', score: 0.95, box: { xmin, ymin, xmax, ymax } }, ...]
187
+ ```
188
+
189
+ #### Image Segmentation
190
+ ```javascript
191
+ const segmenter = await pipeline('image-segmentation');
192
+ const segments = await segmenter('https://example.com/image.jpg');
193
+ ```
194
+
195
+ #### Depth Estimation
196
+ ```javascript
197
+ const depthEstimator = await pipeline('depth-estimation');
198
+ const depth = await depthEstimator('https://example.com/image.jpg');
199
+ ```
200
+
201
+ #### Zero-Shot Image Classification
202
+ ```javascript
203
+ const classifier = await pipeline('zero-shot-image-classification');
204
+ const result = await classifier('image.jpg', ['cat', 'dog', 'bird']);
205
+ ```
206
+
207
+ ### Audio Processing
208
+
209
+ #### Automatic Speech Recognition
210
+ ```javascript
211
+ const transcriber = await pipeline('automatic-speech-recognition');
212
+ const result = await transcriber('audio.wav');
213
+ // Returns: { text: 'transcribed text here' }
214
+ ```
215
+
216
+ #### Audio Classification
217
+ ```javascript
218
+ const classifier = await pipeline('audio-classification');
219
+ const result = await classifier('audio.wav');
220
+ ```
221
+
222
+ #### Text-to-Speech
223
+ ```javascript
224
+ const synthesizer = await pipeline('text-to-speech', 'Xenova/speecht5_tts');
225
+ const audio = await synthesizer('Hello, this is a test.', {
226
+ speaker_embeddings: speakerEmbeddings
227
+ });
228
+ ```
229
+
230
+ ### Multimodal
231
+
232
+ #### Image-to-Text (Image Captioning)
233
+ ```javascript
234
+ const captioner = await pipeline('image-to-text');
235
+ const caption = await captioner('image.jpg');
236
+ ```
237
+
238
+ #### Document Question Answering
239
+ ```javascript
240
+ const docQA = await pipeline('document-question-answering');
241
+ const answer = await docQA('document-image.jpg', 'What is the total amount?');
242
+ ```
243
+
244
+ #### Zero-Shot Object Detection
245
+ ```javascript
246
+ const detector = await pipeline('zero-shot-object-detection');
247
+ const objects = await detector('image.jpg', ['person', 'car', 'tree']);
248
+ ```
249
+
250
+ ### Feature Extraction (Embeddings)
251
+
252
+ ```javascript
253
+ const extractor = await pipeline('feature-extraction');
254
+ const embeddings = await extractor('This is a sentence to embed.');
255
+ // Returns: tensor of shape [1, sequence_length, hidden_size]
256
+
257
+ // For sentence embeddings (mean pooling)
258
+ const extractor = await pipeline('feature-extraction', 'onnx-community/all-MiniLM-L6-v2-ONNX');
259
+ const embeddings = await extractor('Text to embed', { pooling: 'mean', normalize: true });
260
+ ```
261
+
262
+ ## Finding and Choosing Models
263
+
264
+ ### Browsing the Hugging Face Hub
265
+
266
+ Discover compatible Transformers.js models on Hugging Face Hub:
267
+
268
+ **Base URL (all models):**
269
+ ```
270
+ https://huggingface.co/models?library=transformers.js&sort=trending
271
+ ```
272
+
273
+ **Filter by task** using the `pipeline_tag` parameter:
274
+
275
+ | Task | URL |
276
+ |------|-----|
277
+ | **Text Generation** | https://huggingface.co/models?pipeline_tag=text-generation&library=transformers.js&sort=trending |
278
+ | **Text Classification** | https://huggingface.co/models?pipeline_tag=text-classification&library=transformers.js&sort=trending |
279
+ | **Translation** | https://huggingface.co/models?pipeline_tag=translation&library=transformers.js&sort=trending |
280
+ | **Summarization** | https://huggingface.co/models?pipeline_tag=summarization&library=transformers.js&sort=trending |
281
+ | **Question Answering** | https://huggingface.co/models?pipeline_tag=question-answering&library=transformers.js&sort=trending |
282
+ | **Image Classification** | https://huggingface.co/models?pipeline_tag=image-classification&library=transformers.js&sort=trending |
283
+ | **Object Detection** | https://huggingface.co/models?pipeline_tag=object-detection&library=transformers.js&sort=trending |
284
+ | **Image Segmentation** | https://huggingface.co/models?pipeline_tag=image-segmentation&library=transformers.js&sort=trending |
285
+ | **Speech Recognition** | https://huggingface.co/models?pipeline_tag=automatic-speech-recognition&library=transformers.js&sort=trending |
286
+ | **Audio Classification** | https://huggingface.co/models?pipeline_tag=audio-classification&library=transformers.js&sort=trending |
287
+ | **Image-to-Text** | https://huggingface.co/models?pipeline_tag=image-to-text&library=transformers.js&sort=trending |
288
+ | **Feature Extraction** | https://huggingface.co/models?pipeline_tag=feature-extraction&library=transformers.js&sort=trending |
289
+ | **Zero-Shot Classification** | https://huggingface.co/models?pipeline_tag=zero-shot-classification&library=transformers.js&sort=trending |
290
+
291
+ **Sort options:**
292
+ - `&sort=trending` - Most popular recently
293
+ - `&sort=downloads` - Most downloaded overall
294
+ - `&sort=likes` - Most liked by community
295
+ - `&sort=modified` - Recently updated
296
+
297
+ ### Choosing the Right Model
298
+
299
+ Consider these factors when selecting a model:
300
+
301
+ **1. Model Size**
302
+ - **Small (< 100MB)**: Fast, suitable for browsers, limited accuracy
303
+ - **Medium (100MB - 500MB)**: Balanced performance, good for most use cases
304
+ - **Large (> 500MB)**: High accuracy, slower, better for Node.js or powerful devices
305
+
306
+ **2. Quantization**
307
+ Models are often available in different quantization levels:
308
+ - `fp32` - Full precision (largest, most accurate)
309
+ - `fp16` - Half precision (smaller, still accurate)
310
+ - `q8` - 8-bit quantized (much smaller, slight accuracy loss)
311
+ - `q4` - 4-bit quantized (smallest, noticeable accuracy loss)
312
+
313
+ **3. Task Compatibility**
314
+ Check the model card for:
315
+ - Supported tasks (some models support multiple tasks)
316
+ - Input/output formats
317
+ - Language support (multilingual vs. English-only)
318
+ - License restrictions
319
+
320
+ **4. Performance Metrics**
321
+ Model cards typically show:
322
+ - Accuracy scores
323
+ - Benchmark results
324
+ - Inference speed
325
+ - Memory requirements
326
+
327
+ ### Example: Finding a Text Generation Model
328
+
329
+ ```javascript
330
+ // 1. Visit: https://huggingface.co/models?pipeline_tag=text-generation&library=transformers.js&sort=trending
331
+
332
+ // 2. Browse and select a model (e.g., onnx-community/gemma-3-270m-it-ONNX)
333
+
334
+ // 3. Check model card for:
335
+ // - Model size: ~270M parameters
336
+ // - Quantization: q4 available
337
+ // - Language: English
338
+ // - Use case: Instruction-following chat
339
+
340
+ // 4. Use the model:
341
+ import { pipeline } from '@huggingface/transformers';
342
+
343
+ const generator = await pipeline(
344
+ 'text-generation',
345
+ 'onnx-community/gemma-3-270m-it-ONNX',
346
+ { dtype: 'q4' } // Use quantized version for faster inference
347
+ );
348
+
349
+ const output = await generator('Explain quantum computing in simple terms.', {
350
+ max_new_tokens: 100
351
+ });
352
+
353
+ await generator.dispose();
354
+ ```
355
+
356
+ ### Tips for Model Selection
357
+
358
+ 1. **Start Small**: Test with a smaller model first, then upgrade if needed
359
+ 2. **Check ONNX Support**: Ensure the model has ONNX files (look for `onnx` folder in model repo)
360
+ 3. **Read Model Cards**: Model cards contain usage examples, limitations, and benchmarks
361
+ 4. **Test Locally**: Benchmark inference speed and memory usage in your environment
362
+ 5. **Community Models**: Look for models by `Xenova` (Transformers.js maintainer) or `onnx-community`
363
+ 6. **Version Pin**: Use specific git commits in production for stability:
364
+ ```javascript
365
+ const pipe = await pipeline('task', 'model-id', { revision: 'abc123' });
366
+ ```
367
+
368
+ ## Advanced Configuration
369
+
370
+ ### Environment Configuration (`env`)
371
+
372
+ The `env` object provides comprehensive control over Transformers.js execution, caching, and model loading.
373
+
374
+ **Quick Overview:**
375
+
376
+ ```javascript
377
+ import { env } from '@huggingface/transformers';
378
+
379
+ // View version
380
+ console.log(env.version); // e.g., '3.8.1'
381
+
382
+ // Common settings
383
+ env.allowRemoteModels = true; // Load from Hugging Face Hub
384
+ env.allowLocalModels = false; // Load from file system
385
+ env.localModelPath = '/models/'; // Local model directory
386
+ env.useFSCache = true; // Cache models on disk (Node.js)
387
+ env.useBrowserCache = true; // Cache models in browser
388
+ env.cacheDir = './.cache'; // Cache directory location
389
+ ```
390
+
391
+ **Configuration Patterns:**
392
+
393
+ ```javascript
394
+ // Development: Fast iteration with remote models
395
+ env.allowRemoteModels = true;
396
+ env.useFSCache = true;
397
+
398
+ // Production: Local models only
399
+ env.allowRemoteModels = false;
400
+ env.allowLocalModels = true;
401
+ env.localModelPath = '/app/models/';
402
+
403
+ // Custom CDN
404
+ env.remoteHost = 'https://cdn.example.com/models';
405
+
406
+ // Disable caching (testing)
407
+ env.useFSCache = false;
408
+ env.useBrowserCache = false;
409
+ ```
410
+
411
+ For complete documentation on all configuration options, caching strategies, cache management, pre-downloading models, and more, see:
412
+
413
+ **→ [Configuration Reference](./references/CONFIGURATION.md)**
414
+
415
+ ### Working with Tensors
416
+
417
+ ```javascript
418
+ import { AutoTokenizer, AutoModel } from '@huggingface/transformers';
419
+
420
+ // Load tokenizer and model separately for more control
421
+ const tokenizer = await AutoTokenizer.from_pretrained('bert-base-uncased');
422
+ const model = await AutoModel.from_pretrained('bert-base-uncased');
423
+
424
+ // Tokenize input
425
+ const inputs = await tokenizer('Hello world!');
426
+
427
+ // Run model
428
+ const outputs = await model(inputs);
429
+ ```
430
+
431
+ ### Batch Processing
432
+
433
+ ```javascript
434
+ const classifier = await pipeline('sentiment-analysis');
435
+
436
+ // Process multiple texts
437
+ const results = await classifier([
438
+ 'I love this!',
439
+ 'This is terrible.',
440
+ 'It was okay.'
441
+ ]);
442
+ ```
443
+
444
+ ## Browser-Specific Considerations
445
+
446
+ ### WebGPU Usage
447
+ WebGPU provides GPU acceleration in browsers:
448
+
449
+ ```javascript
450
+ const pipe = await pipeline('text-generation', 'onnx-community/gemma-3-270m-it-ONNX', {
451
+ device: 'webgpu',
452
+ dtype: 'fp32'
453
+ });
454
+ ```
455
+
456
+ **Note**: WebGPU is experimental. Check browser compatibility and file issues if problems occur.
457
+
458
+ ### WASM Performance
459
+ Default browser execution uses WASM:
460
+
461
+ ```javascript
462
+ // Optimized for browsers with quantization
463
+ const pipe = await pipeline('sentiment-analysis', 'model-id', {
464
+ dtype: 'q8' // or 'q4' for even smaller size
465
+ });
466
+ ```
467
+
468
+ ### Progress Tracking & Loading Indicators
469
+
470
+ Models can be large (ranging from a few MB to several GB) and consist of multiple files. Track download progress by passing a callback to the `pipeline()` function:
471
+
472
+ ```javascript
473
+ import { pipeline } from '@huggingface/transformers';
474
+
475
+ // Track progress for each file
476
+ const fileProgress = {};
477
+
478
+ function onProgress(info) {
479
+ console.log(`${info.status}: ${info.file}`);
480
+
481
+ if (info.status === 'progress') {
482
+ fileProgress[info.file] = info.progress;
483
+ console.log(`${info.file}: ${info.progress.toFixed(1)}%`);
484
+ }
485
+
486
+ if (info.status === 'done') {
487
+ console.log(`✓ ${info.file} complete`);
488
+ }
489
+ }
490
+
491
+ // Pass callback to pipeline
492
+ const classifier = await pipeline('sentiment-analysis', null, {
493
+ progress_callback: onProgress
494
+ });
495
+ ```
496
+
497
+ **Progress Info Properties:**
498
+
499
+ ```typescript
500
+ interface ProgressInfo {
501
+ status: 'initiate' | 'download' | 'progress' | 'done' | 'ready';
502
+ name: string; // Model id or path
503
+ file: string; // File being processed
504
+ progress?: number; // Percentage (0-100, only for 'progress' status)
505
+ loaded?: number; // Bytes downloaded (only for 'progress' status)
506
+ total?: number; // Total bytes (only for 'progress' status)
507
+ }
508
+ ```
509
+
510
+ For complete examples including browser UIs, React components, CLI progress bars, and retry logic, see:
511
+
512
+ **→ [Pipeline Options - Progress Callback](./references/PIPELINE_OPTIONS.md#progress-callback)**
513
+
514
+ ## Error Handling
515
+
516
+ ```javascript
517
+ try {
518
+ const pipe = await pipeline('sentiment-analysis', 'model-id');
519
+ const result = await pipe('text to analyze');
520
+ } catch (error) {
521
+ if (error.message.includes('fetch')) {
522
+ console.error('Model download failed. Check internet connection.');
523
+ } else if (error.message.includes('ONNX')) {
524
+ console.error('Model execution failed. Check model compatibility.');
525
+ } else {
526
+ console.error('Unknown error:', error);
527
+ }
528
+ }
529
+ ```
530
+
531
+ ## Performance Tips
532
+
533
+ 1. **Reuse Pipelines**: Create pipeline once, reuse for multiple inferences
534
+ 2. **Use Quantization**: Start with `q8` or `q4` for faster inference
535
+ 3. **Batch Processing**: Process multiple inputs together when possible
536
+ 4. **Cache Models**: Models are cached automatically (see **[Caching Reference](./references/CACHE.md)** for details on browser Cache API, Node.js filesystem cache, and custom implementations)
537
+ 5. **WebGPU for Large Models**: Use WebGPU for models that benefit from GPU acceleration
538
+ 6. **Prune Context**: For text generation, limit `max_new_tokens` to avoid memory issues
539
+ 7. **Clean Up Resources**: Call `pipe.dispose()` when done to free memory
540
+
541
+ ## Memory Management
542
+
543
+ **IMPORTANT:** Always call `pipe.dispose()` when finished to prevent memory leaks.
544
+
545
+ ```javascript
546
+ const pipe = await pipeline('sentiment-analysis');
547
+ const result = await pipe('Great product!');
548
+ await pipe.dispose(); // ✓ Free memory (100MB - several GB per model)
549
+ ```
550
+
551
+ **When to dispose:**
552
+ - Application shutdown or component unmount
553
+ - Before loading a different model
554
+ - After batch processing in long-running apps
555
+
556
+ Models consume significant memory and hold GPU/CPU resources. Disposal is critical for browser memory limits and server stability.
557
+
558
+ For detailed patterns (React cleanup, servers, browser), see **[Code Examples](./references/EXAMPLES.md)**
559
+
560
+ ## Troubleshooting
561
+
562
+ ### Model Not Found
563
+ - Verify model exists on Hugging Face Hub
564
+ - Check model name spelling
565
+ - Ensure model has ONNX files (look for `onnx` folder in model repo)
566
+
567
+ ### Memory Issues
568
+ - Use smaller models or quantized versions (`dtype: 'q4'`)
569
+ - Reduce batch size
570
+ - Limit sequence length with `max_length`
571
+
572
+ ### WebGPU Errors
573
+ - Check browser compatibility (Chrome 113+, Edge 113+)
574
+ - Try `dtype: 'fp16'` if `fp32` fails
575
+ - Fall back to WASM if WebGPU unavailable
576
+
577
+ ## Reference Documentation
578
+
579
+ ### This Skill
580
+ - **[Pipeline Options](./references/PIPELINE_OPTIONS.md)** - Configure `pipeline()` with `progress_callback`, `device`, `dtype`, etc.
581
+ - **[Configuration Reference](./references/CONFIGURATION.md)** - Global `env` configuration for caching and model loading
582
+ - **[Caching Reference](./references/CACHE.md)** - Browser Cache API, Node.js filesystem cache, and custom cache implementations
583
+ - **[Text Generation Guide](./references/TEXT_GENERATION.md)** - Streaming, chat format, and generation parameters
584
+ - **[Model Architectures](./references/MODEL_ARCHITECTURES.md)** - Supported models and selection tips
585
+ - **[Code Examples](./references/EXAMPLES.md)** - Real-world implementations for different runtimes
586
+
587
+ ### Official Transformers.js
588
+ - Official docs: https://huggingface.co/docs/transformers.js
589
+ - API reference: https://huggingface.co/docs/transformers.js/api/pipelines
590
+ - Model hub: https://huggingface.co/models?library=transformers.js
591
+ - GitHub: https://github.com/huggingface/transformers.js
592
+ - Examples: https://github.com/huggingface/transformers.js/tree/main/examples
593
+
594
+ ## Best Practices
595
+
596
+ 1. **Always Dispose Pipelines**: Call `pipe.dispose()` when done - critical for preventing memory leaks
597
+ 2. **Start with Pipelines**: Use the pipeline API unless you need fine-grained control
598
+ 3. **Test Locally First**: Test models with small inputs before deploying
599
+ 4. **Monitor Model Sizes**: Be aware of model download sizes for web applications
600
+ 5. **Handle Loading States**: Show progress indicators for better UX
601
+ 6. **Version Pin**: Pin specific model versions for production stability
602
+ 7. **Error Boundaries**: Always wrap pipeline calls in try-catch blocks
603
+ 8. **Progressive Enhancement**: Provide fallbacks for unsupported browsers
604
+ 9. **Reuse Models**: Load once, use many times - don't recreate pipelines unnecessarily
605
+ 10. **Graceful Shutdown**: Dispose models on SIGTERM/SIGINT in servers
606
+
607
+ ## Quick Reference: Task IDs
608
+
609
+ | Task | Task ID |
610
+ |------|---------|
611
+ | Text classification | `text-classification` or `sentiment-analysis` |
612
+ | Token classification | `token-classification` or `ner` |
613
+ | Question answering | `question-answering` |
614
+ | Fill mask | `fill-mask` |
615
+ | Summarization | `summarization` |
616
+ | Translation | `translation` |
617
+ | Text generation | `text-generation` |
618
+ | Text-to-text generation | `text2text-generation` |
619
+ | Zero-shot classification | `zero-shot-classification` |
620
+ | Image classification | `image-classification` |
621
+ | Image segmentation | `image-segmentation` |
622
+ | Object detection | `object-detection` |
623
+ | Depth estimation | `depth-estimation` |
624
+ | Image-to-image | `image-to-image` |
625
+ | Zero-shot image classification | `zero-shot-image-classification` |
626
+ | Zero-shot object detection | `zero-shot-object-detection` |
627
+ | Automatic speech recognition | `automatic-speech-recognition` |
628
+ | Audio classification | `audio-classification` |
629
+ | Text-to-speech | `text-to-speech` or `text-to-audio` |
630
+ | Image-to-text | `image-to-text` |
631
+ | Document question answering | `document-question-answering` |
632
+ | Feature extraction | `feature-extraction` |
633
+ | Sentence similarity | `sentence-similarity` |
634
+
635
+ ---
636
+
637
+ 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.