-
Couldn't load subscription status.
- Fork 2k
Add MongoDB Atlas vector store auto-configuration #460
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
49 changes: 49 additions & 0 deletions
49
...ramework/ai/autoconfigure/vectorstore/mongo/MongoDBAtlasVectorStoreAutoConfiguration.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,49 @@ | ||
| /* | ||
| * Copyright 2023 - 2024 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.autoconfigure.vectorstore.mongo; | ||
|
|
||
| import org.springframework.ai.embedding.EmbeddingClient; | ||
| import org.springframework.ai.vectorstore.MongoDBAtlasVectorStore; | ||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
| import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.data.mongodb.core.MongoTemplate; | ||
|
|
||
| /** | ||
| * @author Eddú Meléndez | ||
| */ | ||
| @AutoConfiguration(after = MongoDataAutoConfiguration.class) | ||
| @ConditionalOnClass({ MongoDBAtlasVectorStore.class, EmbeddingClient.class, MongoTemplate.class }) | ||
| @EnableConfigurationProperties(MongoDBAtlasVectorStoreProperties.class) | ||
| public class MongoDBAtlasVectorStoreAutoConfiguration { | ||
|
|
||
| @Bean | ||
| @ConditionalOnMissingBean | ||
| MongoDBAtlasVectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingClient embeddingClient, | ||
| MongoDBAtlasVectorStoreProperties properties) { | ||
| MongoDBAtlasVectorStore.MongoDBVectorStoreConfig config = MongoDBAtlasVectorStore.MongoDBVectorStoreConfig | ||
| .builder() | ||
| .withCollectionName(properties.getCollectionName()) | ||
| .withPathName(properties.getPathName()) | ||
| .withVectorIndexName(properties.getIndexName()) | ||
| .build(); | ||
| return new MongoDBAtlasVectorStore(mongoTemplate, embeddingClient, config); | ||
| } | ||
|
|
||
| } |
56 changes: 56 additions & 0 deletions
56
...springframework/ai/autoconfigure/vectorstore/mongo/MongoDBAtlasVectorStoreProperties.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,56 @@ | ||
| /* | ||
| * Copyright 2023 - 2024 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.autoconfigure.vectorstore.mongo; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| /** | ||
| * @author Eddú Meléndez | ||
| */ | ||
| @ConfigurationProperties("spring.ai.vectorstore.mongodb") | ||
| public class MongoDBAtlasVectorStoreProperties { | ||
|
|
||
| private String collectionName; | ||
|
|
||
| private String pathName; | ||
|
|
||
| private String indexName; | ||
|
|
||
| public String getCollectionName() { | ||
| return this.collectionName; | ||
| } | ||
|
|
||
| public void setCollectionName(String collectionName) { | ||
| this.collectionName = collectionName; | ||
| } | ||
|
|
||
| public String getPathName() { | ||
| return this.pathName; | ||
| } | ||
|
|
||
| public void setPathName(String pathName) { | ||
| this.pathName = pathName; | ||
| } | ||
|
|
||
| public String getIndexName() { | ||
| return this.indexName; | ||
| } | ||
|
|
||
| public void setIndexName(String indexName) { | ||
| this.indexName = indexName; | ||
| } | ||
|
|
||
| } |
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
102 changes: 102 additions & 0 deletions
102
...mework/ai/autoconfigure/vectorstore/mongo/MongoDBAtlasVectorStoreAutoConfigurationIT.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,102 @@ | ||
| /* | ||
| * Copyright 2023 - 2024 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.autoconfigure.vectorstore.mongo; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration; | ||
| import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration; | ||
| import org.springframework.ai.document.Document; | ||
| import org.springframework.ai.vectorstore.SearchRequest; | ||
| import org.springframework.ai.vectorstore.VectorStore; | ||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
| import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration; | ||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.containers.wait.strategy.Wait; | ||
| import org.testcontainers.junit.jupiter.Container; | ||
| import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * @author Eddú Meléndez | ||
| */ | ||
| @Testcontainers | ||
| class MongoDBAtlasVectorStoreAutoConfigurationIT { | ||
|
|
||
| @Container | ||
| static GenericContainer<?> mongo = new GenericContainer<>("mongodb/atlas:v1.15.1").withPrivilegedMode(true) | ||
| .withCommand("/bin/bash", "-c", | ||
| "atlas deployments setup local-test --type local --port 27778 --bindIpAll --username root --password root --force && tail -f /dev/null") | ||
| .withExposedPorts(27778) | ||
| .waitingFor(Wait.forLogMessage(".*Deployment created!.*\\n", 1)) | ||
| .withStartupTimeout(Duration.ofMinutes(5)); | ||
|
|
||
| List<Document> documents = List.of( | ||
| new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", | ||
| Collections.singletonMap("meta1", "meta1")), | ||
| new Document("Hello World Hello World Hello World Hello World Hello World Hello World Hello World"), | ||
| new Document( | ||
| "Great Depression Great Depression Great Depression Great Depression Great Depression Great Depression", | ||
| Collections.singletonMap("meta2", "meta2"))); | ||
|
|
||
| private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
| .withConfiguration(AutoConfigurations.of(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, | ||
| MongoDBAtlasVectorStoreAutoConfiguration.class, RestClientAutoConfiguration.class, | ||
| SpringAiRetryAutoConfiguration.class, OpenAiAutoConfiguration.class)) | ||
| .withPropertyValues("spring.data.mongodb.database=springaisample", | ||
| "spring.ai.vectorstore.mongodb.collection-name=test_collection", | ||
| "spring.ai.vectorstore.mongodb.path-name=testembedding", | ||
| "spring.ai.vectorstore.mongodb.index-name=text_index", | ||
| "spring.ai.openai.api-key=" + System.getenv("OPENAI_API_KEY"), | ||
| String.format( | ||
| "spring.data.mongodb.uri=" + String.format("mongodb://root:root@%s:%s/?directConnection=true", | ||
| mongo.getHost(), mongo.getMappedPort(27778)))); | ||
|
|
||
| @Test | ||
| public void addAndSearch() { | ||
| contextRunner.run(context -> { | ||
|
|
||
| VectorStore vectorStore = context.getBean(VectorStore.class); | ||
|
|
||
| vectorStore.add(documents); | ||
| Thread.sleep(5000); // Await a second for the document to be indexed | ||
|
|
||
| List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Great").withTopK(1)); | ||
|
|
||
| assertThat(results).hasSize(1); | ||
| Document resultDoc = results.get(0); | ||
| assertThat(resultDoc.getId()).isEqualTo(documents.get(2).getId()); | ||
| assertThat(resultDoc.getContent()).isEqualTo( | ||
| "Great Depression Great Depression Great Depression Great Depression Great Depression Great Depression"); | ||
| assertThat(resultDoc.getMetadata()).containsEntry("meta2", "meta2"); | ||
|
|
||
| // Remove all documents from the store | ||
| vectorStore.delete(documents.stream().map(Document::getId).collect(Collectors.toList())); | ||
|
|
||
| List<Document> results2 = vectorStore.similaritySearch(SearchRequest.query("Great").withTopK(1)); | ||
| assertThat(results2).isEmpty(); | ||
| }); | ||
| } | ||
|
|
||
| } | ||
42 changes: 42 additions & 0 deletions
42
spring-ai-spring-boot-starters/spring-ai-starter-mongodb-atlas-store/pom.xml
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,42 @@ | ||
| <?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.ai</groupId> | ||
| <artifactId>spring-ai</artifactId> | ||
| <version>1.0.0-SNAPSHOT</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
| <artifactId>spring-ai-mongodb-atlas-store-spring-boot-starter</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>Spring AI Starter - MongoDB Atlas Store</name> | ||
| <description>Spring AI MongoDB Atlas Store Auto Configuration</description> | ||
| <url>https://github.com/spring-projects/spring-ai</url> | ||
|
|
||
| <scm> | ||
| <url>https://github.com/spring-projects/spring-ai</url> | ||
| <connection>git://github.com/spring-projects/spring-ai.git</connection> | ||
| <developerConnection>git@github.com:spring-projects/spring-ai.git</developerConnection> | ||
| </scm> | ||
|
|
||
| <dependencies> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.ai</groupId> | ||
| <artifactId>spring-ai-spring-boot-autoconfigure</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework.ai</groupId> | ||
| <artifactId>spring-ai-mongodb-atlas-store</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
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
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.
Uh oh!
There was an error while loading. Please reload this page.