AS AgentScopeAPI 手册 / API Reference
类class agentscope.rag

VectorStoreBase

class VectorStoreBase(ABC)
源码Source agentscope/rag/_vdb/_vector_store.py:104–365

向量存储基类,把私有文档转化为可检索的知识。位于 agentscope.rag 包,继承自 ABC。核心成员包括 create_collection、delete_collection、has_collection、insert、delete、search。

Subclasses implement the concrete connection and query logic for a specific vector database (Chroma, Milvus, Qdrant, FAISS, etc.).

A single instance is shared across the entire application. The underlying client SDK is expected to handle connection pooling and thread safety internally.

方法摘要Method Summary

方法Method说明Description
async def __aenter__() -> Selfasync
进入异步上下文并返回自身。
Enter the async context — open connections if needed.
async def __aexit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: Any) -> Noneasync
退出异步上下文并清理资源。
Exit the async context — close connections if needed.
async def create_collection(name: str, dimensions: int) -> Noneasyncabstractabstractmethod
创建新的集合(向量索引)。
Create a new collection (vector index).
async def delete_collection(name: str) -> Noneasyncabstractabstractmethod
删除集合。
Delete a collection and all its data.
async def has_collection(name: str) -> boolasyncabstractabstractmethod
判断是否具有集合。
Check whether a collection exists.
async def insert(collection: str, records: list[VectorRecord]) -> Noneasyncabstractabstractmethod
执行插入相关操作。
Insert records into a collection.
async def delete(collection: str, document_id: str, metadata_filter: dict[str, Any] | None = None) -> Noneasyncabstractabstractmethod
删除数据。
Delete all records belonging to one source document.
async def search(collection: str, query_vector: list[float], top_k: int = 5, metadata_filter: dict[str, Any] | None = N…asyncabstractabstractmethod
执行搜索相关操作。
Find the most similar records to a query vector.
async def list_documents(collection: str, metadata_filter: dict[str, Any] | None = None) -> list[DocumentSummary]asyncabstractabstractmethod
列出文档。
List all distinct source documents indexed in a collection.
async def list_chunks(collection: str, document_id: str, *, offset: int = 0, limit: int = 30, metadata_filter: dict[str…async
列出 `list_chunks`。
List the chunks of one source document in ``chunk_index`` order.

方法详情Method Detail

__aenter__

async def __aenter__() -> Self
async

进入异步上下文并返回自身。

The default implementation is a no-op.

返回Returns Self
返回自身。`VectorStoreBase`: `self`.
源码Source agentscope/rag/_vdb/_vector_store.py:119–128

__aexit__

async def __aexit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: Any) -> None
async

退出异步上下文并清理资源。

The default implementation is a no-op.

参数Parameters
名称Name类型Type说明Description
exc_typetype[BaseException] | Noneexc 类型(类型 `type[BaseException] | None`)。The exc type.
exc_valBaseException | Noneexc val(类型 `BaseException | None`)。The exc val.
exc_tbAnyexc tb(类型 `Any`)。The exc tb.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/rag/_vdb/_vector_store.py:130–139

create_collection

async def create_collection(name: str, dimensions: int) -> None
asyncabstractabstractmethod

创建新的集合(向量索引)。

If the collection already exists, implementations should raise or silently no-op depending on the backend's semantics.

参数Parameters
名称Name类型Type说明Description
namestr名称(类型 `str`)。The collection name. Typically, the knowledge base ID.
dimensionsint向量维度(类型 `int`)。The fixed vector dimensionality for this collection. All vectors inserted later must have this many elements.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/rag/_vdb/_vector_store.py:146–162

delete_collection

async def delete_collection(name: str) -> None
asyncabstractabstractmethod

删除集合。

Delete a collection and all its data.

参数Parameters
名称Name类型Type说明Description
namestr集合名称到删除。The collection name to delete.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/rag/_vdb/_vector_store.py:165–171

has_collection

async def has_collection(name: str) -> bool
asyncabstractabstractmethod

判断是否具有集合。

Check whether a collection exists.

参数Parameters
名称Name类型Type说明Description
namestr集合名称到检查。The collection name to check.
返回Returns bool
返回布尔值,表示判断结果。`bool`: `True` if the collection exists.
源码Source agentscope/rag/_vdb/_vector_store.py:174–183

insert

async def insert(collection: str, records: list[VectorRecord]) -> None
asyncabstractabstractmethod

执行插入相关操作。

Insert records into a collection.

参数Parameters
名称Name类型Type说明Description
collectionstr目标集合名称。The target collection name.
recordslist[VectorRecord]records(类型 `list[VectorRecord]`)。The records to insert (each carrying a :class:`Chunk` and its embedding vector).
返回Returns None
无返回值。No value is returned.
源码Source agentscope/rag/_vdb/_vector_store.py:190–203

delete

async def delete(collection: str, document_id: str, metadata_filter: dict[str, Any] | None = None) -> None
asyncabstractabstractmethod

删除数据。

Identifies records by the :attr:VectorRecord.document_id field that backends persist at insertion time.

参数Parameters
名称Name类型Type说明Description
collectionstr目标集合名称。The target collection name.
document_idstr文档标识(类型 `str`)。The source document ID whose records should be removed.
metadata_filter默认default Nonedict[str, Any] | None元数据过滤器(类型 `dict[str, Any] | None`)。Restrict deletion to records whose chunk metadata matches every `key == value` pair in addition to the document ID. `None` or an empty dict deletes all records of the document. Backends must not ignore a non-empty filter.
返回Returns None
无返回值。No value is returned.
源码Source agentscope/rag/_vdb/_vector_store.py:206–230

list_documents

async def list_documents(collection: str, metadata_filter: dict[str, Any] | None = None) -> list[DocumentSummary]
asyncabstractabstractmethod

列出文档。

Aggregates records by :attr:VectorRecord.document_id and returns one :class:DocumentSummary per document.

参数Parameters
名称Name类型Type说明Description
collectionstr目标集合名称。The target collection name.
metadata_filter默认default Nonedict[str, Any] | None元数据过滤器(类型 `dict[str, Any] | None`)。If provided, restrict aggregation to records whose `chunk.metadata` matches every `key == value` pair in this dict. Used together with the search-time filter when an isolation strategy co-locates multiple knowledge bases inside the same collection.
返回Returns list[DocumentSummary]
返回DocumentSummary列表。`list[DocumentSummary]`:
源码Source agentscope/rag/_vdb/_vector_store.py:275–302

list_chunks

async def list_chunks(collection: str, document_id: str, *, offset: int = 0, limit: int = 30, metadata_filter: dict[str, Any] | None = None) -> list[Chunk]
async

列出 list_chunks。

Returns the slice [offset, offset + limit) of the document's chunks, ordered by :attr:Chunk.chunk_index ascending.

参数Parameters
名称Name类型Type说明Description
collectionstr目标集合名称。The target collection name.
document_idstr文档标识(类型 `str`)。The source document whose chunks should be listed — the same value carried by :attr:`VectorRecord.document_id` at insertion time.
offsetkw默认default 0int偏移量(类型 `int`)。Number of leading chunks to skip; equals the first returned chunk's `chunk_index`.
limitkw默认default 30int数量上限(类型 `int`)。Maximum number of chunks to return.
metadata_filterkw默认default Nonedict[str, Any] | None元数据过滤器(类型 `dict[str, Any] | None`)。If provided, restrict listing to records whose `chunk.metadata` matches every `key == value` pair in this dict. Used for defense-in-depth cross-tenant scoping when an isolation strategy co-locates multiple knowledge bases inside the same collection.
返回Returns list[Chunk]
返回分块列表。`list[Chunk]`:
异常Raises
  • 可能抛出 `Exception`。`NotImplementedError`:
  • 可能抛出 `Exception`。If the backend does not support chunk listing.
源码Source agentscope/rag/_vdb/_vector_store.py:308–365
基于官方固定版本源码静态生成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.