|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import type { MockedFunction } from "vitest"; |
| 3 | +import { VectorSearchEmbeddings } from "../../../../src/common/search/vectorSearchEmbeddings.js"; |
| 4 | +import type { EmbeddingNamespace } from "../../../../src/common/search/vectorSearchEmbeddings.js"; |
| 5 | +import { BSON } from "bson"; |
| 6 | +import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver"; |
| 7 | + |
| 8 | +type MockedServiceProvider = NodeDriverServiceProvider & { |
| 9 | + getSearchIndexes: MockedFunction<NodeDriverServiceProvider["getSearchIndexes"]>; |
| 10 | +}; |
| 11 | + |
| 12 | +describe("VectorSearchEmbeddings", () => { |
| 13 | + const database = "my" as const; |
| 14 | + const collection = "collection" as const; |
| 15 | + const mapKey = `${database}.${collection}` as EmbeddingNamespace; |
| 16 | + |
| 17 | + const provider: MockedServiceProvider = { |
| 18 | + getSearchIndexes: vi.fn(), |
| 19 | + } as unknown as MockedServiceProvider; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + provider.getSearchIndexes.mockReset(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe("embedding retrieval", () => { |
| 26 | + describe("when the embeddings have not been cached", () => { |
| 27 | + beforeEach(() => { |
| 28 | + provider.getSearchIndexes.mockImplementation(() => { |
| 29 | + return Promise.resolve([ |
| 30 | + { |
| 31 | + id: "65e8c766d0450e3e7ab9855f", |
| 32 | + name: "search-test", |
| 33 | + type: "search", |
| 34 | + status: "READY", |
| 35 | + queryable: true, |
| 36 | + latestDefinition: { dynamic: true }, |
| 37 | + }, |
| 38 | + { |
| 39 | + id: "65e8c766d0450e3e7ab9855f", |
| 40 | + name: "vector-search-test", |
| 41 | + type: "vectorSearch", |
| 42 | + status: "READY", |
| 43 | + queryable: true, |
| 44 | + latestDefinition: { |
| 45 | + fields: [ |
| 46 | + { |
| 47 | + type: "vector", |
| 48 | + path: "plot_embedding", |
| 49 | + numDimensions: 1536, |
| 50 | + similarity: "euclidean", |
| 51 | + }, |
| 52 | + { type: "filter", path: "genres" }, |
| 53 | + { type: "filter", path: "year" }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + }, |
| 57 | + ]); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it("retrieves the list of vector search indexes for that collection from the cluster", async () => { |
| 62 | + const embeddings = new VectorSearchEmbeddings(); |
| 63 | + const result = await embeddings.embeddingsForNamespace({ database, collection, provider }); |
| 64 | + |
| 65 | + expect(result).toContainEqual({ |
| 66 | + type: "vector", |
| 67 | + path: "plot_embedding", |
| 68 | + numDimensions: 1536, |
| 69 | + similarity: "euclidean", |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("ignores any other type of index", async () => { |
| 74 | + const embeddings = new VectorSearchEmbeddings(); |
| 75 | + const result = await embeddings.embeddingsForNamespace({ database, collection, provider }); |
| 76 | + |
| 77 | + expect(result?.filter((emb) => emb.type !== "vector")).toHaveLength(0); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe("embedding validation", () => { |
| 83 | + it("when there are no embeddings, all documents are valid", async () => { |
| 84 | + const embeddings = new VectorSearchEmbeddings(new Map([[mapKey, []]])); |
| 85 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 86 | + { database, collection, provider }, |
| 87 | + { field: "yay" } |
| 88 | + ); |
| 89 | + |
| 90 | + expect(result).toHaveLength(0); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("when there are embeddings", () => { |
| 94 | + const embeddings = new VectorSearchEmbeddings( |
| 95 | + new Map([ |
| 96 | + [ |
| 97 | + mapKey, |
| 98 | + [ |
| 99 | + { |
| 100 | + type: "vector", |
| 101 | + path: "embedding_field", |
| 102 | + numDimensions: 8, |
| 103 | + quantization: "none", |
| 104 | + similarity: "euclidean", |
| 105 | + }, |
| 106 | + { |
| 107 | + type: "vector", |
| 108 | + path: "embedding_field_binary", |
| 109 | + numDimensions: 8, |
| 110 | + quantization: "binary", |
| 111 | + similarity: "euclidean", |
| 112 | + }, |
| 113 | + { |
| 114 | + type: "vector", |
| 115 | + path: "a.nasty.scalar.field", |
| 116 | + numDimensions: 8, |
| 117 | + quantization: "none", |
| 118 | + similarity: "euclidean", |
| 119 | + }, |
| 120 | + { |
| 121 | + type: "vector", |
| 122 | + path: "a.nasty.binary.field", |
| 123 | + numDimensions: 8, |
| 124 | + quantization: "binary", |
| 125 | + similarity: "euclidean", |
| 126 | + }, |
| 127 | + ], |
| 128 | + ], |
| 129 | + ]) |
| 130 | + ); |
| 131 | + |
| 132 | + it("documents not inserting the field with embeddings are valid", async () => { |
| 133 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 134 | + { database, collection, provider }, |
| 135 | + { field: "yay" } |
| 136 | + ); |
| 137 | + |
| 138 | + expect(result).toHaveLength(0); |
| 139 | + }); |
| 140 | + |
| 141 | + it("documents inserting the field with wrong type are invalid", async () => { |
| 142 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 143 | + { database, collection, provider }, |
| 144 | + { embedding_field: "some text" } |
| 145 | + ); |
| 146 | + |
| 147 | + expect(result).toHaveLength(1); |
| 148 | + }); |
| 149 | + |
| 150 | + it("documents inserting the field with wrong dimensions are invalid", async () => { |
| 151 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 152 | + { database, collection, provider }, |
| 153 | + { embedding_field: [1, 2, 3] } |
| 154 | + ); |
| 155 | + |
| 156 | + expect(result).toHaveLength(1); |
| 157 | + }); |
| 158 | + |
| 159 | + it("documents inserting the field with correct dimensions, but wrong type are invalid", async () => { |
| 160 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 161 | + { database, collection, provider }, |
| 162 | + { embedding_field: ["1", "2", "3", "4", "5", "6", "7", "8"] } |
| 163 | + ); |
| 164 | + |
| 165 | + expect(result).toHaveLength(1); |
| 166 | + }); |
| 167 | + |
| 168 | + it("documents inserting the field with correct dimensions, but wrong quantization are invalid", async () => { |
| 169 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 170 | + { database, collection, provider }, |
| 171 | + { embedding_field_binary: [1, 2, 3, 4, 5, 6, 7, 8] } |
| 172 | + ); |
| 173 | + |
| 174 | + expect(result).toHaveLength(1); |
| 175 | + }); |
| 176 | + |
| 177 | + it("documents inserting the field with correct dimensions and quantization in binary are valid", async () => { |
| 178 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 179 | + { database, collection, provider }, |
| 180 | + { embedding_field_binary: BSON.Binary.fromBits([0, 0, 0, 0, 0, 0, 0, 0]) } |
| 181 | + ); |
| 182 | + |
| 183 | + expect(result).toHaveLength(0); |
| 184 | + }); |
| 185 | + |
| 186 | + it("documents inserting the field with correct dimensions and quantization in scalar/none are valid", async () => { |
| 187 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 188 | + { database, collection, provider }, |
| 189 | + { embedding_field: [1, 2, 3, 4, 5, 6, 7, 8] } |
| 190 | + ); |
| 191 | + |
| 192 | + expect(result).toHaveLength(0); |
| 193 | + }); |
| 194 | + |
| 195 | + it("documents inserting the field with correct dimensions and quantization in scalar/none are valid also on nested fields", async () => { |
| 196 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 197 | + { database, collection, provider }, |
| 198 | + { a: { nasty: { scalar: { field: [1, 2, 3, 4, 5, 6, 7, 8] } } } } |
| 199 | + ); |
| 200 | + |
| 201 | + expect(result).toHaveLength(0); |
| 202 | + }); |
| 203 | + |
| 204 | + it("documents inserting the field with correct dimensions and quantization in binary are valid also on nested fields", async () => { |
| 205 | + const result = await embeddings.findFieldsWithWrongEmbeddings( |
| 206 | + { database, collection, provider }, |
| 207 | + { a: { nasty: { binary: { field: BSON.Binary.fromBits([0, 0, 0, 0, 0, 0, 0, 0]) } } } } |
| 208 | + ); |
| 209 | + |
| 210 | + expect(result).toHaveLength(0); |
| 211 | + }); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments