@h-ai/vecdb 0.1.0-alpha.66 → 0.1.0-alpha.68
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/README.en.md +54 -13
- package/README.ja.md +55 -14
- package/README.md +55 -14
- package/dist/index.d.ts +212 -132
- package/dist/index.js +434 -263
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -5,21 +5,21 @@ import { HaiResult } from '@h-ai/core';
|
|
|
5
5
|
/**
|
|
6
6
|
* @h-ai/vecdb — 向量数据库配置 Schema
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* 本文件定义向量数据库模块的 Zod Schema 和配置类型;错误码见 vecdb-types。
|
|
9
9
|
* 支持 LanceDB(默认)、pgvector、Qdrant、Chroma 四种后端。
|
|
10
10
|
* @module vecdb-config
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* 向量数据库类型
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import type { VecdbType } from '@h-ai/vecdb'
|
|
15
18
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* - `qdrant` — Qdrant 向量搜索引擎
|
|
20
|
-
* - `chroma` — Chroma 向量数据库(支持嵌入式自动拉起本地服务)
|
|
19
|
+
* const value: VecdbType = 'qdrant'
|
|
20
|
+
* void value
|
|
21
|
+
* ```
|
|
21
22
|
*/
|
|
22
|
-
/** 向量数据库类型 */
|
|
23
23
|
type VecdbType = 'lancedb' | 'pgvector' | 'qdrant' | 'chroma';
|
|
24
24
|
/**
|
|
25
25
|
* 距离度量类型
|
|
@@ -27,14 +27,40 @@ type VecdbType = 'lancedb' | 'pgvector' | 'qdrant' | 'chroma';
|
|
|
27
27
|
* - `cosine` — 余弦相似度(默认,适用于文本嵌入)
|
|
28
28
|
* - `euclidean` — 欧氏距离(L2)
|
|
29
29
|
* - `dot` — 内积(点积)
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { DistanceMetricSchema } from '@h-ai/vecdb'
|
|
33
|
+
*
|
|
34
|
+
* const value = DistanceMetricSchema.parse('cosine')
|
|
35
|
+
* void value
|
|
36
|
+
* ```
|
|
30
37
|
*/
|
|
31
38
|
declare const DistanceMetricSchema: z.ZodDefault<z.ZodEnum<{
|
|
32
39
|
cosine: "cosine";
|
|
33
40
|
euclidean: "euclidean";
|
|
34
41
|
dot: "dot";
|
|
35
42
|
}>>;
|
|
36
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* 距离度量类型
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* import type { DistanceMetric } from '@h-ai/vecdb'
|
|
48
|
+
*
|
|
49
|
+
* const value: DistanceMetric = 'cosine'
|
|
50
|
+
* void value
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
37
53
|
type DistanceMetric = z.infer<typeof DistanceMetricSchema>;
|
|
54
|
+
/**
|
|
55
|
+
* 操作日志配置;read/write 默认关闭,长度和级别由 Schema 校验。
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { VecdbOperationLogConfigSchema } from '@h-ai/vecdb'
|
|
59
|
+
*
|
|
60
|
+
* const value = VecdbOperationLogConfigSchema.parse({ read: false, write: true, maxLength: 500, level: 'debug' })
|
|
61
|
+
* void value
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
38
64
|
declare const VecdbOperationLogConfigSchema: z.ZodObject<{
|
|
39
65
|
read: z.ZodDefault<z.ZodBoolean>;
|
|
40
66
|
write: z.ZodDefault<z.ZodBoolean>;
|
|
@@ -45,15 +71,21 @@ declare const VecdbOperationLogConfigSchema: z.ZodObject<{
|
|
|
45
71
|
trace: "trace";
|
|
46
72
|
}>>;
|
|
47
73
|
}, z.core.$strip>;
|
|
48
|
-
type VecdbOperationLogConfig = z.infer<typeof VecdbOperationLogConfigSchema>;
|
|
49
74
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
75
|
+
* 已解析的操作日志配置,单位和默认值与 Schema 保持一致。
|
|
52
76
|
* @example
|
|
53
77
|
* ```ts
|
|
54
|
-
* { type
|
|
78
|
+
* import { VecdbOperationLogConfigSchema, type VecdbOperationLogConfig } from '@h-ai/vecdb'
|
|
79
|
+
*
|
|
80
|
+
* const value: VecdbOperationLogConfig = VecdbOperationLogConfigSchema.parse({})
|
|
81
|
+
* void value
|
|
55
82
|
* ```
|
|
56
83
|
*/
|
|
84
|
+
type VecdbOperationLogConfig = z.infer<typeof VecdbOperationLogConfigSchema>;
|
|
85
|
+
/**
|
|
86
|
+
* LanceDB 配置 Schema
|
|
87
|
+
*
|
|
88
|
+
*/
|
|
57
89
|
declare const LancedbConfigSchema: z.ZodObject<{
|
|
58
90
|
operationLog: z.ZodOptional<z.ZodObject<{
|
|
59
91
|
read: z.ZodDefault<z.ZodBoolean>;
|
|
@@ -73,27 +105,24 @@ declare const LancedbConfigSchema: z.ZodObject<{
|
|
|
73
105
|
dot: "dot";
|
|
74
106
|
}>>>;
|
|
75
107
|
}, z.core.$strip>;
|
|
76
|
-
/** LanceDB 配置类型 */
|
|
77
|
-
type LancedbConfig = z.infer<typeof LancedbConfigSchema>;
|
|
78
108
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
109
|
+
* LanceDB 配置类型
|
|
81
110
|
* @example
|
|
82
111
|
* ```ts
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* host: 'localhost',
|
|
90
|
-
* port: 5432,
|
|
91
|
-
* database: 'mydb',
|
|
92
|
-
* user: 'admin',
|
|
93
|
-
* password: 'secret'
|
|
112
|
+
* import { VecdbConfigSchema, type LancedbConfig } from '@h-ai/vecdb'
|
|
113
|
+
*
|
|
114
|
+
* const parsed = VecdbConfigSchema.parse({ type: 'lancedb', path: './data/vecdb' })
|
|
115
|
+
* if (parsed.type === 'lancedb') {
|
|
116
|
+
* const config: LancedbConfig = parsed
|
|
117
|
+
* void config
|
|
94
118
|
* }
|
|
95
119
|
* ```
|
|
96
120
|
*/
|
|
121
|
+
type LancedbConfig = z.infer<typeof LancedbConfigSchema>;
|
|
122
|
+
/**
|
|
123
|
+
* pgvector 配置 Schema
|
|
124
|
+
*
|
|
125
|
+
*/
|
|
97
126
|
declare const PgvectorConfigSchema: z.ZodObject<{
|
|
98
127
|
operationLog: z.ZodOptional<z.ZodObject<{
|
|
99
128
|
read: z.ZodDefault<z.ZodBoolean>;
|
|
@@ -123,16 +152,24 @@ declare const PgvectorConfigSchema: z.ZodObject<{
|
|
|
123
152
|
}>>>;
|
|
124
153
|
tablePrefix: z.ZodDefault<z.ZodString>;
|
|
125
154
|
}, z.core.$strip>;
|
|
126
|
-
/** pgvector 配置类型 */
|
|
127
|
-
type PgvectorConfig = z.infer<typeof PgvectorConfigSchema>;
|
|
128
155
|
/**
|
|
129
|
-
*
|
|
130
|
-
*
|
|
156
|
+
* pgvector 配置类型
|
|
131
157
|
* @example
|
|
132
158
|
* ```ts
|
|
133
|
-
* { type
|
|
159
|
+
* import { VecdbConfigSchema, type PgvectorConfig } from '@h-ai/vecdb'
|
|
160
|
+
*
|
|
161
|
+
* const parsed = VecdbConfigSchema.parse({ type: 'pgvector', host: 'db', database: 'shop' })
|
|
162
|
+
* if (parsed.type === 'pgvector') {
|
|
163
|
+
* const config: PgvectorConfig = parsed
|
|
164
|
+
* void config
|
|
165
|
+
* }
|
|
134
166
|
* ```
|
|
135
167
|
*/
|
|
168
|
+
type PgvectorConfig = z.infer<typeof PgvectorConfigSchema>;
|
|
169
|
+
/**
|
|
170
|
+
* Qdrant 配置 Schema
|
|
171
|
+
*
|
|
172
|
+
*/
|
|
136
173
|
declare const QdrantConfigSchema: z.ZodObject<{
|
|
137
174
|
operationLog: z.ZodOptional<z.ZodObject<{
|
|
138
175
|
read: z.ZodDefault<z.ZodBoolean>;
|
|
@@ -153,7 +190,19 @@ declare const QdrantConfigSchema: z.ZodObject<{
|
|
|
153
190
|
dot: "dot";
|
|
154
191
|
}>>>;
|
|
155
192
|
}, z.core.$strip>;
|
|
156
|
-
/**
|
|
193
|
+
/**
|
|
194
|
+
* Qdrant 配置类型
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* import { VecdbConfigSchema, type QdrantConfig } from '@h-ai/vecdb'
|
|
198
|
+
*
|
|
199
|
+
* const parsed = VecdbConfigSchema.parse({ type: 'qdrant', url: 'http://localhost:6333' })
|
|
200
|
+
* if (parsed.type === 'qdrant') {
|
|
201
|
+
* const config: QdrantConfig = parsed
|
|
202
|
+
* void config
|
|
203
|
+
* }
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
157
206
|
type QdrantConfig = z.infer<typeof QdrantConfigSchema>;
|
|
158
207
|
/**
|
|
159
208
|
* Chroma 配置 Schema
|
|
@@ -163,14 +212,6 @@ type QdrantConfig = z.infer<typeof QdrantConfigSchema>;
|
|
|
163
212
|
* (持久化到 `path`),close 时关闭;数据落地本地目录,无需手动启动服务。
|
|
164
213
|
* - **直连**:提供 `url`(或已运行的 host:port)时,直接连接已有 Chroma 服务,不拉起进程。
|
|
165
214
|
*
|
|
166
|
-
* @example
|
|
167
|
-
* ```ts
|
|
168
|
-
* // 嵌入式(自动拉起本地服务)
|
|
169
|
-
* { type: 'chroma', path: './data/chroma' }
|
|
170
|
-
*
|
|
171
|
-
* // 直连已有服务
|
|
172
|
-
* { type: 'chroma', url: 'http://localhost:8000' }
|
|
173
|
-
* ```
|
|
174
215
|
*/
|
|
175
216
|
declare const ChromaConfigSchema: z.ZodObject<{
|
|
176
217
|
operationLog: z.ZodOptional<z.ZodObject<{
|
|
@@ -197,12 +238,31 @@ declare const ChromaConfigSchema: z.ZodObject<{
|
|
|
197
238
|
dot: "dot";
|
|
198
239
|
}>>>;
|
|
199
240
|
}, z.core.$strip>;
|
|
200
|
-
/**
|
|
241
|
+
/**
|
|
242
|
+
* Chroma 配置类型
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* import { VecdbConfigSchema, type ChromaConfig } from '@h-ai/vecdb'
|
|
246
|
+
*
|
|
247
|
+
* const parsed = VecdbConfigSchema.parse({ type: 'chroma', url: 'http://localhost:8000' })
|
|
248
|
+
* if (parsed.type === 'chroma') {
|
|
249
|
+
* const config: ChromaConfig = parsed
|
|
250
|
+
* void config
|
|
251
|
+
* }
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
201
254
|
type ChromaConfig = z.infer<typeof ChromaConfigSchema>;
|
|
202
255
|
/**
|
|
203
256
|
* 统一向量数据库配置 Schema(判别联合体)
|
|
204
257
|
*
|
|
205
258
|
* 根据 `type` 字段区分不同向量数据库类型的配置。
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* import { VecdbConfigSchema } from '@h-ai/vecdb'
|
|
262
|
+
*
|
|
263
|
+
* const value = VecdbConfigSchema.parse({ type: 'qdrant', url: 'http://localhost:6333' })
|
|
264
|
+
* void value
|
|
265
|
+
* ```
|
|
206
266
|
*/
|
|
207
267
|
declare const VecdbConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
208
268
|
operationLog: z.ZodOptional<z.ZodObject<{
|
|
@@ -294,18 +354,43 @@ declare const VecdbConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
294
354
|
dot: "dot";
|
|
295
355
|
}>>>;
|
|
296
356
|
}, z.core.$strip>], "type">;
|
|
297
|
-
/**
|
|
357
|
+
/**
|
|
358
|
+
* 向量数据库配置类型(判别联合体)
|
|
359
|
+
* @example
|
|
360
|
+
* ```ts
|
|
361
|
+
* import { VecdbConfigSchema, type VecdbConfig } from '@h-ai/vecdb'
|
|
362
|
+
*
|
|
363
|
+
* const parsed = VecdbConfigSchema.parse({ type: 'qdrant', url: 'http://localhost:6333' })
|
|
364
|
+
* const config: VecdbConfig = parsed
|
|
365
|
+
* void config
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
298
368
|
type VecdbConfig = z.infer<typeof VecdbConfigSchema>;
|
|
299
369
|
/**
|
|
300
370
|
* 向量数据库配置输入类型(用于 init 等入口)
|
|
301
371
|
*
|
|
302
372
|
* 说明:Zod 的 default 会让输入端字段可省略,但输出端字段为必填。
|
|
303
373
|
* 因此对外 API(如 vecdb.init)更适合接收 VecdbConfigInput。
|
|
374
|
+
* @example
|
|
375
|
+
* ```ts
|
|
376
|
+
* import type { VecdbConfigInput } from '@h-ai/vecdb'
|
|
377
|
+
*
|
|
378
|
+
* const value: VecdbConfigInput = { type: 'qdrant', url: 'http://localhost:6333' }
|
|
379
|
+
* void value
|
|
380
|
+
* ```
|
|
304
381
|
*/
|
|
305
382
|
type VecdbConfigInput = z.input<typeof VecdbConfigSchema>;
|
|
306
383
|
|
|
307
384
|
/**
|
|
308
385
|
* Vecdb 模块标准错误定义对象。
|
|
386
|
+
* @example
|
|
387
|
+
* ```ts
|
|
388
|
+
* import { HaiVecdbError } from '@h-ai/vecdb'
|
|
389
|
+
* import { err } from '@h-ai/core'
|
|
390
|
+
*
|
|
391
|
+
* const result = err(HaiVecdbError.COLLECTION_NOT_FOUND, 'Collection missing')
|
|
392
|
+
* if (!result.success) void result.error.code
|
|
393
|
+
* ```
|
|
309
394
|
*/
|
|
310
395
|
declare const HaiVecdbError: {
|
|
311
396
|
readonly CONNECTION_FAILED: _h_ai_core.HaiErrorDef;
|
|
@@ -327,15 +412,12 @@ declare const HaiVecdbError: {
|
|
|
327
412
|
* 向量文档接口
|
|
328
413
|
*
|
|
329
414
|
* 表示一条存入向量数据库的记录,包含向量、文本内容和元数据。
|
|
330
|
-
*
|
|
331
415
|
* @example
|
|
332
416
|
* ```ts
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
* metadata: { source: 'wiki', page: 1 },
|
|
338
|
-
* }
|
|
417
|
+
* import type { VectorDocument } from '@h-ai/vecdb'
|
|
418
|
+
*
|
|
419
|
+
* const value: VectorDocument = { id: 'doc_1', vector: [0.1, 0.2, 0.3], content: 'Example text', metadata: { tenant: 'shop' } }
|
|
420
|
+
* void value
|
|
339
421
|
* ```
|
|
340
422
|
*/
|
|
341
423
|
interface VectorDocument {
|
|
@@ -350,20 +432,24 @@ interface VectorDocument {
|
|
|
350
432
|
}
|
|
351
433
|
/**
|
|
352
434
|
* 向量搜索选项
|
|
353
|
-
*
|
|
354
435
|
* @example
|
|
355
436
|
* ```ts
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
* }
|
|
437
|
+
* import type { VectorSearchOptions } from '@h-ai/vecdb'
|
|
438
|
+
*
|
|
439
|
+
* const value: VectorSearchOptions = { topK: 5, filter: { tenant: 'shop' }, minScore: 0.7 }
|
|
440
|
+
* void value
|
|
361
441
|
* ```
|
|
362
442
|
*/
|
|
363
443
|
interface VectorSearchOptions {
|
|
364
444
|
/** 返回的最大结果数(默认 10) */
|
|
365
445
|
topK?: number;
|
|
366
|
-
/**
|
|
446
|
+
/**
|
|
447
|
+
* 元数据过滤,具体能力以 Provider 为准。
|
|
448
|
+
* LanceDB:顶层字面量键的 AND 精确匹配,仅支持 string、有限 number、boolean、null;
|
|
449
|
+
* 类型严格区分,null 不匹配缺失字段;空对象不筛选,不支持对象/数组/undefined。
|
|
450
|
+
* 条件在 topK 前执行,不支持的值返回 CONFIG_ERROR。其它后端不保证相同 null/复杂值语义。
|
|
451
|
+
* @example { tenant: 'acme', active: true, page: 1, removedAt: null }
|
|
452
|
+
*/
|
|
367
453
|
filter?: Record<string, unknown>;
|
|
368
454
|
/** 最低相似度阈值(0-1,低于此值的结果将被过滤) */
|
|
369
455
|
minScore?: number;
|
|
@@ -372,15 +458,12 @@ interface VectorSearchOptions {
|
|
|
372
458
|
* 向量搜索结果
|
|
373
459
|
*
|
|
374
460
|
* 包含匹配的文档和相似度得分。
|
|
375
|
-
*
|
|
376
461
|
* @example
|
|
377
462
|
* ```ts
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
* metadata: { source: 'wiki' },
|
|
383
|
-
* }
|
|
463
|
+
* import type { VectorSearchResult } from '@h-ai/vecdb'
|
|
464
|
+
*
|
|
465
|
+
* const value: VectorSearchResult = { id: 'doc_1', score: 0.95, content: 'Example text', metadata: { tenant: 'shop' } }
|
|
466
|
+
* void value
|
|
384
467
|
* ```
|
|
385
468
|
*/
|
|
386
469
|
interface VectorSearchResult {
|
|
@@ -399,15 +482,12 @@ interface VectorSearchResult {
|
|
|
399
482
|
* 集合信息接口
|
|
400
483
|
*
|
|
401
484
|
* 描述一个向量集合的元信息。
|
|
402
|
-
*
|
|
403
485
|
* @example
|
|
404
486
|
* ```ts
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
*
|
|
408
|
-
*
|
|
409
|
-
* count: 42,
|
|
410
|
-
* }
|
|
487
|
+
* import type { CollectionInfo } from '@h-ai/vecdb'
|
|
488
|
+
*
|
|
489
|
+
* const value: CollectionInfo = { name: 'docs', dimension: 3, metric: 'cosine', count: 1 }
|
|
490
|
+
* void value
|
|
411
491
|
* ```
|
|
412
492
|
*/
|
|
413
493
|
interface CollectionInfo {
|
|
@@ -424,14 +504,15 @@ interface CollectionInfo {
|
|
|
424
504
|
* 集合管理操作接口
|
|
425
505
|
*
|
|
426
506
|
* 通过 `vecdb.collection` 访问,管理向量集合的创建、删除和查询。
|
|
427
|
-
*
|
|
428
507
|
* @example
|
|
429
508
|
* ```ts
|
|
430
|
-
*
|
|
431
|
-
* await vecdb.collection.create('my-docs', { dimension: 1536 })
|
|
509
|
+
* import type { CollectionOperations } from '@h-ai/vecdb'
|
|
432
510
|
*
|
|
433
|
-
*
|
|
434
|
-
*
|
|
511
|
+
* async function read(operations: CollectionOperations) {
|
|
512
|
+
* const result = await operations.list()
|
|
513
|
+
* if (!result.success) return result.error.message
|
|
514
|
+
* return result.data
|
|
515
|
+
* }
|
|
435
516
|
* ```
|
|
436
517
|
*/
|
|
437
518
|
interface CollectionOperations {
|
|
@@ -473,13 +554,12 @@ interface CollectionOperations {
|
|
|
473
554
|
}
|
|
474
555
|
/**
|
|
475
556
|
* 集合创建选项
|
|
476
|
-
*
|
|
477
557
|
* @example
|
|
478
558
|
* ```ts
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
*
|
|
559
|
+
* import type { CollectionCreateOptions } from '@h-ai/vecdb'
|
|
560
|
+
*
|
|
561
|
+
* const value: CollectionCreateOptions = { dimension: 3, metric: 'cosine' }
|
|
562
|
+
* void value
|
|
483
563
|
* ```
|
|
484
564
|
*/
|
|
485
565
|
interface CollectionCreateOptions {
|
|
@@ -492,17 +572,15 @@ interface CollectionCreateOptions {
|
|
|
492
572
|
* 向量操作接口
|
|
493
573
|
*
|
|
494
574
|
* 通过 `vecdb.vector` 访问,管理向量文档的增删改查和搜索。
|
|
495
|
-
*
|
|
496
575
|
* @example
|
|
497
576
|
* ```ts
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
502
|
-
*
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
* const result = await vecdb.vector.search('my-docs', queryVector, { topK: 5 })
|
|
577
|
+
* import type { VectorOperations } from '@h-ai/vecdb'
|
|
578
|
+
*
|
|
579
|
+
* async function read(operations: VectorOperations) {
|
|
580
|
+
* const result = await operations.search('docs', [0.1, 0.2, 0.3], { topK: 5 })
|
|
581
|
+
* if (!result.success) return result.error.message
|
|
582
|
+
* return result.data
|
|
583
|
+
* }
|
|
506
584
|
* ```
|
|
507
585
|
*/
|
|
508
586
|
interface VectorOperations {
|
|
@@ -511,15 +589,19 @@ interface VectorOperations {
|
|
|
511
589
|
*
|
|
512
590
|
* @param collection - 集合名称
|
|
513
591
|
* @param documents - 文档列表
|
|
514
|
-
* @returns 成功返回 ok(undefined)
|
|
592
|
+
* @returns 成功返回 ok(undefined);LanceDB 已有 ID 保留不覆盖,同批重复 ID 返回 CONFIG_ERROR。
|
|
593
|
+
* Qdrant 成功前等待服务端应用写入。
|
|
594
|
+
* @example await vecdb.vector.insert('docs', [{ id: 'doc-1', vector: [1, 0] }])
|
|
515
595
|
*/
|
|
516
596
|
insert: (collection: string, documents: VectorDocument[]) => Promise<HaiResult<void>>;
|
|
517
597
|
/**
|
|
518
|
-
*
|
|
598
|
+
* 插入或更新向量文档(按 id 匹配,整体替换)
|
|
519
599
|
*
|
|
520
600
|
* @param collection - 集合名称
|
|
521
|
-
* @param documents -
|
|
522
|
-
* @returns 成功返回 ok(undefined)
|
|
601
|
+
* @param documents - 待写入文档;已有 ID 更新,不存在的 ID 插入
|
|
602
|
+
* @returns 成功返回 ok(undefined);LanceDB 缺少原子主键能力或同批重复 ID 返回 CONFIG_ERROR。
|
|
603
|
+
* Qdrant 成功前等待服务端应用写入。
|
|
604
|
+
* @example await vecdb.vector.upsert('docs', [{ id: 'doc-1', vector: [1, 0], content: 'updated' }])
|
|
523
605
|
*/
|
|
524
606
|
upsert: (collection: string, documents: VectorDocument[]) => Promise<HaiResult<void>>;
|
|
525
607
|
/**
|
|
@@ -528,6 +610,7 @@ interface VectorOperations {
|
|
|
528
610
|
* @param collection - 集合名称
|
|
529
611
|
* @param ids - 文档 ID 列表
|
|
530
612
|
* @returns 成功返回 ok(undefined)
|
|
613
|
+
* Qdrant 成功前等待服务端应用删除。
|
|
531
614
|
*/
|
|
532
615
|
delete: (collection: string, ids: string[]) => Promise<HaiResult<void>>;
|
|
533
616
|
/**
|
|
@@ -537,6 +620,7 @@ interface VectorOperations {
|
|
|
537
620
|
* @param vector - 查询向量
|
|
538
621
|
* @param options - 搜索选项
|
|
539
622
|
* @returns 搜索结果列表(按相似度降序排列)
|
|
623
|
+
* @example await vecdb.vector.search('docs', [1, 0], { topK: 5, filter: { tenant: 'acme', active: true } })
|
|
540
624
|
*/
|
|
541
625
|
search: (collection: string, vector: number[], options?: VectorSearchOptions) => Promise<HaiResult<VectorSearchResult[]>>;
|
|
542
626
|
/**
|
|
@@ -544,30 +628,26 @@ interface VectorOperations {
|
|
|
544
628
|
*
|
|
545
629
|
* @param collection - 集合名称
|
|
546
630
|
* @returns 文档数量
|
|
631
|
+
* Qdrant 使用精确 count,不依赖集合信息的近似统计。
|
|
547
632
|
*/
|
|
548
633
|
count: (collection: string) => Promise<HaiResult<number>>;
|
|
549
634
|
}
|
|
550
635
|
/**
|
|
551
636
|
* 向量数据库服务接口(通过 `vecdb` 对象访问)
|
|
552
|
-
*
|
|
553
637
|
* @example
|
|
554
638
|
* ```ts
|
|
555
639
|
* import { vecdb } from '@h-ai/vecdb'
|
|
556
640
|
*
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
*
|
|
564
|
-
* await vecdb.
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
* const result = await vecdb.vector.search('docs', queryVector, { topK: 5 })
|
|
568
|
-
*
|
|
569
|
-
* // 关闭
|
|
570
|
-
* await vecdb.close()
|
|
641
|
+
* const initialized = await vecdb.init({ type: 'qdrant', url: 'http://localhost:6333' })
|
|
642
|
+
* if (!initialized.success) throw new Error(initialized.error.message)
|
|
643
|
+
* try {
|
|
644
|
+
* const result = await vecdb.collection.list()
|
|
645
|
+
* if (!result.success) throw new Error(result.error.message)
|
|
646
|
+
* void result.data
|
|
647
|
+
* } finally {
|
|
648
|
+
* const closed = await vecdb.close()
|
|
649
|
+
* if (!closed.success) throw new Error(closed.error.message)
|
|
650
|
+
* }
|
|
571
651
|
* ```
|
|
572
652
|
*/
|
|
573
653
|
interface VecdbFunctions {
|
|
@@ -595,17 +675,24 @@ interface VecdbFunctions {
|
|
|
595
675
|
* 向量库原始连接信息
|
|
596
676
|
*
|
|
597
677
|
* 由 `vecdb.rawConnection` 暴露,字段按后端类型填充。包含凭证,仅供同进程内集成使用。
|
|
678
|
+
* @example
|
|
679
|
+
* ```ts
|
|
680
|
+
* import type { VecdbConnection } from '@h-ai/vecdb'
|
|
681
|
+
*
|
|
682
|
+
* const value: VecdbConnection = { type: 'qdrant', url: 'http://localhost:6333' }
|
|
683
|
+
* void value
|
|
684
|
+
* ```
|
|
598
685
|
*/
|
|
599
686
|
interface VecdbConnection {
|
|
600
687
|
/** 后端类型 */
|
|
601
688
|
type: VecdbConfig['type'];
|
|
602
|
-
/** 服务 URL(qdrant
|
|
689
|
+
/** 服务 URL(qdrant、chroma;Chroma 内嵌模式含实际本地端口) */
|
|
603
690
|
url?: string;
|
|
604
|
-
/** API Key(qdrant) */
|
|
691
|
+
/** API Key / Bearer token(qdrant、chroma) */
|
|
605
692
|
apiKey?: string;
|
|
606
|
-
/** 主机(pgvector) */
|
|
693
|
+
/** 主机(pgvector、chroma) */
|
|
607
694
|
host?: string;
|
|
608
|
-
/** 端口(pgvector) */
|
|
695
|
+
/** 端口(pgvector、chroma) */
|
|
609
696
|
port?: number;
|
|
610
697
|
/** 数据库名(pgvector) */
|
|
611
698
|
database?: string;
|
|
@@ -615,7 +702,7 @@ interface VecdbConnection {
|
|
|
615
702
|
password?: string;
|
|
616
703
|
/** 连接串(pgvector) */
|
|
617
704
|
connectionString?: string;
|
|
618
|
-
/**
|
|
705
|
+
/** 本地持久化路径(lancedb 或内嵌 chroma) */
|
|
619
706
|
path?: string;
|
|
620
707
|
}
|
|
621
708
|
|
|
@@ -636,27 +723,20 @@ interface VecdbConnection {
|
|
|
636
723
|
* - `vecdb.vector` - 向量操作(插入/搜索/删除)
|
|
637
724
|
* - `vecdb.config` - 当前配置
|
|
638
725
|
* - `vecdb.isInitialized` - 初始化状态
|
|
639
|
-
*
|
|
640
726
|
* @example
|
|
641
727
|
* ```ts
|
|
642
728
|
* import { vecdb } from '@h-ai/vecdb'
|
|
643
729
|
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
647
|
-
*
|
|
648
|
-
*
|
|
649
|
-
*
|
|
650
|
-
*
|
|
651
|
-
* await vecdb.
|
|
652
|
-
*
|
|
653
|
-
*
|
|
654
|
-
*
|
|
655
|
-
* // 搜索
|
|
656
|
-
* const result = await vecdb.vector.search('docs', queryVector, { topK: 5 })
|
|
657
|
-
*
|
|
658
|
-
* // 关闭
|
|
659
|
-
* await vecdb.close()
|
|
730
|
+
* const initialized = await vecdb.init({ type: 'qdrant', url: 'http://localhost:6333' })
|
|
731
|
+
* if (!initialized.success) throw new Error(initialized.error.message)
|
|
732
|
+
* try {
|
|
733
|
+
* const result = await vecdb.collection.list()
|
|
734
|
+
* if (!result.success) throw new Error(result.error.message)
|
|
735
|
+
* void result.data
|
|
736
|
+
* } finally {
|
|
737
|
+
* const closed = await vecdb.close()
|
|
738
|
+
* if (!closed.success) throw new Error(closed.error.message)
|
|
739
|
+
* }
|
|
660
740
|
* ```
|
|
661
741
|
*/
|
|
662
742
|
declare const vecdb: VecdbFunctions;
|