HKUDS / LightRAG

"LightRAG: Simple and Fast Retrieval-Augmented Generation"
https://arxiv.org/abs/2410.05779
MIT License
8.35k stars 989 forks source link

ImportError: cannot import name 'EmbeddingFunc' from 'lightrag.utils' #227

Open pleabargain opened 1 week ago

pleabargain commented 1 week ago

running in venv I ran

pip install lightrag-hku

pip list

lightrag is installed

image

here's the error

Traceback (most recent call last):
  File "c:\Users\denni\Documents\LightRAG\examples\claude_ollama.py", line 5, in <module>
    from lightrag import LightRAG, QueryParam
  File "C:\Users\denni\Documents\LightRAG\.venv\Lib\site-packages\lightrag\__init__.py", line 1, in <module>
    from .lightrag import LightRAG as LightRAG, QueryParam as QueryParam
  File "C:\Users\denni\Documents\LightRAG\.venv\Lib\site-packages\lightrag\lightrag.py", line 8, in <module>
    from .llm import (
  File "C:\Users\denni\Documents\LightRAG\.venv\Lib\site-packages\lightrag\llm.py", line 31, in <module>
    from .base import BaseKVStorage
  File "C:\Users\denni\Documents\LightRAG\.venv\Lib\site-packages\lightrag\base.py", line 6, in <module>
    from .utils import EmbeddingFunc
ImportError: cannot import name 'EmbeddingFunc' from 'lightrag.utils' 

here's the code

import logging
from typing import Optional
from contextlib import contextmanager
from lightrag import LightRAG, QueryParam
from lightrag.llm import ollama_model_complete, ollama_embedding
from lightrag.utils.embedding import EmbeddingFunc  # Updated import path

# Configuration
WORKING_DIR = "./dickens"
OLLAMA_HOST = "http://localhost:11434"
MODEL_NAME = "gemma2:2b"
EMBED_MODEL = "nomic-embed-text"
BOOK_PATH = "./book.txt"

# Setup logging with more detailed format
logging.basicConfig(
    format='%(asctime)s - %(levelname)s - %(message)s',
    level=logging.INFO,
    datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)

class RAGManager:
    def __init__(self, working_dir: str):
        self.working_dir = working_dir
        self._ensure_directory()
        self.rag = self._initialize_rag()

    def _ensure_directory(self):
        """Ensure working directory exists"""
        try:
            if not os.path.exists(self.working_dir):
                os.makedirs(self.working_dir)
                logger.info(f"Created working directory: {self.working_dir}")
        except OSError as e:
            logger.error(f"Failed to create directory {self.working_dir}: {e}")
            raise

    def _initialize_rag(self) -> LightRAG:
        """Initialize RAG with configuration"""
        try:
            embedding_func = EmbeddingFunc(
                embedding_dim=768,
                max_token_size=8192,
                func=lambda texts: ollama_embedding(
                    texts,
                    embed_model=EMBED_MODEL,
                    host=OLLAMA_HOST
                ),
            )

            return LightRAG(
                working_dir=self.working_dir,
                llm_model_func=ollama_model_complete,
                llm_model_name=MODEL_NAME,
                llm_model_max_async=4,
                llm_model_max_token_size=32768,
                llm_model_kwargs={
                    "host": OLLAMA_HOST,
                    "options": {"num_ctx": 32768}
                },
                embedding_func=embedding_func,
            )
        except Exception as e:
            logger.error(f"Failed to initialize RAG: {e}")
            raise

    def load_document(self, file_path: str) -> None:
        """Load document into RAG"""
        try:
            with open(file_path, "r", encoding="utf-8") as f:
                content = f.read()
                if not content.strip():
                    raise ValueError("Empty document")
                self.rag.insert(content)
                logger.info(f"Successfully loaded document: {file_path}")
        except FileNotFoundError:
            logger.error(f"Document not found: {file_path}")
            raise
        except Exception as e:
            logger.error(f"Failed to load document {file_path}: {e}")
            raise

    def query_document(self, question: str, mode: str = "hybrid") -> Optional[str]:
        """Query the document with different search modes"""
        try:
            valid_modes = ["naive", "local", "global", "hybrid"]
            if mode not in valid_modes:
                raise ValueError(f"Invalid mode. Must be one of {valid_modes}")

            result = self.rag.query(
                question,
                param=QueryParam(mode=mode)
            )
            logger.info(f"Successfully queried document using {mode} mode")
            return result
        except Exception as e:
            logger.error(f"Query failed with mode {mode}: {e}")
            return None

def main():
    try:
        # Initialize RAG manager
        rag_manager = RAGManager(WORKING_DIR)

        # Load document
        rag_manager.load_document(BOOK_PATH)

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

        for mode in modes:
            logger.info(f"\nQuerying with {mode} mode:")
            result = rag_manager.query_document(question, mode)
            if result:
                print(f"\n{mode.capitalize()} Search Result:")
                print(result)
                print("-" * 80)
            else:
                logger.warning(f"No result returned for {mode} mode")

    except Exception as e:
        logger.error(f"Application failed: {e}")
        raise

if __name__ == "__main__":
    main()
ehab-x99 commented 4 days ago

i have the sane error, were you able to solve it ?

hfyeomans commented 4 days ago

I had this error but makging sure that lightrag was 1. Installed in the same environment I'm trying to run from (multiple python venvs) and 2. the right import statement which is "from lightrag.utils import EmbeddingFunc"

LarFii commented 2 days ago

This issue is likely caused by a conflict with the existing 'lightrag' library on PyPI. To resolve this, we can only use the name 'lightrag-hku' for our library. You can uninstall any existing 'lightrag' installations and try installing from the source instead.