From c590d5878c71cdf815fef882f6329b33dfb05e51 Mon Sep 17 00:00:00 2001 From: yangdx Date: Sat, 29 Mar 2025 03:37:23 +0800 Subject: [PATCH 1/2] refactor: standardize .env loading behavior across modules - Use .env from current folder for each lightrag instance - Allow different .env files for different instances - Make OS env vars take precedence over .env file --- lightrag/api/auth.py | 5 ++++- lightrag/api/lightrag_server.py | 8 ++++---- lightrag/api/run_with_gunicorn.py | 7 ++++--- lightrag/api/utils_api.py | 6 ++++-- lightrag/utils.py | 7 ++++--- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lightrag/api/auth.py b/lightrag/api/auth.py index 50648504..58175b9d 100644 --- a/lightrag/api/auth.py +++ b/lightrag/api/auth.py @@ -5,7 +5,10 @@ from fastapi import HTTPException, status from pydantic import BaseModel from dotenv import load_dotenv -load_dotenv() +# use the .env that is inside the current folder +# allows to use different .env file for each lightrag instance +# the OS environment variables take precedence over the .env file +load_dotenv(dotenv_path=".env", override=False) class TokenPayload(BaseModel): diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index 266b0f92..8110d6d4 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -47,10 +47,10 @@ from lightrag.kg.shared_storage import ( from fastapi.security import OAuth2PasswordRequestForm from lightrag.api.auth import auth_handler -# Load environment variables -# Updated to use the .env that is inside the current folder -# This update allows the user to put a different.env file for each lightrag folder -load_dotenv(".env") +# use the .env that is inside the current folder +# allows to use different .env file for each lightrag instance +# the OS environment variables take precedence over the .env file +load_dotenv(dotenv_path=".env", override=False) # Initialize config parser config = configparser.ConfigParser() diff --git a/lightrag/api/run_with_gunicorn.py b/lightrag/api/run_with_gunicorn.py index 8d71ba36..065a12a1 100644 --- a/lightrag/api/run_with_gunicorn.py +++ b/lightrag/api/run_with_gunicorn.py @@ -11,9 +11,10 @@ from lightrag.api.utils_api import parse_args, display_splash_screen, check_env_ from lightrag.kg.shared_storage import initialize_share_data, finalize_share_data from dotenv import load_dotenv -# Updated to use the .env that is inside the current folder -# This update allows the user to put a different.env file for each lightrag folder -load_dotenv(".env") +# use the .env that is inside the current folder +# allows to use different .env file for each lightrag instance +# the OS environment variables take precedence over the .env file +load_dotenv(dotenv_path=".env", override=False) def check_and_install_dependencies(): diff --git a/lightrag/api/utils_api.py b/lightrag/api/utils_api.py index e88d545f..75e42c92 100644 --- a/lightrag/api/utils_api.py +++ b/lightrag/api/utils_api.py @@ -38,8 +38,10 @@ def check_env_file(): return True -# Load environment variables -load_dotenv() +# use the .env that is inside the current folder +# allows to use different .env file for each lightrag instance +# the OS environment variables take precedence over the .env file +load_dotenv(dotenv_path=".env", override=False) global_args = {"main_args": None} diff --git a/lightrag/utils.py b/lightrag/utils.py index 31f49ee1..44a85425 100644 --- a/lightrag/utils.py +++ b/lightrag/utils.py @@ -19,9 +19,10 @@ import tiktoken from lightrag.prompt import PROMPTS from dotenv import load_dotenv -# Load environment variables -load_dotenv(override=True) - +# use the .env that is inside the current folder +# allows to use different .env file for each lightrag instance +# the OS environment variables take precedence over the .env file +load_dotenv(dotenv_path=".env", override=False) VERBOSE_DEBUG = os.getenv("VERBOSE", "false").lower() == "true" From 539fcd5ebb2f0fa87bd9176c564e1608163d9cf1 Mon Sep 17 00:00:00 2001 From: yangdx Date: Sat, 29 Mar 2025 03:42:51 +0800 Subject: [PATCH 2/2] Update env file warning message to be more specific --- lightrag/api/utils_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lightrag/api/utils_api.py b/lightrag/api/utils_api.py index 75e42c92..02715b87 100644 --- a/lightrag/api/utils_api.py +++ b/lightrag/api/utils_api.py @@ -25,7 +25,7 @@ def check_env_file(): """ if not os.path.exists(".env"): warning_msg = ( - "Warning: .env file not found. Some features may not work properly." + "Warning: Startup directory must contain .env file for multi-instance support." ) ASCIIColors.yellow(warning_msg)