Skip to content

Commit

Permalink
misc cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Oct 6, 2023
1 parent 43dd522 commit 7c4a53b
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions langchain/src/vectorstores/mongodb_atlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,15 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
k: number,
filter?: MongoDBAtlasFilter
): Promise<[Document, number][]> {
let preFilter: MongoDBDocument | undefined;
let postFilterPipeline: MongoDBDocument[] | undefined;
let includeEmbeddings: boolean | undefined;
if (
filter?.preFilter ||
filter?.postFilterPipeline ||
filter?.includeEmbeddings
) {
preFilter = filter.preFilter;
postFilterPipeline = filter.postFilterPipeline;
includeEmbeddings = filter.includeEmbeddings || false;
} else preFilter = filter;

const postFilterPipeline = filter?.postFilterPipeline ?? [];
const preFilter: MongoDBDocument | undefined = filter?.preFilter || filter?.postFilterPipeline || filter?.includeEmbeddings
? filter.preFilter
: filter;
const removeEmbeddingsPipeline = !filter?.includeEmbeddings ? [{
$project: {
[this.embeddingKey]: 0,
},
}] : []

const pipeline: MongoDBDocument[] = [
{
Expand All @@ -132,29 +128,16 @@ export class MongoDBAtlasVectorSearch extends VectorStore {
score: { $meta: "vectorSearchScore" },
},
},
...removeEmbeddingsPipeline,
...postFilterPipeline
];

if (!includeEmbeddings) {
const removeEmbeddingsStage = {
$project: {
[this.embeddingKey]: 0,
},
};
pipeline.push(removeEmbeddingsStage);
}

if (postFilterPipeline) {
pipeline.push(...postFilterPipeline);
}
const results = this.collection.aggregate(pipeline);

const ret: [Document, number][] = [];
for await (const result of results) {
const results = this.collection.aggregate(pipeline).map<[Document, number]>((result) => {
const { score, [this.textKey]: text, ...metadata } = result;
ret.push([new Document({ pageContent: text, metadata }), score]);
}
return [new Document({ pageContent: text, metadata }), score];
});

return ret;
return results.toArray();
}

/**
Expand Down

0 comments on commit 7c4a53b

Please sign in to comment.