LightRAG icon indicating copy to clipboard operation
LightRAG copied to clipboard

[Bug]: Error while clearing cache: 'PGKVStorage' object has no attribute 'delete'

Open vmahe35 opened this issue 8 months ago • 0 comments

Do you need to file an issue?

  • [x] I have searched the existing issues and this bug is not already filed.
  • [x] I believe this is a legitimate bug, not just a question or feature request.

Describe the bug

I am using instructions provided in the doc in order to clear the cache:

await rag.aclear_cache(modes=["naive", "local", "global", "hybrid", "mix"])

But it displays the following error message in the console:

Error while clearing cache: 'PGKVStorage' object has no attribute 'delete'

Steps to reproduce

Just launch the following code:

import os
import asyncio
import time
from lightrag import LightRAG, QueryParam
from lightrag.utils import EmbeddingFunc
import numpy as np
from lightrag.kg.shared_storage import initialize_pipeline_status
from dotenv import load_dotenv
from sentence_transformers import SentenceTransformer
from lightrag.utils import setup_logger
from google import genai
from google.genai import types
import nest_asyncio

# Apply nest_asyncio to solve event loop issues
nest_asyncio.apply()

setup_logger("lightrag-gemini-demo", level="DEBUG")

# Read env variables from the .env file
load_dotenv()

WORKING_DIR = "./cache/gemini-demo"

if not os.path.exists(WORKING_DIR):
    os.mkdir(WORKING_DIR)


async def llm_model_func(
    prompt, system_prompt=None, history_messages=[], keyword_extraction=False, **kwargs
) -> str:
    # 1. Initialize the GenAI Client with your Gemini API Key
    client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])

    # 2. Combine prompts: system prompt, history, and user prompt
    if history_messages is None:
        history_messages = []

    combined_prompt = ""
    if system_prompt:
        combined_prompt += f"{system_prompt}\n"

    for msg in history_messages:
        # Each msg is expected to be a dict: {"role": "...", "content": "..."}
        combined_prompt += f"{msg['role']}: {msg['content']}\n"

    # Finally, add the new user prompt
    combined_prompt += f"user: {prompt}"

    # 3. Call the Gemini model
    response = client.models.generate_content(
        model=os.environ["GOOGLE_MODEL"],
        contents=[combined_prompt],
        config=types.GenerateContentConfig(max_output_tokens=500, temperature=0.1),
    )

    # 4. Return the response text
    return response.text

async def embedding_func(texts: list[str]) -> np.ndarray:
    model = SentenceTransformer("all-MiniLM-L6-v2")
    embeddings = model.encode(texts, convert_to_numpy=True)
    return embeddings

async def get_embedding_dim():
    test_text = ["This is a test sentence."]
    embedding = await embedding_func(test_text)
    embedding_dim = embedding.shape[1]
    return embedding_dim


# function test
async def test_funcs():
    result = await llm_model_func("How are you?")
    print("llm_model_func: ", result)

    result = await embedding_func(["How are you?"])
    print("embedding_func: ", result)


async def initialize_rag():
    embedding_dimension = await get_embedding_dim()
    print(f"Detected embedding dimension: {embedding_dimension}")

    rag = LightRAG(
        working_dir=WORKING_DIR,
        llm_model_func=llm_model_func,
        embedding_func=EmbeddingFunc(
            embedding_dim=embedding_dimension,
            max_token_size=8192,
            func=embedding_func,
        ),
        llm_model_max_async=4,
        enable_llm_cache_for_entity_extract=True,
        graph_storage="Neo4JStorage", 
        kv_storage="PGKVStorage",
        doc_status_storage="PGDocStatusStorage",
        vector_storage="PGVectorStorage",
    )

    await rag.initialize_storages()
    await initialize_pipeline_status()

    return rag


async def main():
    try:
        # Initialize RAG instance
        rag = await initialize_rag()

        # Clear cache for multiple modes
        await rag.aclear_cache(modes=["naive", "local", "global", "hybrid", "mix"])

        book_file_path = "book.txt"
        with open(book_file_path, "r", encoding="utf-8") as f:
            await rag.ainsert(f.read(), file_paths = [book_file_path])

        # Query using different retrieval modes
        modes = ["naive", "local", "global", "hybrid"]
        query = "What are the top themes in this story?"

        for mode in modes:
            start_time = time.time()
            print(f"-------------------------------------------------------------")
            print(f"Results using {mode} mode:\n")
            print(await rag.aquery(query, param=QueryParam(mode=mode)))
            print(f"Query Time (mode={mode}): {time.time() - start_time} seconds")
        

    except Exception as e:
        print(f"An error occurred: {e}")


if __name__ == "__main__":
    asyncio.run(main())

Expected Behavior

There should not be any error message related to cache cleanup.

LightRAG Config Used

Using:

  • PosgreSQL 17.4
  • Neo4J 5.27.0 (community)

Logs and screenshots

No response

Additional Information

  • LightRAG Version: 3.0
  • Operating System: MacOS (Darwin Kernel Version 24.3.0)
  • Python Version: 3.10.16
  • Related Issues:

vmahe35 avatar Mar 31 '25 10:03 vmahe35