From d697e58c056aa0a3aeadd058bac8554a6f2e0736 Mon Sep 17 00:00:00 2001 From: Soby Chacko Date: Fri, 20 Dec 2024 18:06:56 -0500 Subject: [PATCH] Update vector store builder examples Update documentation examples to show correct builder usage across all vector store implementations. Demonstrate passing required parameters directly in builder() method. --- .../pages/api/vectordbs/apache-cassandra.adoc | 4 +-- .../pages/api/vectordbs/azure-cosmos-db.adoc | 13 +++++----- .../ROOT/pages/api/vectordbs/azure.adoc | 10 +++++--- .../ROOT/pages/api/vectordbs/chroma.adoc | 5 +++- .../pages/api/vectordbs/elasticsearch.adoc | 4 +-- .../ROOT/pages/api/vectordbs/gemfire.adoc | 11 +++++--- .../ROOT/pages/api/vectordbs/mariadb.adoc | 3 +-- .../ROOT/pages/api/vectordbs/milvus.adoc | 15 +++++------ .../ROOT/pages/api/vectordbs/mongodb.adoc | 4 +-- .../ROOT/pages/api/vectordbs/neo4j.adoc | 4 +-- .../ROOT/pages/api/vectordbs/opensearch.adoc | 4 +-- .../ROOT/pages/api/vectordbs/pgvector.adoc | 4 +-- .../ROOT/pages/api/vectordbs/pinecone.adoc | 25 ++++--------------- .../ROOT/pages/api/vectordbs/qdrant.adoc | 3 +-- .../ROOT/pages/api/vectordbs/redis.adoc | 4 +-- .../ROOT/pages/api/vectordbs/typesense.adoc | 4 +-- .../ROOT/pages/api/vectordbs/weaviate.adoc | 6 ++--- 17 files changed, 49 insertions(+), 74 deletions(-) diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/apache-cassandra.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/apache-cassandra.adoc index 1e0885e0f..23afd0cf1 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/apache-cassandra.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/apache-cassandra.adoc @@ -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) .keyspace("my_keyspace") .table("my_vectors") .partitionKeys(List.of(new SchemaColumn("id", DataTypes.TEXT))) diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure-cosmos-db.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure-cosmos-db.adoc index cc1bb3130..2cab35025 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure-cosmos-db.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure-cosmos-db.adoc @@ -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")) @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure.adoc index bc508da47..0b8a7579f 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/azure.adoc @@ -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(); } ---- diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/chroma.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/chroma.adoc index d208feb88..3bb1b4cf6 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/chroma.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/chroma.adoc @@ -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(); } ---- diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/elasticsearch.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/elasticsearch.adoc index 5b18a7ec0..58751ed92 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/elasticsearch.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/elasticsearch.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/gemfire.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/gemfire.adoc index 0424951a6..a459074b2 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/gemfire.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/gemfire.adoc @@ -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(); } ---- diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mariadb.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mariadb.adoc index cde2b2dce..b6c0b0ac5 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mariadb.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mariadb.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/milvus.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/milvus.adoc index f900a427b..71c9b69d3 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/milvus.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/milvus.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mongodb.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mongodb.adoc index 8d9452d6b..522ef9f96 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mongodb.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/mongodb.adoc @@ -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" diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/neo4j.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/neo4j.adoc index 0b7e55966..aa9b73bc0 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/neo4j.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/neo4j.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/opensearch.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/opensearch.adoc index f209000a1..fb43aa705 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/opensearch.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/opensearch.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc index f2b70d63e..32c7341d2 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pinecone.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pinecone.adoc index ece6ec25f..b320b02d1 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pinecone.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pinecone.adoc @@ -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() - .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] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/qdrant.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/qdrant.adoc index 745ba8aa2..b3c82501e 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/qdrant.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/qdrant.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/redis.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/redis.adoc index 03b5ec07d..4928009f7 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/redis.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/redis.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/typesense.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/typesense.adoc index 42cd08c04..f21edce01 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/typesense.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/typesense.adoc @@ -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 diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/weaviate.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/weaviate.adoc index e4d93a4da..0ad4586f0 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/weaviate.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/weaviate.adoc @@ -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