llama-index
95 строк · 2.9 Кб
1from typing import List, Optional2
3import fsspec4
5from llama_index.legacy.bridge.pydantic import BaseModel, Field6from llama_index.legacy.schema import BaseNode7from llama_index.legacy.storage.docstore.utils import doc_to_json, json_to_doc8from llama_index.legacy.storage.kvstore import (9FirestoreKVStore as FirestoreCache,10)
11from llama_index.legacy.storage.kvstore import (12MongoDBKVStore as MongoDBCache,13)
14from llama_index.legacy.storage.kvstore import (15RedisKVStore as RedisCache,16)
17from llama_index.legacy.storage.kvstore import (18SimpleKVStore as SimpleCache,19)
20from llama_index.legacy.storage.kvstore.types import (21BaseKVStore as BaseCache,22)
23
24DEFAULT_CACHE_NAME = "llama_cache"25
26
27class IngestionCache(BaseModel):28class Config:29arbitrary_types_allowed = True30
31nodes_key = "nodes"32
33collection: str = Field(34default=DEFAULT_CACHE_NAME, description="Collection name of the cache."35)36cache: BaseCache = Field(default_factory=SimpleCache, description="Cache to use.")37
38# TODO: add async get/put methods?39def put(40self, key: str, nodes: List[BaseNode], collection: Optional[str] = None41) -> None:42"""Put a value into the cache."""43collection = collection or self.collection44
45val = {self.nodes_key: [doc_to_json(node) for node in nodes]}46self.cache.put(key, val, collection=collection)47
48def get(49self, key: str, collection: Optional[str] = None50) -> Optional[List[BaseNode]]:51"""Get a value from the cache."""52collection = collection or self.collection53node_dicts = self.cache.get(key, collection=collection)54
55if node_dicts is None:56return None57
58return [json_to_doc(node_dict) for node_dict in node_dicts[self.nodes_key]]59
60def clear(self, collection: Optional[str] = None) -> None:61"""Clear the cache."""62collection = collection or self.collection63data = self.cache.get_all(collection=collection)64for key in data:65self.cache.delete(key, collection=collection)66
67def persist(68self, persist_path: str, fs: Optional[fsspec.AbstractFileSystem] = None69) -> None:70"""Persist the cache to a directory, if possible."""71if isinstance(self.cache, SimpleCache):72self.cache.persist(persist_path, fs=fs)73else:74print("Warning: skipping persist, only needed for SimpleCache.")75
76@classmethod77def from_persist_path(78cls,79persist_path: str,80collection: str = DEFAULT_CACHE_NAME,81fs: Optional[fsspec.AbstractFileSystem] = None,82) -> "IngestionCache":83"""Create a IngestionCache from a persist directory."""84return cls(85collection=collection,86cache=SimpleCache.from_persist_path(persist_path, fs=fs),87)88
89
90__all__ = [91"SimpleCache",92"RedisCache",93"MongoDBCache",94"FirestoreCache",95]
96