Refactor: Move get_env_value from api.config to utils

Relocates the `get_env_value` utility function
from `lightrag.api.config` to `lightrag.utils` to decouple
LightRAG core from API Server
This commit is contained in:
yangdx
2025-05-10 08:58:18 +08:00
parent 7ddfdc69e6
commit 4d57370c94
6 changed files with 38 additions and 37 deletions

View File

@@ -6,6 +6,7 @@ import os
import argparse
import logging
from dotenv import load_dotenv
from lightrag.utils import get_env_value
from lightrag.constants import (
DEFAULT_WOKERS,
@@ -50,37 +51,6 @@ def get_default_host(binding_type: str) -> str:
) # fallback to ollama if unknown
def get_env_value(
env_key: str, default: any, value_type: type = str, special_none: bool = False
) -> any:
"""
Get value from environment variable with type conversion
Args:
env_key (str): Environment variable key
default (any): Default value if env variable is not set
value_type (type): Type to convert the value to
special_none (bool): If True, return None when value is "None"
Returns:
any: Converted value from environment or default
"""
value = os.getenv(env_key)
if value is None:
return default
# Handle special case for "None" string
if special_none and value == "None":
return None
if value_type is bool:
return value.lower() in ("true", "1", "yes", "t", "on")
try:
return value_type(value)
except (ValueError, TypeError):
return default
def parse_args() -> argparse.Namespace:
"""
Parse command line arguments with environment variable fallback

View File

@@ -2,8 +2,7 @@
import os
import logging
from lightrag.kg.shared_storage import finalize_share_data
from lightrag.utils import setup_logger
from lightrag.api.config import get_env_value
from lightrag.utils import setup_logger, get_env_value
from lightrag.constants import (
DEFAULT_LOG_MAX_BYTES,
DEFAULT_LOG_BACKUP_COUNT,

View File

@@ -26,8 +26,8 @@ from .config import (
global_args,
update_uvicorn_mode_config,
get_default_host,
get_env_value,
)
from lightrag.utils import get_env_value
import sys
from lightrag import LightRAG, __version__ as core_version
from lightrag.api import __api_version__

View File

@@ -8,7 +8,8 @@ import sys
import signal
import pipmaster as pm
from lightrag.api.utils_api import display_splash_screen, check_env_file
from lightrag.api.config import global_args, get_env_value
from lightrag.api.config import global_args
from lightrag.utils import get_env_value
from lightrag.kg.shared_storage import initialize_share_data, finalize_share_data
from lightrag.constants import (

View File

@@ -24,7 +24,7 @@ from lightrag.constants import (
DEFAULT_MAX_TOKEN_SUMMARY,
DEFAULT_FORCE_LLM_SUMMARY_ON_MERGE,
)
from lightrag.api.config import get_env_value
from lightrag.utils import get_env_value
from lightrag.kg import (
STORAGES,

View File

@@ -22,7 +22,38 @@ from lightrag.constants import (
DEFAULT_LOG_BACKUP_COUNT,
DEFAULT_LOG_FILENAME,
)
from lightrag.api.config import get_env_value
def get_env_value(
env_key: str, default: any, value_type: type = str, special_none: bool = False
) -> any:
"""
Get value from environment variable with type conversion
Args:
env_key (str): Environment variable key
default (any): Default value if env variable is not set
value_type (type): Type to convert the value to
special_none (bool): If True, return None when value is "None"
Returns:
any: Converted value from environment or default
"""
value = os.getenv(env_key)
if value is None:
return default
# Handle special case for "None" string
if special_none and value == "None":
return None
if value_type is bool:
return value.lower() in ("true", "1", "yes", "t", "on")
try:
return value_type(value)
except (ValueError, TypeError):
return default
# Use TYPE_CHECKING to avoid circular imports
if TYPE_CHECKING: