-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/consolidate-bicep
- Loading branch information
Showing
7 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"ignore": [ | ||
"shared" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# type: ignore | ||
|
||
import datetime | ||
import json | ||
import subprocess | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import requests | ||
import time | ||
|
||
runs = 10 | ||
sleep_time_ms = 100 | ||
url = f"{apim_resource_gateway_url}/openai/deployments/{openai_deployment_name}/chat/completions?api-version={openai_api_version}" | ||
api_runs = [] | ||
|
||
for i in range(runs): | ||
print(f"▶️ Run {i+1}/{runs}:") | ||
|
||
messages = {"messages": [ | ||
{"role": "system", "content": "You are a sarcastic, unhelpful assistant."}, | ||
{"role": "user", "content": "Can you tell me the time, please?"} | ||
]} | ||
|
||
start_time = time.time() | ||
response = requests.post(url, headers = {'api-key':apim_subscription_key}, json = messages) | ||
response_time = time.time() - start_time | ||
print(f"⌚ {response_time:.2f} seconds") | ||
|
||
# Check the response status code and apply formatting | ||
if 200 <= response.status_code < 300: | ||
status_code_str = f"\x1b[1;32m{response.status_code} - {response.reason}\x1b[0m" # Bold and green | ||
elif response.status_code >= 400: | ||
status_code_str = f"\x1b[1;31m{response.status_code} - {response.reason}\x1b[0m" # Bold and red | ||
else: | ||
status_code_str = str(response.status_code) # No formatting | ||
|
||
# Print the response status with the appropriate formatting | ||
print(f"Response status: {status_code_str}") | ||
print(f"Response headers: {response.headers}") | ||
|
||
if "x-ms-region" in response.headers: | ||
print(f"x-ms-region: \x1b[1;31m{response.headers.get("x-ms-region")}\x1b[0m") # this header is useful to determine the region of the backend that served the request | ||
api_runs.append((response_time, response.headers.get("x-ms-region"))) | ||
|
||
if (response.status_code == 200): | ||
data = json.loads(response.text) | ||
print(f"Token usage: {data.get("usage")}\n") | ||
print(f"💬 {data.get("choices")[0].get("message").get("content")}\n") | ||
else: | ||
print(response.text) | ||
|
||
time.sleep(sleep_time_ms/1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import json | ||
|
||
backend_id = "openai-backend-pool" if len(openai_resources) > 1 else openai_resources[0].get("name") | ||
|
||
with open("policy.xml", 'r') as policy_xml_file: | ||
policy_template_xml = policy_xml_file.read() | ||
policy_xml = policy_template_xml.replace("{backend-id}", backend_id) | ||
policy_xml_file.close() | ||
open("policy-updated.xml", 'w').write(policy_xml) | ||
|
||
bicep_parameters = { | ||
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#", | ||
"contentVersion": "1.0.0.0", | ||
"parameters": { | ||
"openAIConfig": { "value": openai_resources }, | ||
"openAIDeploymentName": { "value": openai_deployment_name }, | ||
"openAIModelName": { "value": openai_model_name }, | ||
"openAIModelVersion": { "value": openai_model_version }, | ||
} | ||
} | ||
|
||
with open('params.json', 'w') as bicep_parameters_file: | ||
bicep_parameters_file.write(json.dumps(bicep_parameters)) | ||
|
||
! az deployment group create --name {deployment_name} --resource-group {resource_group_name} --template-file "main.bicep" --parameters "params.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
resource_group_stdout = ! az group create --name {resource_group_name} --location {resource_group_location} | ||
|
||
if resource_group_stdout.n.startswith("ERROR"): | ||
print(resource_group_stdout) | ||
else: | ||
print(f"✅ Azure Resource Group {resource_group_name} created ⌚ {datetime.datetime.now().time()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.apimServiceId.value -o tsv | ||
apim_service_id = stdout.n | ||
print(f"👉🏻 APIM Service Id: {apim_service_id}") | ||
|
||
stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.apimSubscriptionKey.value -o tsv | ||
print(f"👉🏻 APIM Subscription Key (masked): ***...{apim_subscription_key[-4:]}") | ||
|
||
stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.apimResourceGatewayURL.value -o tsv | ||
apim_resource_gateway_url = stdout.n | ||
print(f"👉🏻 APIM API Gateway URL: {apim_resource_gateway_url}") | ||
|
||
stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.logAnalyticsWorkspaceId.value -o tsv | ||
workspace_id = stdout.n | ||
print(f"👉🏻 Workspace ID: {workspace_id}") | ||
|
||
stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.applicationInsightsAppId.value -o tsv | ||
app_id = stdout.n | ||
print(f"👉🏻 App ID: {app_id}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import time | ||
from openai import AzureOpenAI | ||
|
||
runs = 10 | ||
sleep_time_ms = 100 | ||
|
||
client = AzureOpenAI( | ||
azure_endpoint = apim_resource_gateway_url, | ||
api_key = apim_subscription_key, | ||
api_version = openai_api_version | ||
) | ||
|
||
for i in range(runs): | ||
print(f"▶️ Run {i+1}/{runs}:") | ||
|
||
messages = {"messages": [ | ||
{"role": "system", "content": "You are a sarcastic, unhelpful assistant."}, | ||
{"role": "user", "content": "Can you tell me the time, please?"} | ||
]} | ||
|
||
start_time = time.time() | ||
response = client.chat.completions.create(model = openai_model_name, messages = messages) | ||
response_time = time.time() - start_time | ||
print(f"⌚ {response_time:.2f} seconds") | ||
print(f"💬 {response.choices[0].message.content}\n") | ||
|
||
time.sleep(sleep_time_ms/1000) |