Spaces:
Building
Building
Update config_provider.py
Browse files- config_provider.py +90 -7
config_provider.py
CHANGED
@@ -81,7 +81,10 @@ class ConfigProvider:
|
|
81 |
# Ensure required fields
|
82 |
if 'config' not in config_data:
|
83 |
config_data['config'] = {}
|
84 |
-
|
|
|
|
|
|
|
85 |
# Parse API configs (handle JSON strings)
|
86 |
if 'apis' in config_data:
|
87 |
cls._parse_api_configs(config_data['apis'])
|
@@ -228,8 +231,11 @@ class ConfigProvider:
|
|
228 |
)
|
229 |
|
230 |
except Exception as e:
|
231 |
-
log_error("Failed to save
|
232 |
-
raise
|
|
|
|
|
|
|
233 |
|
234 |
# ===================== Environment Methods =====================
|
235 |
|
@@ -258,7 +264,72 @@ class ConfigProvider:
|
|
258 |
|
259 |
# Save
|
260 |
cls.save(config, username)
|
261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
# ===================== Project Methods =====================
|
263 |
|
264 |
@classmethod
|
@@ -636,8 +707,21 @@ class ConfigProvider:
|
|
636 |
# Update fields
|
637 |
for key, value in update_data.items():
|
638 |
if hasattr(version, key) and key not in ['no', 'created_date', 'created_by', 'published', 'last_update_date']:
|
639 |
-
|
640 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
641 |
version.last_update_date = get_current_timestamp()
|
642 |
version.last_update_user = username
|
643 |
|
@@ -705,7 +789,6 @@ class ConfigProvider:
|
|
705 |
)
|
706 |
|
707 |
# ===================== API Methods =====================
|
708 |
-
|
709 |
@classmethod
|
710 |
def create_api(cls, api_data: dict, username: str) -> APIConfig:
|
711 |
"""Create new API"""
|
|
|
81 |
# Ensure required fields
|
82 |
if 'config' not in config_data:
|
83 |
config_data['config'] = {}
|
84 |
+
|
85 |
+
# Ensure providers exist
|
86 |
+
cls._ensure_providers(config_data)
|
87 |
+
|
88 |
# Parse API configs (handle JSON strings)
|
89 |
if 'apis' in config_data:
|
90 |
cls._parse_api_configs(config_data['apis'])
|
|
|
231 |
)
|
232 |
|
233 |
except Exception as e:
|
234 |
+
log_error(f"Failed to save config", error=str(e))
|
235 |
+
raise ConfigurationError(
|
236 |
+
f"Failed to save configuration: {str(e)}",
|
237 |
+
config_key="service_config.jsonc"
|
238 |
+
)
|
239 |
|
240 |
# ===================== Environment Methods =====================
|
241 |
|
|
|
264 |
|
265 |
# Save
|
266 |
cls.save(config, username)
|
267 |
+
|
268 |
+
@classmethod
|
269 |
+
def _ensure_providers(cls, config_data: Dict[str, Any]) -> None:
|
270 |
+
"""Ensure config has required provider structure"""
|
271 |
+
if 'config' not in config_data:
|
272 |
+
config_data['config'] = {}
|
273 |
+
|
274 |
+
config = config_data['config']
|
275 |
+
|
276 |
+
# Ensure provider settings exist
|
277 |
+
if 'llm_provider' not in config:
|
278 |
+
config['llm_provider'] = {
|
279 |
+
'name': 'spark_cloud',
|
280 |
+
'api_key': '',
|
281 |
+
'endpoint': 'http://localhost:8080',
|
282 |
+
'settings': {}
|
283 |
+
}
|
284 |
+
|
285 |
+
if 'tts_provider' not in config:
|
286 |
+
config['tts_provider'] = {
|
287 |
+
'name': 'no_tts',
|
288 |
+
'api_key': '',
|
289 |
+
'endpoint': None,
|
290 |
+
'settings': {}
|
291 |
+
}
|
292 |
+
|
293 |
+
if 'stt_provider' not in config:
|
294 |
+
config['stt_provider'] = {
|
295 |
+
'name': 'no_stt',
|
296 |
+
'api_key': '',
|
297 |
+
'endpoint': None,
|
298 |
+
'settings': {}
|
299 |
+
}
|
300 |
+
|
301 |
+
# Ensure providers list exists
|
302 |
+
if 'providers' not in config:
|
303 |
+
config['providers'] = [
|
304 |
+
{
|
305 |
+
"type": "llm",
|
306 |
+
"name": "spark_cloud",
|
307 |
+
"display_name": "Spark LLM (Cloud)",
|
308 |
+
"requires_endpoint": True,
|
309 |
+
"requires_api_key": True,
|
310 |
+
"requires_repo_info": False,
|
311 |
+
"description": "Spark Cloud LLM Service"
|
312 |
+
},
|
313 |
+
{
|
314 |
+
"type": "tts",
|
315 |
+
"name": "no_tts",
|
316 |
+
"display_name": "No TTS",
|
317 |
+
"requires_endpoint": False,
|
318 |
+
"requires_api_key": False,
|
319 |
+
"requires_repo_info": False,
|
320 |
+
"description": "Text-to-Speech disabled"
|
321 |
+
},
|
322 |
+
{
|
323 |
+
"type": "stt",
|
324 |
+
"name": "no_stt",
|
325 |
+
"display_name": "No STT",
|
326 |
+
"requires_endpoint": False,
|
327 |
+
"requires_api_key": False,
|
328 |
+
"requires_repo_info": False,
|
329 |
+
"description": "Speech-to-Text disabled"
|
330 |
+
}
|
331 |
+
]
|
332 |
+
|
333 |
# ===================== Project Methods =====================
|
334 |
|
335 |
@classmethod
|
|
|
707 |
# Update fields
|
708 |
for key, value in update_data.items():
|
709 |
if hasattr(version, key) and key not in ['no', 'created_date', 'created_by', 'published', 'last_update_date']:
|
710 |
+
# Handle LLM config
|
711 |
+
if key == 'llm' and isinstance(value, dict):
|
712 |
+
setattr(version, key, LLMConfiguration(**value))
|
713 |
+
# Handle intents
|
714 |
+
elif key == 'intents' and isinstance(value, list):
|
715 |
+
intents = []
|
716 |
+
for intent_data in value:
|
717 |
+
if isinstance(intent_data, dict):
|
718 |
+
intents.append(IntentConfig(**intent_data))
|
719 |
+
else:
|
720 |
+
intents.append(intent_data)
|
721 |
+
setattr(version, key, intents)
|
722 |
+
else:
|
723 |
+
setattr(version, key, value)
|
724 |
+
|
725 |
version.last_update_date = get_current_timestamp()
|
726 |
version.last_update_user = username
|
727 |
|
|
|
789 |
)
|
790 |
|
791 |
# ===================== API Methods =====================
|
|
|
792 |
@classmethod
|
793 |
def create_api(cls, api_data: dict, username: str) -> APIConfig:
|
794 |
"""Create new API"""
|