@anvia/qdrant 0.1.0
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/LICENSE +21 -0
- package/README.md +104 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +180 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Indra Zulfi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# @anvia/qdrant
|
|
2
|
+
|
|
3
|
+
Qdrant vector store adapter for Anvia.
|
|
4
|
+
|
|
5
|
+
Use this package when you want to store Anvia embedded documents in Qdrant and query them through Anvia's vector search interfaces.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @anvia/qdrant @anvia/core @qdrant/js-client-rest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
In this monorepo, the package is available through the workspace:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm --filter @anvia/qdrant build
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { embedDocuments } from "@anvia/core";
|
|
23
|
+
import { OpenAIClient } from "@anvia/openai";
|
|
24
|
+
import { QdrantVectorStore } from "@anvia/qdrant";
|
|
25
|
+
|
|
26
|
+
const openai = new OpenAIClient({
|
|
27
|
+
apiKey,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const embeddings = openai.embeddingModel("text-embedding-3-small");
|
|
31
|
+
|
|
32
|
+
const documents = await embedDocuments(
|
|
33
|
+
embeddings,
|
|
34
|
+
[
|
|
35
|
+
{
|
|
36
|
+
id: "password-reset",
|
|
37
|
+
title: "Password reset policy",
|
|
38
|
+
body: "Password reset links expire after 30 minutes.",
|
|
39
|
+
product: "support",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: "priority-support",
|
|
43
|
+
title: "Priority support",
|
|
44
|
+
body: "Enterprise customers receive priority support.",
|
|
45
|
+
product: "support",
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
{
|
|
49
|
+
id: (document) => document.id,
|
|
50
|
+
content: (document) => `${document.title}\n${document.body}`,
|
|
51
|
+
metadata: (document) => ({
|
|
52
|
+
product: document.product,
|
|
53
|
+
title: document.title,
|
|
54
|
+
}),
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const store = await QdrantVectorStore.connect({
|
|
59
|
+
collectionName: "support_docs",
|
|
60
|
+
vectorSize: 1536,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await store.upsertDocuments(documents);
|
|
64
|
+
|
|
65
|
+
const index = store.index(embeddings);
|
|
66
|
+
const results = await index.search({
|
|
67
|
+
query: "How long does a password reset link last?",
|
|
68
|
+
topK: 3,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
console.log(results);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Qdrant
|
|
75
|
+
|
|
76
|
+
By default, `QdrantVectorStore.connect` creates a `QdrantClient` from the `@qdrant/js-client-rest` package. You can also pass a custom client:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const store = await QdrantVectorStore.connect({
|
|
80
|
+
client,
|
|
81
|
+
collectionName: "support_docs",
|
|
82
|
+
vectorSize: 1536,
|
|
83
|
+
createIfMissing: true,
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Qdrant requires collection dimensions before creating a collection, so `vectorSize` is required.
|
|
88
|
+
|
|
89
|
+
`connect(...)` is async by design. It verifies or creates the Qdrant collection before returning a store, so configuration and connection errors fail early instead of surfacing later from `upsertDocuments(...)` or `search(...)`. Constructors stay synchronous and side-effect free.
|
|
90
|
+
|
|
91
|
+
## Exports
|
|
92
|
+
|
|
93
|
+
- `QdrantVectorStore`
|
|
94
|
+
- `QdrantVectorIndex`
|
|
95
|
+
- `filterToQdrantFilter`
|
|
96
|
+
- `QdrantVectorStoreConnectOptions`
|
|
97
|
+
|
|
98
|
+
## Development
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
pnpm --filter @anvia/qdrant typecheck
|
|
102
|
+
pnpm --filter @anvia/qdrant test
|
|
103
|
+
pnpm --filter @anvia/qdrant build
|
|
104
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { VectorMetadata, VectorSearchIndex, EmbeddingModel, VectorSearchRequest, VectorSearchResult, VectorSearchToolOptions, Tool, EmbeddedDocument, VectorFilter } from '@anvia/core';
|
|
2
|
+
|
|
3
|
+
type QdrantDistance = "Cosine" | "Dot" | "Euclid";
|
|
4
|
+
type QdrantClientLike = {
|
|
5
|
+
getCollection(collectionName: string): Promise<unknown>;
|
|
6
|
+
createCollection(collectionName: string, options: Record<string, unknown>): Promise<unknown>;
|
|
7
|
+
upsert(collectionName: string, options: Record<string, unknown>): Promise<unknown>;
|
|
8
|
+
search(collectionName: string, options: Record<string, unknown>): Promise<unknown>;
|
|
9
|
+
};
|
|
10
|
+
type QdrantVectorStoreConnectOptions = {
|
|
11
|
+
client?: QdrantClientLike | undefined;
|
|
12
|
+
collectionName: string;
|
|
13
|
+
vectorSize: number;
|
|
14
|
+
createIfMissing?: boolean | undefined;
|
|
15
|
+
distance?: QdrantDistance | undefined;
|
|
16
|
+
};
|
|
17
|
+
declare class QdrantVectorStore<T, Metadata extends VectorMetadata = VectorMetadata> {
|
|
18
|
+
private readonly client;
|
|
19
|
+
private readonly collectionName;
|
|
20
|
+
private constructor();
|
|
21
|
+
static connect<T, Metadata extends VectorMetadata = VectorMetadata>(options: QdrantVectorStoreConnectOptions): Promise<QdrantVectorStore<T, Metadata>>;
|
|
22
|
+
upsertDocuments(documents: Array<EmbeddedDocument<T, Metadata>>): Promise<void>;
|
|
23
|
+
index(model: EmbeddingModel): QdrantVectorIndex<T, Metadata>;
|
|
24
|
+
}
|
|
25
|
+
declare class QdrantVectorIndex<T, Metadata extends VectorMetadata = VectorMetadata> implements VectorSearchIndex<T, Metadata> {
|
|
26
|
+
private readonly model;
|
|
27
|
+
private readonly client;
|
|
28
|
+
private readonly collectionName;
|
|
29
|
+
constructor(model: EmbeddingModel, client: QdrantClientLike, collectionName: string);
|
|
30
|
+
search(request: VectorSearchRequest): Promise<Array<VectorSearchResult<T, Metadata>>>;
|
|
31
|
+
searchIds(request: VectorSearchRequest): Promise<Array<{
|
|
32
|
+
score: number;
|
|
33
|
+
id: string;
|
|
34
|
+
}>>;
|
|
35
|
+
asTool(options: VectorSearchToolOptions): Tool<{
|
|
36
|
+
query: string;
|
|
37
|
+
topK?: number;
|
|
38
|
+
}, unknown>;
|
|
39
|
+
}
|
|
40
|
+
declare function filterToQdrantFilter(filter: VectorFilter | undefined): unknown;
|
|
41
|
+
|
|
42
|
+
export { type QdrantDistance, QdrantVectorIndex, QdrantVectorStore, type QdrantVectorStoreConnectOptions, filterToQdrantFilter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createHash } from "crypto";
|
|
3
|
+
import {
|
|
4
|
+
createVectorSearchTool,
|
|
5
|
+
embedText
|
|
6
|
+
} from "@anvia/core";
|
|
7
|
+
var documentIdPayloadKey = "__anvia_document_id";
|
|
8
|
+
var documentPayloadKey = "__anvia_document";
|
|
9
|
+
var reservedPayloadPrefix = "__anvia_";
|
|
10
|
+
var QdrantVectorStore = class _QdrantVectorStore {
|
|
11
|
+
constructor(client, collectionName) {
|
|
12
|
+
this.client = client;
|
|
13
|
+
this.collectionName = collectionName;
|
|
14
|
+
}
|
|
15
|
+
client;
|
|
16
|
+
collectionName;
|
|
17
|
+
static async connect(options) {
|
|
18
|
+
const client = options.client ?? await defaultQdrantClient();
|
|
19
|
+
if (options.createIfMissing === false) {
|
|
20
|
+
await client.getCollection(options.collectionName);
|
|
21
|
+
return new _QdrantVectorStore(client, options.collectionName);
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
await client.getCollection(options.collectionName);
|
|
25
|
+
} catch {
|
|
26
|
+
await client.createCollection(options.collectionName, {
|
|
27
|
+
vectors: {
|
|
28
|
+
size: options.vectorSize,
|
|
29
|
+
distance: options.distance ?? "Cosine"
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return new _QdrantVectorStore(client, options.collectionName);
|
|
34
|
+
}
|
|
35
|
+
async upsertDocuments(documents) {
|
|
36
|
+
const points = documents.flatMap((document) => qdrantPoints(document));
|
|
37
|
+
await this.client.upsert(this.collectionName, { points });
|
|
38
|
+
}
|
|
39
|
+
index(model) {
|
|
40
|
+
return new QdrantVectorIndex(model, this.client, this.collectionName);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var QdrantVectorIndex = class {
|
|
44
|
+
constructor(model, client, collectionName) {
|
|
45
|
+
this.model = model;
|
|
46
|
+
this.client = client;
|
|
47
|
+
this.collectionName = collectionName;
|
|
48
|
+
}
|
|
49
|
+
model;
|
|
50
|
+
client;
|
|
51
|
+
collectionName;
|
|
52
|
+
async search(request) {
|
|
53
|
+
const queryEmbedding = await embedText(this.model, request.query);
|
|
54
|
+
const response = await this.client.search(this.collectionName, {
|
|
55
|
+
vector: queryEmbedding.vector,
|
|
56
|
+
limit: request.topK,
|
|
57
|
+
filter: filterToQdrantFilter(request.filter),
|
|
58
|
+
with_payload: true
|
|
59
|
+
});
|
|
60
|
+
return parseQueryResults(response, request.threshold);
|
|
61
|
+
}
|
|
62
|
+
async searchIds(request) {
|
|
63
|
+
return (await this.search(request)).map(({ score, id }) => ({ score, id }));
|
|
64
|
+
}
|
|
65
|
+
asTool(options) {
|
|
66
|
+
return createVectorSearchTool(this, options);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
function filterToQdrantFilter(filter) {
|
|
70
|
+
if (filter === void 0) {
|
|
71
|
+
return void 0;
|
|
72
|
+
}
|
|
73
|
+
switch (filter.type) {
|
|
74
|
+
case "eq":
|
|
75
|
+
return { must: [{ key: filter.key, match: { value: filter.value } }] };
|
|
76
|
+
case "gt":
|
|
77
|
+
return { must: [{ key: filter.key, range: { gt: filter.value } }] };
|
|
78
|
+
case "lt":
|
|
79
|
+
return { must: [{ key: filter.key, range: { lt: filter.value } }] };
|
|
80
|
+
case "and":
|
|
81
|
+
return { must: filter.filters.map(filterToQdrantFilter) };
|
|
82
|
+
case "or":
|
|
83
|
+
return { should: filter.filters.map(filterToQdrantFilter) };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async function defaultQdrantClient() {
|
|
87
|
+
const qdrant = await import("@qdrant/js-client-rest");
|
|
88
|
+
return new qdrant.QdrantClient({});
|
|
89
|
+
}
|
|
90
|
+
function qdrantPoints(document) {
|
|
91
|
+
if (document.embeddings.length === 0) {
|
|
92
|
+
throw new Error(`Document ${document.id} has no embeddings`);
|
|
93
|
+
}
|
|
94
|
+
assertNoReservedMetadata(document.metadata);
|
|
95
|
+
return document.embeddings.map((embedding, index) => {
|
|
96
|
+
const logicalId = document.embeddings.length === 1 ? document.id : `${document.id}#embedding:${index}`;
|
|
97
|
+
return {
|
|
98
|
+
id: pointId(logicalId),
|
|
99
|
+
vector: embedding.vector,
|
|
100
|
+
payload: {
|
|
101
|
+
[documentIdPayloadKey]: document.id,
|
|
102
|
+
[documentPayloadKey]: serializeDocument(document.document),
|
|
103
|
+
...document.metadata ?? {}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function assertNoReservedMetadata(metadata) {
|
|
109
|
+
for (const key of Object.keys(metadata ?? {})) {
|
|
110
|
+
if (key.startsWith(reservedPayloadPrefix)) {
|
|
111
|
+
throw new Error(`Metadata key ${key} is reserved for Anvia Qdrant payloads`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function pointId(id) {
|
|
116
|
+
const hex = createHash("sha256").update(id).digest("hex").slice(0, 32);
|
|
117
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(
|
|
118
|
+
16,
|
|
119
|
+
20
|
|
120
|
+
)}-${hex.slice(20)}`;
|
|
121
|
+
}
|
|
122
|
+
function serializeDocument(document) {
|
|
123
|
+
return typeof document === "string" ? document : JSON.stringify(document);
|
|
124
|
+
}
|
|
125
|
+
function parseQueryResults(response, threshold) {
|
|
126
|
+
const points = rawPoints(response);
|
|
127
|
+
const byId = /* @__PURE__ */ new Map();
|
|
128
|
+
for (const point of points) {
|
|
129
|
+
if (threshold !== void 0 && point.score < threshold) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const id = String(point.payload?.[documentIdPayloadKey] ?? point.id);
|
|
133
|
+
const result = {
|
|
134
|
+
id,
|
|
135
|
+
score: point.score,
|
|
136
|
+
document: parseDocument(point.payload?.[documentPayloadKey]),
|
|
137
|
+
...metadataFromPayload(point.payload)
|
|
138
|
+
};
|
|
139
|
+
const current = byId.get(id);
|
|
140
|
+
if (current === void 0 || result.score > current.score) {
|
|
141
|
+
byId.set(id, result);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return [...byId.values()];
|
|
145
|
+
}
|
|
146
|
+
function rawPoints(response) {
|
|
147
|
+
const raw = response;
|
|
148
|
+
const responseArray = Array.isArray(response) ? response : void 0;
|
|
149
|
+
const points = responseArray ?? (Array.isArray(raw.result) ? raw.result : Array.isArray(raw.result?.points) ? raw.result.points : raw.points);
|
|
150
|
+
return (points ?? []).map((point) => ({
|
|
151
|
+
id: point.id,
|
|
152
|
+
score: point.score ?? 0,
|
|
153
|
+
...point.payload === void 0 ? {} : { payload: point.payload }
|
|
154
|
+
}));
|
|
155
|
+
}
|
|
156
|
+
function parseDocument(document) {
|
|
157
|
+
if (document === null || document === void 0) {
|
|
158
|
+
return "";
|
|
159
|
+
}
|
|
160
|
+
if (typeof document !== "string") {
|
|
161
|
+
return document;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
return JSON.parse(document);
|
|
165
|
+
} catch {
|
|
166
|
+
return document;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function metadataFromPayload(payload) {
|
|
170
|
+
const metadata = Object.fromEntries(
|
|
171
|
+
Object.entries(payload ?? {}).filter(([key]) => !key.startsWith(reservedPayloadPrefix))
|
|
172
|
+
);
|
|
173
|
+
return Object.keys(metadata).length === 0 ? {} : { metadata };
|
|
174
|
+
}
|
|
175
|
+
export {
|
|
176
|
+
QdrantVectorIndex,
|
|
177
|
+
QdrantVectorStore,
|
|
178
|
+
filterToQdrantFilter
|
|
179
|
+
};
|
|
180
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport {\n createVectorSearchTool,\n type EmbeddedDocument,\n type EmbeddingModel,\n embedText,\n type Tool,\n type VectorFilter,\n type VectorMetadata,\n type VectorSearchIndex,\n type VectorSearchRequest,\n type VectorSearchResult,\n type VectorSearchToolOptions,\n} from \"@anvia/core\";\n\nconst documentIdPayloadKey = \"__anvia_document_id\";\nconst documentPayloadKey = \"__anvia_document\";\nconst reservedPayloadPrefix = \"__anvia_\";\n\nexport type QdrantDistance = \"Cosine\" | \"Dot\" | \"Euclid\";\n\ntype QdrantClientLike = {\n getCollection(collectionName: string): Promise<unknown>;\n createCollection(collectionName: string, options: Record<string, unknown>): Promise<unknown>;\n upsert(collectionName: string, options: Record<string, unknown>): Promise<unknown>;\n search(collectionName: string, options: Record<string, unknown>): Promise<unknown>;\n};\n\nexport type QdrantVectorStoreConnectOptions = {\n client?: QdrantClientLike | undefined;\n collectionName: string;\n vectorSize: number;\n createIfMissing?: boolean | undefined;\n distance?: QdrantDistance | undefined;\n};\n\nexport class QdrantVectorStore<T, Metadata extends VectorMetadata = VectorMetadata> {\n private constructor(\n private readonly client: QdrantClientLike,\n private readonly collectionName: string,\n ) {}\n\n static async connect<T, Metadata extends VectorMetadata = VectorMetadata>(\n options: QdrantVectorStoreConnectOptions,\n ): Promise<QdrantVectorStore<T, Metadata>> {\n const client = options.client ?? (await defaultQdrantClient());\n if (options.createIfMissing === false) {\n await client.getCollection(options.collectionName);\n return new QdrantVectorStore<T, Metadata>(client, options.collectionName);\n }\n\n try {\n await client.getCollection(options.collectionName);\n } catch {\n await client.createCollection(options.collectionName, {\n vectors: {\n size: options.vectorSize,\n distance: options.distance ?? \"Cosine\",\n },\n });\n }\n return new QdrantVectorStore<T, Metadata>(client, options.collectionName);\n }\n\n async upsertDocuments(documents: Array<EmbeddedDocument<T, Metadata>>): Promise<void> {\n const points = documents.flatMap((document) => qdrantPoints(document));\n await this.client.upsert(this.collectionName, { points });\n }\n\n index(model: EmbeddingModel): QdrantVectorIndex<T, Metadata> {\n return new QdrantVectorIndex(model, this.client, this.collectionName);\n }\n}\n\nexport class QdrantVectorIndex<T, Metadata extends VectorMetadata = VectorMetadata>\n implements VectorSearchIndex<T, Metadata>\n{\n constructor(\n private readonly model: EmbeddingModel,\n private readonly client: QdrantClientLike,\n private readonly collectionName: string,\n ) {}\n\n async search(request: VectorSearchRequest): Promise<Array<VectorSearchResult<T, Metadata>>> {\n const queryEmbedding = await embedText(this.model, request.query);\n const response = await this.client.search(this.collectionName, {\n vector: queryEmbedding.vector,\n limit: request.topK,\n filter: filterToQdrantFilter(request.filter),\n with_payload: true,\n });\n return parseQueryResults<T, Metadata>(response, request.threshold);\n }\n\n async searchIds(request: VectorSearchRequest): Promise<Array<{ score: number; id: string }>> {\n return (await this.search(request)).map(({ score, id }) => ({ score, id }));\n }\n\n asTool(options: VectorSearchToolOptions): Tool<{ query: string; topK?: number }, unknown> {\n return createVectorSearchTool(this, options);\n }\n}\n\nexport function filterToQdrantFilter(filter: VectorFilter | undefined): unknown {\n if (filter === undefined) {\n return undefined;\n }\n\n switch (filter.type) {\n case \"eq\":\n return { must: [{ key: filter.key, match: { value: filter.value } }] };\n case \"gt\":\n return { must: [{ key: filter.key, range: { gt: filter.value } }] };\n case \"lt\":\n return { must: [{ key: filter.key, range: { lt: filter.value } }] };\n case \"and\":\n return { must: filter.filters.map(filterToQdrantFilter) };\n case \"or\":\n return { should: filter.filters.map(filterToQdrantFilter) };\n }\n}\n\nasync function defaultQdrantClient(): Promise<QdrantClientLike> {\n const qdrant = await import(\"@qdrant/js-client-rest\");\n return new qdrant.QdrantClient({}) as QdrantClientLike;\n}\n\nfunction qdrantPoints<T, Metadata extends VectorMetadata>(\n document: EmbeddedDocument<T, Metadata>,\n): Array<{\n id: string;\n vector: number[];\n payload: Record<string, unknown>;\n}> {\n if (document.embeddings.length === 0) {\n throw new Error(`Document ${document.id} has no embeddings`);\n }\n assertNoReservedMetadata(document.metadata);\n\n return document.embeddings.map((embedding, index) => {\n const logicalId =\n document.embeddings.length === 1 ? document.id : `${document.id}#embedding:${index}`;\n return {\n id: pointId(logicalId),\n vector: embedding.vector,\n payload: {\n [documentIdPayloadKey]: document.id,\n [documentPayloadKey]: serializeDocument(document.document),\n ...(document.metadata ?? {}),\n },\n };\n });\n}\n\nfunction assertNoReservedMetadata(metadata: VectorMetadata | undefined): void {\n for (const key of Object.keys(metadata ?? {})) {\n if (key.startsWith(reservedPayloadPrefix)) {\n throw new Error(`Metadata key ${key} is reserved for Anvia Qdrant payloads`);\n }\n }\n}\n\nfunction pointId(id: string): string {\n const hex = createHash(\"sha256\").update(id).digest(\"hex\").slice(0, 32);\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(\n 16,\n 20,\n )}-${hex.slice(20)}`;\n}\n\nfunction serializeDocument(document: unknown): string {\n return typeof document === \"string\" ? document : JSON.stringify(document);\n}\n\nfunction parseQueryResults<T, Metadata extends VectorMetadata>(\n response: unknown,\n threshold: number | undefined,\n): Array<VectorSearchResult<T, Metadata>> {\n const points = rawPoints(response);\n const byId = new Map<string, VectorSearchResult<T, Metadata>>();\n\n for (const point of points) {\n if (threshold !== undefined && point.score < threshold) {\n continue;\n }\n\n const id = String(point.payload?.[documentIdPayloadKey] ?? point.id);\n const result = {\n id,\n score: point.score,\n document: parseDocument(point.payload?.[documentPayloadKey]),\n ...metadataFromPayload<Metadata>(point.payload),\n } as VectorSearchResult<T, Metadata>;\n const current = byId.get(id);\n if (current === undefined || result.score > current.score) {\n byId.set(id, result);\n }\n }\n\n return [...byId.values()];\n}\n\nfunction rawPoints(response: unknown): Array<{\n id: string | number;\n score: number;\n payload?: Record<string, unknown> | null;\n}> {\n const raw = response as {\n points?: Array<{\n id: string | number;\n score?: number;\n payload?: Record<string, unknown> | null;\n }>;\n result?:\n | {\n points?: Array<{\n id: string | number;\n score?: number;\n payload?: Record<string, unknown> | null;\n }>;\n }\n | Array<{ id: string | number; score?: number; payload?: Record<string, unknown> | null }>;\n };\n const responseArray = Array.isArray(response)\n ? (response as Array<{\n id: string | number;\n score?: number;\n payload?: Record<string, unknown> | null;\n }>)\n : undefined;\n const points =\n responseArray ??\n (Array.isArray(raw.result)\n ? raw.result\n : Array.isArray(raw.result?.points)\n ? raw.result.points\n : raw.points);\n return (points ?? []).map((point) => ({\n id: point.id,\n score: point.score ?? 0,\n ...(point.payload === undefined ? {} : { payload: point.payload }),\n }));\n}\n\nfunction parseDocument<T>(document: unknown): T {\n if (document === null || document === undefined) {\n return \"\" as T;\n }\n if (typeof document !== \"string\") {\n return document as T;\n }\n try {\n return JSON.parse(document) as T;\n } catch {\n return document as T;\n }\n}\n\nfunction metadataFromPayload<Metadata extends VectorMetadata>(\n payload: Record<string, unknown> | null | undefined,\n): { metadata?: Metadata | undefined } {\n const metadata = Object.fromEntries(\n Object.entries(payload ?? {}).filter(([key]) => !key.startsWith(reservedPayloadPrefix)),\n ) as Metadata;\n return Object.keys(metadata).length === 0 ? {} : { metadata };\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AAC3B;AAAA,EACE;AAAA,EAGA;AAAA,OAQK;AAEP,IAAM,uBAAuB;AAC7B,IAAM,qBAAqB;AAC3B,IAAM,wBAAwB;AAmBvB,IAAM,oBAAN,MAAM,mBAAuE;AAAA,EAC1E,YACW,QACA,gBACjB;AAFiB;AACA;AAAA,EAChB;AAAA,EAFgB;AAAA,EACA;AAAA,EAGnB,aAAa,QACX,SACyC;AACzC,UAAM,SAAS,QAAQ,UAAW,MAAM,oBAAoB;AAC5D,QAAI,QAAQ,oBAAoB,OAAO;AACrC,YAAM,OAAO,cAAc,QAAQ,cAAc;AACjD,aAAO,IAAI,mBAA+B,QAAQ,QAAQ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AG,KAAK,OAAO,CAAC;AAC1B;AAEA,SAAS,UAAU,UAIhB;AACD,QAAM,MAAM;AAgBZ,QAAM,gBAAgB,MAAM,QAAQ,QAAQ,IACvC,WAKD;AACJ,QAAM,SACJ,kBACC,MAAM,QAAQ,IAAI,MAAM,IACrB,IAAI,SACJ,MAAM,QAAQ,IAAI,QAAQ,MAAM,IAC9B,IAAI,OAAO,SACX,IAAI;AACZ,UAAQ,UAAU,CAAC,GAAG,IAAI,CAAC,WAAW;AAAA,IACpC,IAAI,MAAM;AAAA,IACV,OAAO,MAAM,SAAS;AAAA,IACtB,GAAI,MAAM,YAAY,SAAY,CAAC,IAAI,EAAE,SAAS,MAAM,QAAQ;AAAA,EAClE,EAAE;AACJ;AAEA,SAAS,cAAiB,UAAsB;AAC9C,MAAI,aAAa,QAAQ,aAAa,QAAW;AAC/C,WAAO;AAAA,EACT;AACA,MAAI,OAAO,aAAa,UAAU;AAChC,WAAO;AAAA,EACT;AACA,MAAI;AACF,WAAO,KAAK,MAAM,QAAQ;AAAA,EAC5B,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,oBACP,SACqC;AACrC,QAAM,WAAW,OAAO;AAAA,IACtB,OAAO,QAAQ,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,GAAG,MAAM,CAAC,IAAI,WAAW,qBAAqB,CAAC;AAAA,EACxF;AACA,SAAO,OAAO,KAAK,QAAQ,EAAE,WAAW,IAAI,CAAC,IAAI,EAAE,SAAS;AAC9D;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anvia/qdrant",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Qdrant vector store adapter for Anvia.",
|
|
5
|
+
"author": "anvia",
|
|
6
|
+
"maintainer": "Indra Zulfi",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@qdrant/js-client-rest": "^1.17.0",
|
|
22
|
+
"@anvia/core": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^24.9.1",
|
|
26
|
+
"tsup": "^8.5.0",
|
|
27
|
+
"typescript": "^5.9.3",
|
|
28
|
+
"vitest": "^4.0.8"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsup src/index.ts --format esm --dts --sourcemap --clean",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"typecheck": "tsc --noEmit"
|
|
34
|
+
}
|
|
35
|
+
}
|