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.
This commit is contained in:
committed by
Mark Pollack
parent
f9d741dd85
commit
d697e58c05
@@ -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)))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user