diff --git a/env.example b/env.example index ac6fb40e8d..db41c7df34 100644 --- a/env.example +++ b/env.example @@ -191,6 +191,21 @@ SUMMARY_LANGUAGE=English ### Entity types that the LLM will attempt to recognize # ENTITY_TYPES='["Person", "Creature", "Organization", "Location", "Event", "Concept", "Method", "Content", "Data", "Artifact", "NaturalObject"]' +### Prompt overrides (optional, env vars) +### Override extraction/query prompts without modifying source code. +### String prompts: set the env var to the full prompt text. +### List prompts (examples): set the env var to a JSON array of strings. +### If not set, built-in defaults are used. See lightrag/prompt.py for defaults. +### Note: this is a lightweight bridge until the full prompt template system (#2623) lands. +# ENTITY_EXTRACTION_SYSTEM_PROMPT= +# ENTITY_EXTRACTION_USER_PROMPT= +# ENTITY_EXTRACTION_EXAMPLES= +# SUMMARIZE_ENTITY_DESCRIPTIONS_PROMPT= +# RAG_RESPONSE_PROMPT= +# NAIVE_RAG_RESPONSE_PROMPT= +# KEYWORDS_EXTRACTION_PROMPT= +# KEYWORDS_EXTRACTION_EXAMPLES= + ### Chunk size for document splitting, 500~1500 is recommended # CHUNK_SIZE=1200 # CHUNK_OVERLAP_SIZE=100 diff --git a/lightrag/prompt.py b/lightrag/prompt.py index dcd829d487..ed318f6992 100644 --- a/lightrag/prompt.py +++ b/lightrag/prompt.py @@ -1,4 +1,6 @@ from __future__ import annotations +import json +import os from typing import Any @@ -430,3 +432,43 @@ """, ] + +# ── Environment variable overrides ─────────────────────────────────── +# Allow users to customize prompts via environment variables without +# modifying this file. Each override is optional — if the env var is +# not set, the hardcoded default above is used. +# +# This is a lightweight bridge until the full prompt template management +# system (see #2623 / #2652) is available. +# +# String prompts: set the env var to the full prompt text. +# List prompts (examples): set the env var to a JSON array of strings. + +_PROMPT_ENV_OVERRIDES: dict[str, str] = { + "entity_extraction_system_prompt": "ENTITY_EXTRACTION_SYSTEM_PROMPT", + "entity_extraction_user_prompt": "ENTITY_EXTRACTION_USER_PROMPT", + "summarize_entity_descriptions": "SUMMARIZE_ENTITY_DESCRIPTIONS_PROMPT", + "rag_response": "RAG_RESPONSE_PROMPT", + "naive_rag_response": "NAIVE_RAG_RESPONSE_PROMPT", + "keywords_extraction": "KEYWORDS_EXTRACTION_PROMPT", +} + +_LIST_PROMPT_ENV_OVERRIDES: dict[str, str] = { + "entity_extraction_examples": "ENTITY_EXTRACTION_EXAMPLES", + "keywords_extraction_examples": "KEYWORDS_EXTRACTION_EXAMPLES", +} + +for _key, _env_var in _PROMPT_ENV_OVERRIDES.items(): + _val = os.environ.get(_env_var) + if _val is not None: + PROMPTS[_key] = _val + +for _key, _env_var in _LIST_PROMPT_ENV_OVERRIDES.items(): + _val = os.environ.get(_env_var) + if _val is not None: + try: + parsed = json.loads(_val) + if isinstance(parsed, list): + PROMPTS[_key] = parsed + except (json.JSONDecodeError, TypeError): + pass # Ignore malformed JSON — keep the default