@yh-ui/ai-sdk 0.1.21

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.
@@ -0,0 +1,74 @@
1
+ /**
2
+ * YH-UI AI SDK - 向量存储抽象层
3
+ *
4
+ * 支持多种后端,便于对接 Pinecone / Weaviate / Qdrant 等生产级向量库。
5
+ * 内置内存实现,可选接入外部向量服务。
6
+ */
7
+ export interface VectorDocument {
8
+ id: string;
9
+ content: string;
10
+ embedding: number[];
11
+ metadata?: Record<string, unknown>;
12
+ }
13
+ export interface VectorSearchResult {
14
+ id: string;
15
+ content: string;
16
+ metadata?: Record<string, unknown>;
17
+ score: number;
18
+ embedding?: number[];
19
+ }
20
+ export interface VectorStoreConfig {
21
+ /** 命名空间/索引名 */
22
+ namespace?: string;
23
+ /** 向量维度 (部分后端需要) */
24
+ dimension?: number;
25
+ }
26
+ /**
27
+ * 向量存储统一接口
28
+ * 实现此接口即可接入 Pinecone / Weaviate / Qdrant 等
29
+ */
30
+ export interface IVectorStore {
31
+ /** 添加文档(含向量) */
32
+ add(documents: VectorDocument[], namespace?: string): Promise<void>;
33
+ /** 相似度检索 */
34
+ search(queryEmbedding: number[], options: {
35
+ topK?: number;
36
+ threshold?: number;
37
+ namespace?: string;
38
+ filter?: Record<string, unknown>;
39
+ }): Promise<VectorSearchResult[]>;
40
+ /** 按 ID 删除 */
41
+ delete(ids: string[], namespace?: string): Promise<void>;
42
+ /** 清空命名空间 */
43
+ clear(namespace?: string): Promise<void>;
44
+ }
45
+ /**
46
+ * 内存向量存储(内置实现,适用于开发与小规模)
47
+ * 使用余弦相似度检索
48
+ */
49
+ export declare function createInMemoryVectorStore(_config?: VectorStoreConfig): IVectorStore;
50
+ /**
51
+ * Pinecone 适配器接口(由用户或可选包实现)
52
+ * 安装 @pinecone-database/pinecone 后可实现
53
+ */
54
+ export type PineconeVectorStoreConfig = VectorStoreConfig & {
55
+ apiKey: string;
56
+ indexName: string;
57
+ environment?: string;
58
+ };
59
+ /**
60
+ * Weaviate 适配器接口
61
+ */
62
+ export type WeaviateVectorStoreConfig = VectorStoreConfig & {
63
+ url: string;
64
+ apiKey?: string;
65
+ className?: string;
66
+ };
67
+ /**
68
+ * Qdrant 适配器接口
69
+ */
70
+ export type QdrantVectorStoreConfig = VectorStoreConfig & {
71
+ url: string;
72
+ apiKey?: string;
73
+ collectionName?: string;
74
+ };
@@ -0,0 +1,55 @@
1
+ export function createInMemoryVectorStore(_config) {
2
+ const store = /* @__PURE__ */ new Map();
3
+ function cosineSimilarity(a, b) {
4
+ if (a.length !== b.length) return 0;
5
+ let dot = 0;
6
+ let normA = 0;
7
+ let normB = 0;
8
+ for (let i = 0; i < a.length; i++) {
9
+ dot += a[i] * b[i];
10
+ normA += a[i] * a[i];
11
+ normB += b[i] * b[i];
12
+ }
13
+ const denom = Math.sqrt(normA) * Math.sqrt(normB);
14
+ return denom === 0 ? 0 : dot / denom;
15
+ }
16
+ return {
17
+ async add(documents, namespace = "default") {
18
+ const list = store.get(namespace) || [];
19
+ for (const doc of documents) {
20
+ const idx = list.findIndex((d) => d.id === doc.id);
21
+ if (idx >= 0) list[idx] = doc;
22
+ else list.push(doc);
23
+ }
24
+ store.set(namespace, list);
25
+ },
26
+ async search(queryEmbedding, options = {}) {
27
+ const topK = options.topK ?? 10;
28
+ const threshold = options.threshold ?? 0;
29
+ const namespaceOpt = options.namespace ?? "default";
30
+ const list = store.get(namespaceOpt) || [];
31
+ const scored = list.map((doc) => ({
32
+ ...doc,
33
+ score: cosineSimilarity(doc.embedding, queryEmbedding)
34
+ })).filter((d) => d.score >= threshold).sort((a, b) => b.score - a.score).slice(0, topK);
35
+ return scored.map(({ id, content, metadata, score, embedding }) => ({
36
+ id,
37
+ content,
38
+ metadata,
39
+ score,
40
+ embedding
41
+ }));
42
+ },
43
+ async delete(ids, namespace = "default") {
44
+ const list = store.get(namespace) || [];
45
+ const set = new Set(ids);
46
+ store.set(
47
+ namespace,
48
+ list.filter((d) => !set.has(d.id))
49
+ );
50
+ },
51
+ async clear(namespace = "default") {
52
+ store.set(namespace, []);
53
+ }
54
+ };
55
+ }