Skip to content

Commit 4284c09

Browse files
committed
chore: fix style issues and typings
1 parent a098234 commit 4284c09

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

package-lock.json

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
"@typescript-eslint/parser": "^8.44.0",
7676
"@vitest/coverage-v8": "^3.2.4",
7777
"@vitest/eslint-plugin": "^1.3.4",
78-
"ai": "^5.0.72",
7978
"duplexpair": "^1.0.2",
8079
"eslint": "^9.34.0",
8180
"eslint-config-prettier": "^10.1.8",
@@ -103,6 +102,7 @@
103102
"@mongodb-js/devtools-proxy-support": "^0.5.3",
104103
"@mongosh/arg-parser": "^3.19.0",
105104
"@mongosh/service-provider-node-driver": "^3.17.0",
105+
"ai": "^5.0.72",
106106
"bson": "^6.10.4",
107107
"express": "^5.1.0",
108108
"lru-cache": "^11.1.0",

src/common/search/embeddingsProvider.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import assert from "assert";
66
import { createFetch } from "@mongodb-js/devtools-proxy-support";
77
import { z } from "zod";
88

9-
const zEmbeddingsInput = z.string();
10-
type EmbeddingsInput = z.infer<typeof zEmbeddingsInput>;
9+
type EmbeddingsInput = string;
1110
type Embeddings = number[];
1211

1312
type EmbeddingParameters = {
@@ -20,7 +19,7 @@ interface EmbeddingsProvider<SupportedModels extends string> {
2019
embed(modelId: SupportedModels, content: EmbeddingsInput[], parameters: EmbeddingParameters): Promise<Embeddings[]>;
2120
}
2221

23-
export const zVoyageModels = z.enum(["voyage-3-large", "voyage-3.5", "voyage-3.5-lite", "voyage-code-3"]);
22+
const zVoyageModels = z.enum(["voyage-3-large", "voyage-3.5", "voyage-3.5-lite", "voyage-code-3"]);
2423

2524
type VoyageModels = z.infer<typeof zVoyageModels>;
2625
class VoyageEmbeddingsProvider implements EmbeddingsProvider<VoyageModels> {

src/common/search/vectorSearchEmbeddingsManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,15 @@ export class VectorSearchEmbeddingsManager {
273273
return providerEmbeddings;
274274
}
275275

276-
const oneDocument: Document = await provider
276+
const oneDocument: Document = (await provider
277277
.aggregate(database, collection, [{ $sample: { size: 1 } }, { $project: { embeddings: path } }])
278-
.next();
278+
.next()) as Document;
279279

280280
if (!oneDocument) {
281281
return providerEmbeddings;
282282
}
283283

284-
const sampleEmbeddings = oneDocument.embeddings;
284+
const sampleEmbeddings = oneDocument.embeddings as number[] | BSON.Binary;
285285
const adaptedEmbeddings = providerEmbeddings.map((embeddings) => {
286286
// now map based on the sample embeddings
287287
if (Array.isArray(sampleEmbeddings) && Array.isArray(embeddings)) {

src/tools/mongodb/read/aggregate.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { operationWithFallback } from "../../../helpers/operationWithFallback.js
1313
import { AGG_COUNT_MAX_TIME_MS_CAP, ONE_MB, CURSOR_LIMITS_TO_LLM_TEXT } from "../../../helpers/constants.js";
1414
import { zEJSON } from "../../args.js";
1515
import { LogId } from "../../../common/logger.js";
16-
import { SupportedEmbeddingModels, zSupportedEmbeddingModels } from "../../../common/search/embeddingsProvider.js";
16+
import { zSupportedEmbeddingModels } from "../../../common/search/embeddingsProvider.js";
1717

1818
const AnyStage = zEJSON();
1919
const VectorSearchStage = z.object({
@@ -47,9 +47,11 @@ const VectorSearchStage = z.object({
4747
filter: zEJSON()
4848
.optional()
4949
.describe("MQL filter that can only use pre-filter fields from the index definition."),
50-
embeddingModel: zSupportedEmbeddingModels.describe(
51-
"The embedding model to use to generate embeddings before search. Note to LLM: If unsure, ask the user before providing one."
52-
),
50+
embeddingModel: zSupportedEmbeddingModels
51+
.optional()
52+
.describe(
53+
"The embedding model to use to generate embeddings before search. Note to LLM: If unsure, ask the user before providing one."
54+
),
5355
})
5456
.passthrough(),
5557
});
@@ -224,32 +226,36 @@ export class AggregateTool extends MongoDBToolBase {
224226
pipeline: Document[];
225227
}): Promise<Document[]> {
226228
for (const stage of pipeline) {
227-
if (stage.$vectorSearch) {
228-
if ("queryVector" in stage.$vectorSearch && Array.isArray(stage.$vectorSearch.queryVector)) {
229-
// if it's already embeddings, don't do anything
229+
if ("$vectorSearch" in stage) {
230+
const { $vectorSearch: vectorSearchStage } = stage as z.infer<typeof VectorSearchStage>;
231+
232+
if (Array.isArray(vectorSearchStage.queryVector)) {
230233
continue;
231234
}
232235

233-
if (!("embeddingModel" in stage.$vectorSearch)) {
236+
if (!vectorSearchStage.embeddingModel) {
234237
throw new MongoDBError(
235238
ErrorCodes.AtlasVectorSearchInvalidQuery,
236239
"embeddingModel is mandatory if queryVector is a raw string."
237240
);
238241
}
239242

240-
const model = stage.$vectorSearch.embeddingModel as SupportedEmbeddingModels;
241-
delete stage.$vectorSearch.embeddingModel;
243+
const model = vectorSearchStage.embeddingModel;
244+
delete vectorSearchStage.embeddingModel;
242245

243246
const [embeddings] = await this.session.vectorSearchEmbeddingsManager.generateEmbeddings({
244247
database,
245248
collection,
246-
path: stage.$vectorSearch.path,
249+
path: vectorSearchStage.path,
247250
model,
248-
rawValues: stage.$vectorSearch.queryVector,
251+
rawValues: [vectorSearchStage.queryVector],
249252
inputType: "query",
250253
});
251254

252-
stage.$vectorSearch.queryVector = embeddings;
255+
// $vectorSearch.queryVector can be a BSON.Binary: that it's not either number or an array.
256+
// It's not exactly valid from the LLM perspective. That's why we overwrite the
257+
// stage in an untyped way, as what we expose and what we can use are different.
258+
vectorSearchStage.queryVector = embeddings as number[];
253259
}
254260
}
255261

0 commit comments

Comments
 (0)