Chroma

This section will walk you through setting up the Chroma VectorStore to store document embeddings and perform similarity searches.spring-doc.cn

Chroma is the open-source embedding database. It gives you the tools to store document embeddings, content, and metadata and to search through those embeddings, including metadata filtering.spring-doc.cn

Prerequisites

  1. Access to ChromeDB. The setup local ChromaDB appendix shows how to set up a DB locally with a Docker container.spring-doc.cn

  2. EmbeddingModel instance to compute the document embeddings. Several options are available:spring-doc.cn

On startup, the ChromaVectorStore creates the required collection if one is not provisioned already.spring-doc.cn

Auto-configuration

Spring AI provides Spring Boot auto-configuration for the Chroma Vector Store. To enable it, add the following dependency to your project’s Maven pom.xml file:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-chroma-store-spring-boot-starter</artifactId>
</dependency>

or to your Gradle build.gradle build file.spring-doc.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-chroma-store-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.
Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file.

The vector store implementation can initialize the requisite schema for you, but you must opt-in by specifying the initializeSchema boolean in the appropriate constructor or by setting …​initialize-schema=true in the application.properties file.spring-doc.cn

this is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default.

Additionally, you will need a configured EmbeddingModel bean. Refer to the EmbeddingModel section for more information.spring-doc.cn

Here is an example of the needed bean:spring-doc.cn

@Bean
public EmbeddingModel embeddingModel() {
    // Can be any other EmbeddingModel implementation.
    return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
}

To connect to Chroma you need to provide access details for your instance. A simple configuration can either be provided via Spring Boot’s application.properties,spring-doc.cn

# Chroma Vector Store connection properties
spring.ai.vectorstore.chroma.client.host=<your Chroma instance host>
spring.ai.vectorstore.chroma.client.port=<your Chroma instance port>
spring.ai.vectorstore.chroma.client.key-token=<your access token (if configure)>
spring.ai.vectorstore.chroma.client.username=<your username (if configure)>
spring.ai.vectorstore.chroma.client.password=<your password (if configure)>

# Chroma Vector Store collection properties
spring.ai.vectorstore.chroma.initialize-schema=<true or false>
spring.ai.vectorstore.chroma.collection-name=<your collection name>

# Chroma Vector Store configuration properties

# OpenAI API key if the OpenAI auto-configuration is used.
spring.ai.openai.api.key=<OpenAI Api-key>

Please have a look at the list of configuration parameters for the vector store to learn about the default values and configuration options.spring-doc.cn

Now you can auto-wire the Chroma Vector Store in your application and use itspring-doc.cn

@Autowired VectorStore vectorStore;

// ...

List <Document> documents = List.of(
    new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
    new Document("The World is Big and Salvation Lurks Around the Corner"),
    new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

// Add the documents
vectorStore.add(documents);

// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));

Configuration properties

You can use the following properties in your Spring Boot configuration to customize the vector store.spring-doc.cn

Property Description Default value

spring.ai.vectorstore.chroma.client.hostspring-doc.cn

Server connection hostspring-doc.cn

http://localhostspring-doc.cn

spring.ai.vectorstore.chroma.client.portspring-doc.cn

Server connection portspring-doc.cn

8000spring-doc.cn

spring.ai.vectorstore.chroma.client.key-tokenspring-doc.cn

Access token (if configured)spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.chroma.client.usernamespring-doc.cn

Access username (if configured)spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.chroma.client.passwordspring-doc.cn

Access password (if configured)spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.chroma.collection-namespring-doc.cn

Collection namespring-doc.cn

SpringAiCollectionspring-doc.cn

spring.ai.vectorstore.chroma.initialize-schemaspring-doc.cn

Whether to initialize the required schemaspring-doc.cn

falsespring-doc.cn

For ChromaDB secured with Static API Token Authentication use the ChromaApi#withKeyToken(<Your Token Credentials>) method to set your credentials. Check the ChromaWhereIT for an example.spring-doc.cn

For ChromaDB secured with Basic Authentication use the ChromaApi#withBasicAuth(<your user>, <your password>) method to set your credentials. Check the BasicAuthChromaWhereIT for an example.spring-doc.cn

Metadata filtering

You can leverage the generic, portable metadata filters with ChromaVector store as well.spring-doc.cn

For example, you can use either the text expression language:spring-doc.cn

vectorStore.similaritySearch(
                    SearchRequest.defaults()
                            .withQuery("The World")
                            .withTopK(TOP_K)
                            .withSimilarityThreshold(SIMILARITY_THRESHOLD)
                            .withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));

or programmatically using the Filter.Expression DSL:spring-doc.cn

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.defaults()
                    .withQuery("The World")
                    .withTopK(TOP_K)
                    .withSimilarityThreshold(SIMILARITY_THRESHOLD)
                    .withFilterExpression(b.and(
                            b.in("john", "jill"),
                            b.eq("article_type", "blog")).build()));
Those (portable) filter expressions get automatically converted into the proprietary Chroma where filter expressions.

For example, this portable filter expression:spring-doc.cn

author in ['john', 'jill'] && article_type == 'blog'

is converted into the proprietary Chroma formatspring-doc.cn

{"$and":[
	{"author": {"$in": ["john", "jill"]}},
	{"article_type":{"$eq":"blog"}}]
}

Manual Configuration

If you prefer to configure the Chroma Vector Store manually, you can do so by creating a ChromaVectorStore bean in your Spring Boot application.spring-doc.cn

Add these dependencies to your project: * Chroma VectorStore.spring-doc.cn

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-chroma-store</artifactId>
</dependency>
  • OpenAI: Required for calculating embeddings. You can use any other embedding model implementation.spring-doc.cn

<dependency>
 <groupId>org.springframework.ai</groupId>
 <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Sample Code

Create a RestClient.Builder instance with proper ChromaDB authorization configurations and Use it to create a ChromaApi instance:spring-doc.cn

@Bean
public RestClient.Builder builder() {
    return RestClient.builder().requestFactory(new SimpleClientHttpRequestFactory());
}


@Bean
public ChromaApi chromaApi(RestClient.Builder restClientBuilder) {
   String chromaUrl = "http://localhost:8000";
   ChromaApi chromaApi = new ChromaApi(chromaUrl, restClientBuilder);
   return chromaApi;
}

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:spring-doc.cn

@Bean
public VectorStore chromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi) {
 return new ChromaVectorStore(embeddingModel, chromaApi, "TestCollection", false);
}

In your main code, create some documents:spring-doc.cn

List<Document> documents = List.of(
 new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
 new Document("The World is Big and Salvation Lurks Around the Corner"),
 new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

Add the documents to your vector store:spring-doc.cn

vectorStore.add(documents);

And finally, retrieve documents similar to a query:spring-doc.cn

List<Document> results = vectorStore.similaritySearch("Spring");

If all goes well, you should retrieve the document containing the text "Spring AI rocks!!".spring-doc.cn

Run Chroma Locally

docker run -it --rm --name chroma -p 8000:8000 ghcr.io/chroma-core/chroma:0.5.20

Starts a chroma store at localhost:8000/api/v1spring-doc.cn