This tutorial is your quick and easy guide to getting started with Spring AI and Couchbase as a Vector Store. Let's dive in and explore how these powerful tools can work together to enhance your applications.
Example source code for this tutorial can be obtained from Spring AI demo application with Couchbase Vector Store. To do this, clone the repository using git:
git clone https://github.com/couchbase-examples/couchbase-spring-ai-demo.git
cd couchbase-spring-ai-demo
Spring AI is an extension of the Spring Framework that simplifies the integration of AI capabilities into Spring applications. It provides abstractions and integrations for working with various AI services and models, making it easier for developers to incorporate AI functionality without having to manage low-level implementation details.
Key features of Spring AI include:
Spring AI brings several benefits to Java developers:
Couchbase spring-ai integration stores each embedding in a separate document and uses an FTS vector index to perform queries against stored vectors.
src/main/java/com/couchbase_spring_ai/demo/
├── Config.java # Application configuration
├── Controller.java # REST API endpoints
└── CouchbaseSpringAiDemoApplications.java # Application entry point
src/main/resources/
├── application.properties # Application settings
└── bbc_news_data.json # Sample data
The application is configured in application.properties
:
spring.application.name=spring-ai-demo
spring.ai.openai.api-key=your-openai-api-key
spring.couchbase.connection-string=couchbase://127.0.0.1
spring.couchbase.username=Administrator
spring.couchbase.password=password
Config.java
)This class creates the necessary beans for:
public class Config {
@Value("${spring.couchbase.connection-string}")
private String connectionUrl;
@Value("${spring.couchbase.username}")
private String username;
@Value("${spring.couchbase.password}")
private String password;
@Value("${spring.ai.openai.api-key}")
private String openaiKey;
public Config() {
}
@Bean
public Cluster cluster() {
return Cluster.connect(this.connectionUrl, this.username, this.password);
}
@Bean
public Boolean initializeSchema() {
return true;
}
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(OpenAiApi.builder().apiKey(this.openaiKey).build());
}
@Bean
public VectorStore couchbaseSearchVectorStore(Cluster cluster,
EmbeddingModel embeddingModel,
Boolean initializeSchema) {
return CouchbaseSearchVectorStore
.builder(cluster, embeddingModel)
.bucketName("test")
.scopeName("test")
.collectionName("test")
.initializeSchema(initializeSchema)
.build();
}
}
The vector store is configured to use:
The application uses CouchbaseSearchVectorStore
, which:
The embedding store uses an FTS vector index in order to perform vector similarity lookups. If provided with a name for vector index that does not exist on the cluster, the store will attempt to create a new index with default configuration based on the provided initialization settings. It is recommended to manually review the settings for the created index and adjust them according to specific use cases. More information about vector search and FTS index configuration can be found at Couchbase Documentation.
Controller.java
)Provides REST API endpoints:
/tutorial/load
: Loads sample BBC news data into Couchbase/tutorial/search
: Performs a semantic search for sports-related news articles...
Document doc = new Document(String.format("%s", i + 1), j.getString("content"), Map.of("title", j.getString("title")))
...
this.couchbaseSearchVectorStore.add(doc);
...
List<Document> results = this.couchbaseSearchVectorStore.similaritySearch(SearchRequest.builder()
.query("Give me some sports news")
.similarityThreshold((double)0.75F)
.topK(15)
.build());
return (List)results.stream()
.map((doc) -> Map.of("content", doc.getText(), "metadata", doc.getMetadata()))
.collect(Collectors.toList());
This is basically a Spring Boot project with two endpoints tutorial/load
and tutorial/search
.
In order to run this application, use the following command:
./mvnw spring-boot:run
http://localhost:8080/tutorial/load
http://localhost:8080/tutorial/search