Skip to content

Commit

Permalink
fix: Verify prompt payload exists before accessing it
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Nov 21, 2024
1 parent 264f99e commit 01da8f1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ldai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def model_config(
all_variables.update(variables)
all_variables['ldctx'] = context

if isinstance(variation['prompt'], list) and all(
prompt = None
if 'prompt' in variation and isinstance(variation['prompt'], list) and all(
isinstance(entry, dict) for entry in variation['prompt']
):
prompt = [
Expand Down
48 changes: 48 additions & 0 deletions ldai/testing/test_model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ def td() -> TestData:
.variation_for_all(0)
)

td.update(
td.flag('initial-config-disabled')
.variations(
{
'_ldMeta': {'enabled': False},
},
{
'_ldMeta': {'enabled': True},
}
)
.variation_for_all(0)
)

td.update(
td.flag('initial-config-enabled')
.variations(
{
'_ldMeta': {'enabled': False},
},
{
'_ldMeta': {'enabled': True},
}
)
.variation_for_all(1)
)

return td


Expand Down Expand Up @@ -188,3 +214,25 @@ def test_model_config_disabled(ldai_client: LDAIClient, tracker):
assert config.model.id == 'fakeModel'
assert config.model.temperature == 0.1
assert config.model.max_tokens is None


def test_model_initial_config_disabled(ldai_client: LDAIClient, tracker):
context = Context.create('user-key')
default_value = AIConfig(tracker=tracker, enabled=False, model=ModelConfig('fake-model'), prompt=[])

config = ldai_client.model_config('initial-config-disabled', context, default_value, {})

assert config.enabled is False
assert config.model is None
assert config.prompt is None


def test_model_initial_config_enabled(ldai_client: LDAIClient, tracker):
context = Context.create('user-key')
default_value = AIConfig(tracker=tracker, enabled=False, model=ModelConfig('fake-model'), prompt=[])

config = ldai_client.model_config('initial-config-enabled', context, default_value, {})

assert config.enabled is True
assert config.model is None
assert config.prompt is None

0 comments on commit 01da8f1

Please sign in to comment.