document_stores.implementations.haystack_store

基于Haystack框架的文档存储实现,主要使用InMemoryDocumentStore。

该模块提供了HaystackDocumentStore类,封装了Haystack的InMemoryDocumentStore, 用于存储文档及其向量表示,并支持未来迁移到其他存储引擎。

Functions

create_document_store([config, config_manager])

创建文档存储实例的工厂函数。

Classes

HaystackDocumentStore([config, config_manager])

基于Haystack框架的文档存储类,支持InMemoryDocumentStore和MilvusDocumentStore。

class document_stores.implementations.haystack_store.HaystackDocumentStore(config=None, config_manager=None)[source]

Bases: object

基于Haystack框架的文档存储类,支持InMemoryDocumentStore和MilvusDocumentStore。

该类提供统一的接口供系统其他组件使用,并根据配置自动选择合适的存储后端。 支持文档的添加、检索、更新和删除,可通过配置文件调整参数。

支持的存储后端: - InMemoryDocumentStore: 内存存储,适用于开发和小规模场景 - MilvusDocumentStore: Milvus向量数据库,适用于生产环境和大规模场景

__init__(config=None, config_manager=None)[source]

初始化HaystackDocumentStore对象。

Parameters:
  • config (Dict[str, Any] | None) – 文档存储配置字典,默认为None

  • config_manager (ConfigManager | None) – 配置管理器对象,默认为None

add_document(document, embedding=None)[source]

将系统Document对象添加到文档存储中。

Parameters:
  • document (Document) – 系统Document对象

  • embedding (List[float] | None) – 文档的向量表示,默认为None

Returns:

如果成功添加则返回True,否则返回False

Return type:

bool

add_documents(documents, embeddings=None)[source]

批量将系统Document对象添加到文档存储中。

Parameters:
  • documents (List[Document]) – 系统Document对象列表

  • embeddings (List[List[float]] | None) – 文档向量表示列表,默认为None

Returns:

包含成功数量和失败数量的元组

Return type:

Tuple[int, int]

get_document(document_id)[source]

根据文档ID获取Haystack文档。

Parameters:

document_id (str) – 系统文档ID

Returns:

Haystack文档对象,如果不存在则返回None

Return type:

Document | None

update_document(document, embedding=None)[source]

更新Haystack文档存储中的文档。

Parameters:
  • document (Document) – 系统Document对象

  • embedding (List[float] | None) – 文档的新向量表示,默认为None

Returns:

如果成功更新则返回True,否则返回False

Return type:

bool

delete_document(document_id)[source]

从Haystack文档存储中删除文档。

Parameters:

document_id (str) – 系统文档ID

Returns:

如果成功删除则返回True,否则返回False

Return type:

bool

clear()[source]

清空Haystack文档存储中的所有文档。

Returns:

如果成功清空则返回True,否则返回False

Return type:

bool

get_document_count()[source]

获取Haystack文档存储中的文档数量。

Returns:

文档数量

Return type:

int

save_state(file_path)[source]

保存文档存储状态到文件,便于恢复。

Parameters:

file_path (str) – 状态文件路径

Returns:

如果成功保存则返回True,否则返回False

Return type:

bool

load_state(file_path)[source]

从文件加载文档存储状态。

Parameters:

file_path (str) – 状态文件路径

Returns:

如果成功加载则返回True,否则返回False

Return type:

bool

get_document_store()[source]

获取底层文档存储对象。

Returns:

InMemoryDocumentStore或MilvusDocumentStore对象

Return type:

InMemoryDocumentStore

get_haystack_store()[source]

获取Haystack InMemoryDocumentStore对象(用于兼容性)。

Returns:

Haystack InMemoryDocumentStore对象

Return type:

InMemoryDocumentStore

get_milvus_store()[source]

获取MilvusDocumentStore对象。

Returns:

MilvusDocumentStore对象或None

Return type:

MilvusDocumentStore | None

is_using_milvus()[source]

检查是否使用Milvus存储。

Returns:

如果使用Milvus则返回True,否则返回False

Return type:

bool

update_config(config)[source]

更新文档存储配置。

部分配置项更新后需要重新初始化文档存储。

Parameters:

config (Dict[str, Any]) – 新的配置字典

Returns:

如果成功更新则返回True,否则返回False

Return type:

bool

document_stores.implementations.haystack_store.create_document_store(config=None, config_manager=None)[source]

创建文档存储实例的工厂函数。

Parameters:
  • config (Dict[str, Any] | None) – 文档存储配置字典,默认为None

  • config_manager (ConfigManager | None) – 配置管理器对象,默认为None

Returns:

HaystackDocumentStore实例

Return type:

HaystackDocumentStore