Skip to content

Commit

Permalink
[bug fix][AIC-py] add missing global model settings on init
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jonathanlastmileai committed Jan 5, 2024
1 parent 4055228 commit c2f29cf
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions python/src/aiconfig/editor/server/server_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down

0 comments on commit c2f29cf

Please sign in to comment.