跳到主要内容

pgvector

Summary

This article describes how to use pgvector with Spring AI.

The complete source is available on GitHub JavaAIDev/pgvector-sample.

Spring AI supports different types of vector stores. pgvector is used as an example.

To use pgvector, Spring AI module spring-ai-pgvector-store must be added. The easiest approach is using Spring Boot starter for pgvector.

Spring Boot starter for pgvector
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-pgvector</artifactId>
</dependency>

This Spring Boot starter will create required beans to use pgvector. We can simply provide configurations. The configuration prefix of pgvector is spring.ai.vectorstore.pgvector. initializeSchema is set to true to initialize database table schema required for Spring AI.

pgvector configuration
spring:
application.name: pgvector
ai:
openai:
api-key: ${OPENAI_API_KEY:demo}
chat:
options:
model: gpt-4.1-mini
temperature: 0.0
embedding:
options:
model: text-embedding-3-small
vectorstore:
pgvector:
tableName: vector_store
schemaName: public
schemaValidation: false
initializeSchema: true
datasource:
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: postgres

The application code can use VectorStore directly.