Skip to content

Commit ddff9b1

Browse files
authored
add semantic search using pg vector and ollama (#18251)
* add semantic search using pg vector and ollama * remove webflux dependency, use chat client * update annotations * remove test containers * move version number, make lowercase the package name, apply Baeldung code formatter
1 parent 3aa8684 commit ddff9b1

File tree

7 files changed

+176
-0
lines changed

7 files changed

+176
-0
lines changed

spring-ai-2/docker-compose.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
services:
2+
postgres:
3+
image: pgvector/pgvector:pg17
4+
environment:
5+
POSTGRES_DB: vectordb
6+
POSTGRES_USER: postgres
7+
POSTGRES_PASSWORD: postgres
8+
ports:
9+
- "5434:5432"
10+
healthcheck:
11+
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
12+
interval: 10s
13+
timeout: 5s
14+
retries: 5
15+
16+
ollama:
17+
image: ollama/ollama:latest
18+
ports:
19+
- "11435:11434"
20+
volumes:
21+
- ollama_data:/root/.ollama
22+
healthcheck:
23+
test: [ "CMD", "curl", "-f", "http://localhost:11435/api/health" ]
24+
interval: 10s
25+
timeout: 5s
26+
retries: 10
27+
28+
volumes:
29+
ollama_data:

spring-ai-2/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
<artifactId>hsqldb</artifactId>
8585
<scope>runtime</scope>
8686
</dependency>
87+
<dependency>
88+
<groupId>org.springframework.ai</groupId>
89+
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
90+
</dependency>
8791

8892
<!-- Test dependencies -->
8993
<dependency>
@@ -106,6 +110,11 @@
106110
<artifactId>ollama</artifactId>
107111
<scope>test</scope>
108112
</dependency>
113+
<dependency>
114+
<groupId>org.springframework.boot</groupId>
115+
<artifactId>spring-boot-docker-compose</artifactId>
116+
<version>${spring-boot-docker-compose.version}</version>
117+
</dependency>
109118
</dependencies>
110119

111120
<build>
@@ -131,6 +140,7 @@
131140
<spring-boot.version>3.4.1</spring-boot.version>
132141
<spring-ai.version>1.0.0-M6</spring-ai.version>
133142
<junit-jupiter.version>5.9.0</junit-jupiter.version>
143+
<spring-boot-docker-compose.version>3.1.1</spring-boot-docker-compose.version>
134144
</properties>
135145

136146
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.springai.semanticsearch;
2+
3+
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration;
4+
import org.springframework.ai.autoconfigure.vectorstore.chroma.ChromaVectorStoreAutoConfiguration;
5+
import org.springframework.ai.chat.model.ChatModel;
6+
import org.springframework.ai.ollama.OllamaChatModel;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Primary;
11+
import org.springframework.context.annotation.PropertySource;
12+
13+
@SpringBootApplication(exclude = { OpenAiAutoConfiguration.class, ChromaVectorStoreAutoConfiguration.class })
14+
@PropertySource("classpath:application-semantic-search.properties")
15+
public class Application {
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(Application.class, args);
19+
}
20+
21+
@Bean
22+
@Primary
23+
public ChatModel chatModel(OllamaChatModel ollamaChatModel) {
24+
return ollamaChatModel;
25+
}
26+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.springai.semanticsearch;
2+
3+
public record Book(String title, String author, String description) {
4+
5+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.springai.semanticsearch;
2+
3+
import org.springframework.ai.chat.client.ChatClient;
4+
import org.springframework.ai.document.Document;
5+
import org.springframework.ai.vectorstore.SearchRequest;
6+
import org.springframework.ai.vectorstore.VectorStore;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import java.util.List;
13+
14+
@RestController
15+
@RequestMapping("/books")
16+
public class BookSearchController {
17+
18+
private final VectorStore vectorStore;
19+
private final ChatClient chatClient;
20+
21+
public BookSearchController(VectorStore vectorStore, ChatClient.Builder chatClientBuilder) {
22+
this.vectorStore = vectorStore;
23+
this.chatClient = chatClientBuilder.build();
24+
}
25+
26+
@PostMapping("/search")
27+
List<String> semanticSearch(@RequestBody String query) {
28+
return vectorStore.similaritySearch(SearchRequest.builder()
29+
.query(query)
30+
.topK(3)
31+
.build())
32+
.stream()
33+
.map(Document::getText)
34+
.toList();
35+
}
36+
37+
@PostMapping("/enhanced-search")
38+
String enhancedSearch(@RequestBody String query) {
39+
String context = vectorStore.similaritySearch(SearchRequest.builder()
40+
.query(query)
41+
.topK(3)
42+
.build())
43+
.stream()
44+
.map(Document::getText)
45+
.reduce("", (a, b) -> a + b + "\n");
46+
47+
return chatClient.prompt()
48+
.system(context)
49+
.user(query)
50+
.call()
51+
.content();
52+
}
53+
54+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.springai.semanticsearch;
2+
3+
import jakarta.annotation.PostConstruct;
4+
5+
import org.springframework.ai.document.Document;
6+
import org.springframework.ai.vectorstore.VectorStore;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.List;
10+
11+
@Component
12+
public class BooksIngestionPipeline {
13+
14+
private final VectorStore vectorStore;
15+
16+
public BooksIngestionPipeline(VectorStore vectorStore) {
17+
this.vectorStore = vectorStore;
18+
}
19+
20+
@PostConstruct
21+
void run() {
22+
var books = List.of(new Book("The Great Gatsby", "F. Scott Fitzgerald",
23+
"The Great Gatsby is a 1925 novel by American writer F. Scott Fitzgerald. Set in the Jazz Age on Long Island, near New York City, the novel depicts first-person narrator Nick Carraway's interactions with mysterious millionaire Jay Gatsby and Gatsby's obsession to reunite with his former lover, Daisy Buchanan."),
24+
new Book("To Kill a Mockingbird", "Harper Lee",
25+
"To Kill a Mockingbird is a novel by the American author Harper Lee. It was published in 1960 and was instantly successful. In the United States, it is widely read in high schools and middle schools."),
26+
new Book("1984", "George Orwell",
27+
"Nineteen Eighty-Four: A Novel, often referred to as 1984, is a dystopian social science fiction novel by the English novelist George Orwell. It was published on 8 June 1949 by Secker & Warburg as Orwell's ninth and final book completed in his lifetime."),
28+
new Book("The Catcher in the Rye", "J. D. Salinger",
29+
"The Catcher in the Rye is a novel by J. D. Salinger, partially published in serial form in 1945–1946 and as a novel in 1951. It was originally intended for adults but is often read by adolescents for its themes of angst, alienation, and as a critique on superficiality in society."),
30+
new Book("Lord of the Flies", "William Golding",
31+
"Lord of the Flies is a 1954 novel by Nobel Prize-winning British author William Golding. The book focuses on a group of British"));
32+
33+
List<Document> documents = books.stream()
34+
.map(book -> new Document(book.toString()))
35+
.toList();
36+
37+
vectorStore.add(documents);
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring.ai.ollama.init.pull-model-strategy=when_missing
2+
spring.ai.ollama.init.chat.include=true
3+
spring.ai.ollama.embedding.options.model=nomic-embed-text
4+
spring.ai.vectorstore.pgvector.initialize-schema=true
5+
spring.ai.vectorstore.pgvector.dimensions=768
6+
spring.ai.vectorstore.pgvector.index-type=hnsw
7+
spring.docker.compose.file=docker-compose.yml
8+
spring.docker.compose.enabled=true
9+
spring.datasource.url=jdbc:postgresql://localhost:5434/vectordb
10+
spring.datasource.username=postgres
11+
spring.datasource.password=postgres
12+
spring.datasource.driver-class-name=org.postgresql.Driver
13+
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

0 commit comments

Comments
 (0)