zilliztech / GPTCache

Semantic cache for LLMs. Fully integrated with LangChain and llama_index.
https://gptcache.readthedocs.io
MIT License
7.18k stars 502 forks source link

[Bug]: 'SSDataManager' object has no attribute 'eviction_manager' #489

Closed soulzhao closed 1 year ago

soulzhao commented 1 year ago

Current Behavior

class SSDataManager(DataManager): """Generate SSDataManage to manager the data.

:param s: CacheStorage to manager the scalar data, it can be generated with :meth:`gptcache.manager.CacheBase`.
:type s: CacheStorage
:param v: VectorBase to manager the vector data, it can be generated with :meth:`gptcache.manager.VectorBase`.
:type v:  VectorBase
:param max_size: the max size for the cache, defaults to 1000.
:type max_size: int
:param clean_size: the size to clean up, defaults to `max_size * 0.2`.
:type clean_size: int
:param eviction: The eviction policy, it is support "LRU" and "FIFO" now, and defaults to "LRU".
:type eviction:  str
"""

def __init__(
    self,
    s: CacheStorage,
    v: VectorBase,
    o: Optional[ObjectBase],
    max_size,
    clean_size,
    policy="LRU",
):
    self.max_size = max_size
    self.clean_size = clean_size
    self.s = s
    self.v = v
    self.o = o
    self.eviction_base = EvictionBase(
        name="memory",
        policy=policy,
        maxsize=max_size,
        clean_size=clean_size,
        on_evict=self._clear,
    )
    # **Bug here**:when the length of self.s.get_ids greater than max_size,the LRU policy will trigger and the _clear function below is called. But self.eviction_manager has not be declared, so it will throw  'SSDataManager' object has no attribute 'eviction_manager' exception.
   # **I think we need to declare self.eviction_manager first, then call self.eviction_base.put  **
    self.eviction_base.put(self.s.get_ids(deleted=False))
    self.eviction_manager = EvictionManager(self.s, self.v)

def _clear(self, marked_keys):
    self.eviction_manager.soft_evict(marked_keys)
    if self.eviction_manager.check_evict():
        self.eviction_manager.delete()

Expected Behavior

No response

Steps To Reproduce

prepare a cache database with data size greater than max_size, 1000

Environment

No response

Anything else?

No response

SimFG commented 1 year ago

@soulzhao the issue should has been fixed in the latest version, so you can try to upgrade the GPTCache

SimFG commented 1 year ago

@soulzhao if the answer has resolved your issue, i will close the issue. If you encounter other problems, welcome to open a new issue.

amarCerta commented 1 year ago

This is happening in the latest version of GPT cache too, I'm using version 0.1.37

Update: I am using mongoDB as cache base, and qdrant db as vector base, adding code snippet for your reference:

openai_embedding = AzureOpenAIEmbedding()
db_parameters = get_db_parameters() # returns mongo DB credentials
    cache_base = CacheBase(
        "mongo",
        mongo_host=db_parameters["host"],
        dbname="gptcache",
        ssl=db_parameters["ssl"],
        **db_parameters["ssl_cert_param"],
    )
    vector_base = VectorBase(
        "qdrant",
        port=Settings.QDRANT_PORT,
        prefer_grpc=False,
        https=True,
        api_key=Settings.QDRANT_API_KEY,
        prefix=None,
        timeout=None,
        host=Settings.QDRANT_HOST,
        collection_name=Settings.GPT_CACHE_EMBEDDING_COLLECTION,
        location=None,  # this is necessary, otherwise it tries to set up connection with local qdrant db setup.
        dimension=openai_embedding.dimension,
    )
    data_manager = get_data_manager(cache_base=cache_base, vector_base=vector_base)
    cache.init(
        cache_enable_func=cache_enable_func,
        pre_embedding_func=pre_embedding_function,
        embedding_func=openai_embedding.to_embeddings,
        data_manager=data_manager,
        similarity_evaluation=KReciprocalEvaluation(vectordb=vector_base, max_distance=1, positive=True),
        config=Config(similarity_threshold=Settings.GPT_CACHE_SIMILARITY_THRESHOLD),
    )
SimFG commented 1 year ago

@amarCerta i will checkout it, and if you can, please give me your test demo code

rahulsunil2 commented 1 year ago

@SimFG Please resolve this issue as it is causing my system to fail continuously, and I am unable to rely on it because I have to keep clearing the cache when the issue occurs.

SimFG commented 1 year ago

@rahulsunil2 Thank you for your attention, I am confirming this problem, because I have a lot of work recently, which causes a bit slow to solve the problem.

SimFG commented 1 year ago

@rahulsunil2 Updating to the latest version should have fixed it. I'm so sorry thay I didn't read the @soulzhao comment in the issue carefully. If the error is not resolved, please contact me in time.

SimFG commented 1 year ago

The bug has been fixed in version 0.1.38. If you come across a new bug, welcome to report it.

rahulsunil2 commented 1 year ago

Thanks a lot for your prompt response @SimFG