- Notifications
You must be signed in to change notification settings - Fork 2k
Add MongoDB as a vector store #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Kirbstomper wants to merge 5 commits into spring-projects:main from Kirbstomper:featute/mongoDBVector
Closed
Changes from all commits
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
fb0e894 Add MongoDB as a vector store
Kirbstomper 3154b55 refactor delete method to use Query
Kirbstomper e76143c Remove unused no args constructor in Document.java
Kirbstomper 229864a implement feedback from review comments
Kirbstomper b7b7ffd begin implementing using Atlas vector search
Kirbstomper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| | ||
| # MongoDB VectorStore | ||
| | ||
| This use mongo db as a vector store using the same math as the in-memory vector store. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.springframework.experimental.ai</groupId> | ||
| <artifactId>spring-ai</artifactId> | ||
| <version>0.2.0-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
| <artifactId>spring-ai-mongodb-store</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>Spring AI Vector Store - mongodb</name> | ||
| <description>Spring AI MongoDB Vector Store</description> | ||
| <url>https://github.com/spring-projects-experimental/spring-ai</url> | ||
| | ||
| <scm> | ||
| <url>https://github.com/spring-projects-experimental/spring-ai</url> | ||
| <connection>git://github.com/spring-projects-experimental/spring-ai.git</connection> | ||
| <developerConnection>git@github.com:spring-projects-experimental/spring-ai.git</developerConnection> | ||
| </scm> | ||
| | ||
| <properties> | ||
| <spring-ai.version>0.2.0-SNAPSHOT</spring-ai.version> | ||
| <!-- testing --> | ||
| <testcontainers.version>1.19.0</testcontainers.version> | ||
| </properties> | ||
| | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.springframework.experimental.ai</groupId> | ||
| <artifactId>spring-ai-core</artifactId> | ||
| <version>${spring-ai.version}</version> | ||
| </dependency> | ||
| | ||
| <dependency> | ||
| <groupId>org.springframework.data</groupId> | ||
| <artifactId>spring-data-mongodb</artifactId> | ||
| </dependency> | ||
| | ||
| <dependency> | ||
| <groupId>org.mongodb</groupId> | ||
| <artifactId>mongodb-driver-sync</artifactId> | ||
| </dependency> | ||
| <!-- TESTING --> | ||
| <dependency> | ||
| <groupId>org.springframework.experimental.ai</groupId> | ||
| <artifactId>spring-ai-openai-spring-boot-starter</artifactId> | ||
| <version>${spring-ai.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| | ||
| <dependency> | ||
| <groupId>org.testcontainers</groupId> | ||
| <artifactId>testcontainers</artifactId> | ||
| <version>${testcontainers.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| | ||
| <dependency> | ||
| <groupId>org.testcontainers</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <version>${testcontainers.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| | ||
| </project> |
128 changes: 128 additions & 0 deletions 128 ...ai-mongodb-store/src/main/java/org/springframework/ai/vectorstore/MongoDBVectorStore.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| * Copyright 2023-2023 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| | ||
| package org.springframework.ai.vectorstore; | ||
| | ||
| import com.mongodb.BasicDBObject; | ||
| import org.springframework.ai.document.Document; | ||
| import org.springframework.ai.embedding.EmbeddingClient; | ||
| import org.springframework.ai.vectorstore.impl.InMemoryVectorStore; | ||
| import org.springframework.data.domain.Sort; | ||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||
| import org.springframework.data.mongodb.core.aggregation.Aggregation; | ||
| import org.springframework.data.mongodb.core.query.Query; | ||
| | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| | ||
| import static org.springframework.data.mongodb.core.query.Criteria.where; | ||
| | ||
| /** | ||
| * @author Chris Smith | ||
| */ | ||
| public class MongoDBVectorStore implements VectorStore { | ||
| | ||
| private MongoTemplate mongoTemplate; | ||
| | ||
| private EmbeddingClient embeddingClient; | ||
| | ||
| private static final String VECTOR_COLLECTION_NAME = "SampleCollection"; | ||
| | ||
| public MongoDBVectorStore(MongoTemplate mongoTemplate, EmbeddingClient embeddingClient) { | ||
| this.mongoTemplate = mongoTemplate; | ||
| this.embeddingClient = embeddingClient; | ||
| if (!mongoTemplate.collectionExists(VECTOR_COLLECTION_NAME)) { | ||
| mongoTemplate.createCollection(VECTOR_COLLECTION_NAME); | ||
| } | ||
| } | ||
| | ||
| /** | ||
| * Maps a basicDBObject to a Spring AI Document | ||
| * @param basicDBObject | ||
| * @return | ||
| */ | ||
| private Document mapBasicDbObject(BasicDBObject basicDBObject) { | ||
| String id = basicDBObject.getString("_id"); | ||
| String content = basicDBObject.getString("text"); | ||
| Map<String, Object> metadata = (Map<String, Object>) basicDBObject.get("metadata"); | ||
| List<Double> embedding = (List<Double>) basicDBObject.get("embedding"); | ||
| | ||
| Document document = new Document(id, content, metadata); | ||
| document.setEmbedding(embedding); | ||
| | ||
| return document; | ||
| } | ||
| | ||
| @Override | ||
| public void add(List<Document> documents) { | ||
| for (Document document : documents) { | ||
| List<Double> embedding = this.embeddingClient.embed(document); | ||
| document.setEmbedding(embedding); | ||
| | ||
| this.mongoTemplate.save(document, VECTOR_COLLECTION_NAME); | ||
| } | ||
| } | ||
| | ||
| @Override | ||
| public Optional<Boolean> delete(List<String> idList) { | ||
| Query query = new Query(where("_id").in(idList)); | ||
| | ||
| var deleteRes = this.mongoTemplate.remove(query, VECTOR_COLLECTION_NAME); | ||
| long deleteCount = deleteRes.getDeletedCount(); | ||
| | ||
| return Optional.of(deleteCount == idList.size()); | ||
| } | ||
| | ||
| @Override | ||
| public List<Document> similaritySearch(String query) { | ||
| return this.similaritySearch(query, 4); | ||
| } | ||
| | ||
| @Override | ||
| public List<Document> similaritySearch(String query, int k) { | ||
| List<Double> queryEmbedding = this.embeddingClient.embed(query); | ||
| | ||
| //Build aggregation to leverage searching through mongodb | ||
| Aggregation aggregation = Aggregation.newAggregation( | ||
| new VectorSearchAggregation(k, queryEmbedding), Aggregation.sort(Sort.Direction.DESC, "score")); | ||
| return this.mongoTemplate.aggregate(aggregation, VECTOR_COLLECTION_NAME,BasicDBObject.class) | ||
| .getMappedResults().stream() | ||
| .map(this::mapBasicDbObject) | ||
| .toList(); | ||
| } | ||
| | ||
| @Override | ||
| public List<Document> similaritySearch(String query, int k, double threshold) { | ||
| List<Double> queryEmbedding = this.embeddingClient.embed(query); | ||
| | ||
| return this.mongoTemplate.findAll(BasicDBObject.class, VECTOR_COLLECTION_NAME) | ||
| .stream() | ||
| .map(this::mapBasicDbObject) | ||
| .map(entry -> new InMemoryVectorStore.Similarity(entry.getId(), | ||
| InMemoryVectorStore.EmbeddingMath.cosineSimilarity(queryEmbedding, entry.getEmbedding()))) | ||
| .filter(s -> s.getSimilarity() >= threshold) | ||
| .sorted(Comparator.<InMemoryVectorStore.Similarity>comparingDouble(s -> s.getSimilarity()).reversed()) | ||
| .limit(k) | ||
| .map(s -> this.mongoTemplate.findById(s.getKey(), BasicDBObject.class, VECTOR_COLLECTION_NAME)) | ||
| .map(this::mapBasicDbObject) | ||
| .toList(); | ||
| } | ||
| | ||
| | ||
| | ||
| } |
30 changes: 30 additions & 0 deletions 30 ...ngodb-store/src/main/java/org/springframework/ai/vectorstore/VectorSearchAggregation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package org.springframework.ai.vectorstore; | ||
| | ||
| import org.bson.Document; | ||
| import org.springframework.data.mongodb.core.aggregation.AggregationOperation; | ||
| import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext; | ||
| | ||
| import java.util.List; | ||
| | ||
| class VectorSearchAggregation implements AggregationOperation { | ||
| | ||
| private final int count; | ||
| private final List<Double> embeddings; | ||
| | ||
| | ||
| | ||
| public VectorSearchAggregation(int count, List<Double> embeddings){ | ||
| this.count = count; | ||
| this.embeddings = embeddings; | ||
| } | ||
| @Override | ||
| public org.bson.Document toDocument(AggregationOperationContext context) { | ||
| var doc = new Document("$search", | ||
| new Document("index", "default") | ||
| .append("path", "embedding") | ||
| .append("numCandidates", 100) | ||
| .append("limit",count) | ||
| .append("queryVector", embeddings)); | ||
| return context.getMappedObject(doc); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure those methods in the InMemoryVectorStore are relevant to the MongoDB store?