@retrivora-ai/rag-engine 0.1.4 → 0.1.5
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/DocumentChunker-cfaMidtA.d.mts +93 -0
- package/dist/DocumentChunker-cfaMidtA.d.ts +93 -0
- package/dist/{LLMFactory-XC55FTD2.mjs → LLMFactory-JFOY2V4X.mjs} +2 -1
- package/dist/{Pipeline-Bo6CUCox.d.ts → RagConfig-DG_0f8ka.d.mts} +23 -93
- package/dist/{Pipeline-Bo6CUCox.d.mts → RagConfig-DG_0f8ka.d.ts} +23 -93
- package/dist/{chunk-AIAB2IEE.mjs → chunk-BP4U4TT5.mjs} +158 -4
- package/dist/{chunk-GD3QJFNN.mjs → chunk-JI6VD5TJ.mjs} +4 -41
- package/dist/chunk-UKDXCXW7.mjs +49 -0
- package/dist/handlers/index.d.mts +1 -1
- package/dist/handlers/index.d.ts +1 -1
- package/dist/handlers/index.mjs +3 -3
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +19 -1646
- package/dist/index.mjs +1 -11
- package/dist/server.d.mts +135 -5
- package/dist/server.d.ts +135 -5
- package/dist/server.mjs +7 -8
- package/package.json +19 -2
- package/src/core/Pipeline.ts +2 -10
- package/src/core/VectorPlugin.ts +2 -1
- package/src/index.ts +1 -7
- package/src/server.ts +1 -1
- package/src/types/index.ts +11 -0
- package/dist/DocumentChunker-BQ5kQD7B.d.ts +0 -155
- package/dist/DocumentChunker-D9-fObJp.d.mts +0 -155
- package/dist/chunk-FGGSVVSY.mjs +0 -162
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { e as RagConfig, C as ChatMessage, b as ChatResponse, d as IngestDocument, g as VectorDBConfig, f as UpsertDocument, V as VectorMatch, L as LLMConfig, c as EmbeddingConfig, I as ILLMProvider } from './Pipeline-Bo6CUCox.mjs';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* VectorPlugin — main orchestrator class.
|
|
5
|
-
* This is the primary interface for host applications.
|
|
6
|
-
*/
|
|
7
|
-
declare class VectorPlugin {
|
|
8
|
-
private config;
|
|
9
|
-
private pipeline;
|
|
10
|
-
/**
|
|
11
|
-
* Initializes the plugin with the host configuration.
|
|
12
|
-
* @param hostConfig - Configuration object passed from the host application.
|
|
13
|
-
*/
|
|
14
|
-
constructor(hostConfig?: Partial<RagConfig>);
|
|
15
|
-
/**
|
|
16
|
-
* Get the current resolved configuration.
|
|
17
|
-
*/
|
|
18
|
-
getConfig(): RagConfig;
|
|
19
|
-
/**
|
|
20
|
-
* Run a chat query.
|
|
21
|
-
*/
|
|
22
|
-
chat(message: string, history?: ChatMessage[], namespace?: string): Promise<ChatResponse>;
|
|
23
|
-
/**
|
|
24
|
-
* Ingest documents into the vector database.
|
|
25
|
-
*/
|
|
26
|
-
ingest(documents: IngestDocument[], namespace?: string): Promise<Array<{
|
|
27
|
-
docId: string;
|
|
28
|
-
chunksIngested: number;
|
|
29
|
-
}>>;
|
|
30
|
-
/**
|
|
31
|
-
* Check the health of the connected providers.
|
|
32
|
-
*/
|
|
33
|
-
health(): Promise<Record<string, boolean>>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* ConfigResolver — validates and normalizes host configuration.
|
|
38
|
-
* It merges host-provided config with environment defaults.
|
|
39
|
-
*/
|
|
40
|
-
declare class ConfigResolver {
|
|
41
|
-
/**
|
|
42
|
-
* Resolves the final configuration by merging host-provided config with defaults.
|
|
43
|
-
* @param hostConfig - Partial configuration passed from the host application.
|
|
44
|
-
*/
|
|
45
|
-
static resolve(hostConfig?: Partial<RagConfig>): RagConfig;
|
|
46
|
-
/**
|
|
47
|
-
* Validates the configuration for required fields.
|
|
48
|
-
*/
|
|
49
|
-
static validate(config: RagConfig): void;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* BaseVectorProvider — Abstract base class for all vector database providers.
|
|
54
|
-
* Each provider (Pinecone, Milvus, Redis, etc.) must extend this class.
|
|
55
|
-
*/
|
|
56
|
-
declare abstract class BaseVectorProvider {
|
|
57
|
-
protected readonly config: VectorDBConfig;
|
|
58
|
-
protected readonly indexName: string;
|
|
59
|
-
constructor(config: VectorDBConfig);
|
|
60
|
-
/**
|
|
61
|
-
* Initialise the connection (create tables, verify index, etc.)
|
|
62
|
-
*/
|
|
63
|
-
abstract initialize(): Promise<void>;
|
|
64
|
-
/**
|
|
65
|
-
* Insert or update a single vector + content pair.
|
|
66
|
-
*/
|
|
67
|
-
abstract upsert(doc: UpsertDocument, namespace?: string): Promise<void>;
|
|
68
|
-
/**
|
|
69
|
-
* Batch upsert for efficient ingestion of many documents.
|
|
70
|
-
*/
|
|
71
|
-
abstract batchUpsert(docs: UpsertDocument[], namespace?: string): Promise<void>;
|
|
72
|
-
/**
|
|
73
|
-
* Find the top-K most similar vectors to the query vector.
|
|
74
|
-
*/
|
|
75
|
-
abstract query(vector: number[], topK: number, namespace?: string, filter?: Record<string, unknown>): Promise<VectorMatch[]>;
|
|
76
|
-
/**
|
|
77
|
-
* Delete a stored vector by ID.
|
|
78
|
-
*/
|
|
79
|
-
abstract delete(id: string, namespace?: string): Promise<void>;
|
|
80
|
-
/**
|
|
81
|
-
* Delete all vectors in a namespace (full data reset for a project).
|
|
82
|
-
*/
|
|
83
|
-
abstract deleteNamespace(namespace: string): Promise<void>;
|
|
84
|
-
/**
|
|
85
|
-
* Check if the underlying DB is reachable.
|
|
86
|
-
*/
|
|
87
|
-
abstract ping(): Promise<boolean>;
|
|
88
|
-
/**
|
|
89
|
-
* Gracefully close the connection.
|
|
90
|
-
*/
|
|
91
|
-
abstract disconnect(): Promise<void>;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* ProviderRegistry — dynamic provider loader for Vector DBs and LLMs.
|
|
96
|
-
*/
|
|
97
|
-
declare class ProviderRegistry {
|
|
98
|
-
private static vectorProviders;
|
|
99
|
-
/**
|
|
100
|
-
* Register a custom vector provider.
|
|
101
|
-
*/
|
|
102
|
-
static registerVectorProvider(name: string, providerClass: new (config: VectorDBConfig) => BaseVectorProvider): void;
|
|
103
|
-
/**
|
|
104
|
-
* Creates a vector database provider based on the configuration.
|
|
105
|
-
*/
|
|
106
|
-
static createVectorProvider(config: VectorDBConfig): Promise<BaseVectorProvider>;
|
|
107
|
-
/**
|
|
108
|
-
* Creates an LLM provider based on the configuration.
|
|
109
|
-
*/
|
|
110
|
-
static createLLMProvider(llmConfig: LLMConfig, embeddingConfig?: EmbeddingConfig): ILLMProvider;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* DocumentChunker — splits long text into overlapping chunks
|
|
115
|
-
* suitable for vector embedding and retrieval.
|
|
116
|
-
*
|
|
117
|
-
* Strategy: sentence-aware sliding window
|
|
118
|
-
* 1. Split on sentence boundaries
|
|
119
|
-
* 2. Accumulate sentences into chunks up to `chunkSize` characters
|
|
120
|
-
* 3. Carry over `chunkOverlap` characters from the previous chunk
|
|
121
|
-
*/
|
|
122
|
-
interface Chunk {
|
|
123
|
-
id: string;
|
|
124
|
-
content: string;
|
|
125
|
-
metadata?: Record<string, unknown>;
|
|
126
|
-
}
|
|
127
|
-
interface ChunkOptions {
|
|
128
|
-
/** Target chunk size in characters (default 1000) */
|
|
129
|
-
chunkSize?: number;
|
|
130
|
-
/** Overlap between consecutive chunks in characters (default 200) */
|
|
131
|
-
chunkOverlap?: number;
|
|
132
|
-
/** Source document identifier used as ID prefix */
|
|
133
|
-
docId?: string;
|
|
134
|
-
/** Extra metadata to attach to every chunk */
|
|
135
|
-
metadata?: Record<string, unknown>;
|
|
136
|
-
}
|
|
137
|
-
declare class DocumentChunker {
|
|
138
|
-
private readonly chunkSize;
|
|
139
|
-
private readonly chunkOverlap;
|
|
140
|
-
constructor(chunkSize?: number, chunkOverlap?: number);
|
|
141
|
-
/**
|
|
142
|
-
* Split a single text string into overlapping chunks.
|
|
143
|
-
*/
|
|
144
|
-
chunk(text: string, options?: ChunkOptions): Chunk[];
|
|
145
|
-
/**
|
|
146
|
-
* Chunk multiple documents at once.
|
|
147
|
-
*/
|
|
148
|
-
chunkMany(documents: Array<{
|
|
149
|
-
content: string;
|
|
150
|
-
docId?: string;
|
|
151
|
-
metadata?: Record<string, unknown>;
|
|
152
|
-
}>): Chunk[];
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export { BaseVectorProvider as B, type Chunk as C, DocumentChunker as D, ProviderRegistry as P, VectorPlugin as V, type ChunkOptions as a, ConfigResolver as b };
|
package/dist/chunk-FGGSVVSY.mjs
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
VectorPlugin
|
|
3
|
-
} from "./chunk-AIAB2IEE.mjs";
|
|
4
|
-
import {
|
|
5
|
-
__spreadProps,
|
|
6
|
-
__spreadValues
|
|
7
|
-
} from "./chunk-I4E63NIC.mjs";
|
|
8
|
-
|
|
9
|
-
// src/handlers/index.ts
|
|
10
|
-
import { NextResponse } from "next/server";
|
|
11
|
-
|
|
12
|
-
// src/utils/DocumentParser.ts
|
|
13
|
-
var DocumentParser = class {
|
|
14
|
-
/**
|
|
15
|
-
* Extract text from a File or Buffer based on its type.
|
|
16
|
-
*/
|
|
17
|
-
static async parse(file, fileName, mimeType) {
|
|
18
|
-
var _a;
|
|
19
|
-
const extension = (_a = fileName.split(".").pop()) == null ? void 0 : _a.toLowerCase();
|
|
20
|
-
if (extension === "txt" || extension === "md" || mimeType === "text/plain" || mimeType === "text/markdown") {
|
|
21
|
-
return this.readAsText(file);
|
|
22
|
-
}
|
|
23
|
-
if (extension === "json" || mimeType === "application/json") {
|
|
24
|
-
const text = await this.readAsText(file);
|
|
25
|
-
try {
|
|
26
|
-
const obj = JSON.parse(text);
|
|
27
|
-
return JSON.stringify(obj, null, 2);
|
|
28
|
-
} catch (e) {
|
|
29
|
-
return text;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (extension === "csv" || mimeType === "text/csv") {
|
|
33
|
-
return this.readAsText(file);
|
|
34
|
-
}
|
|
35
|
-
if (extension === "pdf" || mimeType === "application/pdf") {
|
|
36
|
-
try {
|
|
37
|
-
const pdf = await import("pdf-parse/lib/pdf-parse.js");
|
|
38
|
-
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
39
|
-
const data = await pdf.default(buffer);
|
|
40
|
-
return data.text;
|
|
41
|
-
} catch (err) {
|
|
42
|
-
console.warn('[DocumentParser] PDF parsing failed. Make sure "pdf-parse" is installed.', err);
|
|
43
|
-
return `[PDF Parsing Error for ${fileName}]`;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (extension === "docx" || mimeType === "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
|
|
47
|
-
try {
|
|
48
|
-
const mammoth = await import("mammoth");
|
|
49
|
-
const buffer = Buffer.isBuffer(file) ? file : Buffer.from(await file.arrayBuffer());
|
|
50
|
-
const result = await mammoth.extractRawText({ buffer });
|
|
51
|
-
return result.value;
|
|
52
|
-
} catch (err) {
|
|
53
|
-
console.warn('[DocumentParser] DOCX parsing failed. Make sure "mammoth" is installed.', err);
|
|
54
|
-
return `[DOCX Parsing Error for ${fileName}]`;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
return await this.readAsText(file);
|
|
59
|
-
} catch (e) {
|
|
60
|
-
throw new Error(`[DocumentParser] Unsupported file format: ${fileName} (${mimeType})`);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
static async readAsText(file) {
|
|
64
|
-
if (Buffer.isBuffer(file)) {
|
|
65
|
-
return file.toString("utf-8");
|
|
66
|
-
}
|
|
67
|
-
return await file.text();
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// src/handlers/index.ts
|
|
72
|
-
function createChatHandler(config) {
|
|
73
|
-
const plugin = new VectorPlugin(config);
|
|
74
|
-
return async function POST(req) {
|
|
75
|
-
try {
|
|
76
|
-
const body = await req.json();
|
|
77
|
-
const { message, history = [], namespace } = body;
|
|
78
|
-
if (!(message == null ? void 0 : message.trim())) {
|
|
79
|
-
return NextResponse.json({ error: "message is required" }, { status: 400 });
|
|
80
|
-
}
|
|
81
|
-
const result = await plugin.chat(message, history, namespace);
|
|
82
|
-
return NextResponse.json(result);
|
|
83
|
-
} catch (err) {
|
|
84
|
-
const message = err instanceof Error ? err.message : "Internal server error";
|
|
85
|
-
return NextResponse.json({ error: message }, { status: 500 });
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
function createIngestHandler(config) {
|
|
90
|
-
const plugin = new VectorPlugin(config);
|
|
91
|
-
return async function POST(req) {
|
|
92
|
-
try {
|
|
93
|
-
const body = await req.json();
|
|
94
|
-
const { documents, namespace } = body;
|
|
95
|
-
if (!Array.isArray(documents) || documents.length === 0) {
|
|
96
|
-
return NextResponse.json({ error: "documents array is required" }, { status: 400 });
|
|
97
|
-
}
|
|
98
|
-
const results = await plugin.ingest(documents, namespace);
|
|
99
|
-
return NextResponse.json({ results });
|
|
100
|
-
} catch (err) {
|
|
101
|
-
const message = err instanceof Error ? err.message : "Internal server error";
|
|
102
|
-
return NextResponse.json({ error: message }, { status: 500 });
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
function createHealthHandler(config) {
|
|
107
|
-
const plugin = new VectorPlugin(config);
|
|
108
|
-
return async function GET() {
|
|
109
|
-
try {
|
|
110
|
-
const health = await plugin.health();
|
|
111
|
-
const status = health.vectorDB && health.llm ? "ok" : "degraded";
|
|
112
|
-
return NextResponse.json(__spreadProps(__spreadValues({
|
|
113
|
-
status
|
|
114
|
-
}, health), {
|
|
115
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
116
|
-
}));
|
|
117
|
-
} catch (err) {
|
|
118
|
-
const message = err instanceof Error ? err.message : "Unknown error";
|
|
119
|
-
return NextResponse.json({ status: "error", error: message }, { status: 500 });
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
function createUploadHandler(config) {
|
|
124
|
-
const plugin = new VectorPlugin(config);
|
|
125
|
-
return async function POST(req) {
|
|
126
|
-
try {
|
|
127
|
-
const formData = await req.formData();
|
|
128
|
-
const files = formData.getAll("files");
|
|
129
|
-
const namespace = formData.get("namespace") || void 0;
|
|
130
|
-
if (!files || files.length === 0) {
|
|
131
|
-
return NextResponse.json({ error: "No files provided" }, { status: 400 });
|
|
132
|
-
}
|
|
133
|
-
const documents = await Promise.all(
|
|
134
|
-
files.map(async (file) => {
|
|
135
|
-
const content = await DocumentParser.parse(file, file.name, file.type);
|
|
136
|
-
return {
|
|
137
|
-
docId: file.name,
|
|
138
|
-
content,
|
|
139
|
-
metadata: {
|
|
140
|
-
fileName: file.name,
|
|
141
|
-
fileSize: file.size,
|
|
142
|
-
fileType: file.type,
|
|
143
|
-
uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
})
|
|
147
|
-
);
|
|
148
|
-
const results = await plugin.ingest(documents, namespace);
|
|
149
|
-
return NextResponse.json({ message: "Upload successful", results });
|
|
150
|
-
} catch (err) {
|
|
151
|
-
const message = err instanceof Error ? err.message : "Upload failed";
|
|
152
|
-
return NextResponse.json({ error: message }, { status: 500 });
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export {
|
|
158
|
-
createChatHandler,
|
|
159
|
-
createIngestHandler,
|
|
160
|
-
createHealthHandler,
|
|
161
|
-
createUploadHandler
|
|
162
|
-
};
|