@devilsdev/rag-pipeline-utils 1.0.0 → 1.0.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.
Files changed (44) hide show
  1. package/.dockerignore +14 -14
  2. package/.eslintignore +5 -0
  3. package/.eslintrc.json +21 -0
  4. package/.github/workflows/ci.yml +46 -42
  5. package/.github/workflows/release.yml +20 -21
  6. package/CHANGELOG.md +13 -0
  7. package/Dockerfile +22 -22
  8. package/LICENSE +674 -674
  9. package/README.md +117 -116
  10. package/__tests__/fixtures/sample-eval-dataset.json +14 -14
  11. package/__tests__/fixtures/sample.pdf +1 -0
  12. package/__tests__/integration/cli/config-flow.test.js +29 -44
  13. package/__tests__/integration/config/load-config.test.js +52 -52
  14. package/__tests__/unit/core/plugin-registry.test.js +55 -55
  15. package/__tests__/unit/reranker/llm-reranker.test.js +42 -42
  16. package/__tests__/unit/reranker/reranker.snapshot.test.js +17 -17
  17. package/bin/cli.js +168 -168
  18. package/docs-site/docs/Architecture.md +76 -76
  19. package/docs-site/docs/CLI.md +69 -69
  20. package/docs-site/docs/Evaluation.md +61 -61
  21. package/docs-site/docs/FAQ.md +50 -50
  22. package/docs-site/docs/Plugins.md +61 -61
  23. package/docs-site/docs/Usage.md +72 -72
  24. package/jest.config.js +20 -13
  25. package/package.json +4 -2
  26. package/public/src/App.jsx +57 -57
  27. package/release.config.js +36 -23
  28. package/scripts/setup.js +7 -0
  29. package/scripts/test-global-setup.js +18 -0
  30. package/server.js +31 -31
  31. package/src/config/load-config.js +35 -35
  32. package/src/config/validate-schema.js +49 -49
  33. package/src/core/create-pipeline.js +82 -82
  34. package/src/core/plugin-registry.js +61 -61
  35. package/src/dag/dag-engine.js +69 -69
  36. package/src/evaluate/evaluator.js +44 -44
  37. package/src/evaluate/scoring.js +73 -73
  38. package/src/loader/csv-loader.js +30 -30
  39. package/src/loader/directory-loader.js +37 -37
  40. package/src/loader/html-loader.js +57 -57
  41. package/src/loader/markdown-loader.js +58 -58
  42. package/src/reranker/llm-reranker.js +48 -48
  43. package/src/utils/logger.js +29 -29
  44. package/src/utils/retry.js +42 -42
package/README.md CHANGED
@@ -1,116 +1,117 @@
1
- # @DevilsDev/rag-pipeline-utils
2
-
3
- [![CI](https://github.com/DevilsDev/rag-pipeline-utils/actions/workflows/ci.yml/badge.svg)](https://github.com/DevilsDev/rag-pipeline-utils/actions)
4
- [![codecov](https://codecov.io/gh/DevilsDev/rag-pipeline-utils/branch/main/graph/badge.svg)](https://codecov.io/gh/DevilsDev/rag-pipeline-utils)
5
-
6
- **A modular Node.js toolkit for building RAG (Retrieval-Augmented Generation) pipelines with clean abstractions, pluggable components, and scalable architecture.**
7
-
8
- ---
9
-
10
- ## Features
11
-
12
- - ✅ Plugin-based architecture for RAG pipelines
13
- - ✅ Embedding + retrieval abstraction
14
- - ✅ Vector DB integrations (e.g., Pinecone)
15
- - ✅ Modular document loaders (PDF, HTML, Markdown, CSV, Directory)
16
- - ✅ CLI with `.ragrc.json` config fallback
17
- - ✅ Retry, backoff, and logging built-in
18
- - ✅ Reranker support and evaluation tooling
19
- - ✅ React dashboard for evaluation results
20
-
21
- ---
22
-
23
- ## Installation
24
-
25
- ```bash
26
- npm install @yourorg/rag-pipeline-utils
27
- ```
28
-
29
- ---
30
-
31
- ## Usage
32
-
33
- ```js
34
- import { createRagPipeline } from '@yourorg/rag-pipeline-utils';
35
-
36
- const rag = createRagPipeline({
37
- loader: 'markdown',
38
- embedder: 'openai',
39
- retriever: 'pinecone',
40
- llm: 'openai-gpt-4',
41
- useReranker: true
42
- });
43
-
44
- const answer = await rag.query('What is a vector database?');
45
- console.log(answer);
46
- ```
47
-
48
- ---
49
-
50
- ## Testing
51
-
52
- ```bash
53
- npm test
54
- ```
55
-
56
- ---
57
-
58
- ## CLI
59
-
60
- ```bash
61
- rag-pipeline ingest ./docs/my.pdf --loader pdf
62
- rag-pipeline query "Explain RAG pipelines" --llm openai-gpt-4
63
- rag-pipeline evaluate ./fixtures/sample-eval-dataset.json
64
- rag-pipeline rerank "Explain embeddings" --retriever pinecone
65
- ```
66
-
67
- Supports `.ragrc.json` fallback configuration:
68
-
69
- ```json
70
- {
71
- "loader": "directory",
72
- "embedder": "openai",
73
- "retriever": "pinecone",
74
- "llm": "openai-gpt-4",
75
- "useReranker": true
76
- }
77
- ```
78
-
79
- ---
80
-
81
- ## Project Structure
82
-
83
- ```
84
- /src
85
- ├── core/ # Pipeline + registry
86
- ├── loader/ # Loaders for md, html, csv, dir
87
- ├── reranker/ # LLM-based context reranker
88
- ├── evaluate/ # Scoring, batch QA evaluation
89
- ├── utils/ # Logger, retry, schema
90
- /bin/cli.js # CLI entrypoint
91
- /__tests__/ # Unit, integration, snapshot tests
92
- /public/ # Evaluation dashboard (React)
93
- .github/workflows/ci.yml # CI config
94
- ```
95
-
96
- ---
97
-
98
- ## Evaluation Dashboard
99
-
100
- ```bash
101
- node server.js
102
- ```
103
-
104
- Visit: [http://localhost:3000](http://localhost:3000) → Visualizes metrics, BLEU/ROUGE, pass rate.
105
-
106
- ---
107
-
108
- ## License
109
-
110
- Apache-2.0
111
-
112
- ---
113
-
114
- ## 👤 Author
115
-
116
- Ali Kahwaji — [@DevilsDev](https://github.com/DevilsDev)
1
+ # @DevilsDev/rag-pipeline-utils
2
+
3
+ [![CI](https://github.com/DevilsDev/rag-pipeline-utils/actions/workflows/ci.yml/badge.svg)](https://github.com/DevilsDev/rag-pipeline-utils/actions)
4
+ [![codecov](https://codecov.io/gh/DevilsDev/rag-pipeline-utils/branch/main/graph/badge.svg)](https://codecov.io/gh/DevilsDev/rag-pipeline-utils)
5
+ ![Release](https://img.shields.io/github/v/release/DevilsDev/rag-pipeline-utils?label=release)
6
+
7
+ **A modular Node.js toolkit for building RAG (Retrieval-Augmented Generation) pipelines with clean abstractions, pluggable components, and scalable architecture.**
8
+
9
+ ---
10
+
11
+ ## Features
12
+
13
+ - ✅ Plugin-based architecture for RAG pipelines
14
+ - ✅ Embedding + retrieval abstraction
15
+ - ✅ Vector DB integrations (e.g., Pinecone)
16
+ - ✅ Modular document loaders (PDF, HTML, Markdown, CSV, Directory)
17
+ - ✅ CLI with `.ragrc.json` config fallback
18
+ - ✅ Retry, backoff, and logging built-in
19
+ - ✅ Reranker support and evaluation tooling
20
+ - ✅ React dashboard for evaluation results
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ npm install @yourorg/rag-pipeline-utils
28
+ ```
29
+
30
+ ---
31
+
32
+ ## Usage
33
+
34
+ ```js
35
+ import { createRagPipeline } from '@yourorg/rag-pipeline-utils';
36
+
37
+ const rag = createRagPipeline({
38
+ loader: 'markdown',
39
+ embedder: 'openai',
40
+ retriever: 'pinecone',
41
+ llm: 'openai-gpt-4',
42
+ useReranker: true
43
+ });
44
+
45
+ const answer = await rag.query('What is a vector database?');
46
+ console.log(answer);
47
+ ```
48
+
49
+ ---
50
+
51
+ ## Testing
52
+
53
+ ```bash
54
+ npm test
55
+ ```
56
+
57
+ ---
58
+
59
+ ## CLI
60
+
61
+ ```bash
62
+ rag-pipeline ingest ./docs/my.pdf --loader pdf
63
+ rag-pipeline query "Explain RAG pipelines" --llm openai-gpt-4
64
+ rag-pipeline evaluate ./fixtures/sample-eval-dataset.json
65
+ rag-pipeline rerank "Explain embeddings" --retriever pinecone
66
+ ```
67
+
68
+ Supports `.ragrc.json` fallback configuration:
69
+
70
+ ```json
71
+ {
72
+ "loader": "directory",
73
+ "embedder": "openai",
74
+ "retriever": "pinecone",
75
+ "llm": "openai-gpt-4",
76
+ "useReranker": true
77
+ }
78
+ ```
79
+
80
+ ---
81
+
82
+ ## Project Structure
83
+
84
+ ```
85
+ /src
86
+ ├── core/ # Pipeline + registry
87
+ ├── loader/ # Loaders for md, html, csv, dir
88
+ ├── reranker/ # LLM-based context reranker
89
+ ├── evaluate/ # Scoring, batch QA evaluation
90
+ ├── utils/ # Logger, retry, schema
91
+ /bin/cli.js # CLI entrypoint
92
+ /__tests__/ # Unit, integration, snapshot tests
93
+ /public/ # Evaluation dashboard (React)
94
+ .github/workflows/ci.yml # CI config
95
+ ```
96
+
97
+ ---
98
+
99
+ ## Evaluation Dashboard
100
+
101
+ ```bash
102
+ node server.js
103
+ ```
104
+
105
+ Visit: [http://localhost:3000](http://localhost:3000) → Visualizes metrics, BLEU/ROUGE, pass rate.
106
+
107
+ ---
108
+
109
+ ## License
110
+
111
+ Apache-2.0
112
+
113
+ ---
114
+
115
+ ## 👤 Author
116
+
117
+ Ali Kahwaji — [@DevilsDev](https://github.com/DevilsDev)
@@ -1,15 +1,15 @@
1
- [
2
- {
3
- "prompt": "What is a vector database?",
4
- "expected": "A vector database is a system optimized for storing and retrieving data represented as vectors, often used in similarity search."
5
- },
6
- {
7
- "prompt": "Explain what chunking means in RAG.",
8
- "expected": "Chunking is the process of splitting documents into smaller, manageable pieces for embedding and retrieval in a RAG pipeline."
9
- },
10
- {
11
- "prompt": "What is the role of embedding in a RAG pipeline?",
12
- "expected": "Embeddings convert text into numerical vector form so they can be compared in a vector database during retrieval."
13
- }
14
- ]
1
+ [
2
+ {
3
+ "prompt": "What is a vector database?",
4
+ "expected": "A vector database is a system optimized for storing and retrieving data represented as vectors, often used in similarity search."
5
+ },
6
+ {
7
+ "prompt": "Explain what chunking means in RAG.",
8
+ "expected": "Chunking is the process of splitting documents into smaller, manageable pieces for embedding and retrieval in a RAG pipeline."
9
+ },
10
+ {
11
+ "prompt": "What is the role of embedding in a RAG pipeline?",
12
+ "expected": "Embeddings convert text into numerical vector form so they can be compared in a vector database during retrieval."
13
+ }
14
+ ]
15
15
 
@@ -0,0 +1 @@
1
+ %PDF-1.4\n%âãÏÓ\n1 0 obj\n<<>>\nendobj\nxref\n0 2\n0000000000 65535 f\n0000000010 00000 n\ntrailer\n<<>>\nstartxref\n9\n%%EOF
@@ -1,44 +1,29 @@
1
- /**
2
- * Version: 0.1.0
3
- * Path: /__tests__/integration/cli/config-flow.test.js
4
- * Description: Integration tests for CLI fallback to .ragrc.json and full flow execution
5
- * Author: Ali Kahwaji
6
- */
7
-
8
- import fs from 'fs';
9
- import path from 'path';
10
- import { execSync } from 'child_process';
11
-
12
- const CONFIG_PATH = path.resolve(process.cwd(), '.ragrc.json');
13
- const CLI_PATH = path.resolve('./bin/cli.js');
14
-
15
- const validConfig = {
16
- loader: 'pdf',
17
- embedder: 'openai',
18
- retriever: 'pinecone',
19
- llm: 'openai-gpt-4',
20
- chunk: {
21
- strategy: 'semantic',
22
- size: 256
23
- }
24
- };
25
-
26
- describe('CLI integration with .ragrc.json config fallback', () => {
27
- beforeEach(() => {
28
- fs.writeFileSync(CONFIG_PATH, JSON.stringify(validConfig, null, 2));
29
- });
30
-
31
- afterEach(() => {
32
- if (fs.existsSync(CONFIG_PATH)) fs.unlinkSync(CONFIG_PATH);
33
- });
34
-
35
- test('executes CLI ingest using config fallback', () => {
36
- const result = execSync(`node ${CLI_PATH} ingest ./sample.pdf`, { encoding: 'utf-8' });
37
- expect(result).toContain('Ingestion complete');
38
- });
39
-
40
- test('executes CLI query using config fallback', () => {
41
- const output = execSync(`node ${CLI_PATH} query "What is a vector?"`, { encoding: 'utf-8' });
42
- expect(output).toContain('Answer:');
43
- });
44
- });
1
+ /**
2
+ * Version: 0.1.3
3
+ * Description: Integration test for CLI with config fallback
4
+ * Author: Ali Kahwaji
5
+ */
6
+
7
+ import { execSync } from 'child_process';
8
+ import path from 'path';
9
+
10
+ const CLI_PATH = path.resolve('bin/cli.js');
11
+
12
+ describe('CLI integration with .ragrc.json config fallback', () => {
13
+ test('executes CLI ingest using config fallback', () => {
14
+ const result = execSync(
15
+ `node ${CLI_PATH} ingest ./__tests__/fixtures/sample.pdf --loader pdf --embedder openai --retriever pinecone`,
16
+ { encoding: 'utf-8' }
17
+ );
18
+ expect(result).toContain('Ingestion complete');
19
+ });
20
+
21
+ test('executes CLI query using config fallback', () => {
22
+ const result = execSync(
23
+ `node ${CLI_PATH} query "What is this test about?" --embedder openai --retriever pinecone --llm openai-gpt-4`,
24
+ { encoding: 'utf-8' }
25
+ );
26
+ // Adjust expectation to check for a known part of the generated answer
27
+ expect(result).toContain('Generated answer using: Chunk about pine trees');
28
+ });
29
+ });
@@ -1,52 +1,52 @@
1
- /**
2
- * Version: 0.1.0
3
- * Path: /__tests__/integration/config/load-config.test.js
4
- * Description: Integration tests for loading and validating .ragrc.json config
5
- * Author: Ali Kahwaji
6
- */
7
-
8
- import fs from 'fs';
9
- import path from 'path';
10
- import { loadRagConfig } from '../../../src/config/load-config.js';
11
-
12
- const FIXTURE_PATH = path.resolve(process.cwd(), '.ragrc.json');
13
-
14
- const validConfig = {
15
- loader: 'pdf',
16
- embedder: 'openai',
17
- retriever: 'pinecone',
18
- llm: 'openai-gpt-4',
19
- chunk: {
20
- strategy: 'semantic',
21
- size: 500
22
- }
23
- };
24
-
25
- const invalidConfig = {
26
- loader: 'pdf',
27
- chunk: { size: 'large' } // Missing required keys + invalid type
28
- };
29
-
30
- describe('loadRagConfig()', () => {
31
- afterEach(() => {
32
- if (fs.existsSync(FIXTURE_PATH)) {
33
- fs.unlinkSync(FIXTURE_PATH);
34
- }
35
- });
36
-
37
- test('loads a valid .ragrc.json config file successfully', () => {
38
- fs.writeFileSync(FIXTURE_PATH, JSON.stringify(validConfig, null, 2));
39
- const result = loadRagConfig();
40
- expect(result).toMatchObject(validConfig);
41
- });
42
-
43
- test('throws validation error for invalid .ragrc.json config', () => {
44
- fs.writeFileSync(FIXTURE_PATH, JSON.stringify(invalidConfig, null, 2));
45
- expect(() => loadRagConfig()).toThrow(/Invalid config/);
46
- });
47
-
48
- test('throws error if config file does not exist', () => {
49
- expect(() => loadRagConfig()).toThrow(/Config file not found/);
50
- });
51
- });
52
-
1
+ /**
2
+ * Version: 0.1.0
3
+ * Path: /__tests__/integration/config/load-config.test.js
4
+ * Description: Integration tests for loading and validating .ragrc.json config
5
+ * Author: Ali Kahwaji
6
+ */
7
+
8
+ import fs from 'fs';
9
+ import path from 'path';
10
+ import { loadRagConfig } from '../../../src/config/load-config.js';
11
+
12
+ const FIXTURE_PATH = path.resolve(process.cwd(), '.ragrc.json');
13
+
14
+ const validConfig = {
15
+ loader: 'pdf',
16
+ embedder: 'openai',
17
+ retriever: 'pinecone',
18
+ llm: 'openai-gpt-4',
19
+ chunk: {
20
+ strategy: 'semantic',
21
+ size: 500
22
+ }
23
+ };
24
+
25
+ const invalidConfig = {
26
+ loader: 'pdf',
27
+ chunk: { size: 'large' } // Missing required keys + invalid type
28
+ };
29
+
30
+ describe('loadRagConfig()', () => {
31
+ afterEach(() => {
32
+ if (fs.existsSync(FIXTURE_PATH)) {
33
+ fs.unlinkSync(FIXTURE_PATH);
34
+ }
35
+ });
36
+
37
+ test('loads a valid .ragrc.json config file successfully', () => {
38
+ fs.writeFileSync(FIXTURE_PATH, JSON.stringify(validConfig, null, 2));
39
+ const result = loadRagConfig();
40
+ expect(result).toMatchObject(validConfig);
41
+ });
42
+
43
+ test('throws validation error for invalid .ragrc.json config', () => {
44
+ fs.writeFileSync(FIXTURE_PATH, JSON.stringify(invalidConfig, null, 2));
45
+ expect(() => loadRagConfig()).toThrow(/Invalid config/);
46
+ });
47
+
48
+ test('throws error if config file does not exist', () => {
49
+ expect(() => loadRagConfig()).toThrow(/Config file not found/);
50
+ });
51
+ });
52
+
@@ -1,55 +1,55 @@
1
- /**
2
- * Version: 0.1.0
3
- * Path: /__tests__/unit/core/plugin-registry.test.js
4
- * Description: Unit tests for the PluginRegistry class
5
- * Author: Ali Kahwaji
6
- */
7
-
8
- import { PluginRegistry } from '../../../src/core/plugin-registry.js';
9
-
10
- describe('PluginRegistry', () => {
11
- let registry;
12
-
13
- beforeEach(() => {
14
- registry = new PluginRegistry();
15
- });
16
-
17
- test('registers and retrieves a plugin successfully', () => {
18
- const mockPlugin = { name: 'test-loader' };
19
- registry.register('loader', 'test', mockPlugin);
20
- const result = registry.get('loader', 'test');
21
- expect(result).toBe(mockPlugin);
22
- });
23
-
24
- test('overwrites existing plugin under the same name', () => {
25
- const firstPlugin = { name: 'first' };
26
- const secondPlugin = { name: 'second' };
27
- registry.register('embedder', 'dup', firstPlugin);
28
- registry.register('embedder', 'dup', secondPlugin);
29
- const result = registry.get('embedder', 'dup');
30
- expect(result).toBe(secondPlugin);
31
- });
32
-
33
- test('throws error for unknown plugin type on registration', () => {
34
- expect(() => registry.register('unknown-type', 'x', {})).toThrow(/Unknown plugin type/);
35
- });
36
-
37
- test('throws error when retrieving a nonexistent plugin', () => {
38
- expect(() => registry.get('retriever', 'missing')).toThrow(/Plugin not found/);
39
- });
40
-
41
- test('lists all registered plugin names for a type', () => {
42
- registry.register('llm', 'gpt-4', {});
43
- registry.register('llm', 'gpt-3.5', {});
44
- const list = registry.list('llm');
45
- expect(list).toContain('gpt-4');
46
- expect(list).toContain('gpt-3.5');
47
- expect(list.length).toBe(2);
48
- });
49
-
50
- test('throws error when listing plugins for invalid type', () => {
51
- expect(() => registry.list('invalid')).toThrow(/Unknown plugin type/);
52
- });
53
- });
54
-
55
-
1
+ /**
2
+ * Version: 0.1.0
3
+ * Path: /__tests__/unit/core/plugin-registry.test.js
4
+ * Description: Unit tests for the PluginRegistry class
5
+ * Author: Ali Kahwaji
6
+ */
7
+
8
+ import { PluginRegistry } from '../../../src/core/plugin-registry.js';
9
+
10
+ describe('PluginRegistry', () => {
11
+ let registry;
12
+
13
+ beforeEach(() => {
14
+ registry = new PluginRegistry();
15
+ });
16
+
17
+ test('registers and retrieves a plugin successfully', () => {
18
+ const mockPlugin = { name: 'test-loader' };
19
+ registry.register('loader', 'test', mockPlugin);
20
+ const result = registry.get('loader', 'test');
21
+ expect(result).toBe(mockPlugin);
22
+ });
23
+
24
+ test('overwrites existing plugin under the same name', () => {
25
+ const firstPlugin = { name: 'first' };
26
+ const secondPlugin = { name: 'second' };
27
+ registry.register('embedder', 'dup', firstPlugin);
28
+ registry.register('embedder', 'dup', secondPlugin);
29
+ const result = registry.get('embedder', 'dup');
30
+ expect(result).toBe(secondPlugin);
31
+ });
32
+
33
+ test('throws error for unknown plugin type on registration', () => {
34
+ expect(() => registry.register('unknown-type', 'x', {})).toThrow(/Unknown plugin type/);
35
+ });
36
+
37
+ test('throws error when retrieving a nonexistent plugin', () => {
38
+ expect(() => registry.get('retriever', 'missing')).toThrow(/Plugin not found/);
39
+ });
40
+
41
+ test('lists all registered plugin names for a type', () => {
42
+ registry.register('llm', 'gpt-4', {});
43
+ registry.register('llm', 'gpt-3.5', {});
44
+ const list = registry.list('llm');
45
+ expect(list).toContain('gpt-4');
46
+ expect(list).toContain('gpt-3.5');
47
+ expect(list.length).toBe(2);
48
+ });
49
+
50
+ test('throws error when listing plugins for invalid type', () => {
51
+ expect(() => registry.list('invalid')).toThrow(/Unknown plugin type/);
52
+ });
53
+ });
54
+
55
+
@@ -1,42 +1,42 @@
1
- /**
2
- * Version: 0.1.0
3
- * Path: /__tests__/unit/reranker/llm-reranker.test.js
4
- * Description: Unit tests for LLM-based context reranker
5
- * Author: Ali Kahwaji
6
- */
7
-
8
- import { LLMReranker } from '../../../src/reranker/llm-reranker.js';
9
-
10
- describe('LLMReranker', () => {
11
- const mockDocs = [
12
- { text: 'context about math' },
13
- { text: 'context about vectors' },
14
- { text: 'context about nothing' }
15
- ];
16
-
17
- test('returns reranked documents based on mock LLM JSON output', async () => {
18
- const mockLLM = {
19
- async generate() {
20
- return '[1, 0, 2]';
21
- }
22
- };
23
- const reranker = new LLMReranker({ llm: mockLLM });
24
- const result = await reranker.rerank('What are vectors?', mockDocs);
25
- expect(result.map(d => d.text)).toEqual([
26
- 'context about vectors',
27
- 'context about math',
28
- 'context about nothing'
29
- ]);
30
- });
31
-
32
- test('gracefully falls back if LLM returns invalid JSON', async () => {
33
- const mockLLM = {
34
- async generate() {
35
- return 'this is not JSON';
36
- }
37
- };
38
- const reranker = new LLMReranker({ llm: mockLLM });
39
- const result = await reranker.rerank('Explain.', mockDocs);
40
- expect(result.length).toBe(0);
41
- });
42
- });
1
+ /**
2
+ * Version: 0.1.0
3
+ * Path: /__tests__/unit/reranker/llm-reranker.test.js
4
+ * Description: Unit tests for LLM-based context reranker
5
+ * Author: Ali Kahwaji
6
+ */
7
+
8
+ import { LLMReranker } from '../../../src/reranker/llm-reranker.js';
9
+
10
+ describe('LLMReranker', () => {
11
+ const mockDocs = [
12
+ { text: 'context about math' },
13
+ { text: 'context about vectors' },
14
+ { text: 'context about nothing' }
15
+ ];
16
+
17
+ test('returns reranked documents based on mock LLM JSON output', async () => {
18
+ const mockLLM = {
19
+ async generate() {
20
+ return '[1, 0, 2]';
21
+ }
22
+ };
23
+ const reranker = new LLMReranker({ llm: mockLLM });
24
+ const result = await reranker.rerank('What are vectors?', mockDocs);
25
+ expect(result.map(d => d.text)).toEqual([
26
+ 'context about vectors',
27
+ 'context about math',
28
+ 'context about nothing'
29
+ ]);
30
+ });
31
+
32
+ test('gracefully falls back if LLM returns invalid JSON', async () => {
33
+ const mockLLM = {
34
+ async generate() {
35
+ return 'this is not JSON';
36
+ }
37
+ };
38
+ const reranker = new LLMReranker({ llm: mockLLM });
39
+ const result = await reranker.rerank('Explain.', mockDocs);
40
+ expect(result.length).toBe(0);
41
+ });
42
+ });