@@ -29,19 +29,29 @@ class DbConfig:
2929 connection_string : str
3030
3131
32- def _get_config_value (key : str , config_raw : dict , default : Optional [str ] = None ) -> str :
33- """Get configuration value with priority: ENV > .env > JSON."""
34- # First check environment variables
35- env_value = os .getenv (key )
36- if env_value is not None :
37- return env_value
38-
39- # Then check nested JSON structure
32+ def _get_config_value (key : str , config_raw : dict , saved_config_raw : dict , default : Optional [str ] = None ) -> str :
33+ """Get configuration value with priority: saved.json > ENV > .env > JSON."""
34+ # First check saved configuration (highest priority)
4035 keys = key .lower ().split ('_' )
4136 # Skip the 'gc_qa_rag' prefix for JSON lookup
4237 if keys [0 ] == 'gc' and keys [1 ] == 'qa' and keys [2 ] == 'rag' :
4338 keys = keys [3 :]
4439
40+ # Check saved config first
41+ current = saved_config_raw
42+ try :
43+ for k in keys :
44+ current = current [k ]
45+ return str (current )
46+ except (KeyError , TypeError ):
47+ pass
48+
49+ # Then check environment variables
50+ env_value = os .getenv (key )
51+ if env_value is not None :
52+ return env_value
53+
54+ # Then check nested JSON structure
4555 current = config_raw
4656 try :
4757 for k in keys :
@@ -58,10 +68,25 @@ def _get_config_value(key: str, config_raw: dict, default: Optional[str] = None)
5868
5969
6070def _get_llm_config (
61- config_raw : dict , config_type : str , default_config : Optional [LlmConfig ] = None
71+ config_raw : dict , saved_config_raw : dict , config_type : str , default_config : Optional [LlmConfig ] = None
6272) -> LlmConfig :
6373 """Get LLM configuration with fallback to default config if specified config doesn't exist."""
64- # Try to get from environment variables first
74+ # Try to get from saved config first (highest priority)
75+ if config_type in saved_config_raw :
76+ saved_config = saved_config_raw [config_type ]
77+ api_key = saved_config .get ("api_key" )
78+ api_base = saved_config .get ("api_base" )
79+ model_name = saved_config .get ("model_name" )
80+
81+ # If all saved config values are set, use them
82+ if api_key and api_base and model_name :
83+ return LlmConfig (
84+ api_key = api_key ,
85+ api_base = api_base ,
86+ model_name = model_name ,
87+ )
88+
89+ # Try to get from environment variables
6590 env_prefix = f"GC_QA_RAG_{ config_type .upper ()} "
6691 api_key = os .getenv (f"{ env_prefix } _API_KEY" )
6792 api_base = os .getenv (f"{ env_prefix } _API_BASE" )
@@ -94,9 +119,9 @@ def _get_llm_config(
94119
95120 # Last resort: use default LLM config from env/JSON
96121 return LlmConfig (
97- api_key = api_key or _get_config_value ("GC_QA_RAG_LLM_DEFAULT_API_KEY" , config_raw ),
98- api_base = api_base or _get_config_value ("GC_QA_RAG_LLM_DEFAULT_API_BASE" , config_raw , "https://dashscope.aliyuncs.com/compatible-mode/v1" ),
99- model_name = model_name or _get_config_value ("GC_QA_RAG_LLM_DEFAULT_MODEL_NAME" , config_raw , "qwen-plus" ),
122+ api_key = api_key or _get_config_value ("GC_QA_RAG_LLM_DEFAULT_API_KEY" , config_raw , saved_config_raw ),
123+ api_base = api_base or _get_config_value ("GC_QA_RAG_LLM_DEFAULT_API_BASE" , config_raw , saved_config_raw , "https://dashscope.aliyuncs.com/compatible-mode/v1" ),
124+ model_name = model_name or _get_config_value ("GC_QA_RAG_LLM_DEFAULT_MODEL_NAME" , config_raw , saved_config_raw , "qwen-plus" ),
100125 )
101126
102127
@@ -130,27 +155,38 @@ def from_environment(cls, environment: str) -> "Config":
130155 except json .JSONDecodeError as e :
131156 print (f"Warning: Invalid JSON in configuration file: { e } " )
132157
158+ # Try to load saved config (highest priority)
159+ saved_config_raw = {}
160+ saved_config_path = Path (f".config.{ environment } .saved.json" )
161+ if saved_config_path .exists ():
162+ try :
163+ with open (saved_config_path ) as f :
164+ saved_config_raw = json .load (f )
165+ print (f"Loaded saved configuration from: { saved_config_path } " )
166+ except json .JSONDecodeError as e :
167+ print (f"Warning: Invalid JSON in saved configuration file: { e } " )
168+
133169 # Initialize default config first
134- llm_default = _get_llm_config (config_raw , "llm_default" )
170+ llm_default = _get_llm_config (config_raw , saved_config_raw , "llm_default" )
135171
136172 return cls (
137173 environment = environment ,
138174 llm_default = llm_default ,
139- llm_summary = _get_llm_config (config_raw , "llm_summary" , llm_default ),
140- llm_think = _get_llm_config (config_raw , "llm_think" , llm_default ),
141- llm_query = _get_llm_config (config_raw , "llm_query" , llm_default ),
142- llm_research = _get_llm_config (config_raw , "llm_research" , llm_default ),
175+ llm_summary = _get_llm_config (config_raw , saved_config_raw , "llm_summary" , llm_default ),
176+ llm_think = _get_llm_config (config_raw , saved_config_raw , "llm_think" , llm_default ),
177+ llm_query = _get_llm_config (config_raw , saved_config_raw , "llm_query" , llm_default ),
178+ llm_research = _get_llm_config (config_raw , saved_config_raw , "llm_research" , llm_default ),
143179 embedding = EmbeddingConfig (
144- api_key = _get_config_value ("GC_QA_RAG_EMBEDDING_API_KEY" , config_raw )
180+ api_key = _get_config_value ("GC_QA_RAG_EMBEDDING_API_KEY" , config_raw , saved_config_raw )
145181 ),
146182 vector_db = VectorDbConfig (
147- host = _get_config_value ("GC_QA_RAG_VECTOR_DB_HOST" , config_raw , "http://rag_qdrant_container:6333" )
183+ host = _get_config_value ("GC_QA_RAG_VECTOR_DB_HOST" , config_raw , saved_config_raw , "http://rag_qdrant_container:6333" )
148184 ),
149185 db = DbConfig (
150- connection_string = _get_config_value ("GC_QA_RAG_DB_CONNECTION_STRING" , config_raw , "mysql+mysqlconnector://root:12345678@rag_mysql_container:3306/search_db" )
186+ connection_string = _get_config_value ("GC_QA_RAG_DB_CONNECTION_STRING" , config_raw , saved_config_raw , "mysql+mysqlconnector://root:12345678@rag_mysql_container:3306/search_db" )
151187 ),
152- log_path = _get_config_value ("GC_QA_RAG_LOG_PATH" , config_raw , user_log_dir ("gc-qa-rag-server" , ensure_exists = True )),
153- etl_base_url = _get_config_value ("GC_QA_RAG_ETL_BASE_URL" , config_raw , "http://host.docker.internal:8001" ),
188+ log_path = _get_config_value ("GC_QA_RAG_LOG_PATH" , config_raw , saved_config_raw , user_log_dir ("gc-qa-rag-server" , ensure_exists = True )),
189+ etl_base_url = _get_config_value ("GC_QA_RAG_ETL_BASE_URL" , config_raw , saved_config_raw , "http://host.docker.internal:8001" ),
154190 )
155191
156192
0 commit comments