Skip to content

Commit

Permalink
Only create the endpoint if it does not already exist, fixes #182
Browse files Browse the repository at this point in the history
  • Loading branch information
simonransom-dan committed Jul 12, 2024
1 parent d8ee083 commit 26912b8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
39 changes: 23 additions & 16 deletions llmops/common/deployment/provision_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
This module creates Managed endpoint as flow deployment process.
This module creates Managed endpoint as flow deployment process if it does not already exist.
Args:
--base_path: Base path of the use case. Where flows, data,
Expand Down Expand Up @@ -51,6 +51,8 @@ def create_endpoint(
credential=DefaultAzureCredential(),
)

existing_endpoints = ml_client.online_endpoints.list(local=False)

config_file = open(real_config)
endpoint_config = json.load(config_file)

Expand All @@ -59,21 +61,26 @@ def create_endpoint(
if env_name == elem["ENV_NAME"]:
endpoint_name = elem["ENDPOINT_NAME"]
endpoint_desc = elem["ENDPOINT_DESC"]
endpoint = ManagedOnlineEndpoint(
name=endpoint_name,
description=endpoint_desc,
auth_mode="key",
tags={"build_id": build_id} if build_id else {},
properties={
"enforce_access_to_default_secret_stores": True,
},

)

logger.info(f"Creating endpoint {endpoint.name}")
ml_client.online_endpoints.begin_create_or_update(
endpoint=endpoint
).result()

# See if endpoint with name endpoint_name already exists
endpoint = next((e for e in existing_endpoints if e.name == endpoint_name), None)

if endpoint is None:
logger.info(f"Creating endpoint {endpoint_name}")
endpoint = ManagedOnlineEndpoint(
name=endpoint_name,
description=endpoint_desc,
auth_mode="key",
tags={"build_id": build_id} if build_id else {},
properties={
"enforce_access_to_default_secret_stores": True,
})

ml_client.online_endpoints.begin_create_or_update(
endpoint=endpoint
).result()
else:
logger.info(f"Skipping create as endpoint {endpoint.name} already exists")

logger.info(f"Obtaining endpoint {endpoint.name} identity")
principal_id = ml_client.online_endpoints.get(
Expand Down
34 changes: 33 additions & 1 deletion tests/test_create_aml_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _set_required_env_vars():
monkeypatch.setenv("WORKSPACE_NAME", "TEST_WORKSPACE_NAME")


def test_create_provision_endpoint():
def test_create_provision_endpoint_when_not_exists():
"""Test create_provision_endpoint."""
env_name = "dev"
endpoint_name = "test-endpoint"
Expand All @@ -31,6 +31,8 @@ def test_create_provision_endpoint():
# Mock the MLClient
ml_client_instance = Mock()
mock_ml_client.return_value = ml_client_instance
ml_client_instance.online_endpoints.list.return_value = []

# Create the endpoint
create_endpoint(env_name, str(RESOURCE_PATH))

Expand All @@ -56,3 +58,33 @@ def test_create_provision_endpoint():
assert created_endpoint.name == endpoint_name
assert created_endpoint.description == endpoint_description
assert created_endpoint.auth_mode == "key"

def test_create_provision_endpoint_when_exists():
"""Test create_provision_endpoint."""
env_name = "dev"
endpoint_name = "test-endpoint"
with patch(
"llmops.common.deployment.provision_endpoint.MLClient"
) as mock_ml_client:
# Mock the MLClient
ml_client_instance = Mock()
mock_ml_client.return_value = ml_client_instance

mock_endpoint = Mock()
mock_endpoint.name = endpoint_name
ml_client_instance.online_endpoints.list.return_value = [
mock_endpoint
]

# Create the endpoint
create_endpoint(env_name, str(RESOURCE_PATH))

# Assert online_endpoints.begin_create_or_update is called once
create_endpoint_calls = (
ml_client_instance.online_endpoints.begin_create_or_update
)

# Endpoint should not be created if it already exists as it would
# set the traffic to zero for existing deployments and there are
# no properties we need to update on the endpoint
assert create_endpoint_calls.call_count == 0

0 comments on commit 26912b8

Please sign in to comment.