@arnilo/prism-rag 0.0.96 → 0.1.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/CHANGELOG.md +118 -2
- package/README.md +11 -5
- package/dist/chunk.d.ts +1 -1
- package/dist/chunk.js +1 -1
- package/dist/context.js +6 -4
- package/dist/index.d.ts +10 -5
- package/dist/index.js +7 -3
- package/dist/indexing.d.ts +2 -0
- package/dist/indexing.js +79 -57
- package/dist/ingestion-status.d.ts +7 -0
- package/dist/ingestion-status.js +87 -0
- package/dist/limits.d.ts +21 -0
- package/dist/limits.js +21 -0
- package/dist/loaders.d.ts +24 -0
- package/dist/loaders.js +78 -0
- package/dist/parsers.d.ts +5 -0
- package/dist/parsers.js +101 -0
- package/dist/rerank.d.ts +11 -0
- package/dist/rerank.js +62 -0
- package/dist/retrieve.js +64 -36
- package/dist/sources.d.ts +9 -0
- package/dist/sources.js +118 -0
- package/dist/types.d.ts +128 -1
- package/dist/util.d.ts +1 -2
- package/dist/util.js +1 -7
- package/package.json +11 -3
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ContextProvider, JsonObject, Message, SecretRedactor } from "@arnilo/prism";
|
|
2
|
-
import type { Embedder, VectorStore } from "@arnilo/prism-memory";
|
|
2
|
+
import type { Embedder, MemoryVectorRecord, VectorStore } from "@arnilo/prism-memory";
|
|
3
3
|
export interface RagScope {
|
|
4
4
|
readonly tenantId: string;
|
|
5
5
|
readonly resourceId: string;
|
|
@@ -23,6 +23,80 @@ export interface ChunkOptions {
|
|
|
23
23
|
readonly maxDocumentChars?: number;
|
|
24
24
|
readonly maxChunks?: number;
|
|
25
25
|
}
|
|
26
|
+
export interface LoadedDocument {
|
|
27
|
+
readonly uri: string;
|
|
28
|
+
readonly sourceId?: string;
|
|
29
|
+
readonly mediaType?: string;
|
|
30
|
+
readonly text?: string;
|
|
31
|
+
readonly data?: Uint8Array;
|
|
32
|
+
readonly metadata?: JsonObject;
|
|
33
|
+
}
|
|
34
|
+
export interface ParsedDocument {
|
|
35
|
+
readonly text: string;
|
|
36
|
+
readonly metadata?: JsonObject;
|
|
37
|
+
}
|
|
38
|
+
export interface DocumentLoadOptions {
|
|
39
|
+
readonly signal?: AbortSignal;
|
|
40
|
+
readonly maxBytes?: number;
|
|
41
|
+
}
|
|
42
|
+
export interface DocumentParseOptions extends DocumentLoadOptions {
|
|
43
|
+
readonly maxParseMs?: number;
|
|
44
|
+
readonly maxPages?: number;
|
|
45
|
+
}
|
|
46
|
+
export interface DocumentLoader {
|
|
47
|
+
load(uri: string, options?: DocumentLoadOptions): Promise<LoadedDocument>;
|
|
48
|
+
}
|
|
49
|
+
export interface Parser {
|
|
50
|
+
parse(document: LoadedDocument, options?: DocumentParseOptions): Promise<ParsedDocument>;
|
|
51
|
+
}
|
|
52
|
+
export type Chunker = (text: string, options: ChunkOptions) => readonly RagChunk[];
|
|
53
|
+
export interface SourceVectorStore extends VectorStore {
|
|
54
|
+
getBySource(scope: {
|
|
55
|
+
readonly tenantId: string;
|
|
56
|
+
readonly resourceId: string;
|
|
57
|
+
readonly threadId: string;
|
|
58
|
+
}, sourceId: string, options?: {
|
|
59
|
+
readonly signal?: AbortSignal;
|
|
60
|
+
}): Promise<readonly MemoryVectorRecord[]>;
|
|
61
|
+
}
|
|
62
|
+
export interface TransactionalVectorStore extends SourceVectorStore {
|
|
63
|
+
transaction<T>(operation: (store: SourceVectorStore) => Promise<T>, options?: {
|
|
64
|
+
readonly signal?: AbortSignal;
|
|
65
|
+
}): Promise<T>;
|
|
66
|
+
}
|
|
67
|
+
export type IngestionState = "pending" | "indexed" | "failed" | "partial";
|
|
68
|
+
export interface IngestionStatus {
|
|
69
|
+
readonly sourceId: string;
|
|
70
|
+
readonly scope: RagScope;
|
|
71
|
+
readonly state: IngestionState;
|
|
72
|
+
readonly bytes: number;
|
|
73
|
+
readonly chunks: number;
|
|
74
|
+
readonly error?: string;
|
|
75
|
+
readonly updatedAt: string;
|
|
76
|
+
}
|
|
77
|
+
export interface IngestionStatusStore {
|
|
78
|
+
set(status: IngestionStatus, options?: {
|
|
79
|
+
readonly signal?: AbortSignal;
|
|
80
|
+
}): Promise<void>;
|
|
81
|
+
delete(scope: RagScope, sourceId: string, options?: {
|
|
82
|
+
readonly signal?: AbortSignal;
|
|
83
|
+
}): Promise<void>;
|
|
84
|
+
list(scope: RagScope, options: {
|
|
85
|
+
readonly limit: number;
|
|
86
|
+
readonly cursor?: string;
|
|
87
|
+
readonly signal?: AbortSignal;
|
|
88
|
+
}): Promise<{
|
|
89
|
+
readonly entries: readonly IngestionStatus[];
|
|
90
|
+
readonly nextCursor?: string;
|
|
91
|
+
}>;
|
|
92
|
+
}
|
|
93
|
+
export interface IngestionStatusQuery {
|
|
94
|
+
readonly store: IngestionStatusStore;
|
|
95
|
+
readonly scope: RagScope;
|
|
96
|
+
readonly limit?: number;
|
|
97
|
+
readonly cursor?: string;
|
|
98
|
+
readonly signal?: AbortSignal;
|
|
99
|
+
}
|
|
26
100
|
export interface IndexChunksOptions {
|
|
27
101
|
readonly chunks: readonly RagChunk[];
|
|
28
102
|
readonly embedder: Embedder;
|
|
@@ -35,20 +109,68 @@ export interface IndexChunksOptions {
|
|
|
35
109
|
readonly maxMetadataBytes?: number;
|
|
36
110
|
readonly redactor?: SecretRedactor;
|
|
37
111
|
readonly secrets?: readonly (string | undefined)[];
|
|
112
|
+
readonly statusStore?: IngestionStatusStore;
|
|
38
113
|
readonly signal?: AbortSignal;
|
|
39
114
|
}
|
|
40
115
|
export interface IndexChunksResult {
|
|
41
116
|
readonly indexed: number;
|
|
42
117
|
readonly sourceIds: readonly string[];
|
|
43
118
|
}
|
|
119
|
+
export interface ReplaceSourceOptions extends Omit<IndexChunksOptions, "chunks" | "store"> {
|
|
120
|
+
readonly sourceId: string;
|
|
121
|
+
readonly chunks: readonly RagChunk[];
|
|
122
|
+
readonly store: TransactionalVectorStore;
|
|
123
|
+
}
|
|
124
|
+
export interface DeleteSourceOptions {
|
|
125
|
+
readonly sourceId: string;
|
|
126
|
+
readonly store: SourceVectorStore;
|
|
127
|
+
readonly scope: RagScope;
|
|
128
|
+
readonly statusStore?: IngestionStatusStore;
|
|
129
|
+
readonly signal?: AbortSignal;
|
|
130
|
+
}
|
|
131
|
+
export interface ReplaceDocumentOptions extends Omit<ReplaceSourceOptions, "sourceId" | "chunks"> {
|
|
132
|
+
readonly uri: string;
|
|
133
|
+
readonly sourceId?: string;
|
|
134
|
+
readonly loader: DocumentLoader;
|
|
135
|
+
readonly parser: Parser;
|
|
136
|
+
readonly chunker?: Chunker;
|
|
137
|
+
readonly chunk?: Omit<ChunkOptions, "sourceId">;
|
|
138
|
+
readonly loaderOptions?: DocumentLoadOptions;
|
|
139
|
+
readonly parserOptions?: DocumentParseOptions;
|
|
140
|
+
}
|
|
141
|
+
export interface RagProvenance {
|
|
142
|
+
readonly sourceId: string;
|
|
143
|
+
readonly chunkId: string;
|
|
144
|
+
readonly citationId: string;
|
|
145
|
+
readonly provider: string;
|
|
146
|
+
readonly retrieval: "vector";
|
|
147
|
+
readonly retrievedAt: string;
|
|
148
|
+
}
|
|
149
|
+
export interface RagContentTrust {
|
|
150
|
+
readonly untrusted: true;
|
|
151
|
+
readonly inert: true;
|
|
152
|
+
readonly injectionCapable: true;
|
|
153
|
+
}
|
|
44
154
|
export interface RagCitation {
|
|
45
155
|
readonly id: string;
|
|
46
156
|
readonly sourceId: string;
|
|
47
157
|
readonly chunkId: string;
|
|
158
|
+
readonly provenance: RagProvenance;
|
|
159
|
+
readonly trust: RagContentTrust;
|
|
48
160
|
readonly metadata?: JsonObject;
|
|
49
161
|
}
|
|
50
162
|
export interface RagHit extends RagChunk {
|
|
51
163
|
readonly score: number;
|
|
164
|
+
readonly retrievalRank: number;
|
|
165
|
+
readonly provenance: RagProvenance;
|
|
166
|
+
readonly trust: RagContentTrust;
|
|
167
|
+
}
|
|
168
|
+
export interface Reranker {
|
|
169
|
+
rerank(input: {
|
|
170
|
+
readonly query: string;
|
|
171
|
+
readonly hits: readonly RagHit[];
|
|
172
|
+
readonly signal?: AbortSignal;
|
|
173
|
+
}): Promise<readonly RagHit[]>;
|
|
52
174
|
}
|
|
53
175
|
export interface RetrieveContextOptions {
|
|
54
176
|
readonly embedder: Embedder;
|
|
@@ -61,12 +183,17 @@ export interface RetrieveContextOptions {
|
|
|
61
183
|
readonly maxContextTokens?: number;
|
|
62
184
|
readonly maxMetadataBytes?: number;
|
|
63
185
|
readonly maxVectorDimensions?: number;
|
|
186
|
+
readonly reranker?: Reranker;
|
|
187
|
+
readonly maxRerankBytes?: number;
|
|
188
|
+
readonly maxRerankMs?: number;
|
|
189
|
+
readonly rerankConcurrency?: number;
|
|
64
190
|
readonly redactor?: SecretRedactor;
|
|
65
191
|
readonly secrets?: readonly (string | undefined)[];
|
|
66
192
|
readonly signal?: AbortSignal;
|
|
67
193
|
}
|
|
68
194
|
export interface RagContextResult {
|
|
69
195
|
readonly query: string;
|
|
196
|
+
readonly trust: RagContentTrust;
|
|
70
197
|
readonly text: string;
|
|
71
198
|
readonly hits: readonly RagHit[];
|
|
72
199
|
readonly citations: readonly RagCitation[];
|
package/dist/util.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type JsonObject, type JsonValue, type Message
|
|
1
|
+
import { type JsonObject, type JsonValue, type Message } from "@arnilo/prism";
|
|
2
2
|
import type { RagScope } from "./types.js";
|
|
3
3
|
export declare function assertNotAborted(signal?: AbortSignal): void;
|
|
4
4
|
export declare function nonEmpty(value: unknown, label: string): string;
|
|
@@ -9,7 +9,6 @@ export declare function assertScope(expected: RagScope, actual: {
|
|
|
9
9
|
resourceId: string;
|
|
10
10
|
threadId: string;
|
|
11
11
|
}): void;
|
|
12
|
-
export declare function resolveRedactor(redactor?: SecretRedactor, secrets?: readonly (string | undefined)[]): SecretRedactor | undefined;
|
|
13
12
|
export declare function byteLength(value: unknown): number;
|
|
14
13
|
export declare function assertBytes(value: unknown, limit: number, label: string): void;
|
|
15
14
|
export declare function latestUserText(messages: readonly Message[]): string | undefined;
|
package/dist/util.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createSecretRedactor } from "@arnilo/prism";
|
|
2
1
|
import { RagAbortError, RagLimitError, RagScopeError, RagValidationError } from "./errors.js";
|
|
3
2
|
export function assertNotAborted(signal) {
|
|
4
3
|
if (signal?.aborted)
|
|
@@ -24,15 +23,10 @@ export function requireScope(scope) {
|
|
|
24
23
|
};
|
|
25
24
|
}
|
|
26
25
|
export function assertScope(expected, actual) {
|
|
27
|
-
if (actual.tenantId !== expected.tenantId
|
|
28
|
-
|| actual.resourceId !== expected.resourceId
|
|
29
|
-
|| actual.threadId !== expected.corpusId) {
|
|
26
|
+
if (actual.tenantId !== expected.tenantId || actual.resourceId !== expected.resourceId || actual.threadId !== expected.corpusId) {
|
|
30
27
|
throw new RagScopeError("vector hit crossed tenant/resource/corpus boundary");
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
|
-
export function resolveRedactor(redactor, secrets) {
|
|
34
|
-
return redactor ?? (secrets?.length ? createSecretRedactor(secrets) : undefined);
|
|
35
|
-
}
|
|
36
30
|
export function byteLength(value) {
|
|
37
31
|
return Buffer.byteLength(typeof value === "string" ? value : JSON.stringify(value), "utf8");
|
|
38
32
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arnilo/prism-rag",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Optional bounded text and Markdown RAG primitives for Prism.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -9,6 +9,14 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./loaders": {
|
|
14
|
+
"types": "./dist/loaders.d.ts",
|
|
15
|
+
"default": "./dist/loaders.js"
|
|
16
|
+
},
|
|
17
|
+
"./parsers": {
|
|
18
|
+
"types": "./dist/parsers.d.ts",
|
|
19
|
+
"default": "./dist/parsers.js"
|
|
12
20
|
}
|
|
13
21
|
},
|
|
14
22
|
"files": [
|
|
@@ -25,8 +33,8 @@
|
|
|
25
33
|
"pack:dry-run": "npm pack --dry-run"
|
|
26
34
|
},
|
|
27
35
|
"peerDependencies": {
|
|
28
|
-
"@arnilo/prism": "0.
|
|
29
|
-
"@arnilo/prism-memory": "0.
|
|
36
|
+
"@arnilo/prism": "0.1.1",
|
|
37
|
+
"@arnilo/prism-memory": "0.1.1"
|
|
30
38
|
},
|
|
31
39
|
"devDependencies": {
|
|
32
40
|
"@arnilo/prism": "file:../..",
|