From 2e62cf1c6687c1016a3b5ad4e7977b61bd90e6c9 Mon Sep 17 00:00:00 2001 From: yangdx Date: Tue, 21 Jan 2025 00:50:11 +0800 Subject: [PATCH 1/5] Add document indexing during FastAPI startup, Fix docx package name in requirements - Initialize DocumentManager earlier - Add lifespan context manager - Scan and index documents on startup - Fix docx package name in requirements --- lightrag/api/lightrag_server.py | 35 ++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index 52881860..d5dc9682 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -574,6 +574,29 @@ def create_app(args): # Check if API key is provided either through env var or args api_key = os.getenv("LIGHTRAG_API_KEY") or args.key + # Initialize document manager + doc_manager = DocumentManager(args.input_dir) + + @asynccontextmanager + async def lifespan(app: FastAPI): + """Lifespan context manager for startup and shutdown events""" + # Startup logic + try: + new_files = doc_manager.scan_directory() + for file_path in new_files: + try: + await index_file(file_path) + except Exception as e: + trace_exception(e) + logging.error(f"Error indexing file {file_path}: {str(e)}") + + logging.info(f"Indexed {len(new_files)} documents from {args.input_dir}") + except Exception as e: + logging.error(f"Error during startup indexing: {str(e)}") + yield + # Cleanup logic (if needed) + pass + # Initialize FastAPI app = FastAPI( title="LightRAG API", @@ -583,6 +606,7 @@ def create_app(args): else "", version=__api_version__, openapi_tags=[{"name": "api"}], + lifespan=lifespan ) # Add CORS middleware @@ -600,9 +624,6 @@ def create_app(args): # Create working directory if it doesn't exist Path(args.working_dir).mkdir(parents=True, exist_ok=True) - # Initialize document manager - doc_manager = DocumentManager(args.input_dir) - async def openai_alike_model_complete( prompt, system_prompt=None, @@ -737,8 +758,8 @@ async def index_file(file_path: Union[str, Path]) -> None: content += page.extract_text() + "\n" case ".docx": - if not pm.is_installed("docx"): - pm.install("docx") + if not pm.is_installed("python-docx"): + pm.install("python-docx") from docx import Document # Word document handling @@ -971,8 +992,8 @@ async def insert_file(file: UploadFile = File(...), description: str = Form(None content += page.extract_text() + "\n" case ".docx": - if not pm.is_installed("docx"): - pm.install("docx") + if not pm.is_installed("python-docx"): + pm.install("python-docx") from docx import Document from io import BytesIO From 81a0a7cff0e72ce9490e3d593913c8d64257a5e9 Mon Sep 17 00:00:00 2001 From: yangdx Date: Tue, 21 Jan 2025 01:03:37 +0800 Subject: [PATCH 2/5] fixed linting --- lightrag/api/lightrag_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index d5dc9682..fd656be0 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -606,7 +606,7 @@ async def lifespan(app: FastAPI): else "", version=__api_version__, openapi_tags=[{"name": "api"}], - lifespan=lifespan + lifespan=lifespan, ) # Add CORS middleware From ae824f287876601121dc6f8724adf52cf91ad994 Mon Sep 17 00:00:00 2001 From: yangdx Date: Tue, 21 Jan 2025 02:48:07 +0800 Subject: [PATCH 3/5] fix linux sample service file script name error --- lightrag-server.service.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightrag-server.service.example b/lightrag-server.service.example index 3e02935e..9f3ee7a6 100644 --- a/lightrag-server.service.example +++ b/lightrag-server.service.example @@ -9,7 +9,7 @@ User=netman MemoryHigh=8G MemoryMax=12G WorkingDirectory=/home/netman/lightrag-xyj -ExecStart=/home/netman/lightrag-xyj/start_lightrag_server.sh +ExecStart=/home/netman/lightrag-xyj/start_lightrag.sh Restart=always RestartSec=10 From 8a05ff459f48070c5f35f8dff1f7ee2843edc300 Mon Sep 17 00:00:00 2001 From: yangdx Date: Tue, 21 Jan 2025 03:13:13 +0800 Subject: [PATCH 4/5] Add environment variable for Ollama model tag --- lightrag/api/lightrag_server.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index fd656be0..30423d0e 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -51,8 +51,8 @@ def estimate_tokens(text: str) -> int: # Constants for emulated Ollama model information LIGHTRAG_NAME = "lightrag" -LIGHTRAG_TAG = "latest" -LIGHTRAG_MODEL = "lightrag:latest" +LIGHTRAG_TAG = os.getenv("OLLAMA_EMULATING_MODEL_TAG", "latest") +LIGHTRAG_MODEL = f"{LIGHTRAG_NAME}:{LIGHTRAG_TAG}" LIGHTRAG_SIZE = 7365960935 # it's a dummy value LIGHTRAG_CREATED_AT = "2024-01-15T00:00:00Z" LIGHTRAG_DIGEST = "sha256:lightrag" @@ -161,6 +161,8 @@ def display_splash_screen(args: argparse.Namespace) -> None: # System Configuration ASCIIColors.magenta("\nšŸ› ļø System Configuration:") + ASCIIColors.white(" ā”œā”€ Ollama Emulating Model: ", end="") + ASCIIColors.yellow(f"{LIGHTRAG_MODEL}") ASCIIColors.white(" ā”œā”€ Log Level: ", end="") ASCIIColors.yellow(f"{args.log_level}") ASCIIColors.white(" ā”œā”€ Timeout: ", end="") From a85c148249b07572883b1c18a29c1bc18a59f66e Mon Sep 17 00:00:00 2001 From: yangdx Date: Tue, 21 Jan 2025 03:31:50 +0800 Subject: [PATCH 5/5] Add sample environment variable for Ollama model tag --- .env.example | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.env.example b/.env.example index 9217eb5d..8f043e7c 100644 --- a/.env.example +++ b/.env.example @@ -64,3 +64,7 @@ LOG_LEVEL=INFO # AZURE_EMBEDDING_DEPLOYMENT=text-embedding-3-large # AZURE_EMBEDDING_API_VERSION=2023-05-15 + + +# Ollama Emulating Model Tag +# OLLAMA_EMULATING_MODEL_TAG=latest