Skip to content

Commit

Permalink
Merge pull request #371 from MicrosoftDocs/test_postgres
Browse files Browse the repository at this point in the history
Merge test_postgres to main
  • Loading branch information
pjsingh28 authored Oct 30, 2024
2 parents 1814cd1 + 378b9f7 commit a7cc56c
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 1 deletion.
162 changes: 162 additions & 0 deletions scenarios/AksKaito/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
---
title: Deploy an AI model on Azure Kubernetes Service (AKS) with the AI toolchain operator (preview)
description: Learn how to enable the AI toolchain operator add-on on Azure Kubernetes Service (AKS) to simplify OSS AI model management and deployment.
ms.topic: article
ms.custom: azure-kubernetes-service, devx-track-azurecli
ms.date: 02/28/2024
author: schaffererin
ms.author: schaffererin

---

## Deploy an AI model on Azure Kubernetes Service (AKS) with the AI toolchain operator (preview)

The AI toolchain operator (KAITO) is a managed add-on for AKS that simplifies the experience of running OSS AI models on your AKS clusters. The AI toolchain operator automatically provisions the necessary GPU nodes and sets up the associated inference server as an endpoint server to your AI models. Using this add-on reduces your onboarding time and enables you to focus on AI model usage and development rather than infrastructure setup.

This article shows you how to enable the AI toolchain operator add-on and deploy an AI model on AKS.

[!INCLUDE [preview features callout](~/reusable-content/ce-skilling/azure/includes/aks/includes/preview/preview-callout.md)]

## Before you begin

* This article assumes a basic understanding of Kubernetes concepts. For more information, see [Kubernetes core concepts for AKS](./concepts-clusters-workloads.md).
* For ***all hosted model inference images*** and recommended infrastructure setup, see the [KAITO GitHub repository](https://github.com/Azure/kaito).
* The AI toolchain operator add-on currently supports KAITO version **v0.1.0**, please make a note of this in considering your choice of model from the KAITO model repository.

## Prerequisites

* If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
* If you have multiple Azure subscriptions, make sure you select the correct subscription in which the resources will be created and charged using the [az account set](https://learn.microsoft.com/en-us/cli/azure/account?view=azure-cli-latest#az-account-set) command.

> [!NOTE]
> The subscription you use must have GPU VM quota.
* Azure CLI version 2.47.0 or later installed and configured. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI](/cli/azure/install-azure-cli).
* The Kubernetes command-line client, kubectl, installed and configured. For more information, see [Install kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/).
* [Install the Azure CLI AKS preview extension](#install-the-azure-cli-preview-extension).
* [Register the AI toolchain operator add-on feature flag](#register-the-ai-toolchain-operator-add-on-feature-flag).

## Set up resource group

Set up a resource group with a random ID. Create an Azure resource group using the [az group create](https://learn.microsoft.com/en-us/cli/azure/group?view=azure-cli-latest#az-group-create) command.

```bash
export RANDOM_ID="$(openssl rand -hex 3)"
export AZURE_RESOURCE_GROUP="myKaitoResourceGroup$RANDOM_ID"
export REGION="centralus"
export CLUSTER_NAME="myClusterName$RANDOM_ID"

az group create \
--name $AZURE_RESOURCE_GROUP \
--location $REGION \
```

## Install the Azure CLI preview extension

Install the Azure CLI preview extension using the [az extension add](https://learn.microsoft.com/en-us/cli/azure/extension?view=azure-cli-latest#az-extension-add) command. Then update the extension to make sure you have the latest version using the [az extension update](https://learn.microsoft.com/en-us/cli/azure/extension?view=azure-cli-latest#az-extension-update) command.

```bash
az extension add --name aks-preview
az extension update --name aks-preview
```

## Register the AI toolchain operator add-on feature flag

Register the AIToolchainOperatorPreview feature flag using the az feature register command.
It takes a few minutes for the registration to complete.

```bash
az feature register --namespace "Microsoft.ContainerService" --name "AIToolchainOperatorPreview"
```

## Verify the AI toolchain operator add-on registration

Verify the registration using the [az feature show](https://learn.microsoft.com/en-us/cli/azure/feature?view=azure-cli-latest#az-feature-show) command.

```bash
while true; do
status=$(az feature show --namespace "Microsoft.ContainerService" --name "AIToolchainOperatorPreview" --query "properties.state" -o tsv)
if [ "$status" == "Registered" ]; then
break
else
sleep 15
fi
done
```

## Create an AKS cluster with the AI toolchain operator add-on enabled

Create an AKS cluster with the AI toolchain operator add-on enabled using the [az aks create](https://learn.microsoft.com/en-us/cli/azure/aks?view=azure-cli-latest#az-aks-create) command with the `--enable-ai-toolchain-operator` and `--enable-oidc-issuer` flags.

```bash
az aks create --location ${REGION} \
--resource-group ${AZURE_RESOURCE_GROUP} \
--name ${CLUSTER_NAME} \
--enable-oidc-issuer \
--node-os-upgrade-channel SecurityPatch \
--auto-upgrade-channel stable \
--enable-ai-toolchain-operator \
--generate-ssh-keys \
--k8s-support-plan KubernetesOfficial
```

## Connect to your cluster

Configure `kubectl` to connect to your cluster using the [az aks get-credentials](https://learn.microsoft.com/en-us/cli/azure/aks?view=azure-cli-latest#az-aks-get-credentials) command.

```bash
az aks get-credentials --resource-group ${AZURE_RESOURCE_GROUP} --name ${CLUSTER_NAME}
```

## Establish a federated identity credential

Create the federated identity credential between the managed identity, AKS OIDC issuer, and subject using the [az identity federated-credential create](https://learn.microsoft.com/en-us/cli/azure/identity/federated-credential?view=azure-cli-latest) command.

```bash
export MC_RESOURCE_GROUP=$(az aks show --resource-group ${AZURE_RESOURCE_GROUP} \
--name ${CLUSTER_NAME} \
--query nodeResourceGroup \
-o tsv)
export KAITO_IDENTITY_NAME="ai-toolchain-operator-${CLUSTER_NAME}"
export AKS_OIDC_ISSUER=$(az aks show --resource-group "${AZURE_RESOURCE_GROUP}" \
--name "${CLUSTER_NAME}" \
--query "oidcIssuerProfile.issuerUrl" \
-o tsv)

az identity federated-credential create --name "kaito-federated-identity" \
--identity-name "${KAITO_IDENTITY_NAME}" \
-g "${MC_RESOURCE_GROUP}" \
--issuer "${AKS_OIDC_ISSUER}" \
--subject system:serviceaccount:"kube-system:kaito-gpu-provisioner" \
--audience api://AzureADTokenExchange
```

## Verify that your deployment is running

Restart the KAITO GPU provisioner deployment on your pods using the `kubectl rollout restart` command:

```bash
kubectl rollout restart deployment/kaito-gpu-provisioner -n kube-system
```

## Deploy a default hosted AI model

Deploy the Falcon 7B-instruct model from the KAITO model repository using the `kubectl apply` command.

```bash
kubectl apply -f https://raw.githubusercontent.com/Azure/kaito/main/examples/inference/kaito_workspace_falcon_7b-instruct.yaml
```

## Ask a question

Verify deployment done: `kubectl get workspace workspace-falcon-7b-instruct -w`.
Store IP: `export SERVICE_IP=$(kubectl get svc workspace-falcon-7b-instruct -o jsonpath='{.spec.clusterIP}')`.
Ask question: `kubectl run -it --rm --restart=Never curl --image=curlimages/curl -- curl -X POST http://$SERVICE_IP/chat -H "accept: application/json" -H "Content-Type: application/json" -d "{\"prompt\":\"YOUR QUESTION HERE\"}"`

```bash
echo "See last step for details on how to ask questions to the model."
```

## Next steps

For more inference model options, see the [KAITO GitHub repository](https://github.com/Azure/kaito).
149 changes: 149 additions & 0 deletions scenarios/PostgresRagLlmDemo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
---
title: 'Quickstart: Deploy a Postgres vector database'
description: Setup a Postgres vector database and openai resources to run a RAG-LLM model.
ms.topic: quickstart
ms.date: 09/06/2024
author: aamini7
ms.author: ariaamini
ms.custom: innovation-engine, linux-related-content
---

## Introduction

In this doc, we go over how to host the infrastructure required to run a basic LLM model with RAG capabilities on Azure.
We first set up a Postgres database capable of storing vector embeddings for documents/knowledge files that we want to use to
augment our queries. We then create an Azure OpenAI deployment capable of generating embeddings and answering questions using the latest 'gpt-4-turbo' model.
We then use a python script to fill our postgres database with embeddings from a sample "knowledge.txt" file containing information about an imaginary
resource called 'Zytonium'. Once the database is filled with those embeddings, we use the same python script to answer any
questions we have about 'Zytonium'. The script will search the database for relevant information for our query using an embeddings search and
then augment our query with that relevant information before being sent our LLM to answer.

## Set up resource group

Set up a resource group with a random ID.

```bash
export RANDOM_ID="$(openssl rand -hex 3)"
export RG_NAME="myPostgresResourceGroup$RANDOM_ID"
export REGION="centralus"

az group create \
--name $RG_NAME \
--location $REGION \
```

## Create OpenAI resources

Create the openai resource

```bash
export OPEN_AI_SERVICE_NAME="openai-service-$RANDOM_ID"
export EMBEDDING_MODEL="text-embedding-ada-002"
export CHAT_MODEL="gpt-4-turbo-2024-04-09"

az cognitiveservices account create \
--name $OPEN_AI_SERVICE_NAME \
--resource-group $RG_NAME \
--location westus \
--kind OpenAI \
--sku s0 \
```

## Create OpenAI deployments

```bash
export EMBEDDING_MODEL="text-embedding-ada-002"
export CHAT_MODEL="gpt-4"

az cognitiveservices account deployment create \
--name $OPEN_AI_SERVICE_NAME \
--resource-group $RG_NAME \
--deployment-name $EMBEDDING_MODEL \
--model-name $EMBEDDING_MODEL \
--model-version "2" \
--model-format OpenAI \
--sku-capacity "1" \
--sku-name "Standard"

az cognitiveservices account deployment create \
--name $OPEN_AI_SERVICE_NAME \
--resource-group $RG_NAME \
--deployment-name $CHAT_MODEL \
--model-name $CHAT_MODEL \
--model-version "turbo-2024-04-09" \
--model-format OpenAI \
--sku-capacity "1" \
--sku-name "Standard"
```

## Create Database

Create an Azure postgres database.

```bash
export POSTGRES_SERVER_NAME="mydb$RANDOM_ID"
export PGHOST="${POSTGRES_SERVER_NAME}.postgres.database.azure.com"
export PGUSER="dbadmin$RANDOM_ID"
export PGPORT=5432
export PGDATABASE="azure-ai-demo"
export PGPASSWORD="$(openssl rand -base64 32)"

az postgres flexible-server create \
--admin-password $PGPASSWORD \
--admin-user $PGUSER \
--location $REGION \
--name $POSTGRES_SERVER_NAME \
--database-name $PGDATABASE \
--resource-group $RG_NAME \
--sku-name Standard_B2s \
--storage-auto-grow Disabled \
--storage-size 32 \
--tier Burstable \
--version 16 \
--yes -o JSON \
--public-access 0.0.0.0
```

## Enable postgres vector extension

Set up the vector extension for postgres to allow storing vectors/embeddings.

```bash
az postgres flexible-server parameter set \
--resource-group $RG_NAME \
--server-name $POSTGRES_SERVER_NAME \
--name azure.extensions --value vector

psql -c "CREATE EXTENSION IF NOT EXISTS vector;"

psql \
-c "CREATE TABLE embeddings(id int PRIMARY KEY, data text, embedding vector(1536));" \
-c "CREATE INDEX ON embeddings USING hnsw (embedding vector_ip_ops);"
```

## Populate with data from knowledge file

The chat bot uses a local file called "knowledge.txt" as the sample document to generate embeddings for
and to store those embeddings in the newly created postgres database. Then any questions you ask will
be augmented with context from the "knowledge.txt" after searching the document for the most relevant
pieces of context using the embeddings. The "knowledge.txt" is about a fictional material called Zytonium.
You can view the full knowledge.txt and the code for the chatbot by looking in the "scenarios/PostgresRagLlmDemo" directory.

```bash
export ENDPOINT=$(az cognitiveservices account show --name $OPEN_AI_SERVICE_NAME --resource-group $RG_NAME | jq -r .properties.endpoint)
export API_KEY=$(az cognitiveservices account keys list --name $OPEN_AI_SERVICE_NAME --resource-group $RG_NAME | jq -r .key1)

cd ~/scenarios/PostgresRagLlmDemo
pip install -r requirements.txt
python chat.py --populate --api-key $API_KEY --endpoint $ENDPOINT --pguser $PGUSER --phhost $PGHOST --pgpassword $PGPASSWORD --pgdatabase $PGDATABASE
```

## Run Chat bot

This final step prints out the command you can copy/paste into the terminal to run the chatbot. `cd ~/scenarios/PostgresRagLlmDemo && python chat.py --api-key $API_KEY --endpoint $ENDPOINT --pguser $PGUSER --phhost $PGHOST --pgpassword $PGPASSWORD --pgdatabase $PGDATABASE`

```bash
echo "
To run the chatbot, see the last step for more info.
"
```
Loading

0 comments on commit a7cc56c

Please sign in to comment.