From c2f29cfa5bce92d248b61f8c392e12599ee13748 Mon Sep 17 00:00:00 2001 From: Jonathan Lessinger Date: Thu, 4 Jan 2024 21:47:34 -0500 Subject: [PATCH] [bug fix][AIC-py] add missing global model settings on init Adding prompt with a model name to a new AIConfig makes it invalid because essentially the name is a dangling foreign key to the AIConfig's global model settings mapping. Immediate fix: When we add prompt_1, also add a default global mapping entry for each registered model. Better fix: Reject updates to AIConfig that would make it invalid. (inside add_prompt). However, this could break existing code that adds a prompt and then immediately adds the missing model, like we're doing in this PR. In the future, we should make AIConfig more constrained by definition so that this kind of invalid object can't exist to begin with. Test: run server, type into prompt_1, it runs --- .../aiconfig/editor/server/server_utils.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/python/src/aiconfig/editor/server/server_utils.py b/python/src/aiconfig/editor/server/server_utils.py index f48704a27..568d31ea5 100644 --- a/python/src/aiconfig/editor/server/server_utils.py +++ b/python/src/aiconfig/editor/server/server_utils.py @@ -219,10 +219,9 @@ def init_server_state(app: Flask, edit_config: EditServerConfig) -> Result[None, 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 + aiconfig_runtime = _create_new_aiconfig() 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]))) + 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") @@ -236,6 +235,20 @@ def init_server_state(app: Flask, edit_config: EditServerConfig) -> Result[None, return core_utils.ErrWithTraceback(e) +def _create_new_aiconfig() -> AIConfigRuntime: + aiconfig_runtime = AIConfigRuntime.create() # type: ignore + model_ids = ModelParserRegistry.parser_ids() + if len(model_ids) > 0: + LOGGER.info(f"{model_ids=}") + for model_id in model_ids: + if aiconfig_runtime.metadata.models is None: + aiconfig_runtime.metadata.models = {} + if model_id not in aiconfig_runtime.metadata.models: + aiconfig_runtime.add_model(model_id, {"model": model_id}) + + return aiconfig_runtime + + def _safe_run_aiconfig_method(aiconfig: AIConfigRuntime, method_name: MethodName, method_args: OpArgs) -> Result[None, str]: # TODO: use `out` try: