-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Deployment with automatic scripts
Using managed identity now
1 parent
81925b2
commit e6d12da
Showing
16 changed files
with
388 additions
and
47 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
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,67 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
CHALLENGE="$1" | ||
|
||
if [ "$CHALLENGE" == "" ]; then | ||
echo "No challenge name provided - aborting" | ||
exit 0; | ||
fi | ||
|
||
AZURE_ENV_NAME="$2" | ||
|
||
if [ "$AZURE_ENV_NAME" == "" ]; then | ||
echo "No environment name provided - aborting" | ||
exit 0; | ||
fi | ||
|
||
if [[ $CHALLENGE =~ ^[a-z0-9]{5,12}$ ]]; then | ||
echo "challenge name $CHALLENGE is valid" | ||
else | ||
echo "challenge name $CHALLENGE is invalid - only numbers and lower case min 5 and max 12 characters allowed - aborting" | ||
exit 0; | ||
fi | ||
|
||
RESOURCE_GROUP="rg-$AZURE_ENV_NAME" | ||
|
||
if [ $(az group exists --name $RESOURCE_GROUP) = false ]; then | ||
echo "resource group $RESOURCE_GROUP does not exist" | ||
error=1 | ||
else | ||
echo "resource group $RESOURCE_GROUP already exists" | ||
LOCATION=$(az group show -n $RESOURCE_GROUP --query location -o tsv) | ||
fi | ||
|
||
APPINSIGHTS_NAME=$(az resource list -g $RESOURCE_GROUP --resource-type "Microsoft.Insights/components" --query "[0].name" -o tsv) | ||
AZURE_CONTAINER_REGISTRY_NAME=$(az resource list -g $RESOURCE_GROUP --resource-type "Microsoft.ContainerRegistry/registries" --query "[0].name" -o tsv) | ||
OPENAI_NAME=$(az resource list -g $RESOURCE_GROUP --resource-type "Microsoft.CognitiveServices/accounts" --query "[0].name" -o tsv) | ||
ENVIRONMENT_NAME=$(az resource list -g $RESOURCE_GROUP --resource-type "Microsoft.App/managedEnvironments" --query "[0].name" -o tsv) | ||
IDENTITY_NAME=$(az resource list -g $RESOURCE_GROUP --resource-type "Microsoft.ManagedIdentity/userAssignedIdentities" --query "[0].name" -o tsv) | ||
SERVICE_NAME=$CHALLENGE | ||
AZURE_SUBSCRIPTION_ID=$(az account show --query id -o tsv) | ||
|
||
echo "container registry name: $AZURE_CONTAINER_REGISTRY_NAME" | ||
echo "application insights name: $APPINSIGHTS_NAME" | ||
echo "openai name: $OPENAI_NAME" | ||
echo "environment name: $ENVIRONMENT_NAME" | ||
echo "identity name: $IDENTITY_NAME" | ||
echo "service name: $SERVICE_NAME" | ||
|
||
CONTAINER_APP_EXISTS=$(az resource list -g $RESOURCE_GROUP --resource-type "Microsoft.App/containerApps" --query "[?contains(name, '$SERVICE_NAME')].id" -o tsv) | ||
EXISTS="false" | ||
|
||
if [ "$CONTAINER_APP_EXISTS" == "" ]; then | ||
echo "container app $SERVICE_NAME does not exist" | ||
else | ||
echo "container app $SERVICE_NAME already exists" | ||
EXISTS="true" | ||
fi | ||
|
||
az acr build --subscription ${AZURE_SUBSCRIPTION_ID} --registry ${AZURE_CONTAINER_REGISTRY_NAME} --image $SERVICE_NAME:latest ./src-agents/$SERVICE_NAME | ||
IMAGE_NAME="${AZURE_CONTAINER_REGISTRY_NAME}.azurecr.io/$SERVICE_NAME:latest" | ||
|
||
az deployment group create -g $RESOURCE_GROUP -f ./infra/app/challengeX.bicep \ | ||
-p name=$SERVICE_NAME -p location=$LOCATION -p containerAppsEnvironmentName=$ENVIRONMENT_NAME \ | ||
-p containerRegistryName=$AZURE_CONTAINER_REGISTRY_NAME -p applicationInsightsName=$APPINSIGHTS_NAME \ | ||
-p openaiName=$OPENAI_NAME -p identityName=$IDENTITY_NAME -p imageName=$IMAGE_NAME -p exists=$EXISTS |
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,4 @@ | ||
#!/bin/bash | ||
|
||
echo "Deployed environment $AZURE_ENV_NAME successfully." | ||
echo $AZURE_ENV_NAME |
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,14 +1,26 @@ | ||
name: adventure-day-agents | ||
metadata: | ||
template: openai-plugin-fastapi@0.0.1-beta | ||
services: | ||
challenge1: | ||
project: ./src-agents/challenge1 | ||
dist: build | ||
language: python | ||
host: containerapp | ||
docker: | ||
path: ./Dockerfile | ||
context: ./ | ||
hooks: | ||
postprovision: | ||
posix: | ||
shell: sh | ||
continueOnError: false | ||
interactive: true | ||
run: azd-hooks/post-provision.sh | ||
# services: | ||
# challenge1: | ||
# project: ./src-agents/challenge1 | ||
# language: py | ||
# host: containerapp | ||
# module: challenge1 | ||
# challenge2: | ||
# project: ./src-agents/challenge2 | ||
# dist: build | ||
# language: python | ||
# host: containerapp | ||
# docker: | ||
# path: ./Dockerfile | ||
# context: ./ | ||
infra: | ||
provider: bicep |
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,47 @@ | ||
param name string | ||
param location string = resourceGroup().location | ||
param tags object = {} | ||
|
||
param kind string = 'OpenAI' | ||
// Public network access of the Azure OpenAI service | ||
param publicNetworkAccess string = 'Enabled' | ||
// SKU of the Azure OpenAI service | ||
param sku object = { | ||
name: 'S0' | ||
} | ||
|
||
param customDomainName string | ||
|
||
param deployments array | ||
|
||
resource account 'Microsoft.CognitiveServices/accounts@2022-10-01' = { | ||
name: name | ||
location: location | ||
tags: tags | ||
kind: kind | ||
identity: { | ||
type: 'SystemAssigned' | ||
} | ||
properties: { | ||
customSubDomainName: customDomainName | ||
publicNetworkAccess: publicNetworkAccess | ||
} | ||
sku: sku | ||
} | ||
|
||
// Deployments for the Azure OpenAI service | ||
@batchSize(1) | ||
resource deployment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = [for deployment in deployments: { | ||
parent: account | ||
name: deployment.name | ||
sku: { | ||
name: 'Standard' | ||
capacity: 300 | ||
} | ||
properties: { | ||
model: deployment.model | ||
} | ||
}] | ||
|
||
output openaiEndpoint string = account.properties.endpoint | ||
output openaiKey string = listKeys(account.id, '2022-10-01').key1 |
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
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,77 @@ | ||
param name string | ||
param location string = resourceGroup().location | ||
param exists bool = true | ||
param containerAppsEnvironmentName string | ||
param containerRegistryName string | ||
param applicationInsightsName string | ||
param identityName string | ||
param openaiName string | ||
param imageName string | ||
|
||
var tags = { 'azd-env-name': containerAppsEnvironmentName } | ||
var completionDeploymentModelName = 'gpt-35-turbo' | ||
|
||
resource apiIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = { | ||
name: identityName | ||
} | ||
|
||
resource account 'Microsoft.CognitiveServices/accounts@2022-10-01' existing = { | ||
name: openaiName | ||
} | ||
|
||
var openaiEndpoint = account.properties.endpoint | ||
var openaiApiKey = listKeys(account.id, '2022-10-01').key1 | ||
|
||
module app '../core/host/container-app-upsert.bicep' = { | ||
name: '${name}-container-app' | ||
params: { | ||
name: name | ||
location: location | ||
tags: union(tags, { 'azd-service-name': name }) | ||
identityName: identityName | ||
imageName: imageName | ||
exists: exists | ||
containerAppsEnvironmentName: containerAppsEnvironmentName | ||
containerRegistryName: containerRegistryName | ||
env: [ | ||
{ | ||
name: 'AZURE_CLIENT_ID' | ||
value: apiIdentity.properties.clientId | ||
} | ||
{ | ||
name: 'APPLICATIONINSIGHTS_CONNECTION_STRING' | ||
value: applicationInsights.properties.ConnectionString | ||
} | ||
{ | ||
name: 'AZURE_OPENAI_API_KEY' | ||
value: openaiApiKey | ||
} | ||
{ | ||
name: 'AZURE_OPENAI_ENDPOINT' | ||
value: openaiEndpoint | ||
} | ||
{ | ||
name: 'AZURE_OPENAI_COMPLETION_DEPLOYMENT_NAME' | ||
value: completionDeploymentModelName | ||
} | ||
{ | ||
name: 'AZURE_OPENAI_VERSION' | ||
value: '2024-02-01' | ||
} | ||
{ | ||
name: 'OPENAI_API_TYPE' | ||
value: 'azure' | ||
} | ||
] | ||
targetPort: 8080 | ||
} | ||
} | ||
|
||
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' existing = { | ||
name: applicationInsightsName | ||
} | ||
|
||
output SERVICE_API_IDENTITY_PRINCIPAL_ID string = apiIdentity.properties.principalId | ||
output SERVICE_API_NAME string = app.outputs.name | ||
output SERVICE_API_URI string = app.outputs.uri | ||
output SERVICE_API_IMAGE_NAME string = app.outputs.imageName |
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
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
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
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
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
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
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,7 @@ | ||
FROM python:3.11 | ||
|
||
COPY . . | ||
|
||
RUN pip install --no-cache-dir --upgrade -r requirements.txt | ||
|
||
CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-${WEBSITES_PORT:-8080}}"] |
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,67 @@ | ||
import os | ||
from dotenv import load_dotenv | ||
from fastapi import FastAPI | ||
from pydantic import BaseModel | ||
from enum import Enum | ||
from openai import AzureOpenAI | ||
|
||
app = FastAPI() | ||
|
||
load_dotenv() | ||
|
||
class QuestionType(str, Enum): | ||
multiple_choice = "multiple_choice" | ||
true_false = "true_false" | ||
popular_choice = "popular_choice" | ||
estimation = "estimation" | ||
|
||
class Ask(BaseModel): | ||
question: str | None = None | ||
type: QuestionType | ||
correlationToken: str | None = None | ||
|
||
class Answer(BaseModel): | ||
answer: str | ||
correlationToken: str | None = None | ||
promptTokensUsed: int | None = None | ||
completionTokensUsed: int | None = None | ||
|
||
client = AzureOpenAI( | ||
api_key = os.getenv("AZURE_OPENAI_API_KEY"), | ||
api_version = os.getenv("AZURE_OPENAI_VERSION"), | ||
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT") | ||
) | ||
deployment_name = os.getenv("AZURE_OPENAI_COMPLETION_DEPLOYMENT_NAME") | ||
|
||
@app.get("/") | ||
async def root(): | ||
return {"message": "Hello Smorgs"} | ||
|
||
@app.get("/healthz", summary="Health check", operation_id="healthz") | ||
async def get_products(query: str = None): | ||
""" | ||
Returns a status of the app | ||
""" | ||
|
||
@app.post("/ask", summary="Ask a question", operation_id="ask") | ||
async def ask_question(ask: Ask): | ||
""" | ||
Ask a question | ||
""" | ||
|
||
# Send a completion call to generate an answer | ||
print('Sending a request to openai') | ||
start_phrase = ask.question | ||
response = client.chat.completions.create( | ||
model = deployment_name, | ||
messages = [{"role" : "assistant", "content" : start_phrase}], | ||
) | ||
|
||
print(response.choices[0].message.content) | ||
print(response) | ||
answer = Answer(answer=response.choices[0].message.content) | ||
answer.correlationToken = ask.correlationToken | ||
answer.promptTokensUsed = response.usage.prompt_tokens | ||
answer.completionTokensUsed = response.usage.completion_tokens | ||
|
||
return answer |
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 @@ | ||
fastapi==0.110.0 | ||
uvicorn==0.23.2 | ||
pydantic==2.6.4 | ||
openai==1.14.2 | ||
python-dotenv==1.0.0 | ||
azure-identity==1.15.0 |