GemFire 벡터 스토어
GemFire 벡터 스토어 (GemFire Vector Store)
이 섹션에서는 GemFireVectorStore를 설정해서 문서 임베딩을 저장하고 유사도 검색을 수행하는 방법을 안내해요. GemFire는 분산·인메모리 키-값 스토어로 초고속 읽기·쓰기 작업을 수행하며, GemFire VectorDB 확장은 벡터 유사도 검색을 효율적으로 수행하는 다재다능한 벡터 데이터베이스 역할을 해요.
출처: 문서
본문
GemFire 벡터 스토어 (GemFire Vector Store)
이 섹션은 GemFireVectorStore를 설정해 문서 임베딩을 저장하고 유사도 검색을 수행하는 방법을 안내해요.
GemFire는 분산·인메모리 키-값 스토어로 번개처럼 빠른 속도로 읽기·쓰기 작업을 수행해요. 고가용성 병렬 메시지 큐, 지속 가용성, 다운타임 없이 동적으로 확장할 수 있는 이벤트 기반 아키텍처를 제공해요. 고성능 실시간 앱을 지원하기 위해 데이터 크기 요구사항이 늘어나는 만큼 GemFire는 쉽게 선형 확장할 수 있어요.
GemFire VectorDB는 GemFire의 기능을 확장해 벡터를 효율적으로 저장·검색하고 벡터 유사도 검색을 수행하는 다재다능한 벡터 데이터베이스 역할을 해요.
사전 준비 (Prerequisites)
-
GemFire VectorDB 확장이 활성화된 GemFire 클러스터
-
문서 임베딩을 계산할
EmbeddingModel빈. 자세한 내용은 EmbeddingModel 섹션을 참고하세요. 로컬 머신에서 실행되는 옵션은 ONNX와 all-MiniLM-L6-v2 Sentence Transformers예요.
자동 설정 (Auto-configuration)
참고: Spring AI auto-configuration과 starter 모듈의 아티팩트 이름에 큰 변화가 있었어요. 자세한 내용은 upgrade notes를 참고해 주세요.
프로젝트의 Maven 빌드 파일 pom.xml에 GemFire VectorStore Spring Boot starter를 추가하세요:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-gemfire</artifactId>
</dependency>
또는 Gradle build.gradle 파일에:
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-gemfire'
}
구성 프로퍼티 (Configuration properties)
Spring Boot 구성에서 다음 프로퍼티를 사용해 GemFireVectorStore를 추가로 구성할 수 있어요.
| Property | Default value |
|---|---|
spring.ai.vectorstore.gemfire.host |
localhost |
spring.ai.vectorstore.gemfire.port |
8080 |
spring.ai.vectorstore.gemfire.initialize-schema |
false |
spring.ai.vectorstore.gemfire.index-name |
spring-ai-gemfire-store |
spring.ai.vectorstore.gemfire.beam-width |
100 |
spring.ai.vectorstore.gemfire.max-connections |
16 |
spring.ai.vectorstore.gemfire.vector-similarity-function |
COSINE |
spring.ai.vectorstore.gemfire.fields |
[] |
spring.ai.vectorstore.gemfire.buckets |
0 |
spring.ai.vectorstore.gemfire.username |
null |
spring.ai.vectorstore.gemfire.password |
null |
spring.ai.vectorstore.gemfire.token |
null |
수동 구성 (Manual Configuration)
Spring Boot의 Auto-configuration 없이 GemFireVectorStore만 사용하려면 프로젝트의 Maven pom.xml에 다음 의존성을 추가하세요:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store</artifactId>
</dependency>
Gradle 사용자는 build.gradle의 dependencies 블록 아래 다음을 추가해 GemFireVectorStore만 사용할 수 있어요:
dependencies {
implementation 'org.springframework.ai:spring-ai-gemfire-store'
}
사용법 (Usage)
여기 AutoConfiguration 대신 GemfireVectorStore 인스턴스를 만드는 샘플이 있어요.
@Bean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel) {
return GemFireVectorStore.builder(embeddingModel)
.host("localhost")
.port(7071)
.username("my-user-name")
.password("my-password")
.indexName("my-vector-index")
.fields(new String[] {"country", "year", "activationDate"}) // Optional: fields for metadata filtering
.initializeSchema(true)
.build();
}
참고: 기본 구성은
localhost:8080의 GemFire 클러스터에 연결해요.
- 애플리케이션에서 몇 개의 문서를 만드세요:
List<Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "UK", "year", 2020)),
new Document("The World is Big and Salvation Lurks Around the Corner", Map.of()),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("country", "NL", "year", 2023)));
- 벡터 스토어에 문서를 추가하세요:
vectorStore.add(documents);
- 그리고 유사도 검색으로 문서를 검색하세요:
List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder().query("Spring").topK(5).build());
"Spring AI rocks!!" 텍스트를 담은 문서를 검색하게 될 거예요. 유사도 임계값으로 결과 수를 제한할 수도 있어요:
List<Document> results = vectorStore.similaritySearch(
SearchRequest.builder().query("Spring").topK(5)
.similarityThreshold(0.5d).build());
메타데이터 필터링 (Metadata Filtering)
GemFire VectorStore에서도 일반적이고 휴대 가능한 메타데이터 필터를 활용할 수 있어요.
예를 들어 텍스트 표현 언어를 사용할 수 있어요:
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(5)
.similarityThreshold(0.7)
.filterExpression("country == 'BG' && year >= 2020").build());
또는 Filter.Expression DSL로 프로그래밍 방식으로:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(5)
.similarityThreshold(0.7)
.filterExpression(b.and(
b.eq("country", "BG"),
b.gte("year", 2020)).build()).build());
참고: 이 (휴대 가능한) 필터 표현식은 자동으로 고유한 GemFire VectorDB 쿼리 형식으로 변환돼요.
예를 들어 이 휴대 가능한 필터 표현식:
country == 'BG' && year >= 2020
은 고유한 GemFire VectorDB 필터 형식으로 변환돼요:
country:BG AND year:[2020 TO *]
GemFire VectorStore는 광범위한 필터 작업을 지원해요:
- 동등 (Equality):
country == 'BG'→country:BG - 부등 (Inequality):
city != 'Sofia'→city: NOT Sofia - 초과 (Greater Than):
year > 2020→year:{2020 TO *] - 이상 (Greater Than or Equal):
year >= 2020→year:[2020 TO *] - 미만 (Less Than):
year < 2025→year:[* TO 2025} - 이하 (Less Than or Equal):
year ⇐ 2025→year:[* TO 2025] - IN:
country in ['BG', 'NL']→country:(BG OR NL) - NOT IN:
country nin ['BG', 'NL']→NOT country:(BG OR NL) - AND/OR: 조건을 결합하는 논리 연산자
- 그룹화 (Grouping): 복잡한 표현식에 괄호 사용
- 날짜 필터링 (Date Filtering): ISO 8601 형식의 날짜 값 (예:
2024-01-07T14:29:12Z)
참고: GemFire VectorStore로 메타데이터 필터링을 사용하려면 벡터 스토어를 만들 때 필터링할 수 있는 메타데이터 필드를 지정해야 해요. 빌더의
fields파라미터로 이 작업을 해요:
GemFireVectorStore.builder(embeddingModel) .fields(new String[] {"country", "year", "activationDate"}) .build();또는 구성 프로퍼티로:
spring.ai.vectorstore.gemfire.fields=country,year,activationDate