Skip to content

Commit

Permalink
Add builder pattern for MilvusVectorStore
Browse files Browse the repository at this point in the history
Introduces a fluent builder API to improve configuration readability and
type safety when creating MilvusVectorStore instances. This replaces the
existing configuration object approach which was less intuitive and harder
to maintain.

The builder pattern provides better encapsulation of configuration logic
and validation, while maintaining backward compatibility through a
deprecated config class. This change makes the codebase more maintainable
and the API more discoverable for users.

Key changes:
- Replace configuration object with fluent builder pattern
- Move Milvus-related classes to dedicated milvus package
- Deprecate MilvusVectorStoreConfig in favor of builder
- Update constructor to use builder internally
- Maintain backward compatibility with deprecated config
- Add comprehensive builder methods with validation
  • Loading branch information
sobychacko authored and Mark Pollack committed Dec 12, 2024
1 parent 5b11501 commit 218c967
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.springframework.ai.embedding.BatchingStrategy;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.MilvusVectorStore;
import org.springframework.ai.vectorstore.MilvusVectorStore.MilvusVectorStoreConfig;
import org.springframework.ai.milvus.vectorstore.MilvusVectorStore;
import org.springframework.ai.milvus.vectorstore.MilvusVectorStore.MilvusVectorStoreConfig;
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
Expand Down Expand Up @@ -71,23 +71,14 @@ public MilvusVectorStore vectorStore(MilvusServiceClient milvusClient, Embedding
ObjectProvider<ObservationRegistry> observationRegistry,
ObjectProvider<VectorStoreObservationConvention> customObservationConvention) {

MilvusVectorStoreConfig config = MilvusVectorStoreConfig.builder()
.withCollectionName(properties.getCollectionName())
.withDatabaseName(properties.getDatabaseName())
.withIndexType(IndexType.valueOf(properties.getIndexType().name()))
.withMetricType(MetricType.valueOf(properties.getMetricType().name()))
.withIndexParameters(properties.getIndexParameters())
.withEmbeddingDimension(properties.getEmbeddingDimension())
.withIDFieldName(properties.getIdFieldName())
.withAutoId(properties.isAutoId())
.withContentFieldName(properties.getContentFieldName())
.withMetadataFieldName(properties.getMetadataFieldName())
.withEmbeddingFieldName(properties.getEmbeddingFieldName())
return MilvusVectorStore.builder()
.milvusClient(milvusClient)
.embeddingModel(embeddingModel)
.initializeSchema(properties.isInitializeSchema())
.batchingStrategy(batchingStrategy)
.observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
.customObservationConvention(customObservationConvention.getIfAvailable(() -> null))
.build();

return new MilvusVectorStore(milvusClient, embeddingModel, config, properties.isInitializeSchema(),
batchingStrategy, observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP),
customObservationConvention.getIfAvailable(() -> null));
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.springframework.ai.autoconfigure.vectorstore.milvus;

import org.springframework.ai.autoconfigure.vectorstore.CommonVectorStoreProperties;
import org.springframework.ai.vectorstore.MilvusVectorStore;
import org.springframework.ai.milvus.vectorstore.MilvusVectorStore;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.ai.vectorstore;
package org.springframework.ai.milvus.vectorstore;

import org.springframework.ai.vectorstore.filter.Filter.Expression;
import org.springframework.ai.vectorstore.filter.Filter.ExpressionType;
Expand Down
Loading

0 comments on commit 218c967

Please sign in to comment.