From 4c746cec947ba484c443cbfafa2adaf581eb5d0c Mon Sep 17 00:00:00 2001 From: Jonathan Lessinger Date: Tue, 2 Jan 2024 12:25:13 -0500 Subject: [PATCH] [AIC-py][editor] file path initialization 1. path is non-optional and has a default value 2. on init, create a new instance and save to the path if the file doesn't exist (otherwise, try to load as usual) Run server with and without explicit path. Observe in the logs that, in either case, when the file doesn't exist it gets created and written to. Run again pointing at an existing valid path (e.g. the one just created). Observe again in the logs that it loads from the path. https://github.com/lastmile-ai/aiconfig/assets/148090348/ee24a0c0-84d8-47aa-b37f-bc9180948b4e --- .../src/aiconfig/editor/server/server_utils.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/python/src/aiconfig/editor/server/server_utils.py b/python/src/aiconfig/editor/server/server_utils.py index 8754b2c52..63dbaa14c 100644 --- a/python/src/aiconfig/editor/server/server_utils.py +++ b/python/src/aiconfig/editor/server/server_utils.py @@ -7,7 +7,7 @@ from dataclasses import dataclass from enum import Enum from types import ModuleType -from typing import Any, Callable, NewType, Optional, Type, TypeVar, cast +from typing import Any, Callable, NewType, Type, TypeVar, cast import lastmile_utils.lib.core.api as core_utils import result @@ -57,7 +57,7 @@ class ServerMode(Enum): class EditServerConfig(core_utils.Record): server_port: int = 8080 - aiconfig_path: Optional[str] = None + aiconfig_path: str = "my_aiconfig.aiconfig.json" log_level: str | int = "INFO" server_mode: ServerMode parsers_module_path: str = "aiconfig_model_registry.py" @@ -205,11 +205,10 @@ def init_server_state(app: Flask, edit_config: EditServerConfig) -> Result[None, state = get_server_state(app) assert state.aiconfig is None - if edit_config.aiconfig_path: + if os.path.exists(edit_config.aiconfig_path): LOGGER.info(f"Loading AIConfig from {edit_config.aiconfig_path}") val_path = get_validated_path(edit_config.aiconfig_path) aiconfig_runtime = val_path.and_then(safe_load_from_disk) - LOGGER.debug(f"{aiconfig_runtime.is_ok()=}") match aiconfig_runtime: case Ok(aiconfig_runtime_): state.aiconfig = aiconfig_runtime_ @@ -219,6 +218,7 @@ def init_server_state(app: Flask, edit_config: EditServerConfig) -> Result[None, LOGGER.error(f"Failed to load AIConfig from {edit_config.aiconfig_path}: {e}") return Err(f"Failed to load AIConfig from {edit_config.aiconfig_path}: {e}") else: + LOGGER.info(f"Creating new AIConfig at {edit_config.aiconfig_path}") aiconfig_runtime = AIConfigRuntime.create() # type: ignore model_ids = ModelParserRegistry.parser_ids() if len(model_ids) > 0: @@ -226,7 +226,14 @@ def init_server_state(app: Flask, edit_config: EditServerConfig) -> Result[None, state.aiconfig = aiconfig_runtime LOGGER.info("Created new AIConfig") - return Ok(None) + try: + aiconfig_runtime.save(edit_config.aiconfig_path) + LOGGER.info(f"Saved new AIConfig to {edit_config.aiconfig_path}") + state.aiconfig = aiconfig_runtime + return Ok(None) + except Exception as e: + LOGGER.error(f"Failed to create new AIConfig at {edit_config.aiconfig_path}: {e}") + return core_utils.ErrWithTraceback(e) def _safe_run_aiconfig_method(aiconfig: AIConfigRuntime, method_name: MethodName, method_args: OpArgs) -> Result[None, str]: