rag-lite-ts 2.3.0 → 2.3.1
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/cjs/cli/search.js +77 -2
- package/dist/cjs/cli.js +28 -1
- package/dist/cjs/core/abstract-generator.d.ts +97 -0
- package/dist/cjs/core/abstract-generator.js +222 -0
- package/dist/cjs/core/binary-index-format.js +47 -7
- package/dist/cjs/core/generator-registry.d.ts +114 -0
- package/dist/cjs/core/generator-registry.js +280 -0
- package/dist/cjs/core/index.d.ts +4 -0
- package/dist/cjs/core/index.js +11 -0
- package/dist/cjs/core/lazy-dependency-loader.d.ts +43 -0
- package/dist/cjs/core/lazy-dependency-loader.js +111 -2
- package/dist/cjs/core/prompt-templates.d.ts +138 -0
- package/dist/cjs/core/prompt-templates.js +225 -0
- package/dist/cjs/core/response-generator.d.ts +132 -0
- package/dist/cjs/core/response-generator.js +69 -0
- package/dist/cjs/core/search.d.ts +72 -1
- package/dist/cjs/core/search.js +79 -6
- package/dist/cjs/core/types.d.ts +1 -0
- package/dist/cjs/core/vector-index-worker.js +10 -0
- package/dist/cjs/core/vector-index.js +69 -19
- package/dist/cjs/factories/generator-factory.d.ts +88 -0
- package/dist/cjs/factories/generator-factory.js +151 -0
- package/dist/cjs/factories/index.d.ts +1 -0
- package/dist/cjs/factories/index.js +5 -0
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +16 -0
- package/dist/cjs/text/generators/causal-lm-generator.d.ts +65 -0
- package/dist/cjs/text/generators/causal-lm-generator.js +197 -0
- package/dist/cjs/text/generators/index.d.ts +10 -0
- package/dist/cjs/text/generators/index.js +10 -0
- package/dist/cjs/text/generators/instruct-generator.d.ts +62 -0
- package/dist/cjs/text/generators/instruct-generator.js +192 -0
- package/dist/esm/cli/search.js +77 -2
- package/dist/esm/cli.js +28 -1
- package/dist/esm/core/abstract-generator.d.ts +97 -0
- package/dist/esm/core/abstract-generator.js +222 -0
- package/dist/esm/core/binary-index-format.js +47 -7
- package/dist/esm/core/generator-registry.d.ts +114 -0
- package/dist/esm/core/generator-registry.js +280 -0
- package/dist/esm/core/index.d.ts +4 -0
- package/dist/esm/core/index.js +11 -0
- package/dist/esm/core/lazy-dependency-loader.d.ts +43 -0
- package/dist/esm/core/lazy-dependency-loader.js +111 -2
- package/dist/esm/core/prompt-templates.d.ts +138 -0
- package/dist/esm/core/prompt-templates.js +225 -0
- package/dist/esm/core/response-generator.d.ts +132 -0
- package/dist/esm/core/response-generator.js +69 -0
- package/dist/esm/core/search.d.ts +72 -1
- package/dist/esm/core/search.js +79 -6
- package/dist/esm/core/types.d.ts +1 -0
- package/dist/esm/core/vector-index-worker.js +10 -0
- package/dist/esm/core/vector-index.js +69 -19
- package/dist/esm/factories/generator-factory.d.ts +88 -0
- package/dist/esm/factories/generator-factory.js +151 -0
- package/dist/esm/factories/index.d.ts +1 -0
- package/dist/esm/factories/index.js +5 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/text/generators/causal-lm-generator.d.ts +65 -0
- package/dist/esm/text/generators/causal-lm-generator.js +197 -0
- package/dist/esm/text/generators/index.d.ts +10 -0
- package/dist/esm/text/generators/index.js +10 -0
- package/dist/esm/text/generators/instruct-generator.d.ts +62 -0
- package/dist/esm/text/generators/instruct-generator.js +192 -0
- package/package.json +1 -1
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import { Worker } from 'worker_threads';
|
|
8
8
|
import { existsSync } from 'fs';
|
|
9
9
|
import { fileURLToPath } from 'url';
|
|
10
|
-
import { dirname, join } from 'path';
|
|
10
|
+
import { dirname, join, resolve } from 'path';
|
|
11
11
|
import { handleError, ErrorCategory, ErrorSeverity, createError } from './error-handler.js';
|
|
12
12
|
import { createMissingFileError, createDimensionMismatchError } from './actionable-error-messages.js';
|
|
13
13
|
export class VectorIndex {
|
|
@@ -34,20 +34,40 @@ export class VectorIndex {
|
|
|
34
34
|
const currentFile = fileURLToPath(import.meta.url);
|
|
35
35
|
const currentDir = dirname(currentFile);
|
|
36
36
|
// Always prefer .js (compiled output)
|
|
37
|
-
const jsPath = join(currentDir, 'vector-index-worker.js');
|
|
37
|
+
const jsPath = resolve(join(currentDir, 'vector-index-worker.js'));
|
|
38
38
|
// Check if .js exists in current directory (compiled)
|
|
39
39
|
if (existsSync(jsPath)) {
|
|
40
40
|
return jsPath;
|
|
41
41
|
}
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
42
|
+
// Helper function to find project root
|
|
43
|
+
const findProjectRoot = () => {
|
|
44
|
+
let dir = currentDir;
|
|
45
|
+
// Look for dist/ in the path
|
|
46
|
+
const distMatch = dir.match(/^(.+?)[\\/]dist[\\/]/);
|
|
47
|
+
if (distMatch) {
|
|
48
|
+
return distMatch[1];
|
|
49
|
+
}
|
|
50
|
+
// If no dist/, try going up from src/core
|
|
51
|
+
const srcMatch = dir.match(/^(.+?)[\\/]src[\\/]core/);
|
|
52
|
+
if (srcMatch) {
|
|
53
|
+
return srcMatch[1];
|
|
54
|
+
}
|
|
55
|
+
// If in node_modules, extract package root
|
|
56
|
+
const nodeModulesMatch = dir.match(/^(.+?)[\\/]node_modules/);
|
|
57
|
+
if (nodeModulesMatch) {
|
|
58
|
+
return nodeModulesMatch[1];
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
};
|
|
62
|
+
const projectRoot = findProjectRoot();
|
|
63
|
+
if (projectRoot) {
|
|
64
|
+
// Try ESM first (preferred for ES modules)
|
|
65
|
+
const distEsmPath = resolve(join(projectRoot, 'dist', 'esm', 'core', 'vector-index-worker.js'));
|
|
48
66
|
if (existsSync(distEsmPath)) {
|
|
49
67
|
return distEsmPath;
|
|
50
68
|
}
|
|
69
|
+
// Try CJS as fallback
|
|
70
|
+
const distCjsPath = resolve(join(projectRoot, 'dist', 'cjs', 'core', 'vector-index-worker.js'));
|
|
51
71
|
if (existsSync(distCjsPath)) {
|
|
52
72
|
return distCjsPath;
|
|
53
73
|
}
|
|
@@ -55,8 +75,8 @@ export class VectorIndex {
|
|
|
55
75
|
// If running from node_modules (installed package), try dist paths
|
|
56
76
|
if (currentDir.includes('node_modules')) {
|
|
57
77
|
const packageRoot = currentDir.split('node_modules')[0];
|
|
58
|
-
const distEsmPath = join(packageRoot, 'node_modules', 'rag-lite-ts', 'dist', 'esm', 'core', 'vector-index-worker.js');
|
|
59
|
-
const distCjsPath = join(packageRoot, 'node_modules', 'rag-lite-ts', 'dist', 'cjs', 'core', 'vector-index-worker.js');
|
|
78
|
+
const distEsmPath = resolve(join(packageRoot, 'node_modules', 'rag-lite-ts', 'dist', 'esm', 'core', 'vector-index-worker.js'));
|
|
79
|
+
const distCjsPath = resolve(join(packageRoot, 'node_modules', 'rag-lite-ts', 'dist', 'cjs', 'core', 'vector-index-worker.js'));
|
|
60
80
|
if (existsSync(distEsmPath)) {
|
|
61
81
|
return distEsmPath;
|
|
62
82
|
}
|
|
@@ -68,7 +88,8 @@ export class VectorIndex {
|
|
|
68
88
|
throw new Error(`Worker file not found. Expected: ${jsPath}\n` +
|
|
69
89
|
'Please run "npm run build" to compile the vector-index-worker.ts file.\n' +
|
|
70
90
|
`Current directory: ${currentDir}\n` +
|
|
71
|
-
`
|
|
91
|
+
`Project root: ${projectRoot || 'not found'}\n` +
|
|
92
|
+
`Checked paths: ${jsPath}${projectRoot ? `, ${join(projectRoot, 'dist', 'esm', 'core', 'vector-index-worker.js')}, ${join(projectRoot, 'dist', 'cjs', 'core', 'vector-index-worker.js')}` : ''}`);
|
|
72
93
|
}
|
|
73
94
|
/**
|
|
74
95
|
* Ensure worker is created and ready
|
|
@@ -326,18 +347,47 @@ export class VectorIndex {
|
|
|
326
347
|
*/
|
|
327
348
|
async cleanup() {
|
|
328
349
|
if (this.worker) {
|
|
350
|
+
const workerToTerminate = this.worker;
|
|
351
|
+
// Clear state first to prevent new operations
|
|
352
|
+
this.worker = null;
|
|
353
|
+
this.isInitialized = false;
|
|
329
354
|
try {
|
|
330
|
-
// Send cleanup message (worker will acknowledge)
|
|
331
|
-
|
|
355
|
+
// Send cleanup message (worker will acknowledge) with timeout
|
|
356
|
+
// Use the worker directly since we've cleared this.worker
|
|
357
|
+
const cleanupPromise = new Promise((resolve, reject) => {
|
|
358
|
+
const id = this.messageId++;
|
|
359
|
+
const timeout = setTimeout(() => {
|
|
360
|
+
this.messageQueue.delete(id);
|
|
361
|
+
reject(new Error('Cleanup timeout'));
|
|
362
|
+
}, 1000);
|
|
363
|
+
this.messageQueue.set(id, {
|
|
364
|
+
resolve: () => {
|
|
365
|
+
clearTimeout(timeout);
|
|
366
|
+
resolve();
|
|
367
|
+
},
|
|
368
|
+
reject: (error) => {
|
|
369
|
+
clearTimeout(timeout);
|
|
370
|
+
reject(error);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
workerToTerminate.postMessage({ id, type: 'cleanup', payload: undefined });
|
|
374
|
+
});
|
|
375
|
+
await cleanupPromise;
|
|
332
376
|
}
|
|
333
377
|
catch (error) {
|
|
334
|
-
// Ignore errors during cleanup
|
|
378
|
+
// Ignore errors during cleanup - worker might already be terminating
|
|
379
|
+
}
|
|
380
|
+
finally {
|
|
381
|
+
// Clear message queue
|
|
382
|
+
this.messageQueue.clear();
|
|
383
|
+
// Terminate worker - this frees ALL WebAssembly memory
|
|
384
|
+
try {
|
|
385
|
+
await workerToTerminate.terminate();
|
|
386
|
+
}
|
|
387
|
+
catch (error) {
|
|
388
|
+
// Ignore termination errors
|
|
389
|
+
}
|
|
335
390
|
}
|
|
336
|
-
// Terminate worker - this frees ALL WebAssembly memory
|
|
337
|
-
await this.worker.terminate();
|
|
338
|
-
this.worker = null;
|
|
339
|
-
this.isInitialized = false;
|
|
340
|
-
this.messageQueue.clear();
|
|
341
391
|
}
|
|
342
392
|
}
|
|
343
393
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FACTORY MODULE — Generator Factory for RAG Response Generation
|
|
3
|
+
*
|
|
4
|
+
* Factory functions for creating response generator instances.
|
|
5
|
+
* Handles model validation, lazy loading, and proper initialization.
|
|
6
|
+
*
|
|
7
|
+
* SUPPORTED MODELS:
|
|
8
|
+
* - HuggingFaceTB/SmolLM2-135M-Instruct (instruct, balanced, DEFAULT, uses top 3 chunks)
|
|
9
|
+
* - HuggingFaceTB/SmolLM2-360M-Instruct (instruct, higher quality, uses top 5 chunks)
|
|
10
|
+
*
|
|
11
|
+
* PREREQUISITES:
|
|
12
|
+
* - Reranking must be enabled for response generation
|
|
13
|
+
*
|
|
14
|
+
* @experimental This feature is experimental and may change in future versions.
|
|
15
|
+
*/
|
|
16
|
+
import type { ResponseGenerator, GeneratorCreationOptions, GenerateFunction } from '../core/response-generator.js';
|
|
17
|
+
/**
|
|
18
|
+
* Create a response generator for the specified model
|
|
19
|
+
*
|
|
20
|
+
* Uses lazy loading to defer model initialization until first use.
|
|
21
|
+
* Validates model compatibility before creation.
|
|
22
|
+
*
|
|
23
|
+
* @param modelName - Name of the generator model (default: SmolLM2-135M-Instruct)
|
|
24
|
+
* @param options - Optional configuration options
|
|
25
|
+
* @returns Promise resolving to a ResponseGenerator instance
|
|
26
|
+
* @throws {GeneratorValidationError} If model is not supported
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // Create default generator (recommended)
|
|
31
|
+
* const generator = await createResponseGenerator();
|
|
32
|
+
*
|
|
33
|
+
* // Create higher quality generator
|
|
34
|
+
* const generator = await createResponseGenerator('HuggingFaceTB/SmolLM2-360M-Instruct');
|
|
35
|
+
*
|
|
36
|
+
* // Create with options
|
|
37
|
+
* const generator = await createResponseGenerator('HuggingFaceTB/SmolLM2-360M-Instruct', {
|
|
38
|
+
* cachePath: './models'
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @experimental This feature is experimental and may change in future versions.
|
|
43
|
+
*/
|
|
44
|
+
export declare function createResponseGenerator(modelName?: string, options?: GeneratorCreationOptions): Promise<ResponseGenerator>;
|
|
45
|
+
/**
|
|
46
|
+
* Create a GenerateFunction from a model name
|
|
47
|
+
*
|
|
48
|
+
* This is a convenience function that creates a generator and wraps it
|
|
49
|
+
* in a function suitable for dependency injection into SearchEngine.
|
|
50
|
+
*
|
|
51
|
+
* @param modelName - Name of the generator model
|
|
52
|
+
* @param options - Optional configuration options
|
|
53
|
+
* @returns Promise resolving to a GenerateFunction
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const generateFn = await createGenerateFunctionFromModel();
|
|
58
|
+
* const result = await generateFn(query, chunks);
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @experimental This feature is experimental and may change in future versions.
|
|
62
|
+
*/
|
|
63
|
+
export declare function createGenerateFunctionFromModel(modelName?: string, options?: GeneratorCreationOptions): Promise<GenerateFunction>;
|
|
64
|
+
/**
|
|
65
|
+
* Get the default generator model name
|
|
66
|
+
*/
|
|
67
|
+
export declare function getDefaultGeneratorModel(): string;
|
|
68
|
+
/**
|
|
69
|
+
* List available generator models
|
|
70
|
+
*/
|
|
71
|
+
export declare function listGeneratorModels(): string[];
|
|
72
|
+
/**
|
|
73
|
+
* Check if a model name is a valid generator
|
|
74
|
+
*/
|
|
75
|
+
export declare function isValidGeneratorModel(modelName: string): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Get recommended generator for specific use case
|
|
78
|
+
*
|
|
79
|
+
* @param preferSpeed - Prefer faster generation over quality
|
|
80
|
+
* @param preferQuality - Prefer higher quality over speed
|
|
81
|
+
* @returns Recommended model name
|
|
82
|
+
*/
|
|
83
|
+
export declare function getRecommendedGenerator(options?: {
|
|
84
|
+
preferSpeed?: boolean;
|
|
85
|
+
preferQuality?: boolean;
|
|
86
|
+
}): string;
|
|
87
|
+
export { GeneratorRegistry, DEFAULT_GENERATOR_MODEL } from '../core/generator-registry.js';
|
|
88
|
+
//# sourceMappingURL=generator-factory.d.ts.map
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FACTORY MODULE — Generator Factory for RAG Response Generation
|
|
3
|
+
*
|
|
4
|
+
* Factory functions for creating response generator instances.
|
|
5
|
+
* Handles model validation, lazy loading, and proper initialization.
|
|
6
|
+
*
|
|
7
|
+
* SUPPORTED MODELS:
|
|
8
|
+
* - HuggingFaceTB/SmolLM2-135M-Instruct (instruct, balanced, DEFAULT, uses top 3 chunks)
|
|
9
|
+
* - HuggingFaceTB/SmolLM2-360M-Instruct (instruct, higher quality, uses top 5 chunks)
|
|
10
|
+
*
|
|
11
|
+
* PREREQUISITES:
|
|
12
|
+
* - Reranking must be enabled for response generation
|
|
13
|
+
*
|
|
14
|
+
* @experimental This feature is experimental and may change in future versions.
|
|
15
|
+
*/
|
|
16
|
+
import { createGenerateFunction, GeneratorValidationError } from '../core/response-generator.js';
|
|
17
|
+
import { GeneratorRegistry, DEFAULT_GENERATOR_MODEL, getGeneratorType } from '../core/generator-registry.js';
|
|
18
|
+
import { LazyGeneratorLoader } from '../core/lazy-dependency-loader.js';
|
|
19
|
+
// =============================================================================
|
|
20
|
+
// GENERATOR FACTORY
|
|
21
|
+
// =============================================================================
|
|
22
|
+
/**
|
|
23
|
+
* Create a response generator for the specified model
|
|
24
|
+
*
|
|
25
|
+
* Uses lazy loading to defer model initialization until first use.
|
|
26
|
+
* Validates model compatibility before creation.
|
|
27
|
+
*
|
|
28
|
+
* @param modelName - Name of the generator model (default: SmolLM2-135M-Instruct)
|
|
29
|
+
* @param options - Optional configuration options
|
|
30
|
+
* @returns Promise resolving to a ResponseGenerator instance
|
|
31
|
+
* @throws {GeneratorValidationError} If model is not supported
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* // Create default generator (recommended)
|
|
36
|
+
* const generator = await createResponseGenerator();
|
|
37
|
+
*
|
|
38
|
+
* // Create higher quality generator
|
|
39
|
+
* const generator = await createResponseGenerator('HuggingFaceTB/SmolLM2-360M-Instruct');
|
|
40
|
+
*
|
|
41
|
+
* // Create with options
|
|
42
|
+
* const generator = await createResponseGenerator('HuggingFaceTB/SmolLM2-360M-Instruct', {
|
|
43
|
+
* cachePath: './models'
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @experimental This feature is experimental and may change in future versions.
|
|
48
|
+
*/
|
|
49
|
+
export async function createResponseGenerator(modelName = DEFAULT_GENERATOR_MODEL, options = {}) {
|
|
50
|
+
console.log(`🏭 [EXPERIMENTAL] Creating response generator: ${modelName}`);
|
|
51
|
+
// Step 1: Validate model
|
|
52
|
+
const validation = GeneratorRegistry.validateGenerator(modelName);
|
|
53
|
+
if (!validation.isValid) {
|
|
54
|
+
throw new GeneratorValidationError(modelName, GeneratorRegistry.getSupportedGenerators(), validation.errors.join('; '));
|
|
55
|
+
}
|
|
56
|
+
// Log warnings
|
|
57
|
+
if (validation.warnings.length > 0) {
|
|
58
|
+
console.warn(`⚠️ Warnings for generator '${modelName}':`);
|
|
59
|
+
validation.warnings.forEach(w => console.warn(` • ${w}`));
|
|
60
|
+
}
|
|
61
|
+
// Log suggestions
|
|
62
|
+
if (validation.suggestions.length > 0) {
|
|
63
|
+
console.info(`💡 Suggestions for generator '${modelName}':`);
|
|
64
|
+
validation.suggestions.forEach(s => console.info(` • ${s}`));
|
|
65
|
+
}
|
|
66
|
+
// Step 2: Get model type and create appropriate generator
|
|
67
|
+
const modelType = getGeneratorType(modelName);
|
|
68
|
+
if (!modelType) {
|
|
69
|
+
throw new GeneratorValidationError(modelName, GeneratorRegistry.getSupportedGenerators(), `Could not determine model type for '${modelName}'`);
|
|
70
|
+
}
|
|
71
|
+
// Step 3: Use lazy loading to create the generator
|
|
72
|
+
let generator;
|
|
73
|
+
switch (modelType) {
|
|
74
|
+
case 'instruct':
|
|
75
|
+
generator = await LazyGeneratorLoader.loadInstructGenerator(modelName, options);
|
|
76
|
+
break;
|
|
77
|
+
case 'causal-lm':
|
|
78
|
+
generator = await LazyGeneratorLoader.loadCausalLMGenerator(modelName, options);
|
|
79
|
+
break;
|
|
80
|
+
default:
|
|
81
|
+
throw new GeneratorValidationError(modelName, GeneratorRegistry.getSupportedGenerators(), `Unsupported generator type: ${modelType}`);
|
|
82
|
+
}
|
|
83
|
+
console.log(`✅ [EXPERIMENTAL] Response generator created: ${modelName}`);
|
|
84
|
+
return generator;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Create a GenerateFunction from a model name
|
|
88
|
+
*
|
|
89
|
+
* This is a convenience function that creates a generator and wraps it
|
|
90
|
+
* in a function suitable for dependency injection into SearchEngine.
|
|
91
|
+
*
|
|
92
|
+
* @param modelName - Name of the generator model
|
|
93
|
+
* @param options - Optional configuration options
|
|
94
|
+
* @returns Promise resolving to a GenerateFunction
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* const generateFn = await createGenerateFunctionFromModel();
|
|
99
|
+
* const result = await generateFn(query, chunks);
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* @experimental This feature is experimental and may change in future versions.
|
|
103
|
+
*/
|
|
104
|
+
export async function createGenerateFunctionFromModel(modelName = DEFAULT_GENERATOR_MODEL, options = {}) {
|
|
105
|
+
const generator = await createResponseGenerator(modelName, options);
|
|
106
|
+
return createGenerateFunction(generator);
|
|
107
|
+
}
|
|
108
|
+
// =============================================================================
|
|
109
|
+
// UTILITY FUNCTIONS
|
|
110
|
+
// =============================================================================
|
|
111
|
+
/**
|
|
112
|
+
* Get the default generator model name
|
|
113
|
+
*/
|
|
114
|
+
export function getDefaultGeneratorModel() {
|
|
115
|
+
return DEFAULT_GENERATOR_MODEL;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* List available generator models
|
|
119
|
+
*/
|
|
120
|
+
export function listGeneratorModels() {
|
|
121
|
+
return GeneratorRegistry.getSupportedGenerators();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Check if a model name is a valid generator
|
|
125
|
+
*/
|
|
126
|
+
export function isValidGeneratorModel(modelName) {
|
|
127
|
+
return GeneratorRegistry.getGeneratorInfo(modelName) !== null;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get recommended generator for specific use case
|
|
131
|
+
*
|
|
132
|
+
* @param preferSpeed - Prefer faster generation over quality
|
|
133
|
+
* @param preferQuality - Prefer higher quality over speed
|
|
134
|
+
* @returns Recommended model name
|
|
135
|
+
*/
|
|
136
|
+
export function getRecommendedGenerator(options = {}) {
|
|
137
|
+
const { preferSpeed, preferQuality } = options;
|
|
138
|
+
if (preferSpeed) {
|
|
139
|
+
return 'Xenova/distilgpt2';
|
|
140
|
+
}
|
|
141
|
+
if (preferQuality) {
|
|
142
|
+
return 'HuggingFaceTB/SmolLM2-360M-Instruct';
|
|
143
|
+
}
|
|
144
|
+
// Default: balanced option
|
|
145
|
+
return DEFAULT_GENERATOR_MODEL;
|
|
146
|
+
}
|
|
147
|
+
// =============================================================================
|
|
148
|
+
// EXPORTS
|
|
149
|
+
// =============================================================================
|
|
150
|
+
export { GeneratorRegistry, DEFAULT_GENERATOR_MODEL } from '../core/generator-registry.js';
|
|
151
|
+
//# sourceMappingURL=generator-factory.js.map
|
|
@@ -24,4 +24,5 @@
|
|
|
24
24
|
export { IngestionFactory } from './ingestion-factory.js';
|
|
25
25
|
export { SearchFactory } from './search-factory.js';
|
|
26
26
|
export type { IngestionFactoryOptions, ContentSystemConfig } from './ingestion-factory.js';
|
|
27
|
+
export { createResponseGenerator, createGenerateFunctionFromModel, getDefaultGeneratorModel, listGeneratorModels, isValidGeneratorModel, getRecommendedGenerator } from './generator-factory.js';
|
|
27
28
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -26,4 +26,9 @@ export { IngestionFactory } from './ingestion-factory.js';
|
|
|
26
26
|
// Polymorphic search factory (recommended for automatic mode detection)
|
|
27
27
|
// Re-exported from core for convenience
|
|
28
28
|
export { SearchFactory } from './search-factory.js';
|
|
29
|
+
// =============================================================================
|
|
30
|
+
// EXPERIMENTAL: Generator Factory
|
|
31
|
+
// =============================================================================
|
|
32
|
+
// Generator factory for creating response generators (experimental)
|
|
33
|
+
export { createResponseGenerator, createGenerateFunctionFromModel, getDefaultGeneratorModel, listGeneratorModels, isValidGeneratorModel, getRecommendedGenerator } from './generator-factory.js';
|
|
29
34
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -73,4 +73,13 @@ export type { Chunk, Preprocessor, PreprocessorOptions, PreprocessingConfig } fr
|
|
|
73
73
|
export type { IngestionOptions, IngestionResult } from './core/ingestion.js';
|
|
74
74
|
export { handleError, safeExecute, ErrorCategory, ErrorSeverity, createError, type ErrorContext } from './core/error-handler.js';
|
|
75
75
|
export { APIError, IngestionError, SearchError, ResourceError, ModelCompatibilityError, ErrorFactory, CommonErrors, handleAPIError } from './api-errors.js';
|
|
76
|
+
export { createResponseGenerator, createGenerateFunctionFromModel, getDefaultGeneratorModel, listGeneratorModels, isValidGeneratorModel, getRecommendedGenerator } from './factories/generator-factory.js';
|
|
77
|
+
export type { ResponseGenerator, GeneratorModelType, GenerationRequest, GenerationResult, GeneratorCapabilities, GeneratorRequirements, GeneratorModelInfo, GeneratorValidationResult, GeneratorCreationOptions, GenerateFunction, CreateGeneratorFunction } from './core/response-generator.js';
|
|
78
|
+
export { GeneratorValidationError, GenerationError, ContextWindowError } from './core/response-generator.js';
|
|
79
|
+
export { supportsStreaming, isInstructModel, createGenerateFunction } from './core/response-generator.js';
|
|
80
|
+
export { SUPPORTED_GENERATORS, DEFAULT_GENERATOR_MODEL, GeneratorRegistry, getGeneratorType, isInstructionTunedModel, getMaxContextLength, getRecommendedSettings, getDefaultMaxChunksForContext } from './core/generator-registry.js';
|
|
81
|
+
export { DEFAULT_SYSTEM_PROMPT, formatContextChunks, buildPrompt, estimateTokenCount, type ContextFormattingOptions, type FormattedContext, type PromptBuildOptions, type BuiltPrompt } from './core/prompt-templates.js';
|
|
82
|
+
export { InstructGenerator } from './text/generators/instruct-generator.js';
|
|
83
|
+
export { CausalLMGenerator } from './text/generators/causal-lm-generator.js';
|
|
84
|
+
export type { ExtendedSearchOptions, SearchResultWithGeneration } from './core/search.js';
|
|
76
85
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.js
CHANGED
|
@@ -109,4 +109,20 @@ export { resolveRagLitePaths, ensureRagLiteStructure, migrateToRagLiteStructure,
|
|
|
109
109
|
export { handleError, safeExecute, ErrorCategory, ErrorSeverity, createError } from './core/error-handler.js';
|
|
110
110
|
// API-specific errors
|
|
111
111
|
export { APIError, IngestionError, SearchError, ResourceError, ModelCompatibilityError, ErrorFactory, CommonErrors, handleAPIError } from './api-errors.js';
|
|
112
|
+
// =============================================================================
|
|
113
|
+
// EXPERIMENTAL: RESPONSE GENERATION
|
|
114
|
+
// =============================================================================
|
|
115
|
+
// Generator factory functions
|
|
116
|
+
export { createResponseGenerator, createGenerateFunctionFromModel, getDefaultGeneratorModel, listGeneratorModels, isValidGeneratorModel, getRecommendedGenerator } from './factories/generator-factory.js';
|
|
117
|
+
// Generator errors
|
|
118
|
+
export { GeneratorValidationError, GenerationError, ContextWindowError } from './core/response-generator.js';
|
|
119
|
+
// Generator utilities
|
|
120
|
+
export { supportsStreaming, isInstructModel, createGenerateFunction } from './core/response-generator.js';
|
|
121
|
+
// Generator registry
|
|
122
|
+
export { SUPPORTED_GENERATORS, DEFAULT_GENERATOR_MODEL, GeneratorRegistry, getGeneratorType, isInstructionTunedModel, getMaxContextLength, getRecommendedSettings, getDefaultMaxChunksForContext } from './core/generator-registry.js';
|
|
123
|
+
// Prompt templates
|
|
124
|
+
export { DEFAULT_SYSTEM_PROMPT, formatContextChunks, buildPrompt, estimateTokenCount } from './core/prompt-templates.js';
|
|
125
|
+
// Generator implementations
|
|
126
|
+
export { InstructGenerator } from './text/generators/instruct-generator.js';
|
|
127
|
+
export { CausalLMGenerator } from './text/generators/causal-lm-generator.js';
|
|
112
128
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TEXT IMPLEMENTATION — Causal LM Generator for DistilGPT2
|
|
3
|
+
*
|
|
4
|
+
* Implements ResponseGenerator interface for causal language models.
|
|
5
|
+
* Supports Xenova/distilgpt2 for fast, basic text generation.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Simple prompt formatting (no chat template)
|
|
9
|
+
* - Fast generation with smaller model
|
|
10
|
+
* - Streaming generation support
|
|
11
|
+
* - Resource management via ResourceManager
|
|
12
|
+
*
|
|
13
|
+
* Note: Causal LM models don't support system prompts, so responses
|
|
14
|
+
* may be less focused than instruction-tuned models.
|
|
15
|
+
*
|
|
16
|
+
* @experimental This feature is experimental and may change in future versions.
|
|
17
|
+
*/
|
|
18
|
+
import '../../dom-polyfills.js';
|
|
19
|
+
import { BaseResponseGenerator, type GeneratorOptions } from '../../core/abstract-generator.js';
|
|
20
|
+
/**
|
|
21
|
+
* Causal LM generator implementation for DistilGPT2
|
|
22
|
+
*
|
|
23
|
+
* Uses causal language models that generate text based on simple prompts.
|
|
24
|
+
* Faster but may produce less focused responses than instruct models.
|
|
25
|
+
*/
|
|
26
|
+
export declare class CausalLMGenerator extends BaseResponseGenerator {
|
|
27
|
+
private pipeline;
|
|
28
|
+
private tokenizer;
|
|
29
|
+
private resourceManager;
|
|
30
|
+
private resourceId?;
|
|
31
|
+
constructor(modelName: string, options?: GeneratorOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Load the causal LM model using transformers.js
|
|
34
|
+
*/
|
|
35
|
+
loadModel(): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Clean up model resources
|
|
38
|
+
*/
|
|
39
|
+
cleanup(): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Generate text using the causal LM model
|
|
42
|
+
*/
|
|
43
|
+
protected generateText(prompt: string, options: {
|
|
44
|
+
maxTokens: number;
|
|
45
|
+
temperature: number;
|
|
46
|
+
topP: number;
|
|
47
|
+
topK: number;
|
|
48
|
+
repetitionPenalty: number;
|
|
49
|
+
stopSequences: string[];
|
|
50
|
+
}): Promise<{
|
|
51
|
+
text: string;
|
|
52
|
+
promptTokens: number;
|
|
53
|
+
completionTokens: number;
|
|
54
|
+
finishReason: 'complete' | 'length' | 'stop_sequence' | 'error';
|
|
55
|
+
}>;
|
|
56
|
+
/**
|
|
57
|
+
* Generate text with streaming output
|
|
58
|
+
*/
|
|
59
|
+
generateStream(request: import('../../core/response-generator.js').GenerationRequest): AsyncIterable<string>;
|
|
60
|
+
/**
|
|
61
|
+
* Count tokens in a text string
|
|
62
|
+
*/
|
|
63
|
+
private countTokens;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=causal-lm-generator.d.ts.map
|