Introduction
Milvus is an open-source vector database designed for scalable similarity search and high-dimensional vector data analysis. Integrating Milvus with Spring AI allows developers to efficiently store, manage, and query embeddings generated by AI models. This tutorial demonstrates how to set up and use Spring AI with Milvus in a Spring Boot application.
1. Setting Up the Project
Step 1: Create a New Spring Boot Project
Use Spring Initializr to create a new Spring Boot project. Include dependencies for Spring Web and Spring AI.
Using Spring Initializr:
- Go to start.spring.io
- Select:
- Project: Maven Project
- Language: Java
- Spring Boot: 3.0.0 (or latest)
- Dependencies: Spring Web
- Generate the project and unzip it.
Step 2: Add Dependencies
In your project's pom.xml
, add the necessary dependencies, including the Milvus VectorStore Spring Boot Starter.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-milvus-store-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
2. Configuring the Milvus Vector Database
Step 1: Set Up Milvus
Follow the Milvus installation guide to set up Milvus on your local machine or a cloud server.
Step 2: Add Configuration to Spring Boot
Add configuration details for your Milvus vector database in application.properties
or application.yml
.
For application.properties
:
milvus.host=localhost
milvus.port=19530
For application.yml
:
milvus:
host: localhost
port: 19530
Step 3: Create a Configuration Class
Create a configuration class to set up the Milvus client.
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.milvus.store.MilvusClientFactory;
@Configuration
public class MilvusConfig {
@Bean
public MilvusClientFactory milvusClientFactory() {
return new MilvusClientFactory("localhost", 19530);
}
}
3. Implementing Vector Database Integration
Example: Storing and Querying Vectors
Create a service to handle vector operations, including storing and querying vectors.
Service:
package com.example.demo.service;
import org.springframework.ai.milvus.store.MilvusClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.milvus.param.collection.CreateCollectionParam;
import io.milvus.param.collection.FieldType;
import io.milvus.param.collection.VectorFieldType;
import io.milvus.param.dml.InsertParam;
import io.milvus.param.dml.SearchParam;
import io.milvus.param.schema.Schema;
import java.util.Collections;
import java.util.List;
@Service
public class VectorService {
@Autowired
private MilvusClientFactory milvusClientFactory;
private static final String COLLECTION_NAME = "example_collection";
public void createCollection() {
VectorFieldType vectorField = VectorFieldType.newBuilder()
.withName("embedding")
.withDimension(128)
.withMetricType("L2")
.build();
FieldType primaryKey = FieldType.newBuilder()
.withName("id")
.withDataType("INT64")
.withAutoID(true)
.build();
Schema schema = Schema.newBuilder()
.withCollectionName(COLLECTION_NAME)
.addFieldType(primaryKey)
.addFieldType(vectorField)
.build();
milvusClientFactory.getClient().createCollection(CreateCollectionParam.newBuilder()
.withSchema(schema)
.build());
}
public void insertVectors(List<List<Float>> vectors) {
InsertParam insertParam = InsertParam.newBuilder()
.withCollectionName(COLLECTION_NAME)
.withFields(Collections.singletonMap("embedding", vectors))
.build();
milvusClientFactory.getClient().insert(insertParam);
}
public List<List<Float>> searchVectors(List<Float> queryVector) {
SearchParam searchParam = SearchParam.newBuilder()
.withCollectionName(COLLECTION_NAME)
.withTopK(10)
.withVectors(Collections.singletonList(queryVector))
.withMetricType("L2")
.withSearchParams(Collections.singletonMap("nprobe", "10"))
.build();
return milvusClientFactory.getClient().search(searchParam).getResults();
}
}
Controller:
package com.example.demo.controller;
import com.example.demo.service.VectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class VectorController {
@Autowired
private VectorService vectorService;
@PostMapping("/createCollection")
public void createCollection() {
vectorService.createCollection();
}
@PostMapping("/insertVectors")
public void insertVectors(@RequestBody List<List<Float>> vectors) {
vectorService.insertVectors(vectors);
}
@GetMapping("/searchVectors")
public List<List<Float>> searchVectors(@RequestParam List<Float> queryVector) {
return vectorService.searchVectors(queryVector);
}
}
4. Testing the Integration
Step 1: Run the Application
Run your Spring Boot application. Ensure the application starts without errors.
Step 2: Test Endpoints
Use Postman, curl, or your browser to test the endpoints.
-
Create Collection:
POST http://localhost:8080/createCollection
-
Insert Vectors:
POST http://localhost:8080/insertVectors Body: [[1.0, 2.0, 3.0, ..., 128.0], ...]
-
Search Vectors:
GET http://localhost:8080/searchVectors?queryVector=[1.0, 2.0, 3.0, ..., 128.0]
Conclusion
This tutorial demonstrated how to set up and integrate Milvus with a Spring Boot application using Spring AI. You learned how to create a service and controller for storing and querying vectors in the Milvus database, and how to create a simple frontend to interact with the database. This setup provides a foundation for building more complex and feature-rich AI applications. Explore further customization and
Comments
Post a Comment