文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
API 参考

Caching (缓存)

检查来自给定 URL 的任何文档是否已存在于存储中。

模块 cache_checker

CacheChecker

根据每个文档元数据中指定的字段,检查文档是否存在于 Document Store 中。

如果找到匹配的文档,它们将作为“命中”返回。如果未在缓存中找到,这些项目将作为“未命中”返回。

使用示例

from haystack import Document
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.caching.cache_checker import CacheChecker

docstore = InMemoryDocumentStore()
documents = [
    Document(content="doc1", meta={"url": "https://example.com/1"}),
    Document(content="doc2", meta={"url": "https://example.com/2"}),
    Document(content="doc3", meta={"url": "https://example.com/1"}),
    Document(content="doc4", meta={"url": "https://example.com/2"}),
]
docstore.write_documents(documents)
checker = CacheChecker(docstore, cache_field="url")
results = checker.run(items=["https://example.com/1", "https://example.com/5"])
assert results == {"hits": [documents[0], documents[2]], "misses": ["https://example.com/5"]}

CacheChecker.__init__

def __init__(document_store: DocumentStore, cache_field: str)

创建 CacheChecker 组件。

参数:

  • document_store:用于检查特定文档是否存在的 Document Store。
  • cache_field:用于检查缓存命中情况的文档元数据字段名称。

CacheChecker.to_dict

def to_dict() -> dict[str, Any]

将组件序列化为字典。

返回值:

包含序列化数据的字典。

CacheChecker.from_dict

@classmethod
def from_dict(cls, data: dict[str, Any]) -> "CacheChecker"

从字典反序列化组件。

参数:

  • data: 要反序列化的字典。

返回值:

反序列化后的组件。

CacheChecker.run

@component.output_types(hits=list[Document], misses=list)
def run(items: list[Any])

检查与指定的缓存字段相关的任何文档是否已存在于存储中。

参数:

  • items:要与缓存字段进行比较的值。

返回值:

一个包含两个键的字典

  • hits - 与至少一个项目匹配的文档。
  • misses - 任何文档中都不存在的项目。