Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ For more complex scenarios, the builder pattern offers extensive configuration o
----
@Bean
public VectorStore vectorStore(CqlSession session, EmbeddingModel embeddingModel) {
return CassandraVectorStore.builder()
.session(session)
.embeddingModel(embeddingModel)
return CassandraVectorStore.builder(embeddingModel)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't CqlSession a required parameter that should be in the builder signature?

.keyspace("my_keyspace")
.table("my_vectors")
.partitionKeys(List.of(new SchemaColumn("id", DataTypes.TEXT)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ public class DemoApplication implements CommandLineRunner {

@Bean
public VectorStore vectorStore(ObservationRegistry observationRegistry) {
CosmosDBVectorStoreConfig config = new CosmosDBVectorStoreConfig();
config.setDatabaseName("spring-ai-sample");
config.setContainerName("container");
config.setMetadataFields("country,city");
config.setVectorStoreThroughput(400);

CosmosAsyncClient cosmosClient = new CosmosClientBuilder()
.endpoint(System.getenv("COSMOSDB_AI_ENDPOINT"))
Expand All @@ -216,7 +211,13 @@ public class DemoApplication implements CommandLineRunner {
.gatewayMode()
.buildAsyncClient();

return new CosmosDBVectorStore(observationRegistry, null, cosmosClient, config, this.embeddingModel);
return CosmosDBVectorStore.builder(cosmosClient, this.embeddingModel)
.databaseName("test-database")
.containerName("test-container")
.metadataFields(List.of("country", "year", "city"))
.vectorStoreThroughput(1000)
.observationRegistry(observationRegistry)
.build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ To create a vector store, you can use the following code by injecting the `Searc
----
@Bean
public VectorStore vectorStore(SearchIndexClient searchIndexClient, EmbeddingModel embeddingModel) {
return new AzureVectorStore(searchIndexClient, embeddingModel,

return AzureVectorStore.builder(searchIndexClient, embeddingModel)
.initializeSchema(true)
// Define the metadata fields to be used
// in the similarity search filters.
List.of(MetadataField.text("country"),
MetadataField.int64("year"),
MetadataField.bool("active")));
.filterMetadataFields(List.of(MetadataField.text("country"), MetadataField.int64("year"),
MetadataField.date("activationDate")))
.build();
}
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ Integrate with OpenAI's embeddings by adding the Spring Boot OpenAI starter to y
----
@Bean
public VectorStore chromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi) {
return new ChromaVectorStore(embeddingModel, chromaApi, "TestCollection", false);
return ChromaVectorStore.builder(chromaApi, embeddingModel)
.collectionName("TestCollection")
.initializeSchema(true)
.build();
}
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,7 @@ public VectorStore vectorStore(RestClient restClient, EmbeddingModel embeddingMo
options.setSimilarity(COSINE); // Optional: defaults to COSINE
options.setDimensions(1536); // Optional: defaults to model dimensions or 1536

return ElasticsearchVectorStore.builder()
.restClient(restClient)
.embeddingModel(embeddingModel)
return ElasticsearchVectorStore.builder(restClient, embeddingModel)
.options(options) // Optional: use custom options
.initializeSchema(true) // Optional: defaults to false
.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ Here is a sample that creates an instance of the `GemfireVectorStore` instead of
[source,java]
----
@Bean
public VectorStore vectorStore(EmbeddingModel embeddingModel) {
return new GemFireVectorStore(new GemFireVectorStoreConfig()
.setIndexName("my-vector-index")
.setPort(7071), embeddingClient);
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
return GemFireVectorStore.builder(embeddingModel)
.host("localhost")
.port(7071)
.indexName("my-vector-index")
.initializeSchema(true)
.build();
}
----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ Then create the `MariaDBVectorStore` bean using the builder pattern:
----
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return MariaDBVectorStore.builder(jdbcTemplate)
.embeddingModel(embeddingModel)
return MariaDBVectorStore.builder(jdbcTemplate, embeddingModel)
.dimensions(1536) // Optional: defaults to 1536
.distanceType(MariaDBDistanceType.COSINE) // Optional: defaults to COSINE
.schemaName("mydb") // Optional: defaults to null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ To configure MilvusVectorStore in your application, you can use the following se
----
@Bean
public VectorStore vectorStore(MilvusServiceClient milvusClient, EmbeddingModel embeddingModel) {
MilvusVectorStoreConfig config = MilvusVectorStoreConfig.builder()
.withCollectionName("test_vector_store")
.withDatabaseName("default")
.withIndexType(IndexType.IVF_FLAT)
.withMetricType(MetricType.COSINE)
.build();
return new MilvusVectorStore(milvusClient, embeddingModel, config);
return MilvusVectorStore.builder(milvusClient, embeddingModel)
.collectionName("test_vector_store")
.databaseName("default")
.indexType(IndexType.IVF_FLAT)
.metricType(MetricType.COSINE)
.batchingStrategy(new TokenCountBatchingStrategy())
.initializeSchema(true)
.build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ Then create the `MongoDBAtlasVectorStore` bean using the builder pattern:
----
@Bean
public VectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingModel embeddingModel) {
return MongoDBAtlasVectorStore.builder()
.mongoTemplate(mongoTemplate)
.embeddingModel(embeddingModel)
return MongoDBAtlasVectorStore.builder(mongoTemplate, embeddingModel)
.collectionName("custom_vector_store") // Optional: defaults to "vector_store"
.vectorIndexName("custom_vector_index") // Optional: defaults to "vector_index"
.pathName("custom_embedding") // Optional: defaults to "embedding"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ Then create the `Neo4jVectorStore` bean using the builder pattern:
----
@Bean
public VectorStore vectorStore(Driver driver, EmbeddingModel embeddingModel) {
return Neo4jVectorStore.builder()
.driver(driver)
.embeddingModel(embeddingModel)
return Neo4jVectorStore.builder(driver, embeddingModel)
.databaseName("neo4j") // Optional: defaults to "neo4j"
.distanceType(Neo4jDistanceType.COSINE) // Optional: defaults to COSINE
.dimensions(1536) // Optional: defaults to 1536
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ Then create the `OpenSearchVectorStore` bean using the builder pattern:
----
@Bean
public VectorStore vectorStore(OpenSearchClient openSearchClient, EmbeddingModel embeddingModel) {
return OpenSearchVectorStore.builder()
.openSearchClient(openSearchClient)
.embeddingModel(embeddingModel)
return OpenSearchVectorStore.builder(openSearchClient, embeddingModel)
.index("custom-index") // Optional: defaults to "spring-ai-document-index"
.similarityFunction("l2") // Optional: defaults to "cosinesimil"
.initializeSchema(true) // Optional: defaults to false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ To configure PgVector in your application, you can use the following setup:
----
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return PgVectorStore.builder()
.jdbcTemplate(jdbcTemplate)
.embeddingModel(embeddingModel)
return PgVectorStore.builder(jdbcTemplate, embeddingModel)
.dimensions(1536) // Optional: defaults to model dimensions or 1536
.distanceType(COSINE_DISTANCE) // Optional: defaults to COSINE_DISTANCE
.indexType(HNSW) // Optional: defaults to HNSW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,30 +188,15 @@ To configure Pinecone in your application, you can use the following setup:
[source,java]
----
@Bean
public PineconeVectorStoreConfig pineconeVectorStoreConfig() {

return PineconeVectorStoreConfig.builder()
.withApiKey(<PINECONE_API_KEY>)
.withEnvironment("gcp-starter")
.withProjectId("89309e6")
.withIndexName("spring-ai-test-index")
.withNamespace("") // the free tier doesn't support namespaces.
.withContentFieldName("my_content") // optional field to store the original content. Defaults to `document_content`
public VectorStore pineconeVectorStore(EmbeddingModel embeddingModel) {
return PineconeVectorStore
.builder(embeddingModel, PINECONE_API_KEY, PINECONE_PROJECT_ID, PINECONE_ENVIRONMENT, PINECONE_INDEX_NAME)
.namespace(PINECONE_NAMESPACE) // the free tier doesn't support namespaces.
.contentFieldName(CUSTOM_CONTENT_FIELD_NAME) // optional field to store the original content. Defaults to `document_content`
.build();
}
----

Integrate with OpenAI's embeddings by adding the Spring Boot OpenAI starter to your project.
This provides you with an implementation of the Embeddings client:

[source,java]
----
@Bean
public VectorStore vectorStore(PineconeVectorStoreConfig config, EmbeddingModel embeddingModel) {
return new PineconeVectorStore(config, embeddingModel);
}
----

In your main code, create some documents:

[source,java]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ Then create the `QdrantVectorStore` bean using the builder pattern:
----
@Bean
public VectorStore vectorStore(QdrantClient qdrantClient, EmbeddingModel embeddingModel) {
return QdrantVectorStore.builder(qdrantClient)
.embeddingModel(embeddingModel)
return QdrantVectorStore.builder(qdrantClient, embeddingModel)
.collectionName("custom-collection") // Optional: defaults to "vector_store"
.initializeSchema(true) // Optional: defaults to false
.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ Then create the `RedisVectorStore` bean using the builder pattern:
----
@Bean
public VectorStore vectorStore(JedisPooled jedisPooled, EmbeddingModel embeddingModel) {
return RedisVectorStore.builder()
.jedis(jedisPooled)
.embeddingModel(embeddingModel)
return RedisVectorStore.builder(jedisPooled, embeddingModel)
.indexName("custom-index") // Optional: defaults to "spring-ai-index"
.prefix("custom-prefix") // Optional: defaults to "embedding:"
.metadataFields( // Optional: define metadata fields for filtering
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ Then create the `TypesenseVectorStore` bean using the builder pattern:
----
@Bean
public VectorStore vectorStore(Client client, EmbeddingModel embeddingModel) {
return TypesenseVectorStore.builder()
.client(client)
.embeddingModel(embeddingModel)
return TypesenseVectorStore.builder(client, embeddingModel)
.collectionName("custom_vectors") // Optional: defaults to "vector_store"
.embeddingDimension(1536) // Optional: defaults to 1536
.initializeSchema(true) // Optional: defaults to false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,8 @@ public WeaviateClient weaviateClient() {
}

@Bean
public VectorStore vectorStore(EmbeddingModel embeddingModel, WeaviateClient weaviateClient) {
return WeaviateVectorStore.builder()
.weaviateClient(weaviateClient)
.embeddingModel(embeddingModel)
public VectorStore vectorStore(WeaviateClient weaviateClient, EmbeddingModel embeddingModel) {
return WeaviateVectorStore.builder(weaviateClient, embeddingModel)
.objectClass("CustomClass") // Optional: defaults to "SpringAiWeaviate"
.consistencyLevel(ConsistentLevel.QUORUM) // Optional: defaults to ConsistentLevel.ONE
.filterMetadataFields(List.of( // Optional: fields that can be used in filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ MongoDBAtlasVectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingModel
ObjectProvider<VectorStoreObservationConvention> customObservationConvention,
BatchingStrategy batchingStrategy) {

MongoDBAtlasVectorStore.MongoDBBuilder builder = MongoDBAtlasVectorStore.builder(mongoTemplate, embeddingModel)
MongoDBAtlasVectorStore.Builder builder = MongoDBAtlasVectorStore.builder(mongoTemplate, embeddingModel)
.initializeSchema(properties.isInitializeSchema())
.observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
.customObservationConvention(customObservationConvention.getIfAvailable(() -> null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public CosmosDBVectorStore(ObservationRegistry observationRegistry,
* create new CosmosDBVectorStore instances.
* @param builder the configured builder instance
*/
protected CosmosDBVectorStore(CosmosDBBuilder builder) {
protected CosmosDBVectorStore(Builder builder) {
super(builder);

Assert.notNull(builder.cosmosClient, "CosmosClient must not be null");
Expand All @@ -172,8 +172,8 @@ protected CosmosDBVectorStore(CosmosDBBuilder builder) {
initializeContainer(containerName, databaseName, vectorStoreThroughput, vectorDimensions, partitionKeyPath);
}

public static CosmosDBBuilder builder(CosmosAsyncClient cosmosClient, EmbeddingModel embeddingModel) {
return new CosmosDBBuilder(cosmosClient, embeddingModel);
public static Builder builder(CosmosAsyncClient cosmosClient, EmbeddingModel embeddingModel) {
return new Builder(cosmosClient, embeddingModel);
}

private void initializeContainer(String containerName, String databaseName, int vectorStoreThroughput,
Expand Down Expand Up @@ -429,7 +429,7 @@ public VectorStoreObservationContext.Builder createObservationContextBuilder(Str
*
* @since 1.0.0
*/
public static class CosmosDBBuilder extends AbstractVectorStoreBuilder<CosmosDBBuilder> {
public static class Builder extends AbstractVectorStoreBuilder<Builder> {

private final CosmosAsyncClient cosmosClient;

Expand All @@ -450,7 +450,7 @@ public static class CosmosDBBuilder extends AbstractVectorStoreBuilder<CosmosDBB

private BatchingStrategy batchingStrategy = new TokenCountBatchingStrategy();

private CosmosDBBuilder(CosmosAsyncClient cosmosClient, EmbeddingModel embeddingModel) {
private Builder(CosmosAsyncClient cosmosClient, EmbeddingModel embeddingModel) {
super(embeddingModel);
Assert.notNull(cosmosClient, "CosmosClient must not be null");
this.cosmosClient = cosmosClient;
Expand All @@ -462,7 +462,7 @@ private CosmosDBBuilder(CosmosAsyncClient cosmosClient, EmbeddingModel embedding
* @return the builder instance
* @throws IllegalArgumentException if containerName is null or empty
*/
public CosmosDBBuilder containerName(String containerName) {
public Builder containerName(String containerName) {
Assert.hasText(containerName, "Container name must not be empty");
this.containerName = containerName;
return this;
Expand All @@ -474,7 +474,7 @@ public CosmosDBBuilder containerName(String containerName) {
* @return the builder instance
* @throws IllegalArgumentException if databaseName is null or empty
*/
public CosmosDBBuilder databaseName(String databaseName) {
public Builder databaseName(String databaseName) {
Assert.hasText(databaseName, "Database name must not be empty");
this.databaseName = databaseName;
return this;
Expand All @@ -486,7 +486,7 @@ public CosmosDBBuilder databaseName(String databaseName) {
* @return the builder instance
* @throws IllegalArgumentException if partitionKeyPath is null or empty
*/
public CosmosDBBuilder partitionKeyPath(String partitionKeyPath) {
public Builder partitionKeyPath(String partitionKeyPath) {
Assert.hasText(partitionKeyPath, "Partition key path must not be empty");
this.partitionKeyPath = partitionKeyPath;
return this;
Expand All @@ -498,7 +498,7 @@ public CosmosDBBuilder partitionKeyPath(String partitionKeyPath) {
* @return the builder instance
* @throws IllegalArgumentException if vectorStoreThroughput is not positive
*/
public CosmosDBBuilder vectorStoreThroughput(int vectorStoreThroughput) {
public Builder vectorStoreThroughput(int vectorStoreThroughput) {
Assert.isTrue(vectorStoreThroughput > 0, "Vector store throughput must be positive");
this.vectorStoreThroughput = vectorStoreThroughput;
return this;
Expand All @@ -510,7 +510,7 @@ public CosmosDBBuilder vectorStoreThroughput(int vectorStoreThroughput) {
* @return the builder instance
* @throws IllegalArgumentException if vectorDimensions is not positive
*/
public CosmosDBBuilder vectorDimensions(long vectorDimensions) {
public Builder vectorDimensions(long vectorDimensions) {
Assert.isTrue(vectorDimensions > 0, "Vector dimensions must be positive");
this.vectorDimensions = vectorDimensions;
return this;
Expand All @@ -521,7 +521,7 @@ public CosmosDBBuilder vectorDimensions(long vectorDimensions) {
* @param metadataFieldsList the list of metadata fields
* @return the builder instance
*/
public CosmosDBBuilder metadataFields(List<String> metadataFieldsList) {
public Builder metadataFields(List<String> metadataFieldsList) {
this.metadataFieldsList = metadataFieldsList != null ? new ArrayList<>(metadataFieldsList)
: new ArrayList<>();
return this;
Expand All @@ -533,7 +533,7 @@ public CosmosDBBuilder metadataFields(List<String> metadataFieldsList) {
* @return the builder instance
* @throws IllegalArgumentException if batchingStrategy is null
*/
public CosmosDBBuilder batchingStrategy(BatchingStrategy batchingStrategy) {
public Builder batchingStrategy(BatchingStrategy batchingStrategy) {
Assert.notNull(batchingStrategy, "BatchingStrategy must not be null");
this.batchingStrategy = batchingStrategy;
return this;
Expand Down
Loading
Loading