Skip to content

Commit

Permalink
[AIC-py][editor] file path initialization
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jonathanlastmileai committed Jan 2, 2024
1 parent dd273b3 commit 4c746ce
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions python/src/aiconfig/editor/server/server_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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_
Expand All @@ -219,14 +218,22 @@ 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:
aiconfig_runtime.add_prompt("prompt_1", Prompt(name="prompt_1", input="", metadata=PromptMetadata(model=model_ids[0])))

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]:
Expand Down

0 comments on commit 4c746ce

Please sign in to comment.