Skip to content

Commit c4998d9

Browse files
fix aihub
1 parent 5ad6fed commit c4998d9

File tree

1 file changed

+39
-131
lines changed

1 file changed

+39
-131
lines changed

infra/scripts/aihub_scripts/create_ai_hub.py

Lines changed: 39 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
)
1212
from azure.keyvault.secrets import SecretClient
1313
from azure.identity import DefaultAzureCredential
14-
import time
15-
import logging
16-
17-
# Configure logging
18-
logging.basicConfig(level=logging.INFO)
19-
logger = logging.getLogger(__name__)
2014

2115
def get_secrets_from_kv(kv_name, secret_name):
2216
# Set the name of the Azure Key Vault
@@ -43,8 +37,6 @@ def get_secrets_from_kv(kv_name, secret_name):
4337
deployment_name = 'draftsinference-' + 'solutionname_to-be-replaced'
4438
solutionLocation = 'solutionlocation_to-be-replaced'
4539

46-
logger.info(f"Starting AI Hub creation for {aihub_name} in {solutionLocation}")
47-
4840
# Open AI Details
4941
open_ai_key = get_secrets_from_kv(key_vault_name, "AZURE-OPENAI-KEY")
5042
open_ai_res_name = (
@@ -70,133 +62,49 @@ def get_secrets_from_kv(kv_name, secret_name):
7062
# Credentials
7163
credential = DefaultAzureCredential()
7264

73-
# Create an ML client without workspace_name for hub creation
65+
# Create an ML client
7466
ml_client = MLClient(
67+
workspace_name=aihub_name,
7568
resource_group_name=resource_group_name,
7669
subscription_id=subscription_id,
7770
credential=credential,
7871
)
7972

80-
try:
81-
# Check if hub already exists
82-
logger.info(f"Checking if AI Hub {aihub_name} already exists...")
83-
try:
84-
existing_hub = ml_client.workspaces.get(name=aihub_name)
85-
logger.info(f"AI Hub {aihub_name} already exists, skipping creation")
86-
created_hub = existing_hub
87-
except Exception:
88-
# Hub doesn't exist, create it
89-
logger.info(f"Creating AI Hub {aihub_name}...")
90-
91-
# construct a hub
92-
my_hub = Hub(
93-
name=aihub_name,
94-
location=solutionLocation,
95-
display_name=aihub_name,
96-
description=f"AI Hub for {aihub_name}"
97-
)
98-
99-
# Create hub with proper error handling
100-
operation = ml_client.workspaces.begin_create(my_hub)
101-
logger.info("Hub creation initiated, waiting for completion...")
102-
103-
# Poll for completion with timeout
104-
start_time = time.time()
105-
timeout = 1800 # 30 minutes timeout
106-
107-
while not operation.done():
108-
elapsed = time.time() - start_time
109-
if elapsed > timeout:
110-
raise TimeoutError(f"Hub creation timed out after {elapsed/60:.1f} minutes")
111-
112-
logger.info(f"Hub creation in progress... ({elapsed/60:.1f} minutes elapsed)")
113-
time.sleep(30) # Check every 30 seconds
114-
115-
created_hub = operation.result()
116-
logger.info(f"AI Hub {aihub_name} created successfully")
117-
118-
# Create ML client with the hub workspace
119-
ml_client_hub = MLClient(
120-
workspace_name=aihub_name,
121-
resource_group_name=resource_group_name,
122-
subscription_id=subscription_id,
123-
credential=credential,
124-
)
73+
# construct a hub
74+
my_hub = Hub(name=aihub_name, location=solutionLocation, display_name=aihub_name)
75+
76+
created_hub = ml_client.workspaces.begin_create(my_hub).result()
77+
78+
# construct the project
79+
my_project = Project(
80+
name=project_name,
81+
location=solutionLocation,
82+
display_name=project_name,
83+
hub_id=created_hub.id,
84+
)
85+
86+
created_project = ml_client.workspaces.begin_create(workspace=my_project).result()
87+
88+
open_ai_connection = AzureOpenAIConnection(
89+
name="Azure_OpenAI",
90+
api_key=open_ai_key,
91+
api_version=openai_api_version,
92+
azure_endpoint=f"https://{open_ai_res_name}.openai.azure.com/",
93+
open_ai_resource_id=f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.CognitiveServices/accounts/{open_ai_res_name}"
94+
)
95+
96+
ml_client.connections.create_or_update(open_ai_connection)
97+
98+
target = f"https://{ai_search_res_name}.search.windows.net/"
99+
100+
# Create AI Search resource
101+
aisearch_connection = AzureAISearchConnection(
102+
name="Azure_AISearch",
103+
endpoint=target,
104+
credentials=ApiKeyConfiguration(key=ai_search_key),
105+
)
106+
107+
aisearch_connection.tags["ResourceId"] = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Search/searchServices/{ai_search_res_name}"
108+
aisearch_connection.tags["ApiVersion"] = "2024-05-01-preview"
125109

126-
# Check if project already exists
127-
logger.info(f"Checking if AI Project {project_name} already exists...")
128-
try:
129-
existing_project = ml_client.workspaces.get(name=project_name)
130-
logger.info(f"AI Project {project_name} already exists, skipping creation")
131-
created_project = existing_project
132-
except Exception:
133-
# Project doesn't exist, create it
134-
logger.info(f"Creating AI Project {project_name}...")
135-
136-
# construct the project
137-
my_project = Project(
138-
name=project_name,
139-
location=solutionLocation,
140-
display_name=project_name,
141-
hub_id=created_hub.id,
142-
description=f"AI Project for {project_name}"
143-
)
144-
145-
operation = ml_client.workspaces.begin_create(workspace=my_project)
146-
logger.info("Project creation initiated, waiting for completion...")
147-
148-
# Poll for completion
149-
start_time = time.time()
150-
while not operation.done():
151-
elapsed = time.time() - start_time
152-
if elapsed > timeout:
153-
raise TimeoutError(f"Project creation timed out after {elapsed/60:.1f} minutes")
154-
155-
logger.info(f"Project creation in progress... ({elapsed/60:.1f} minutes elapsed)")
156-
time.sleep(30)
157-
158-
created_project = operation.result()
159-
logger.info(f"AI Project {project_name} created successfully")
160-
161-
# Create connections
162-
logger.info("Creating AI connections...")
163-
164-
# OpenAI connection
165-
try:
166-
logger.info("Creating Azure OpenAI connection...")
167-
open_ai_connection = AzureOpenAIConnection(
168-
name="Azure_OpenAI",
169-
api_key=open_ai_key,
170-
api_version=openai_api_version,
171-
azure_endpoint=f"https://{open_ai_res_name}.openai.azure.com/",
172-
open_ai_resource_id=f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.CognitiveServices/accounts/{open_ai_res_name}"
173-
)
174-
ml_client_hub.connections.create_or_update(open_ai_connection)
175-
logger.info("Azure OpenAI connection created successfully")
176-
except Exception as e:
177-
logger.warning(f"Failed to create OpenAI connection: {str(e)}")
178-
179-
# AI Search connection
180-
try:
181-
logger.info("Creating Azure AI Search connection...")
182-
target = f"https://{ai_search_res_name}.search.windows.net/"
183-
184-
aisearch_connection = AzureAISearchConnection(
185-
name="Azure_AISearch",
186-
endpoint=target,
187-
credentials=ApiKeyConfiguration(key=ai_search_key),
188-
)
189-
190-
aisearch_connection.tags["ResourceId"] = f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Search/searchServices/{ai_search_res_name}"
191-
aisearch_connection.tags["ApiVersion"] = "2024-05-01-preview"
192-
193-
ml_client_hub.connections.create_or_update(aisearch_connection)
194-
logger.info("Azure AI Search connection created successfully")
195-
except Exception as e:
196-
logger.warning(f"Failed to create AI Search connection: {str(e)}")
197-
198-
logger.info("AI Hub setup completed successfully!")
199-
200-
except Exception as e:
201-
logger.error(f"Error during AI Hub creation: {str(e)}")
202-
raise
110+
ml_client.connections.create_or_update(aisearch_connection)

0 commit comments

Comments
 (0)