AS AgentScopeAPI 手册 / API Reference

PgVectorStore

public class PgVectorStore implements VDBStoreBase, AutoCloseable
源码Source io/agentscope/core/rag/store/PgVectorStore.java:106–1081

Pg 向量存储,负责持久化与读取数据。位于 io.agentscope.core.rag.store 包,实现 VDBStoreBase、AutoCloseable。核心成员包括 create、add、search、delete、getConnection、getJdbcUrl。

PostgreSQL pgvector database store implementation.

This class provides an interface for storing and searching vectors using PostgreSQL with the pgvector extension. It implements the VDBStoreBase interface to provide a unified API for vector storage operations.

In PostgreSQL with pgvector, we use a table to store the vectors and metadata, including the document ID, chunk ID, content, and custom payload as JSONB.

This implementation uses the pgvector JDBC driver (com.pgvector:pgvector). To use this class, add the following dependencies:

`<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.4</version>
</dependency>
<dependency>
<groupId>com.pgvector</groupId>
<artifactId>pgvector</artifactId>
<version>0.1.6</version>
</dependency>
`

Prerequisites:

  • PostgreSQL 11+ with pgvector extension installed

  • Run: CREATE EXTENSION IF NOT EXISTS vector;

Example usage:

`// Using builder with full configuration (recommended)
try (PgVectorStore store = PgVectorStore.builder()
.jdbcUrl("jdbc:postgresql://localhost:5432/mydb")
.username("postgres")
.password("password")
.schema("my_schema") // optional, defaults to "public"
.tableName("embeddings")
.dimensions(1024)
.distanceType(PgVectorStore.DistanceType.COSINE)
.build()) {
store.add(documents).block();
List<Document> results = store.search(searchDto).block();
}

// Using static factory method (simpler, uses defaults)
try (PgVectorStore store = PgVectorStore.create(
"jdbc:postgresql://localhost:5432/mydb",
"postgres", "password", "embeddings", 1024)) {
store.add(documents).block();
List<Document> results = store.search(searchDto).block();
}
`

Note: The JDBC URL should follow PostgreSQL format:

  • jdbc:postgresql://host:port/database

  • jdbc:postgresql://localhost:5432/vectordb

嵌套类摘要Nested Class Summary

类Class类型Kind说明Description
DistanceType枚举enum
定义Distance 类型的枚举。
Distance types supported by pgvector.
Builder类class
构建器,用于构建并配置目标实例。
Builder for creating PgVectorStore instances with fluent configuration.

方法摘要Method Summary

方法Method说明Description
public static PgVectorStore create(String jdbcUrl, String username, String password, String tableName, int dimensions) throws VectorStoreException
static
创建并返回实例。
Creates a new PgVectorStore with minimal configuration.
public Mono<Void> add(List<Document> documents)
设置添加。
Performs the add operation.
public Mono<List<Document>> search(SearchDocumentDto searchDocumentDto)
设置搜索。
Performs the search operation.
public Mono<Boolean> delete(String docId)
删除数据。
Performs the delete operation.
public Connection getConnection() throws VectorStoreException
获取连接。
Gets the underlying JDBC connection for advanced operations.
public String getJdbcUrl()
获取JDBC URL。
Gets the JDBC URL.
public String getSchema()
获取模式名称。
Gets the schema name.
public String getTableName()
获取 `getTableName`。
Gets the table name.
public int getDimensions()
获取向量维度。
Gets the vector dimensions.
public DistanceType getDistanceType()
获取 `getDistanceType`。
Gets the distance type used for similarity search.
public void close()
关闭连接并释放相关资源。
Closes the PostgreSQL connection and releases all resources.
public boolean isClosed()
判断是否已关闭。
Checks if this store has been closed.
public static Builder builder()
static
返回构建器实例。
Creates a new builder for constructing PgVectorStore instances.

方法详情Method Detail

create

public static PgVectorStore create(String jdbcUrl, String username, String password, String tableName, int dimensions) throws VectorStoreException
static

创建并返回实例。

Creates a new PgVectorStore with minimal configuration.

参数Parameters
名称Name类型Type说明Description
jdbcUrlStringJDBC URL(类型 `String`)。the PostgreSQL JDBC URL (e.g., "jdbc:postgresql://localhost:5432/mydb")
usernameStringusername(类型 `String`)。the database username
passwordString数据库密码。the database password
tableNameStringtable 名称(类型 `String`)。the name of the table to use
dimensionsint向量维度(类型 `int`)。the dimension of vectors that will be stored
返回Returns PgVectorStore
一种新的PgVectorStore 实例。a new PgVectorStore instance
抛出Throws
  • VectorStoreException 可能抛出 `VectorStoreException`。Thrown when the operation cannot be completed.
源码Source io/agentscope/core/rag/store/PgVectorStore.java:256–266

add

public Mono<Void> add(List<Document> documents)

设置添加。

Performs the add operation.

参数Parameters
名称Name类型Type说明Description
documentsList<Document>文档列表(类型 `List`)。The documents.
返回Returns Mono<Void>
返回Mono。Returns mono.
源码Source io/agentscope/core/rag/store/PgVectorStore.java:268–313

delete

public Mono<Boolean> delete(String docId)

删除数据。

Performs the delete operation.

参数Parameters
名称Name类型Type说明Description
docIdString文档标识(类型 `String`)。The doc id.
返回Returns Mono<Boolean>
返回Mono。Returns mono.
源码Source io/agentscope/core/rag/store/PgVectorStore.java:355–378

getConnection

public Connection getConnection() throws VectorStoreException

获取连接。

Gets the underlying JDBC connection for advanced operations.

返回Returns Connection
连接实例。the Connection instance
抛出Throws
  • VectorStoreException 可能抛出 `VectorStoreException`。Thrown when the operation cannot be completed.
源码Source io/agentscope/core/rag/store/PgVectorStore.java:395–398

getJdbcUrl

public String getJdbcUrl()

获取JDBC URL。

Gets the JDBC URL.

返回Returns String
返回字符串结果。the JDBC URL
源码Source io/agentscope/core/rag/store/PgVectorStore.java:792–794

getSchema

public String getSchema()

获取模式名称。

Gets the schema name.

返回Returns String
模式名称。the schema name
源码Source io/agentscope/core/rag/store/PgVectorStore.java:801–803

getTableName

public String getTableName()

获取 getTableName。

Gets the table name.

返回Returns String
返回字符串结果。the table name
源码Source io/agentscope/core/rag/store/PgVectorStore.java:810–812

getDimensions

public int getDimensions()

获取向量维度。

Gets the vector dimensions.

返回Returns int
维度。the dimensions
源码Source io/agentscope/core/rag/store/PgVectorStore.java:819–821

getDistanceType

public DistanceType getDistanceType()

获取 getDistanceType。

Gets the distance type used for similarity search.

返回Returns DistanceType
返回DistanceType。the distance type
源码Source io/agentscope/core/rag/store/PgVectorStore.java:828–830

close

public void close()

关闭连接并释放相关资源。

Closes the PostgreSQL connection and releases all resources.

源码Source io/agentscope/core/rag/store/PgVectorStore.java:852–876

isClosed

public boolean isClosed()

判断是否已关闭。

Checks if this store has been closed.

返回Returns boolean
返回布尔值,表示判断结果。true if the store has been closed, false otherwise
源码Source io/agentscope/core/rag/store/PgVectorStore.java:883–885

builder

public static Builder builder()
static

返回构建器实例。

Creates a new builder for constructing PgVectorStore instances.

返回Returns Builder
一种新的构建器实例。a new builder instance
源码Source io/agentscope/core/rag/store/PgVectorStore.java:1049–1051

嵌套类详情Nested Class Detail

DistanceType

public enum DistanceType

定义Distance 类型的枚举。位于 io.agentscope.core.rag.store 包。核心成员包括 getIndexOps、getOperator、L2、INNER_PRODUCT、COSINE。

Distance types supported by pgvector.

Builder

public static class Builder

构建器,用于构建并配置目标实例。位于 io.agentscope.core.rag.store 包。核心成员包括 jdbcUrl、username、password、schema、tableName、dimensions。

Builder for creating PgVectorStore instances with fluent configuration.

Example usage:

`PgVectorStore store = PgVectorStore.builder()
.jdbcUrl("jdbc:postgresql://localhost:5432/mydb")
.username("postgres")
.password("password")
.schema("my_schema")
.tableName("embeddings")
.dimensions(1024)
.distanceType(PgVectorStore.DistanceType.COSINE)
.connectionTimeoutMs(30000)
.build();
`
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.