@devilsdev/rag-pipeline-utils 2.6.2 → 2.7.0
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/BENCHMARKS.md +7 -1
- package/CHANGELOG.md +121 -0
- package/README.md +35 -24
- package/dist/index.cjs +4164 -1825
- package/dist/index.d.ts +265 -256
- package/dist/index.mjs +1 -1
- package/dist/types/cache/memory-cache.d.ts +4 -1
- package/dist/types/cache/redis-cache.d.ts +4 -1
- package/dist/types/cli/services/instrument.d.ts +14 -0
- package/dist/types/cli/services/rag-service.d.ts +140 -0
- package/dist/types/cli/shared/config-context.d.ts +9 -0
- package/dist/types/config/component-factory.d.ts +57 -0
- package/dist/types/config/enhanced-ragrc-schema.d.ts +648 -0
- package/dist/types/config/load-config.d.ts +18 -0
- package/dist/types/config/normalize-config.d.ts +9 -0
- package/dist/types/core/observability/event-logger.d.ts +211 -0
- package/dist/types/core/observability/metrics.d.ts +585 -0
- package/dist/types/core/performance/parallel-processor.d.ts +92 -0
- package/dist/types/core/plugin-registry.d.ts +5 -0
- package/dist/types/dag/core/execution-scheduler.d.ts +3 -1
- package/dist/types/dag/dag-engine.d.ts +15 -2
- package/dist/types/dev/dev-server.d.ts +136 -0
- package/dist/types/dev/hot-reload.d.ts +226 -0
- package/dist/types/ecosystem/plugin-analytics-dashboard.d.ts +141 -0
- package/dist/types/ecosystem/plugin-hub.d.ts +161 -0
- package/dist/types/ecosystem/plugin-installer.d.ts +40 -0
- package/dist/types/ecosystem/plugin-probe.d.ts +21 -0
- package/dist/types/ecosystem/plugin-scanner.d.ts +30 -0
- package/dist/types/ecosystem/tar-extract.d.ts +24 -0
- package/dist/types/enterprise/audit-logging.d.ts +217 -0
- package/dist/types/evaluate/compare.d.ts +140 -0
- package/dist/types/evaluate/dataset.d.ts +43 -0
- package/dist/types/evaluate/evaluator.d.ts +14 -0
- package/dist/types/evaluate/report.d.ts +56 -0
- package/dist/types/evaluate/synthetic.d.ts +64 -0
- package/dist/types/loader/csv-loader.d.ts +10 -0
- package/dist/types/loader/document-reader.d.ts +18 -0
- package/dist/types/loader/html-loader.d.ts +21 -0
- package/dist/types/loader/markdown-loader.d.ts +21 -0
- package/dist/types/retrieval/bm25-search.d.ts +4 -0
- package/dist/types/retrieval/file-vector-store.d.ts +4 -1
- package/dist/types/retrieval/hybrid-retriever.d.ts +4 -0
- package/dist/types/retrieval/pgvector-retriever.d.ts +123 -0
- package/dist/types/retrieval/qdrant-retriever.d.ts +106 -0
- package/dist/types/security/validator.d.ts +11 -1
- package/dist/types/testing.d.ts +2 -0
- package/dist/types/utils/batch-processor.d.ts +205 -0
- package/dist/types/utils/connection-pool.d.ts +158 -0
- package/dist/types/utils/error-formatter.d.ts +312 -0
- package/dist/types/utils/logger.d.ts +29 -4
- package/dist/types/utils/rate-limiter.d.ts +13 -8
- package/dist/types/utils/retry.d.ts +10 -0
- package/package.json +12 -2
- package/src/cache/memory-cache.js +4 -1
- package/src/cache/redis-cache.js +4 -1
- package/src/cli/enhanced-cli-commands.js +79 -0
- package/src/cli/handlers/ai-handler.js +2 -2
- package/src/cli/handlers/eval-handler.js +201 -0
- package/src/cli/handlers/generate-handler.js +103 -0
- package/src/cli/services/benchmark.js +2 -2
- package/src/cli/services/rag-service.js +59 -10
- package/src/cli/shared/cli-utils.js +40 -1
- package/src/config/component-factory.js +32 -0
- package/src/connectors/local-embedder.js +20 -13
- package/src/core/observability/event-logger.js +135 -1
- package/src/core/observability/metrics.js +223 -5
- package/src/core/performance/parallel-processor.js +2 -0
- package/src/core/plugin-registry.js +5 -1
- package/src/dag/core/execution-scheduler.js +2 -0
- package/src/dag/dag-engine.js +13 -0
- package/src/dev/dev-server.js +6 -0
- package/src/ecosystem/plugin-hub.js +4 -0
- package/src/enterprise/audit-logging.js +5 -0
- package/src/evaluate/compare.js +395 -0
- package/src/evaluate/dataset.js +207 -0
- package/src/evaluate/report.js +350 -0
- package/src/evaluate/synthetic.js +246 -0
- package/src/index.js +17 -0
- package/src/retrieval/bm25-search.js +4 -0
- package/src/retrieval/file-vector-store.js +4 -1
- package/src/retrieval/hybrid-retriever.js +4 -0
- package/src/retrieval/pgvector-retriever.js +420 -0
- package/src/retrieval/qdrant-retriever.js +361 -0
- package/src/security/validator.js +8 -0
- package/src/testing.js +2 -0
- package/src/utils/batch-processor.js +7 -3
- package/src/utils/error-formatter.js +48 -20
- package/src/utils/logger.js +31 -1
- package/src/utils/rate-limiter.js +9 -4
- package/src/utils/retry.js +10 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTMLLoader reads HTML files and extracts visible text.
|
|
3
|
+
* Implements the Loader interface.
|
|
4
|
+
*/
|
|
5
|
+
export class HTMLLoader {
|
|
6
|
+
/**
|
|
7
|
+
* Load and extract text from an HTML file
|
|
8
|
+
* @param {string} filePath - Path to .html file
|
|
9
|
+
* @returns {Promise<Array<{ chunk(): string[] }>>}
|
|
10
|
+
*/
|
|
11
|
+
load(filePath: string): Promise<Array<{
|
|
12
|
+
chunk(): string[];
|
|
13
|
+
}>>;
|
|
14
|
+
/**
|
|
15
|
+
* Chunk HTML text content using the ChunkingEngine
|
|
16
|
+
* @param {string} input
|
|
17
|
+
* @param {number} maxLen
|
|
18
|
+
* @returns {string[]}
|
|
19
|
+
*/
|
|
20
|
+
_chunkText(input: string, maxLen?: number): string[];
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MarkdownLoader reads and parses .md files into plain text chunks.
|
|
3
|
+
* Implements the Loader interface.
|
|
4
|
+
*/
|
|
5
|
+
export class MarkdownLoader {
|
|
6
|
+
/**
|
|
7
|
+
* Load and parse Markdown file
|
|
8
|
+
* @param {string} _filePath - Path to .md file
|
|
9
|
+
* @returns {Promise<Array<{ chunk(): string[] }>>}
|
|
10
|
+
*/
|
|
11
|
+
load(_filePath: string): Promise<Array<{
|
|
12
|
+
chunk(): string[];
|
|
13
|
+
}>>;
|
|
14
|
+
/**
|
|
15
|
+
* Chunk text using the ChunkingEngine
|
|
16
|
+
* @param {string} input
|
|
17
|
+
* @param {number} maxLen
|
|
18
|
+
* @returns {string[]}
|
|
19
|
+
*/
|
|
20
|
+
_chunkText(input: string, maxLen?: number): string[];
|
|
21
|
+
}
|
|
@@ -47,7 +47,10 @@ export class FileVectorStore {
|
|
|
47
47
|
save(): void;
|
|
48
48
|
/** @returns {object|null} Embedder metadata recorded with the vectors */
|
|
49
49
|
getEmbedderInfo(): object | null;
|
|
50
|
-
/**
|
|
50
|
+
/**
|
|
51
|
+
* Records which embedder wrote the index, saved with it.
|
|
52
|
+
* @param {{name: string, dimensions: number, state?: object}} info
|
|
53
|
+
*/
|
|
51
54
|
setEmbedderInfo(info: {
|
|
52
55
|
name: string;
|
|
53
56
|
dimensions: number;
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs several retrievers in parallel (for example vector and BM25) and
|
|
3
|
+
* fuses their results with weighted reciprocal rank fusion.
|
|
4
|
+
*/
|
|
1
5
|
export class HybridRetriever extends EventEmitter<[never]> {
|
|
2
6
|
/**
|
|
3
7
|
* @param {Array<{retriever: object, weight: number, name: string}>} retrievers
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retriever on PostgreSQL with the pgvector extension. Creates the
|
|
3
|
+
* extension, table and index on first use; similarity is cosine.
|
|
4
|
+
*/
|
|
5
|
+
export class PgVectorRetriever {
|
|
6
|
+
/**
|
|
7
|
+
* @param {Object} options
|
|
8
|
+
* @param {{ query: Function, connect?: Function }} [options.pool] - A pg Pool or Client you manage
|
|
9
|
+
* @param {string} [options.connectionString] - Used to create a Pool when no `pool` is given
|
|
10
|
+
* @param {string} [options.table='rag_documents'] - Table name, optionally `schema.table`
|
|
11
|
+
* @param {number} [options.dimensions] - Vector size for a new table; defaults to the first stored vector's
|
|
12
|
+
* @param {boolean} [options.createTable=true] - Create the extension, table and index on first store
|
|
13
|
+
* @param {'hnsw'|'none'} [options.index='hnsw'] - Index created with the table
|
|
14
|
+
* @param {number} [options.efSearch] - HNSW candidate list size per query (pgvector's default is 40). Higher finds more of the true nearest neighbours, more slowly; a query always uses at least its topK
|
|
15
|
+
* @param {string} [options.cacheKey] - Cache identity; change it when the table changes (see getCacheKey)
|
|
16
|
+
*/
|
|
17
|
+
constructor(options?: {
|
|
18
|
+
pool?: {
|
|
19
|
+
query: Function;
|
|
20
|
+
connect?: Function;
|
|
21
|
+
};
|
|
22
|
+
connectionString?: string;
|
|
23
|
+
table?: string;
|
|
24
|
+
dimensions?: number;
|
|
25
|
+
createTable?: boolean;
|
|
26
|
+
index?: "hnsw" | "none";
|
|
27
|
+
efSearch?: number;
|
|
28
|
+
cacheKey?: string;
|
|
29
|
+
});
|
|
30
|
+
efSearch: number;
|
|
31
|
+
table: string;
|
|
32
|
+
tableName: string;
|
|
33
|
+
dimensions: number;
|
|
34
|
+
createTable: boolean;
|
|
35
|
+
index: "none" | "hnsw";
|
|
36
|
+
cacheKey: string;
|
|
37
|
+
_pool: {
|
|
38
|
+
query: Function;
|
|
39
|
+
connect?: Function;
|
|
40
|
+
};
|
|
41
|
+
_connectionString: string;
|
|
42
|
+
_ownsPool: boolean;
|
|
43
|
+
_ready: any;
|
|
44
|
+
/** The pg Pool or Client in use; creates the Pool on first use. */
|
|
45
|
+
_db(): {
|
|
46
|
+
query: Function;
|
|
47
|
+
connect?: Function;
|
|
48
|
+
};
|
|
49
|
+
_query(text: any, params: any, signal: any): Promise<any>;
|
|
50
|
+
/** Runs one query with `SET LOCAL hnsw.ef_search`, in its own transaction. */
|
|
51
|
+
_queryWithEfSearch(text: any, params: any, ef: any, signal: any): Promise<any>;
|
|
52
|
+
/** Creates the extension, table and index once, sized to `dimensions`. */
|
|
53
|
+
_ensureTable(dimensions: any): any;
|
|
54
|
+
/**
|
|
55
|
+
* Cache identity for createRagPipeline({ cache }). Other processes can
|
|
56
|
+
* write to the table without this one knowing, so there is none unless
|
|
57
|
+
* you set `cacheKey`: then cached retrieval results are reused until you
|
|
58
|
+
* change the key or their time to live ends.
|
|
59
|
+
* @param {'retriever'} role
|
|
60
|
+
* @returns {string|null}
|
|
61
|
+
*/
|
|
62
|
+
getCacheKey(role: "retriever"): string | null;
|
|
63
|
+
/**
|
|
64
|
+
* Inserts or replaces documents, in one transaction: a failed batch
|
|
65
|
+
* changes nothing.
|
|
66
|
+
* @param {Array<{ id: string, vector: number[], content?: string, metadata?: Object }>} docs
|
|
67
|
+
* @returns {Promise<void>}
|
|
68
|
+
*/
|
|
69
|
+
store(docs: Array<{
|
|
70
|
+
id: string;
|
|
71
|
+
vector: number[];
|
|
72
|
+
content?: string;
|
|
73
|
+
metadata?: any;
|
|
74
|
+
}>): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* The documents most similar to a query vector.
|
|
77
|
+
* @param {number[]|{ queryVector: number[], topK?: number, signal?: AbortSignal }} request
|
|
78
|
+
* @param {number} [k=10] - Results when `request` is a vector
|
|
79
|
+
* @returns {Promise<Array<{ id: string, score: number, content: string, metadata: Object }>>}
|
|
80
|
+
*/
|
|
81
|
+
retrieve(request: number[] | {
|
|
82
|
+
queryVector: number[];
|
|
83
|
+
topK?: number;
|
|
84
|
+
signal?: AbortSignal;
|
|
85
|
+
}, k?: number): Promise<Array<{
|
|
86
|
+
id: string;
|
|
87
|
+
score: number;
|
|
88
|
+
content: string;
|
|
89
|
+
metadata: any;
|
|
90
|
+
}>>;
|
|
91
|
+
/**
|
|
92
|
+
* Deletes the chunks of one source (`metadata.source`), for re-ingesting.
|
|
93
|
+
* @param {string} source
|
|
94
|
+
* @returns {Promise<number>} Rows deleted
|
|
95
|
+
*/
|
|
96
|
+
removeBySource(source: string): Promise<number>;
|
|
97
|
+
/**
|
|
98
|
+
* Deletes documents by id.
|
|
99
|
+
* @param {string[]} ids
|
|
100
|
+
* @returns {Promise<number>} Rows deleted
|
|
101
|
+
*/
|
|
102
|
+
delete(ids: string[]): Promise<number>;
|
|
103
|
+
/** Deletes every document; the table stays. */
|
|
104
|
+
clear(): Promise<void>;
|
|
105
|
+
/** @returns {Promise<number>} */
|
|
106
|
+
size(): Promise<number>;
|
|
107
|
+
/**
|
|
108
|
+
* Every document, without vectors, in id order.
|
|
109
|
+
* @param {{ limit?: number }} [options]
|
|
110
|
+
* @returns {Promise<Array<{ id: string, content: string, metadata: Object }>>}
|
|
111
|
+
*/
|
|
112
|
+
list({ limit }?: {
|
|
113
|
+
limit?: number;
|
|
114
|
+
}): Promise<Array<{
|
|
115
|
+
id: string;
|
|
116
|
+
content: string;
|
|
117
|
+
metadata: any;
|
|
118
|
+
}>>;
|
|
119
|
+
/** Closes the Pool this retriever created; a pool you passed in stays open. */
|
|
120
|
+
close(): Promise<void>;
|
|
121
|
+
}
|
|
122
|
+
/** Quotes a table name, optionally schema-qualified, after validating it. */
|
|
123
|
+
export function quoteTable(name: any): string;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retriever on Qdrant over its REST API, with no client package. Creates
|
|
3
|
+
* the collection on first store; similarity is cosine.
|
|
4
|
+
*/
|
|
5
|
+
export class QdrantRetriever {
|
|
6
|
+
/**
|
|
7
|
+
* @param {Object} [options]
|
|
8
|
+
* @param {string} [options.url='http://localhost:6333'] - Qdrant's REST endpoint
|
|
9
|
+
* @param {string} [options.apiKey] - Sent as the `api-key` header
|
|
10
|
+
* @param {string} [options.collection='rag_documents']
|
|
11
|
+
* @param {number} [options.dimensions] - Vector size for a new collection; defaults to the first stored vector's
|
|
12
|
+
* @param {boolean} [options.createCollection=true] - Create the collection on first store
|
|
13
|
+
* @param {string} [options.cacheKey] - Cache identity; change it when the collection changes
|
|
14
|
+
* @param {Function} [options.fetch] - fetch implementation (defaults to the global)
|
|
15
|
+
*/
|
|
16
|
+
constructor(options?: {
|
|
17
|
+
url?: string;
|
|
18
|
+
apiKey?: string;
|
|
19
|
+
collection?: string;
|
|
20
|
+
dimensions?: number;
|
|
21
|
+
createCollection?: boolean;
|
|
22
|
+
cacheKey?: string;
|
|
23
|
+
fetch?: Function;
|
|
24
|
+
});
|
|
25
|
+
url: string;
|
|
26
|
+
collection: string;
|
|
27
|
+
dimensions: number;
|
|
28
|
+
createCollection: boolean;
|
|
29
|
+
cacheKey: string;
|
|
30
|
+
_apiKey: string;
|
|
31
|
+
_fetch: Function;
|
|
32
|
+
_ready: any;
|
|
33
|
+
/**
|
|
34
|
+
* Cache identity for createRagPipeline({ cache }): none unless you set
|
|
35
|
+
* `cacheKey`, since other clients can change the collection.
|
|
36
|
+
* @param {'retriever'} role
|
|
37
|
+
* @returns {string|null}
|
|
38
|
+
*/
|
|
39
|
+
getCacheKey(role: "retriever"): string | null;
|
|
40
|
+
/**
|
|
41
|
+
* One REST call to the collection; resolves Qdrant's `result`.
|
|
42
|
+
* @private
|
|
43
|
+
* @param {string} method
|
|
44
|
+
* @param {string} path - After /collections/<name>
|
|
45
|
+
* @param {Object} [body]
|
|
46
|
+
* @param {{ signal?: AbortSignal, allow404?: boolean }} [options] - allow404 resolves null for a missing collection
|
|
47
|
+
* @returns {Promise<any>}
|
|
48
|
+
*/
|
|
49
|
+
private _request;
|
|
50
|
+
_ensureCollection(dimensions: any): any;
|
|
51
|
+
/**
|
|
52
|
+
* Inserts or replaces documents.
|
|
53
|
+
* @param {Array<{ id: string, vector: number[], content?: string, metadata?: Object }>} docs
|
|
54
|
+
* @returns {Promise<void>}
|
|
55
|
+
*/
|
|
56
|
+
store(docs: Array<{
|
|
57
|
+
id: string;
|
|
58
|
+
vector: number[];
|
|
59
|
+
content?: string;
|
|
60
|
+
metadata?: any;
|
|
61
|
+
}>): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* The documents most similar to a query vector.
|
|
64
|
+
* @param {number[]|{ queryVector: number[], topK?: number, signal?: AbortSignal }} request
|
|
65
|
+
* @param {number} [k=10] - Results when `request` is a vector
|
|
66
|
+
* @returns {Promise<Array<{ id: string, score: number, content: string, metadata: Object }>>}
|
|
67
|
+
*/
|
|
68
|
+
retrieve(request: number[] | {
|
|
69
|
+
queryVector: number[];
|
|
70
|
+
topK?: number;
|
|
71
|
+
signal?: AbortSignal;
|
|
72
|
+
}, k?: number): Promise<Array<{
|
|
73
|
+
id: string;
|
|
74
|
+
score: number;
|
|
75
|
+
content: string;
|
|
76
|
+
metadata: any;
|
|
77
|
+
}>>;
|
|
78
|
+
/**
|
|
79
|
+
* Deletes the chunks of one source (`metadata.source`), for re-ingesting.
|
|
80
|
+
* @param {string} source
|
|
81
|
+
*/
|
|
82
|
+
removeBySource(source: string): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Deletes documents by id.
|
|
85
|
+
* @param {string[]} ids
|
|
86
|
+
*/
|
|
87
|
+
delete(ids: string[]): Promise<void>;
|
|
88
|
+
/** Deletes every document this class stored; the collection stays. */
|
|
89
|
+
clear(): Promise<void>;
|
|
90
|
+
/** @returns {Promise<number>} */
|
|
91
|
+
size(): Promise<number>;
|
|
92
|
+
/**
|
|
93
|
+
* Every document, without vectors.
|
|
94
|
+
* @param {{ limit?: number }} [options]
|
|
95
|
+
* @returns {Promise<Array<{ id: string, content: string, metadata: Object }>>}
|
|
96
|
+
*/
|
|
97
|
+
list({ limit }?: {
|
|
98
|
+
limit?: number;
|
|
99
|
+
}): Promise<Array<{
|
|
100
|
+
id: string;
|
|
101
|
+
content: string;
|
|
102
|
+
metadata: any;
|
|
103
|
+
}>>;
|
|
104
|
+
}
|
|
105
|
+
/** A stable UUID (version 8, RFC 9562) for a document id. */
|
|
106
|
+
export function pointId(id: any): string;
|
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Checks input against a maximum length and a fixed list of disallowed
|
|
3
|
+
* tokens (code fences, `${`, `</script`, `--`, `;`, `||`, `&&`).
|
|
4
|
+
* @param {*} input - Converted to a string
|
|
5
|
+
* @param {Object} [opts]
|
|
6
|
+
* @param {number} [opts.maxLength=20000]
|
|
7
|
+
* @returns {{valid: boolean, reasons: string[], sanitized: string}} `sanitized` is the input cut to `maxLength`
|
|
8
|
+
*/
|
|
9
|
+
export function validateInput(input: any, opts?: {
|
|
10
|
+
maxLength?: number;
|
|
11
|
+
}): {
|
|
2
12
|
valid: boolean;
|
|
3
13
|
reasons: string[];
|
|
4
14
|
sanitized: string;
|
package/dist/types/testing.d.ts
CHANGED
|
@@ -66,7 +66,9 @@ export class EchoLLM {
|
|
|
66
66
|
answer?: string | ((arg0: string, arg1: Array<any>) => string);
|
|
67
67
|
fallback?: string;
|
|
68
68
|
});
|
|
69
|
+
/** The fixed answer, or a function of (query, docs) that returns it. */
|
|
69
70
|
answer: string | ((arg0: string, arg1: Array<any>) => string);
|
|
71
|
+
/** Returned when there are no documents and no `answer`. */
|
|
70
72
|
fallback: string;
|
|
71
73
|
/** @type {Array<{query: string, docs: Array<Object>, options: Object}>} */
|
|
72
74
|
calls: Array<{
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch Processor Class
|
|
3
|
+
*/
|
|
4
|
+
export class BatchProcessor extends EventEmitter<[never]> {
|
|
5
|
+
constructor(options?: {});
|
|
6
|
+
config: {
|
|
7
|
+
maxTokensPerBatch: number;
|
|
8
|
+
maxItemsPerBatch: number;
|
|
9
|
+
minItemsPerBatch: number;
|
|
10
|
+
maxMemoryMB: number;
|
|
11
|
+
streamingThreshold: number;
|
|
12
|
+
targetBatchUtilization: number;
|
|
13
|
+
adaptiveSizing: boolean;
|
|
14
|
+
maxRetries: number;
|
|
15
|
+
retryDelay: number;
|
|
16
|
+
retryBackoff: number;
|
|
17
|
+
progressInterval: number;
|
|
18
|
+
enableMetrics: boolean;
|
|
19
|
+
};
|
|
20
|
+
cancelled: boolean;
|
|
21
|
+
processing: boolean;
|
|
22
|
+
abortController: AbortController;
|
|
23
|
+
metrics: {
|
|
24
|
+
totalItems: number;
|
|
25
|
+
processedItems: number;
|
|
26
|
+
totalBatches: number;
|
|
27
|
+
successfulBatches: number;
|
|
28
|
+
failedBatches: number;
|
|
29
|
+
totalTokens: number;
|
|
30
|
+
totalTime: number;
|
|
31
|
+
avgBatchSize: number;
|
|
32
|
+
avgTokensPerBatch: number;
|
|
33
|
+
apiCallsSaved: number;
|
|
34
|
+
memoryUsed: number;
|
|
35
|
+
peakMemory: number;
|
|
36
|
+
};
|
|
37
|
+
adaptiveState: {
|
|
38
|
+
recentBatchSizes: any[];
|
|
39
|
+
recentProcessingTimes: any[];
|
|
40
|
+
optimalBatchSize: any;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Process items in optimized batches
|
|
44
|
+
*
|
|
45
|
+
* @param {Array<string|Object>} items - Items to process; objects are read from `text` or `content`
|
|
46
|
+
* @param {function(string[], {batchIndex: number, estimatedTokens: number, signal: AbortSignal}): Promise<Array>} processFn - Processes one batch of texts, returning one result per text
|
|
47
|
+
* @param {Object} [options] - Processing options
|
|
48
|
+
* @param {function(Object): void} [options.onProgress]
|
|
49
|
+
* @param {function(Object): void} [options.onBatchComplete]
|
|
50
|
+
* @param {AbortController} [options.abortController] - Abort to stop between batches
|
|
51
|
+
* @param {boolean} [options.returnOriginalOrder=true]
|
|
52
|
+
* @returns {Promise<Array>} Processed results
|
|
53
|
+
*/
|
|
54
|
+
processBatches(items: Array<string | any>, processFn: (arg0: string[], arg1: {
|
|
55
|
+
batchIndex: number;
|
|
56
|
+
estimatedTokens: number;
|
|
57
|
+
signal: AbortSignal;
|
|
58
|
+
}) => Promise<any[]>, options?: {
|
|
59
|
+
onProgress?: (arg0: any) => void;
|
|
60
|
+
onBatchComplete?: (arg0: any) => void;
|
|
61
|
+
abortController?: AbortController;
|
|
62
|
+
returnOriginalOrder?: boolean;
|
|
63
|
+
}): Promise<any[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Create optimized batches from items
|
|
66
|
+
* @private
|
|
67
|
+
*/
|
|
68
|
+
private _createBatches;
|
|
69
|
+
/**
|
|
70
|
+
* Process batch with retry logic
|
|
71
|
+
* @private
|
|
72
|
+
*/
|
|
73
|
+
private _processBatchWithRetry;
|
|
74
|
+
/**
|
|
75
|
+
* Estimate token count for text
|
|
76
|
+
* Uses approximation: ~4 characters per token for English text
|
|
77
|
+
* @private
|
|
78
|
+
*/
|
|
79
|
+
private _estimateTokens;
|
|
80
|
+
/**
|
|
81
|
+
* Estimate batch count
|
|
82
|
+
* @private
|
|
83
|
+
*/
|
|
84
|
+
private _estimateBatchCount;
|
|
85
|
+
/**
|
|
86
|
+
* Estimate remaining time
|
|
87
|
+
* @private
|
|
88
|
+
*/
|
|
89
|
+
private _estimateTimeRemaining;
|
|
90
|
+
/**
|
|
91
|
+
* Update memory estimate
|
|
92
|
+
* @private
|
|
93
|
+
*/
|
|
94
|
+
private _updateMemoryEstimate;
|
|
95
|
+
/**
|
|
96
|
+
* Update adaptive sizing state
|
|
97
|
+
* @private
|
|
98
|
+
*/
|
|
99
|
+
private _updateAdaptiveState;
|
|
100
|
+
/**
|
|
101
|
+
* Re-optimize batches based on adaptive learning
|
|
102
|
+
* @private
|
|
103
|
+
*/
|
|
104
|
+
private _reoptimizeBatches;
|
|
105
|
+
/**
|
|
106
|
+
* Restore original order of results
|
|
107
|
+
* @private
|
|
108
|
+
*/
|
|
109
|
+
private _restoreOriginalOrder;
|
|
110
|
+
/**
|
|
111
|
+
* Cancel ongoing processing
|
|
112
|
+
*/
|
|
113
|
+
cancel(): void;
|
|
114
|
+
/**
|
|
115
|
+
* Get current metrics
|
|
116
|
+
*/
|
|
117
|
+
getMetrics(): {
|
|
118
|
+
processing: boolean;
|
|
119
|
+
cancelled: boolean;
|
|
120
|
+
efficiency: {
|
|
121
|
+
batchUtilization: number;
|
|
122
|
+
tokenUtilization: number;
|
|
123
|
+
apiCallReduction: number;
|
|
124
|
+
throughput: number;
|
|
125
|
+
};
|
|
126
|
+
totalItems: number;
|
|
127
|
+
processedItems: number;
|
|
128
|
+
totalBatches: number;
|
|
129
|
+
successfulBatches: number;
|
|
130
|
+
failedBatches: number;
|
|
131
|
+
totalTokens: number;
|
|
132
|
+
totalTime: number;
|
|
133
|
+
avgBatchSize: number;
|
|
134
|
+
avgTokensPerBatch: number;
|
|
135
|
+
apiCallsSaved: number;
|
|
136
|
+
memoryUsed: number;
|
|
137
|
+
peakMemory: number;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Get processing status
|
|
141
|
+
*/
|
|
142
|
+
getStatus(): {
|
|
143
|
+
processing: boolean;
|
|
144
|
+
cancelled: boolean;
|
|
145
|
+
progress: number;
|
|
146
|
+
processedItems: number;
|
|
147
|
+
totalItems: number;
|
|
148
|
+
successfulBatches: number;
|
|
149
|
+
failedBatches: number;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Reset metrics
|
|
153
|
+
*/
|
|
154
|
+
resetMetrics(): void;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Create embedder with batch processing
|
|
158
|
+
*
|
|
159
|
+
* Wraps an embedder to use intelligent batch processing
|
|
160
|
+
*/
|
|
161
|
+
export function createBatchedEmbedder(embedder: any, options?: {}): any;
|
|
162
|
+
export namespace DEFAULT_CONFIG {
|
|
163
|
+
let maxTokensPerBatch: number;
|
|
164
|
+
let maxItemsPerBatch: number;
|
|
165
|
+
let minItemsPerBatch: number;
|
|
166
|
+
let maxMemoryMB: number;
|
|
167
|
+
let streamingThreshold: number;
|
|
168
|
+
let targetBatchUtilization: number;
|
|
169
|
+
let adaptiveSizing: boolean;
|
|
170
|
+
let maxRetries: number;
|
|
171
|
+
let retryDelay: number;
|
|
172
|
+
let retryBackoff: number;
|
|
173
|
+
let progressInterval: number;
|
|
174
|
+
let enableMetrics: boolean;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Model-specific token limits
|
|
178
|
+
*/
|
|
179
|
+
export const MODEL_LIMITS: {
|
|
180
|
+
'text-embedding-ada-002': {
|
|
181
|
+
maxTokens: number;
|
|
182
|
+
maxItems: number;
|
|
183
|
+
};
|
|
184
|
+
'text-embedding-3-small': {
|
|
185
|
+
maxTokens: number;
|
|
186
|
+
maxItems: number;
|
|
187
|
+
};
|
|
188
|
+
'text-embedding-3-large': {
|
|
189
|
+
maxTokens: number;
|
|
190
|
+
maxItems: number;
|
|
191
|
+
};
|
|
192
|
+
'voyage-large-2': {
|
|
193
|
+
maxTokens: number;
|
|
194
|
+
maxItems: number;
|
|
195
|
+
};
|
|
196
|
+
'cohere-embed-v3': {
|
|
197
|
+
maxTokens: number;
|
|
198
|
+
maxItems: number;
|
|
199
|
+
};
|
|
200
|
+
default: {
|
|
201
|
+
maxTokens: number;
|
|
202
|
+
maxItems: number;
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
import { EventEmitter } from "events";
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection Pool Manager Class
|
|
3
|
+
*/
|
|
4
|
+
export class ConnectionPoolManager extends EventEmitter<[never]> {
|
|
5
|
+
constructor(options?: {});
|
|
6
|
+
config: {
|
|
7
|
+
maxSockets: number;
|
|
8
|
+
maxFreeSockets: number;
|
|
9
|
+
maxSocketsPerHost: number;
|
|
10
|
+
timeout: number;
|
|
11
|
+
freeSocketTimeout: number;
|
|
12
|
+
keepAliveTimeout: number;
|
|
13
|
+
keepAlive: boolean;
|
|
14
|
+
keepAliveMsecs: number;
|
|
15
|
+
scheduling: string;
|
|
16
|
+
trackMetrics: boolean;
|
|
17
|
+
metricsInterval: number;
|
|
18
|
+
};
|
|
19
|
+
httpAgent: http.Agent;
|
|
20
|
+
httpsAgent: https.Agent;
|
|
21
|
+
pools: Map<any, any>;
|
|
22
|
+
activeConnections: number;
|
|
23
|
+
totalConnections: number;
|
|
24
|
+
metrics: {
|
|
25
|
+
totalRequests: number;
|
|
26
|
+
cachedConnections: number;
|
|
27
|
+
newConnections: number;
|
|
28
|
+
connectionReuse: number;
|
|
29
|
+
timeouts: number;
|
|
30
|
+
errors: number;
|
|
31
|
+
avgResponseTime: number;
|
|
32
|
+
totalResponseTime: number;
|
|
33
|
+
poolExhausted: number;
|
|
34
|
+
connectionsCreated: number;
|
|
35
|
+
connectionsDestroyed: number;
|
|
36
|
+
currentPoolSize: number;
|
|
37
|
+
peakPoolSize: number;
|
|
38
|
+
};
|
|
39
|
+
requestTimings: any[];
|
|
40
|
+
maxTimingsSamples: number;
|
|
41
|
+
/**
|
|
42
|
+
* Initialize HTTP and HTTPS agents with connection pooling
|
|
43
|
+
* @private
|
|
44
|
+
*/
|
|
45
|
+
private _initializeAgents;
|
|
46
|
+
/**
|
|
47
|
+
* Instrument agent for metrics collection
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
50
|
+
private _instrumentAgent;
|
|
51
|
+
/**
|
|
52
|
+
* Get HTTP agent
|
|
53
|
+
*
|
|
54
|
+
* @returns {http.Agent} HTTP agent with connection pooling
|
|
55
|
+
*/
|
|
56
|
+
getHttpAgent(): http.Agent;
|
|
57
|
+
/**
|
|
58
|
+
* Get HTTPS agent
|
|
59
|
+
*
|
|
60
|
+
* @returns {https.Agent} HTTPS agent with connection pooling
|
|
61
|
+
*/
|
|
62
|
+
getHttpsAgent(): https.Agent;
|
|
63
|
+
/**
|
|
64
|
+
* Get agent for URL
|
|
65
|
+
*
|
|
66
|
+
* @param {string} url - URL to get agent for
|
|
67
|
+
* @returns {http.Agent|https.Agent} Appropriate agent
|
|
68
|
+
*/
|
|
69
|
+
getAgentForUrl(url: string): http.Agent | https.Agent;
|
|
70
|
+
/**
|
|
71
|
+
* Track request timing
|
|
72
|
+
*
|
|
73
|
+
* @param {number} duration - Request duration in milliseconds
|
|
74
|
+
*/
|
|
75
|
+
trackRequest(duration: number): void;
|
|
76
|
+
/**
|
|
77
|
+
* Track request error
|
|
78
|
+
*
|
|
79
|
+
* @param {Error} error - Request error
|
|
80
|
+
* @param {string} type - Error type (timeout, network, etc.)
|
|
81
|
+
*/
|
|
82
|
+
trackError(error: Error, type?: string): void;
|
|
83
|
+
/**
|
|
84
|
+
* Track pool exhaustion
|
|
85
|
+
*/
|
|
86
|
+
trackPoolExhaustion(): void;
|
|
87
|
+
/**
|
|
88
|
+
* Get current pool metrics
|
|
89
|
+
*
|
|
90
|
+
* @returns {Object} Current metrics
|
|
91
|
+
*/
|
|
92
|
+
getMetrics(): any;
|
|
93
|
+
/**
|
|
94
|
+
* Get agent statistics
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
private _getAgentStats;
|
|
98
|
+
/**
|
|
99
|
+
* Get pool status
|
|
100
|
+
*
|
|
101
|
+
* @returns {Object} Pool status information
|
|
102
|
+
*/
|
|
103
|
+
getStatus(): any;
|
|
104
|
+
/**
|
|
105
|
+
* Reset metrics
|
|
106
|
+
*/
|
|
107
|
+
resetMetrics(): void;
|
|
108
|
+
/**
|
|
109
|
+
* Start periodic metrics collection
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
112
|
+
private _startMetricsCollection;
|
|
113
|
+
metricsInterval: NodeJS.Timeout;
|
|
114
|
+
/**
|
|
115
|
+
* Destroy all connections and cleanup
|
|
116
|
+
*/
|
|
117
|
+
destroy(): void;
|
|
118
|
+
/**
|
|
119
|
+
* Get connection pool report
|
|
120
|
+
*
|
|
121
|
+
* @returns {string} Human-readable pool report
|
|
122
|
+
*/
|
|
123
|
+
getReport(): string;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Get or create global connection pool
|
|
127
|
+
*
|
|
128
|
+
* @param {Object} options - Pool configuration options
|
|
129
|
+
* @returns {ConnectionPoolManager} Global pool instance
|
|
130
|
+
*/
|
|
131
|
+
export function getGlobalPool(options?: any): ConnectionPoolManager;
|
|
132
|
+
/**
|
|
133
|
+
* Reset global connection pool
|
|
134
|
+
*/
|
|
135
|
+
export function resetGlobalPool(): void;
|
|
136
|
+
/**
|
|
137
|
+
* Create fetch wrapper with connection pooling
|
|
138
|
+
*
|
|
139
|
+
* @param {ConnectionPoolManager} pool - Connection pool manager
|
|
140
|
+
* @returns {Function} Fetch function with pooling
|
|
141
|
+
*/
|
|
142
|
+
export function createPooledFetch(pool: ConnectionPoolManager): Function;
|
|
143
|
+
export namespace DEFAULT_CONFIG {
|
|
144
|
+
let maxSockets: number;
|
|
145
|
+
let maxFreeSockets: number;
|
|
146
|
+
let maxSocketsPerHost: number;
|
|
147
|
+
let timeout: number;
|
|
148
|
+
let freeSocketTimeout: number;
|
|
149
|
+
let keepAliveTimeout: number;
|
|
150
|
+
let keepAlive: boolean;
|
|
151
|
+
let keepAliveMsecs: number;
|
|
152
|
+
let scheduling: string;
|
|
153
|
+
let trackMetrics: boolean;
|
|
154
|
+
let metricsInterval: number;
|
|
155
|
+
}
|
|
156
|
+
import { EventEmitter } from "events";
|
|
157
|
+
import http = require("http");
|
|
158
|
+
import https = require("https");
|