@miniaxolotl/grove 0.2.4
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 +7 -0
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +27 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +580 -0
- package/dist/index.js.map +1 -0
- package/dist/repositories/entity.repository.d.ts +44 -0
- package/dist/repositories/entity.repository.d.ts.map +1 -0
- package/dist/repositories/entity.repository.js +203 -0
- package/dist/repositories/entity.repository.js.map +1 -0
- package/dist/repositories/memory.repository.d.ts +78 -0
- package/dist/repositories/memory.repository.d.ts.map +1 -0
- package/dist/repositories/memory.repository.js +283 -0
- package/dist/repositories/memory.repository.js.map +1 -0
- package/dist/repositories/relation.repository.d.ts +38 -0
- package/dist/repositories/relation.repository.d.ts.map +1 -0
- package/dist/repositories/relation.repository.js +113 -0
- package/dist/repositories/relation.repository.js.map +1 -0
- package/dist/services/embedding.d.ts +3 -0
- package/dist/services/embedding.d.ts.map +1 -0
- package/dist/services/embedding.js +107 -0
- package/dist/services/embedding.js.map +1 -0
- package/dist/services/qdrant.d.ts +35 -0
- package/dist/services/qdrant.d.ts.map +1 -0
- package/dist/services/qdrant.js +100 -0
- package/dist/services/qdrant.js.map +1 -0
- package/dist/services/reranking.d.ts +6 -0
- package/dist/services/reranking.d.ts.map +1 -0
- package/dist/services/reranking.js +24 -0
- package/dist/services/reranking.js.map +1 -0
- package/eslint.config.js +1 -0
- package/package.json +56 -0
- package/src/config.ts +27 -0
- package/src/index.ts +663 -0
- package/src/repositories/entity.repository.ts +265 -0
- package/src/repositories/memory.repository.ts +392 -0
- package/src/repositories/relation.repository.ts +151 -0
- package/src/services/embedding.ts +142 -0
- package/src/services/qdrant.ts +184 -0
- package/src/services/reranking.ts +40 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
2
|
+
import { config } from "../config.ts";
|
|
3
|
+
import { qdrant, type VectorPoint } from "../services/qdrant.ts";
|
|
4
|
+
|
|
5
|
+
const COLLECTION = `${config.collection.prefix}_relations`;
|
|
6
|
+
|
|
7
|
+
export interface Relation {
|
|
8
|
+
id: string;
|
|
9
|
+
from: string;
|
|
10
|
+
relationType: string;
|
|
11
|
+
to: string;
|
|
12
|
+
metadata?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function createRelation(
|
|
16
|
+
from: string,
|
|
17
|
+
relationType: string,
|
|
18
|
+
to: string,
|
|
19
|
+
metadata?: Record<string, unknown>,
|
|
20
|
+
): Promise<Relation> {
|
|
21
|
+
const existing = await searchRelations({ from, relationType, to, limit: 1 }, 1);
|
|
22
|
+
if (existing.length > 0) {
|
|
23
|
+
const rel = existing[0];
|
|
24
|
+
const mergedMetadata = { ...rel.metadata, ...metadata };
|
|
25
|
+
|
|
26
|
+
await qdrant.upsertPoints(COLLECTION, [
|
|
27
|
+
{
|
|
28
|
+
id: rel.id,
|
|
29
|
+
vector: Array(config.vector.dim).fill(0),
|
|
30
|
+
payload: { id: rel.id, from, relationType, to, metadata: mergedMetadata },
|
|
31
|
+
},
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
return { id: rel.id, from, relationType, to, metadata: mergedMetadata };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const id = randomUUID();
|
|
38
|
+
const point = {
|
|
39
|
+
id,
|
|
40
|
+
vector: Array(config.vector.dim).fill(0),
|
|
41
|
+
payload: { id, from, relationType, to, metadata },
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
await qdrant.upsertPoints(COLLECTION, [point]);
|
|
45
|
+
|
|
46
|
+
return { id, from, relationType, to, metadata };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function searchRelations(
|
|
50
|
+
options: {
|
|
51
|
+
from?: string;
|
|
52
|
+
to?: string;
|
|
53
|
+
relationType?: string;
|
|
54
|
+
limit?: number;
|
|
55
|
+
} = {},
|
|
56
|
+
defaultLimit: number = 20,
|
|
57
|
+
): Promise<Relation[]> {
|
|
58
|
+
const must: Record<string, unknown>[] = [];
|
|
59
|
+
if (options.from) must.push({ key: "from", match: { value: options.from } });
|
|
60
|
+
if (options.to) must.push({ key: "to", match: { value: options.to } });
|
|
61
|
+
if (options.relationType) must.push({ key: "relationType", match: { value: options.relationType } });
|
|
62
|
+
|
|
63
|
+
const filter = must.length > 0 ? { must } : undefined;
|
|
64
|
+
const points = await qdrant.scrollPoints(COLLECTION, filter, options.limit ?? defaultLimit);
|
|
65
|
+
|
|
66
|
+
return points.map((p) => ({
|
|
67
|
+
id: p.id,
|
|
68
|
+
from: p.payload.from as string,
|
|
69
|
+
relationType: p.payload.relationType as string,
|
|
70
|
+
to: p.payload.to as string,
|
|
71
|
+
metadata: p.payload.metadata as Record<string, unknown> | undefined,
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function deleteRelations(options: { from?: string; to?: string; relationType?: string }): Promise<void> {
|
|
76
|
+
const must: Record<string, unknown>[] = [];
|
|
77
|
+
if (options.from) must.push({ key: "from", match: { value: options.from } });
|
|
78
|
+
if (options.to) must.push({ key: "to", match: { value: options.to } });
|
|
79
|
+
if (options.relationType) must.push({ key: "relationType", match: { value: options.relationType } });
|
|
80
|
+
if (must.length === 0) return;
|
|
81
|
+
|
|
82
|
+
await qdrant.deleteByFilter(COLLECTION, { must });
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function getRelationStats(): Promise<{
|
|
86
|
+
count: number;
|
|
87
|
+
vectorsCount: number;
|
|
88
|
+
indexedVectorsCount: number;
|
|
89
|
+
}> {
|
|
90
|
+
const info = await qdrant.getCollectionInfo(COLLECTION);
|
|
91
|
+
if (!info) return { count: 0, vectorsCount: 0, indexedVectorsCount: 0 };
|
|
92
|
+
return {
|
|
93
|
+
count: info.pointsCount,
|
|
94
|
+
vectorsCount: info.vectorsCount,
|
|
95
|
+
indexedVectorsCount: info.indexedVectorsCount,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function listRelations(
|
|
100
|
+
relationType?: string,
|
|
101
|
+
limit: number = 100,
|
|
102
|
+
offset?: string,
|
|
103
|
+
): Promise<{ relations: Relation[]; offset: string | null }> {
|
|
104
|
+
const must: Record<string, unknown>[] = [];
|
|
105
|
+
if (relationType) must.push({ key: "relationType", match: { value: relationType } });
|
|
106
|
+
const filter = must.length > 0 ? { must } : undefined;
|
|
107
|
+
|
|
108
|
+
const url = `${config.qdrant.url}/collections/${COLLECTION}/points/scroll`;
|
|
109
|
+
const body: Record<string, unknown> = { limit, with_payload: true };
|
|
110
|
+
if (filter) body.filter = filter;
|
|
111
|
+
if (offset) body.offset = offset;
|
|
112
|
+
|
|
113
|
+
const response = await fetch(url, {
|
|
114
|
+
method: "POST",
|
|
115
|
+
headers: {
|
|
116
|
+
"Content-Type": "application/json",
|
|
117
|
+
"api-key": config.qdrant.apiKey,
|
|
118
|
+
"User-Agent": "grove/1.0",
|
|
119
|
+
},
|
|
120
|
+
body: JSON.stringify(body),
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
if (!response.ok) throw new Error(`scroll failed: ${response.status}`);
|
|
124
|
+
const data = (await response.json()) as {
|
|
125
|
+
result: { points: VectorPoint[]; next_page_offset: string | null };
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
relations: data.result.points.map((p) => ({
|
|
130
|
+
id: p.id,
|
|
131
|
+
from: p.payload.from as string,
|
|
132
|
+
relationType: p.payload.relationType as string,
|
|
133
|
+
to: p.payload.to as string,
|
|
134
|
+
metadata: p.payload.metadata as Record<string, unknown> | undefined,
|
|
135
|
+
})),
|
|
136
|
+
offset: data.result.next_page_offset ?? null,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export async function initRelationCollection(): Promise<void> {
|
|
141
|
+
await qdrant.ensureCollection(COLLECTION, config.vector.dim);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export const relationRepository = {
|
|
145
|
+
create: createRelation,
|
|
146
|
+
search: searchRelations,
|
|
147
|
+
delete: deleteRelations,
|
|
148
|
+
init: initRelationCollection,
|
|
149
|
+
stats: getRelationStats,
|
|
150
|
+
list: listRelations,
|
|
151
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { config, hasRemoteEmbedding } from "../config.ts";
|
|
2
|
+
|
|
3
|
+
function chunk<T>(arr: T[], size: number): T[][] {
|
|
4
|
+
const chunks: T[][] = [];
|
|
5
|
+
for (let i = 0; i < arr.length; i += size) {
|
|
6
|
+
chunks.push(arr.slice(i, i + size));
|
|
7
|
+
}
|
|
8
|
+
return chunks;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function estimateTokens(text: string): number {
|
|
12
|
+
return Math.ceil(text.length / 4);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function embedRemote(texts: string[]): Promise<number[][]> {
|
|
16
|
+
const batchSize = config.embedding.batchSize;
|
|
17
|
+
const maxTokens = config.embedding.maxTokensPerText;
|
|
18
|
+
|
|
19
|
+
for (const text of texts) {
|
|
20
|
+
const tokens = estimateTokens(text);
|
|
21
|
+
if (tokens > maxTokens) {
|
|
22
|
+
console.warn(
|
|
23
|
+
`Text exceeds max tokens (${tokens} > ${maxTokens}), truncation may occur`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const batches = chunk(texts, batchSize);
|
|
29
|
+
const allResults: number[][] = [];
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < batches.length; i++) {
|
|
32
|
+
const batch = batches[i];
|
|
33
|
+
|
|
34
|
+
const response = await fetch(`${config.embedding.url}/embeddings`, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: { "Content-Type": "application/json" },
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
model: config.embedding.model,
|
|
39
|
+
input: batch,
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
const errorText = await response.text();
|
|
45
|
+
throw new Error(
|
|
46
|
+
`Embedding failed (batch ${i + 1}/${batches.length}): ${response.status} ${errorText}`,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const data = (await response.json()) as {
|
|
51
|
+
data: { embedding: number[]; index: number }[];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const sorted = data.data.sort((a, b) => a.index - b.index);
|
|
55
|
+
allResults.push(...sorted.map((d) => d.embedding));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return allResults;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let _localEmbedder: ((texts: string[]) => Promise<number[][]>) | null = null;
|
|
62
|
+
|
|
63
|
+
async function getLocalEmbedder(): Promise<
|
|
64
|
+
((texts: string[]) => Promise<number[][]>) | null
|
|
65
|
+
> {
|
|
66
|
+
if (_localEmbedder !== null) return _localEmbedder;
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const { pipeline, env } = await import("@huggingface/transformers");
|
|
70
|
+
|
|
71
|
+
env.backends.onnx.enableParallelTensorization = true;
|
|
72
|
+
|
|
73
|
+
const embedder = await pipeline(
|
|
74
|
+
"feature-extraction",
|
|
75
|
+
"Xenova/all-MiniLM-L6-v2",
|
|
76
|
+
{
|
|
77
|
+
device: "cpu",
|
|
78
|
+
dtype: "fp32",
|
|
79
|
+
},
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
_localEmbedder = async (texts: string[]) => {
|
|
83
|
+
const outputs = await embedder(texts, {
|
|
84
|
+
pooling: "mean",
|
|
85
|
+
normalize: true,
|
|
86
|
+
});
|
|
87
|
+
const numTexts = outputs.dims[0];
|
|
88
|
+
const hiddenDim = outputs.dims[1];
|
|
89
|
+
if (numTexts === 1) {
|
|
90
|
+
return [Array.from(outputs.data)];
|
|
91
|
+
}
|
|
92
|
+
return Array.from({ length: numTexts }, (_, i) => {
|
|
93
|
+
const start = i * hiddenDim;
|
|
94
|
+
return Array.from(outputs.data.slice(start, start + hiddenDim));
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return _localEmbedder;
|
|
99
|
+
} catch {
|
|
100
|
+
_localEmbedder = null;
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function embedTexts(texts: string[]): Promise<number[][]> {
|
|
106
|
+
if (texts.length === 0) return [];
|
|
107
|
+
|
|
108
|
+
if (hasRemoteEmbedding) {
|
|
109
|
+
try {
|
|
110
|
+
return await embedRemote(texts);
|
|
111
|
+
} catch (err) {
|
|
112
|
+
console.warn("Remote embedding failed, falling back to local:", err);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const localEmbedder = await getLocalEmbedder();
|
|
117
|
+
if (!localEmbedder) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
"No embedding backend available. Set EMBEDDING_URL or install @huggingface/transformers.",
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const batchSize = config.embedding.batchSize;
|
|
124
|
+
if (texts.length <= batchSize) {
|
|
125
|
+
return localEmbedder(texts);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const batches = chunk(texts, batchSize);
|
|
129
|
+
const allResults: number[][] = [];
|
|
130
|
+
|
|
131
|
+
for (const batch of batches) {
|
|
132
|
+
const results = await localEmbedder(batch);
|
|
133
|
+
allResults.push(...results);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return allResults;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function embedText(text: string): Promise<number[]> {
|
|
140
|
+
const results = await embedTexts([text]);
|
|
141
|
+
return results[0];
|
|
142
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { config } from "../config.ts";
|
|
2
|
+
|
|
3
|
+
const QDRANT_HEADERS = {
|
|
4
|
+
"Content-Type": "application/json",
|
|
5
|
+
"api-key": config.qdrant.apiKey,
|
|
6
|
+
"User-Agent": "grove/1.0",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
async function qdrantFetch(
|
|
10
|
+
method: "GET" | "POST" | "PUT" | "DELETE",
|
|
11
|
+
path: string,
|
|
12
|
+
body?: unknown,
|
|
13
|
+
): Promise<unknown> {
|
|
14
|
+
const url = `${config.qdrant.url}${path}`;
|
|
15
|
+
const response = await fetch(url, {
|
|
16
|
+
method,
|
|
17
|
+
headers: QDRANT_HEADERS,
|
|
18
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
if (!response.ok) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`Qdrant ${method} ${path} failed: ${response.status} ${await response.text()}`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return response.json();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface VectorPoint {
|
|
31
|
+
id: string;
|
|
32
|
+
vector: number[];
|
|
33
|
+
payload: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface SearchResult extends VectorPoint {
|
|
37
|
+
score?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function ensureCollection(
|
|
41
|
+
name: string,
|
|
42
|
+
vectorDim: number = config.vector.dim,
|
|
43
|
+
): Promise<void> {
|
|
44
|
+
const data = (await qdrantFetch("GET", "/collections")) as {
|
|
45
|
+
result: { collections: { name: string }[] };
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
if (!data.result.collections.some((c) => c.name === name)) {
|
|
49
|
+
await qdrantFetch("PUT", `/collections/${name}`, {
|
|
50
|
+
vectors: {
|
|
51
|
+
size: vectorDim,
|
|
52
|
+
distance: "Cosine",
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function createIndex(
|
|
59
|
+
collection: string,
|
|
60
|
+
field: string,
|
|
61
|
+
fieldSchema: string = "keyword",
|
|
62
|
+
): Promise<void> {
|
|
63
|
+
await qdrantFetch("PUT", `/collections/${collection}/index`, {
|
|
64
|
+
field_name: field,
|
|
65
|
+
field_schema: fieldSchema,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function upsertPoints(
|
|
70
|
+
collection: string,
|
|
71
|
+
points: VectorPoint[],
|
|
72
|
+
): Promise<void> {
|
|
73
|
+
await qdrantFetch("PUT", `/collections/${collection}/points`, { points });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function searchVectors(
|
|
77
|
+
collection: string,
|
|
78
|
+
vector: number[],
|
|
79
|
+
limit: number = 5,
|
|
80
|
+
queryFilter?: Record<string, unknown>,
|
|
81
|
+
): Promise<SearchResult[]> {
|
|
82
|
+
const body: Record<string, unknown> = { vector, limit, with_payload: true };
|
|
83
|
+
if (queryFilter) body.filter = queryFilter;
|
|
84
|
+
|
|
85
|
+
const data = (await qdrantFetch(
|
|
86
|
+
"POST",
|
|
87
|
+
`/collections/${collection}/points/search`,
|
|
88
|
+
body,
|
|
89
|
+
)) as {
|
|
90
|
+
result: SearchResult[];
|
|
91
|
+
};
|
|
92
|
+
return data.result;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function deletePoints(
|
|
96
|
+
collection: string,
|
|
97
|
+
ids: string[],
|
|
98
|
+
): Promise<void> {
|
|
99
|
+
await qdrantFetch("POST", `/collections/${collection}/points/delete`, {
|
|
100
|
+
points: ids,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function deleteByFilter(
|
|
105
|
+
collection: string,
|
|
106
|
+
filter: Record<string, unknown>,
|
|
107
|
+
): Promise<void> {
|
|
108
|
+
await qdrantFetch("POST", `/collections/${collection}/points/delete`, {
|
|
109
|
+
filter,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export async function scrollPoints(
|
|
114
|
+
collection: string,
|
|
115
|
+
filter?: Record<string, unknown>,
|
|
116
|
+
limit: number = 100,
|
|
117
|
+
): Promise<VectorPoint[]> {
|
|
118
|
+
const body: Record<string, unknown> = { limit, with_payload: true };
|
|
119
|
+
if (filter) body.filter = filter;
|
|
120
|
+
|
|
121
|
+
const data = (await qdrantFetch(
|
|
122
|
+
"POST",
|
|
123
|
+
`/collections/${collection}/points/scroll`,
|
|
124
|
+
body,
|
|
125
|
+
)) as {
|
|
126
|
+
result: { points: VectorPoint[] };
|
|
127
|
+
};
|
|
128
|
+
return data.result.points;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export async function deleteCollection(name: string): Promise<void> {
|
|
132
|
+
await qdrantFetch("DELETE", `/collections/${name}`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export async function getPoint(
|
|
136
|
+
collection: string,
|
|
137
|
+
id: string,
|
|
138
|
+
): Promise<VectorPoint | null> {
|
|
139
|
+
try {
|
|
140
|
+
const data = (await qdrantFetch(
|
|
141
|
+
"GET",
|
|
142
|
+
`/collections/${collection}/points/${id}`,
|
|
143
|
+
)) as { result: VectorPoint | null };
|
|
144
|
+
return data.result;
|
|
145
|
+
} catch {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function getCollectionInfo(name: string): Promise<{
|
|
151
|
+
vectorsCount: number;
|
|
152
|
+
pointsCount: number;
|
|
153
|
+
indexedVectorsCount: number;
|
|
154
|
+
} | null> {
|
|
155
|
+
try {
|
|
156
|
+
const data = (await qdrantFetch("GET", `/collections/${name}`)) as {
|
|
157
|
+
result: {
|
|
158
|
+
vectors_count: number;
|
|
159
|
+
points_count: number;
|
|
160
|
+
indexed_vectors_count: number;
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
vectorsCount: data.result.vectors_count,
|
|
165
|
+
pointsCount: data.result.points_count,
|
|
166
|
+
indexedVectorsCount: data.result.indexed_vectors_count,
|
|
167
|
+
};
|
|
168
|
+
} catch {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export const qdrant = {
|
|
174
|
+
ensureCollection,
|
|
175
|
+
createIndex,
|
|
176
|
+
upsertPoints,
|
|
177
|
+
searchVectors,
|
|
178
|
+
deletePoints,
|
|
179
|
+
deleteByFilter,
|
|
180
|
+
scrollPoints,
|
|
181
|
+
deleteCollection,
|
|
182
|
+
getPoint,
|
|
183
|
+
getCollectionInfo,
|
|
184
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { config } from "../config.ts";
|
|
2
|
+
|
|
3
|
+
export interface RerankResult {
|
|
4
|
+
index: number;
|
|
5
|
+
relevanceScore: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function rerankDocuments(
|
|
9
|
+
query: string,
|
|
10
|
+
documents: string[],
|
|
11
|
+
topK: number = 5,
|
|
12
|
+
): Promise<RerankResult[]> {
|
|
13
|
+
const response = await fetch(`${config.reranking.url}/rerank`, {
|
|
14
|
+
method: "POST",
|
|
15
|
+
headers: { "Content-Type": "application/json" },
|
|
16
|
+
body: JSON.stringify({
|
|
17
|
+
model: config.reranking.model,
|
|
18
|
+
query,
|
|
19
|
+
documents,
|
|
20
|
+
top_k: topK,
|
|
21
|
+
}),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (!response.ok) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Reranking failed: ${response.status} ${await response.text()}`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const data = (await response.json()) as {
|
|
31
|
+
results: { index: number; relevance_score: number }[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return data.results
|
|
35
|
+
.sort((a, b) => b.relevance_score - a.relevance_score)
|
|
36
|
+
.map((r) => ({
|
|
37
|
+
index: r.index,
|
|
38
|
+
relevanceScore: r.relevance_score,
|
|
39
|
+
}));
|
|
40
|
+
}
|
package/tsconfig.json
ADDED