PGvector
本節將引導您完成 PGvector VectorStore 的設定,以儲存文件嵌入並執行相似性搜尋。
PGvector 是 PostgreSQL 的一個開源擴充套件,它支援儲存和搜尋機器學習生成的嵌入。它提供了不同的功能,允許使用者識別精確和近似的最近鄰居。它旨在與其他 PostgreSQL 功能無縫協作,包括索引和查詢。
先決條件
首先,您需要訪問已啟用 vector、hstore 和 uuid-ossp 擴充套件的 PostgreSQL 例項。
| 您可以透過 Docker Compose 或 Testcontainers 將 PGvector 資料庫作為 Spring Boot 開發服務執行。另外,設定本地 Postgres/PGVector 附錄展示瞭如何使用 Docker 容器在本地設定資料庫。 |
在啟動時,如果明確啟用了模式初始化功能,PgVectorStore 將嘗試安裝所需的資料庫擴充套件,並在不存在的情況下建立所需的帶有索引的 vector_store 表。
或者,您可以手動執行此操作,如下所示:
CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS hstore; CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE TABLE IF NOT EXISTS vector_store ( id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, content text, metadata json, embedding vector(1536) // 1536 is the default embedding dimension ); CREATE INDEX ON vector_store USING HNSW (embedding vector_cosine_ops);
如果使用不同的維度,請將 1536 替換為實際的嵌入維度。PGvector 對 HNSW 索引最多支援 2000 個維度。 |
接下來,如果需要,為 EmbeddingModel 提供 API 金鑰,以生成 PgVectorStore 儲存的嵌入。
自動配置
|
Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。 |
然後將 PgVectorStore 啟動器依賴項新增到您的專案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-pgvector</artifactId>
</dependency>
或新增到您的 Gradle build.gradle 構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-pgvector'
}
向量儲存實現可以為您初始化所需的模式,但您必須透過在適當的建構函式中指定 initializeSchema 布林值或在 application.properties 檔案中設定 …initialize-schema=true 來選擇啟用此功能。
| 這是一個重大更改!在早期版本的 Spring AI 中,此模式初始化是預設發生的。 |
Vector Store 還需要一個 EmbeddingModel 例項來計算文件的嵌入。您可以選擇其中一種可用的 EmbeddingModel 實現。
例如,要使用 OpenAI EmbeddingModel,請將以下依賴項新增到您的專案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
或新增到您的 Gradle build.gradle 構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}
| 請參閱依賴管理部分,將 Spring AI BOM 新增到您的構建檔案中。請參閱Artifact Repositories部分,將 Maven Central 和/或 Snapshot Repositories 新增到您的構建檔案中。 |
要連線和配置 PgVectorStore,您需要提供例項的訪問詳細資訊。可以透過 Spring Boot 的 application.yml 提供簡單的配置。
spring:
datasource:
url: jdbc:postgresql://:5432/postgres
username: postgres
password: postgres
ai:
vectorstore:
pgvector:
index-type: HNSW
distance-type: COSINE_DISTANCE
dimensions: 1536
max-document-batch-size: 10000 # Optional: Maximum number of documents per batch
| 如果您透過 Docker Compose 或 Testcontainers 將 PGvector 作為 Spring Boot 開發服務執行,則無需配置 URL、使用者名稱和密碼,因為它們由 Spring Boot 自動配置。 |
| 檢視 配置引數 列表,瞭解預設值和配置選項。 |
現在您可以在應用程式中自動裝配 VectorStore 並使用它
@Autowired VectorStore vectorStore;
// ...
List<Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
new Document("The World is Big and Salvation Lurks Around the Corner"),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
// Add the documents to PGVector
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置屬性
您可以在 Spring Boot 配置中使用以下屬性來自定義 PGVector 向量儲存。
| 財產 | 描述 | 預設值 |
|---|---|---|
|
最近鄰搜尋索引型別。選項包括 |
HNSW |
|
搜尋距離型別。預設為 |
COSINE_DISTANCE |
|
嵌入維度。如果未明確指定,PgVectorStore 將從提供的 |
- |
|
在啟動時刪除現有的 |
假 |
|
是否初始化所需的模式 |
假 |
|
向量儲存模式名稱 |
|
|
向量儲存表名 |
|
|
啟用模式和表名驗證,以確保它們是有效且存在的物件。 |
假 |
|
單批處理的最大文件數。 |
10000 |
如果您配置了自定義模式和/或表名,請考慮透過設定 spring.ai.vectorstore.pgvector.schema-validation=true 來啟用模式驗證。這可確保名稱的正確性並降低 SQL 注入攻擊的風險。 |
元資料過濾
您可以利用 PgVector 儲存的通用、可移植的 元資料過濾器。
例如,您可以使用文字表示式語言
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());
或使用 `Filter.Expression` DSL 以程式設計方式
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("author","john", "jill"),
b.eq("article_type", "blog")).build()).build());
| 這些過濾表示式被轉換為 PostgreSQL JSON 路徑表示式,以實現高效的元資料過濾。 |
手動配置
除了使用 Spring Boot 自動配置外,您還可以手動配置 PgVectorStore。為此,您需要將 PostgreSQL 連線和 JdbcTemplate 自動配置依賴項新增到您的專案中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store</artifactId>
</dependency>
| 請參閱 依賴項管理 部分,將 Spring AI BOM 新增到您的構建檔案中。 |
要在您的應用程式中配置 PgVector,您可以使用以下設定
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return PgVectorStore.builder(jdbcTemplate, embeddingModel)
.dimensions(1536) // Optional: defaults to model dimensions or 1536
.distanceType(COSINE_DISTANCE) // Optional: defaults to COSINE_DISTANCE
.indexType(HNSW) // Optional: defaults to HNSW
.initializeSchema(true) // Optional: defaults to false
.schemaName("public") // Optional: defaults to "public"
.vectorTableName("vector_store") // Optional: defaults to "vector_store"
.maxDocumentBatchSize(10000) // Optional: defaults to 10000
.build();
}
在本地執行 Postgres & PGVector 資料庫
docker run -it --rm --name postgres -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres pgvector/pgvector
您可以像這樣連線到此伺服器
psql -U postgres -h localhost -p 5432
訪問原生客戶端
PGVector Store 實現透過 getNativeClient() 方法提供對底層原生 JDBC 客戶端 (JdbcTemplate) 的訪問
PgVectorStore vectorStore = context.getBean(PgVectorStore.class);
Optional<JdbcTemplate> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
JdbcTemplate jdbc = nativeClient.get();
// Use the native client for PostgreSQL-specific operations
}
原生客戶端允許您訪問 VectorStore 介面可能未公開的 PostgreSQL 特定功能和操作。