@absolutejs/rag 0.0.20 → 0.0.22
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/index.js +174 -1
- package/dist/index.js.map +5 -4
- package/dist/src/adapters/sync.d.ts +21 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/types/adapters.d.ts +16 -0
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -35670,6 +35670,178 @@ var createInMemoryRAGStore = (options = {}) => {
|
|
|
35670
35670
|
getStatus: () => createInMemoryStatus(dimensions)
|
|
35671
35671
|
};
|
|
35672
35672
|
};
|
|
35673
|
+
// src/adapters/sync.ts
|
|
35674
|
+
import {
|
|
35675
|
+
createSyncEngine,
|
|
35676
|
+
createTextIndex,
|
|
35677
|
+
createVectorIndex,
|
|
35678
|
+
defineSearchCollection
|
|
35679
|
+
} from "@absolutejs/sync/engine";
|
|
35680
|
+
var createSyncRAGStore = (options = {}) => {
|
|
35681
|
+
const dimensions = options.dimensions ?? RAG_VECTOR_DIMENSIONS_DEFAULT;
|
|
35682
|
+
const engine = options.engine ?? createSyncEngine();
|
|
35683
|
+
const retrievalCollection = options.collection ?? "ragRetrieval";
|
|
35684
|
+
const table = options.table ?? "ragChunks";
|
|
35685
|
+
const records = new Map;
|
|
35686
|
+
const toLight = (record) => ({
|
|
35687
|
+
chunkId: record.chunkId,
|
|
35688
|
+
chunkText: record.text,
|
|
35689
|
+
source: record.source,
|
|
35690
|
+
title: record.title
|
|
35691
|
+
});
|
|
35692
|
+
const textIndex = createTextIndex({
|
|
35693
|
+
fields: ["chunkText"],
|
|
35694
|
+
key: (chunk) => chunk.chunkId
|
|
35695
|
+
});
|
|
35696
|
+
const vectorIndex = createVectorIndex({
|
|
35697
|
+
embedding: (chunk) => records.get(chunk.chunkId)?.vector ?? [],
|
|
35698
|
+
key: (chunk) => chunk.chunkId
|
|
35699
|
+
});
|
|
35700
|
+
engine.registerSearch(defineSearchCollection({
|
|
35701
|
+
index: () => textIndex,
|
|
35702
|
+
key: (chunk) => chunk.chunkId,
|
|
35703
|
+
name: retrievalCollection,
|
|
35704
|
+
source: () => [...records.values()].map(toLight),
|
|
35705
|
+
table
|
|
35706
|
+
}));
|
|
35707
|
+
const embed = async (input) => {
|
|
35708
|
+
if (options.embedding) {
|
|
35709
|
+
return options.embedding(input);
|
|
35710
|
+
}
|
|
35711
|
+
if (options.mockEmbedding) {
|
|
35712
|
+
return options.mockEmbedding(input.text);
|
|
35713
|
+
}
|
|
35714
|
+
return normalizeVector(createRAGVector(input.text, dimensions));
|
|
35715
|
+
};
|
|
35716
|
+
const matchesFilter = (record, filter) => matchesMetadataFilterRecord({
|
|
35717
|
+
chunkId: record.chunkId,
|
|
35718
|
+
metadata: record.metadata,
|
|
35719
|
+
source: record.source,
|
|
35720
|
+
title: record.title,
|
|
35721
|
+
...record.metadata ?? {}
|
|
35722
|
+
}, filter);
|
|
35723
|
+
const toResult = (record, score) => ({
|
|
35724
|
+
chunkId: record.chunkId,
|
|
35725
|
+
chunkText: record.text,
|
|
35726
|
+
embedding: record.vector,
|
|
35727
|
+
metadata: record.metadata,
|
|
35728
|
+
score,
|
|
35729
|
+
source: record.source,
|
|
35730
|
+
title: record.title
|
|
35731
|
+
});
|
|
35732
|
+
const upsert = async (input) => {
|
|
35733
|
+
for (const chunk of input.chunks) {
|
|
35734
|
+
const vector = chunk.embedding ? normalizeVector(chunk.embedding) : normalizeVector(await embed({ text: chunk.text }));
|
|
35735
|
+
const record = {
|
|
35736
|
+
chunkId: chunk.chunkId,
|
|
35737
|
+
metadata: chunk.metadata,
|
|
35738
|
+
source: chunk.source,
|
|
35739
|
+
text: chunk.text,
|
|
35740
|
+
title: chunk.title,
|
|
35741
|
+
vector
|
|
35742
|
+
};
|
|
35743
|
+
records.set(record.chunkId, record);
|
|
35744
|
+
const light = toLight(record);
|
|
35745
|
+
vectorIndex.add(light);
|
|
35746
|
+
textIndex.add(light);
|
|
35747
|
+
await engine.applyChange(table, { op: "insert", row: light });
|
|
35748
|
+
}
|
|
35749
|
+
};
|
|
35750
|
+
const query = async (input) => {
|
|
35751
|
+
const queryVector = normalizeVector(input.queryVector);
|
|
35752
|
+
const results = [];
|
|
35753
|
+
for (const record of records.values()) {
|
|
35754
|
+
if (!matchesFilter(record, input.filter)) {
|
|
35755
|
+
continue;
|
|
35756
|
+
}
|
|
35757
|
+
const score = querySimilarity(queryVector, normalizeVector(record.vector));
|
|
35758
|
+
if (Number.isFinite(score)) {
|
|
35759
|
+
results.push(toResult(record, score));
|
|
35760
|
+
}
|
|
35761
|
+
}
|
|
35762
|
+
results.sort((first, second) => second.score - first.score);
|
|
35763
|
+
return results.slice(0, input.topK);
|
|
35764
|
+
};
|
|
35765
|
+
const queryLexical = async (input) => {
|
|
35766
|
+
const hits = textIndex.search(input.query, input.topK * 5);
|
|
35767
|
+
const results = [];
|
|
35768
|
+
for (const hit of hits) {
|
|
35769
|
+
const record = records.get(hit.row.chunkId);
|
|
35770
|
+
if (record && matchesFilter(record, input.filter)) {
|
|
35771
|
+
results.push(toResult(record, hit.score));
|
|
35772
|
+
}
|
|
35773
|
+
}
|
|
35774
|
+
return results.slice(0, input.topK);
|
|
35775
|
+
};
|
|
35776
|
+
const hasFilters = (chunkIds, filter) => ({
|
|
35777
|
+
filtered: Boolean(filter && Object.keys(filter).length > 0),
|
|
35778
|
+
ided: chunkIds.size > 0
|
|
35779
|
+
});
|
|
35780
|
+
const count = async (input = {}) => {
|
|
35781
|
+
const chunkIds = new Set(input.chunkIds ?? []);
|
|
35782
|
+
const { filtered, ided } = hasFilters(chunkIds, input.filter);
|
|
35783
|
+
if (!filtered && !ided) {
|
|
35784
|
+
return records.size;
|
|
35785
|
+
}
|
|
35786
|
+
return [...records.values()].filter((record) => ided && chunkIds.has(record.chunkId) || filtered && matchesFilter(record, input.filter)).length;
|
|
35787
|
+
};
|
|
35788
|
+
const remove = async (input = {}) => {
|
|
35789
|
+
const chunkIds = new Set(input.chunkIds ?? []);
|
|
35790
|
+
const { filtered, ided } = hasFilters(chunkIds, input.filter);
|
|
35791
|
+
if (!filtered && !ided) {
|
|
35792
|
+
return 0;
|
|
35793
|
+
}
|
|
35794
|
+
let removed = 0;
|
|
35795
|
+
for (const record of [...records.values()]) {
|
|
35796
|
+
const matches = ided && chunkIds.has(record.chunkId) || filtered && matchesFilter(record, input.filter);
|
|
35797
|
+
if (!matches) {
|
|
35798
|
+
continue;
|
|
35799
|
+
}
|
|
35800
|
+
records.delete(record.chunkId);
|
|
35801
|
+
vectorIndex.remove(record.chunkId);
|
|
35802
|
+
textIndex.remove(record.chunkId);
|
|
35803
|
+
await engine.applyChange(table, {
|
|
35804
|
+
op: "delete",
|
|
35805
|
+
row: toLight(record)
|
|
35806
|
+
});
|
|
35807
|
+
removed += 1;
|
|
35808
|
+
}
|
|
35809
|
+
return removed;
|
|
35810
|
+
};
|
|
35811
|
+
const clear = async () => {
|
|
35812
|
+
for (const record of [...records.values()]) {
|
|
35813
|
+
records.delete(record.chunkId);
|
|
35814
|
+
vectorIndex.remove(record.chunkId);
|
|
35815
|
+
textIndex.remove(record.chunkId);
|
|
35816
|
+
await engine.applyChange(table, { op: "delete", row: toLight(record) });
|
|
35817
|
+
}
|
|
35818
|
+
};
|
|
35819
|
+
const status = {
|
|
35820
|
+
backend: "in_memory",
|
|
35821
|
+
dimensions,
|
|
35822
|
+
vectorMode: "in_memory"
|
|
35823
|
+
};
|
|
35824
|
+
const capabilities = {
|
|
35825
|
+
backend: "in_memory",
|
|
35826
|
+
nativeVectorSearch: false,
|
|
35827
|
+
persistence: "memory_only",
|
|
35828
|
+
serverSideFiltering: false,
|
|
35829
|
+
streamingIngestStatus: false
|
|
35830
|
+
};
|
|
35831
|
+
return {
|
|
35832
|
+
clear,
|
|
35833
|
+
count,
|
|
35834
|
+
delete: remove,
|
|
35835
|
+
embed,
|
|
35836
|
+
engine,
|
|
35837
|
+
getCapabilities: () => capabilities,
|
|
35838
|
+
getStatus: () => status,
|
|
35839
|
+
query,
|
|
35840
|
+
queryLexical,
|
|
35841
|
+
retrievalCollection,
|
|
35842
|
+
upsert
|
|
35843
|
+
};
|
|
35844
|
+
};
|
|
35673
35845
|
export {
|
|
35674
35846
|
xaiEmbeddings,
|
|
35675
35847
|
validateRAGEmbeddingDimensions,
|
|
@@ -35775,6 +35947,7 @@ export {
|
|
|
35775
35947
|
deepseekEmbeddings,
|
|
35776
35948
|
createVoyageRAGReranker,
|
|
35777
35949
|
createTextFileExtractor,
|
|
35950
|
+
createSyncRAGStore,
|
|
35778
35951
|
createRAGVector,
|
|
35779
35952
|
createRAGUrlSyncSource,
|
|
35780
35953
|
createRAGSyncScheduler,
|
|
@@ -35960,5 +36133,5 @@ export {
|
|
|
35960
36133
|
addRAGEvaluationSuiteCase
|
|
35961
36134
|
};
|
|
35962
36135
|
|
|
35963
|
-
//# debugId=
|
|
36136
|
+
//# debugId=7661BF2D41E88F6A64756E2164756E21
|
|
35964
36137
|
//# sourceMappingURL=index.js.map
|