This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.
In this sample, I demonstrate how to quickly build chat applications using Python and leveraging powerful technologies such as OpenAI ChatGPT models, Embedding models, LangChain framework, ChromaDB vector database, and Chainlit, an open-source Python package that is specifically designed to create user interfaces (UIs) for AI applications. These applications are hosted in an Azure Kubernetes Service (AKS) cluster that communicates with Azure OpenAI Service (AOAI) via an Azure Private Endpoint.
- Simple Chat: This simple chat application utilizes OpenAI's language models to generate real-time completion responses.
- Documents QA Chat: This chat application goes beyond simple conversations. Users can upload up to 10
.pdf
and.docx
documents, which are then processed to create vector embeddings. These embeddings are stored in ChromaDB for efficient retrieval. Users can pose questions about the uploaded documents and view the Chain of Thought, enabling easy exploration of the reasoning process. The completion message contains links to the text chunks in the documents that were used as a source for the response.
Both applications use a user-defined managed identity to authenticate and authorize against Azure OpenAI Service (AOAI) and Azure Container Registry (ACR) and use Azure Private Endpoints to connect privately and securely to these services. The chat UIs are built using Chainlit, an open-source Python package designed explicitly for creating AI applications. Chainlit seamlessly integrates with LangChain, LlamaIndex, and LangFlow, making it a powerful tool for developing ChatGPT-like applications with ease. You can find the companion code in this GitHub repository. For more information on Azure OpenAI Service and Large Language Models (LLMs), see the following articles:
NOTE
You can find thearchitecture.vsdx
file used for the diagram under thevisio
folder.
Prerequisites
- An active Azure subscription. If you don't have one, create a free Azure account before you begin.
- Visual Studio Code installed on one of the supported platforms along with the HashiCorp Terraform.
- Azure CLI version 2.49.0 or later installed. To install or upgrade, see Install Azure CLI.
aks-preview
Azure CLI extension of version 0.5.140 or later installed- Terraform v1.5.2 or later.
- The deployment must be started by a user who has sufficient permissions to assign roles, such as a
User Access Administrator
orOwner
. - Your Azure account also needs
Microsoft.Resources/deployments/write
permissions at the subscription level.
Architecture
The following diagram shows the architecture and network topology deployed by the sample:
This project provides a set of Terraform modules to deploy thw following resources:
- Azure Kubernetes Service: A public or private Azure Kubernetes Service(AKS) cluster composed of a:
- A
system
node pool in a dedicated subnet. The default node pool hosts only critical system pods and services. The worker nodes have node taint which prevents application pods from beings scheduled on this node pool. - A
user
node pool hosting user workloads and artifacts in a dedicated subnet.
- A
- Azure OpenAI Service: an Azure OpenAI Service with a GPT-3.5 model used by the chatbot application. Azure OpenAI Service gives customers advanced language AI with OpenAI GPT-4, GPT-3, Codex, and DALL-E models with the security and enterprise promise of Azure. Azure OpenAI co-develops the APIs with OpenAI, ensuring compatibility and a smooth transition from one to the other.
- User-defined Managed Identity: a user-defined managed identity used by the AKS cluster to create additional resources like load balancers and managed disks in Azure.
- User-defined Managed Identity: a user-defined managed identity used by the chatbot application to acquire a security token via Microsoft Entra Workload ID to call the Chat Completion API of the ChatGPT model provided by the Azure OpenAI Service.
- Azure Virtual Machine: Terraform modules can optionally create a jump-box virtual machine to manage the private AKS cluster.
- Azure Bastion Host: a separate Azure Bastion is deployed in the AKS cluster virtual network to provide SSH connectivity to both agent nodes and virtual machines.
- Azure NAT Gateway: a bring-your-own (BYO) Azure NAT Gateway to manage outbound connections initiated by AKS-hosted workloads. The NAT Gateway is associated to the
SystemSubnet
,UserSubnet
, andPodSubnet
subnets. The outboundType property of the cluster is set touserAssignedNatGateway
to specify that a BYO NAT Gateway is used for outbound connections. NOTE: you can update theoutboundType
after cluster creation and this will deploy or remove resources as required to put the cluster into the new egress configuration. For more information, see Updating outboundType after cluster creation. - Azure Storage Account: this storage account is used to store the boot diagnostics logs of both the service provider and service consumer virtual machines. Boot Diagnostics is a debugging feature that allows you to view console output and screenshots to diagnose virtual machine status.
- Azure Container Registry: an Azure Container Registry (ACR) to build, store, and manage container images and artifacts in a private registry for all container deployments.
- Azure Key Vault: an Azure Key Vault used to store secrets, certificates, and keys that can be mounted as files by pods using Azure Key Vault Provider for Secrets Store CSI Driver. For more information, see Use the Azure Key Vault Provider for Secrets Store CSI Driver in an AKS cluster and Provide an identity to access the Azure Key Vault Provider for Secrets Store CSI Driver.
- Azure Private Endpoints: an Azure Private Endpoint is created for each of the following resources:
- Azure OpenAI Service
- Azure Container Registry
- Azure Key Vault
- Azure Storage Account
- API Server when deploying a private AKS cluster.
- Azure Private DNDS Zones: an Azure Private DNS Zone is created for each of the following resources:
- Azure OpenAI Service
- Azure Container Registry
- Azure Key Vault
- Azure Storage Account
- API Server when deploying a private AKS cluster.
- Azure Network Security Group: subnets hosting virtual machines and Azure Bastion Hosts are protected by Azure Network Security Groups that are used to filter inbound and outbound traffic.
- Azure Log Analytics Workspace: a centralized Azure Log Analytics workspace is used to collect the diagnostics logs and metrics from all the Azure resources:
- Azure OpenAI Service
- Azure Kubernetes Service cluster
- Azure Key Vault
- Azure Network Security Group
- Azure Container Registry
- Azure Storage Account
- Azure jump-box virtual machine
- Azure Monitor workspace: An Azure Monitor workspace is a unique environment for data collected by Azure Monitor. Each workspace has its own data repository, configuration, and permissions. Log Analytics workspaces contain logs and metrics data from multiple Azure resources, whereas Azure Monitor workspaces currently contain only metrics related to Prometheus. Azure Monitor managed service for Prometheus allows you to collect and analyze metrics at scale using a Prometheus-compatible monitoring solution, based on the Prometheus. This fully managed service allows you to use the Prometheus query language (PromQL) to analyze and alert on the performance of monitored infrastructure and workloads without having to operate the underlying infrastructure. The primary method for visualizing Prometheus metrics is Azure Managed Grafana. You can connect your Azure Monitor workspace to an Azure Managed Grafana to visualize Prometheus metrics using a set of built-in and custom Grafana dashboards.
- Azure Managed Grafana: an Azure Managed Grafana instance used to visualize the Prometheus metrics generated by the Azure Kubernetes Service(AKS) cluster deployed by the Bicep modules. Azure Managed Grafana is a fully managed service for analytics and monitoring solutions. It's supported by Grafana Enterprise, which provides extensible data visualizations. This managed service allows to quickly and easily deploy Grafana dashboards with built-in high availability and control access with Azure security.
- NGINX Ingress Controller: this sample compares the managed and unmanaged NGINX Ingress Controller. While the managed version is installed using the Application routing add-on, the unmanaged version is deployed using the Helm Terraform Provider. You can use the Helm provider to deploy software packages in Kubernetes. The provider needs to be configured with the proper credentials before it can be used.
- Cert-Manager: the
cert-manager
package and Let's Encrypt certificate authority are used to issue a TLS/SSL certificate to the chat applications. - Prometheus: the AKS cluster is configured to collect metrics to the Azure Monitor workspace and Azure Managed Grafana. Nonetheless, the kube-prometheus-stack Helm chart is used to install Prometheus and Grafana on the AKS cluster.
- Workload namespace and service account: the Kubectl Terraform Provider and Kubernetes Terraform Provider are used to create the namespace and service account used by the chat applications.
- Azure Monitor ConfigMaps for Azure Monitor managed service for Prometheus and
cert-manager
Cluster Issuer are deployed using the Kubectl Terraform Provider and Kubernetes Terraform Provider.`
The architecture of the chat
application can be seen in the image below. The same architecture is also adopted by the docs
application.
The technologies and frameworks utilized by both applications are depicted in the next figure. Both applications are written in Python.
NOTE
In a production environment, we strongly recommend deploying a private AKS cluster with Uptime SLA. For more information, see private AKS cluster with a Public DNS address. Alternatively, you can deploy a public AKS cluster and secure access to the API server using authorized IP address ranges.
What is Azure OpenAI Service?
The Azure OpenAI Service is a platform offered by Microsoft Azure that provides cognitive services powered by OpenAI models. One of the models available through this service is the ChatGPT model, which is designed for interactive conversational tasks. It allows developers to integrate natural language understanding and generation capabilities into their applications.
Azure OpenAI Service provides REST API access to OpenAI's powerful language models including the GPT-3, Codex and Embeddings model series. In addition, the new GPT-4 and ChatGPT model series have now reached general availability. These models can be easily adapted to your specific task including but not limited to content generation, summarization, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or our web-based interface in the Azure OpenAI Studio.
The Chat Completion API, which is part of the Azure OpenAI Service, provides a dedicated interface for interacting with the ChatGPT and GPT-4 models. This API is currently in preview and is the preferred method for accessing these models. The GPT-4 models can only be accessed through this API.
GPT-3, GPT-3.5, and GPT-4 models from OpenAI are prompt-based. With prompt-based models, the user interacts with the model by entering a text prompt, to which the model responds with a text completion. This completion is the model’s continuation of the input text. While these models are extremely powerful, their behavior is also very sensitive to the prompt. This makes prompt construction an important skill to develop. For more information, see Introduction to prompt engineering.
Prompt construction can be difficult. In practice, the prompt acts to configure the model weights to complete the desired task, but it's more of an art than a science, often requiring experience and intuition to craft a successful prompt. The goal of this article is to help get you started with this learning process. It attempts to capture general concepts and patterns that apply to all GPT models. However it's important to understand that each model behaves differently, so the learnings may not apply equally to all models.
Prompt engineering refers to the process of creating instructions called prompts for Large Language Models (LLMs), such as OpenAI’s ChatGPT. With the immense potential of LLMs to solve a wide range of tasks, leveraging prompt engineering can empower us to save significant time and facilitate the development of impressive applications. It holds the key to unleashing the full capabilities of these huge models, transforming how we interact and benefit from them. For more information, see Prompt engineering techniques.
Vector Databases
A vector database is a specialized database that goes beyond traditional storage by organizing information to simplify the search for similar items. Instead of merely storing words or numbers, it leverages vector embeddings - unique numerical representations of data. These embeddings capture meaning, context, and relationships. For instance, words are represented as vectors, whereas similar words have similar vector values.
The applications of vector databases are numerous and powerful. In language processing, they facilitate the discovery of related documents or sentences. By comparing the vector embeddings of different texts, finding similar or related information becomes faster and more efficient. This capability benefits search engines and recommendation systems, which can suggest relevant articles or products based on user interests.
In the realm of image analysis, vector databases excel in finding visually similar images. By representing images as vectors, a simple comparison of vector values can identify visually similar images. This capability is highly valuable for tasks like reverse image search or content-based image retrieval.
Additionally, vector databases find applications in fraud detection, anomaly detection, and clustering. By comparing vector embeddings of data points, unusual patterns can be detected, and similar items can be grouped together, aiding in effective data analysis and decision-making.
Here is a list of the most popular vector databases:
- ChromaDB is a powerful database solution that stores and retrieves vector embeddings efficiently. It is commonly used in AI applications, including chatbots and document analysis systems. By storing embeddings in ChromaDB, users can easily search and retrieve similar vectors, enabling faster and more accurate matching or recommendation processes. ChromaDB offers excellent scalability high performance, and supports various indexing techniques to optimize search operations. It is a versatile tool that enhances the functionality and efficiency of AI applications that rely on vector embeddings.
- Facebook AI Similarity Search (FAISS) is another widely used vector database. Facebook AI Research develops it and offers highly optimized algorithms for similarity search and clustering of vector embeddings. FAISS is known for its speed and scalability, making it suitable for large-scale applications. It offers different indexing methods like flat, IVF (Inverted File System), and HNSW (Hierarchical Navigable Small World) to organize and search vector data efficiently.
- SingleStore: SingleStore aims to deliver the world’s fastest distributed SQL database for data-intensive applications: SingleStoreDB, which combines transactional + analytical workloads in a single platform.
- Astra DB: DataStax Astra DB is a cloud-native, multi-cloud, fully managed database-as-a-service based on Apache Cassandra, which aims to accelerate application development and reduce deployment time for applications from weeks to minutes.
- Milvus: Milvus is an open source vector database built to power embedding similarity search and AI applications. Milvus makes unstructured data search more accessible and provides a consistent user experience regardless of the deployment environment. Milvus 2.0 is a cloud-native vector database with storage and computation separated by design. All components in this refactored version of Milvus are stateless to enhance elasticity and flexibility.
- Qdrant: Qdrant is a vector similarity search engine and database for AI applications. Along with open-source, Qdrant is also available in the cloud. It provides a production-ready service with an API to store, search, and manage points—vectors with an additional payload. Qdrant is tailored to extended filtering support. It makes it useful for all sorts of neural-network or semantic-based matching, faceted search, and other applications.
- Pinecone: Pinecone is a fully managed vector database that makes adding vector search to production applications accessible. It combines state-of-the-art vector search libraries, advanced features such as filtering, and distributed infrastructure to provide high performance and reliability at any scale.
- Vespa: Vespa is a platform for applications combining data and AI, online. By building such applications on Vespa helps users avoid integration work to get features, and it can scale to support any amount of traffic and data. To deliver that, Vespa provides a broad range of query capabilities, a computation engine with support for modern machine-learned models, hands-off operability, data management, and application development support. It is free and open source to use under the Apache 2.0 license.
- Zilliz: Milvus is an open-source vector database, with over 18,409 stars on GitHub and 3.4 million+ downloads. Milvus supports billion-scale vector search and has over 1,000 enterprise users. Zilliz Cloud provides a fully-managed Milvus service made by the creators of Milvus. This helps to simplify the process of deploying and scaling vector search applications by eliminating the need to create and maintain complex data infrastructure. As a DBaaS, Zilliz simplifies the process of deploying and scaling vector search applications by eliminating the need to create and maintain complex data infrastructure.
- Weaviate: Weaviate is an open-source vector database used to store data objects and vector embeddings from ML-models, and scale into billions of data objects from the same name company in Amsterdam. Users can index billions of data objects to search through and combine multiple search techniques, such as keyword-based and vector search, to provide search experiences.
This sample makes of ChromaDB vector database, but you can easily modify the code to use another vector database. You can even use Azure Cache for Redis Enterprise to store the vector embeddings and compute vector similarity with high performance and low latency. For more information, see Vector Similarity Search with Azure Cache for Redis Enterprise.
LangChain
LangChain is a software framework designed to streamline the development of applications using large language models (LLMs). It serves as a language model integration framework, facilitating various applications like document analysis and summarization, chatbots, and code analysis.
LangChain's integrations cover an extensive range of systems, tools, and services, making it a comprehensive solution for language model-based applications. LangChain integrates with the major cloud platforms such as Microsoft Azure, Amazon AWS, and Google, and with API wrappers for various purposes like news, movie information, and weather, as well as support for Bash, web scraping, and more. It also supports multiple language models, including those from OpenAI, Anthropic, and Hugging Face. Moreover, LangChain offers various functionalities for document handling, code generation, analysis, debugging, and interaction with databases and other data sources.
Chainlit
Chainlit is an open-source Python package that is specifically designed to create user interfaces (UIs) for AI applications. It simplifies the process of building interactive chats and interfaces, making developing AI-powered applications faster and more efficient. While Streamlit is a general-purpose UI library, Chainlit is purpose-built for AI applications and seamlessly integrates with other AI technologies such as LangChain, LlamaIndex, and LangFlow.
With Chainlit, developers can easily create intuitive UIs for their AI models, including ChatGPT-like applications. It provides a user-friendly interface for users to interact with AI models, enabling conversational experiences and information retrieval. Chainlit also offers unique features, such as the ability to display the Chain of Thought, which allows users to explore the reasoning process directly within the UI. This feature enhances transparency and enables users to understand how the AI arrives at its responses or recommendations.
For more information, see the following resources:
Managed NGINX Ingress Controller for Azure Kubernetes Service
One way to route HTTP and secure HTTPS traffic to applications in an Azure Kubernetes Service (AKS) cluster is by using the Kubernetes Ingress object. The application routing add-on for AKS enables you to create, configure, and manage one or more Ingress controllers within your AKS cluster using the NGINX Ingress Controller.
The application routing add-on with NGINX provides several features, including:
- Easy configuration of managed NGINX Ingress controllers based on the Kubernetes NGINX Ingress controller.
- Integration with Azure DNS for public and private zone management.
- SSL termination with certificates stored in Azure Key Vault.
To enable the application routing add-on on an existing cluster, you can use Azure CLI, as shown in the following code snippet.
Once enabled, you can connect to your AKS cluster, deploy applications, and create Ingress objects with appropriate annotations for routing. There are some limitations to be aware of, such as the maximum number of supported Azure DNS zones and namespace editing restrictions. It's recommended to review the application routing add-on configuration for additional information on SSL encryption and DNS integration.
If you are familiar with the NGINX ingress controller, you can just replace the nginx
ingress class name inside an ingress object with the name of the ingress controller deployed by the application routing addon, that, by default is equal to webapprouting.kubernetes.azure.com
:
If you leverage cert-manager
and with Let's Encrypt certificate authority to issue TLS/SSL certificates to your application, make sure to create an issuer or a cluster issuer for the ingress class of the managed NGINX ingress controller installed by the application routing add-on. This can be done using the sample code provided below:
Ensure that you replace admin@contoso.com
with your own email address to receive notifications from Let's Encrypt. By using this configuration, cert-manager
will be able to issue certificates for the ingress class of the managed NGINX ingress controller when using the application routing add-on. Please note that the server URL https://acme-v02.api.letsencrypt.org/directory
is the Let's Encrypt production server. You can also use the staging server https://acme-staging-v02.api.letsencrypt.org/directory
for testing purposes to avoid rate limits. Ensure that the issuer or cluster issuer resource is deployed to your Kubernetes cluster, and cert-manager
is properly installed and configured. For more detailed steps and instructions, refer to Managed nginx Ingress with the application routing add-on.
Deploy the Terraform modules
Before deploying the Terraform modules in the terraform
folder, specify a value for the following variables in the terraform.tfvars variable definitions file.
Description:
prefix
: specifies a prefix for all the Azure resources.domain
: specifies the domain part (e.g., subdomain.domain) of the hostname of the ingress object used to expose the chatbot via the NGINX Ingress Controller.kubernetes_version
: specifies the Kubernetes version installed on the AKS cluster.namespace
: specifies the namespace of the workload application that accesses the Azure OpenAI Service.service_account_name
: specifies the name of the service account of the workload application that accesses the Azure OpenAI Service.ssh_public_key
: specifies the SSH public key used for the AKS nodes and jumpbox virtual machine.vm_enabled
: a boleean value that specifies whether deploying or not a jumpbox virtual machine in the same virtual network of the AKS cluster.location
: specifies the region (e.g., westeurope) where deploying the Azure resources.admin_group_object_ids
: when deploying an AKS cluster with Microsoft Entra ID and Azure RBAC integration, this array parameter contains the list of Microsoft Entra ID group object IDs that will have the admin role of the cluster.web_app_routing_enabled
: specifies whether the application routing add-on is enabled. When enabled, this add-on installs a managed instance of the NGINX Ingress Controller on the AKS cluster.dns_zone_name
: specifies the name of the Azure Public DNS zone used by the application routing add-on.dns_zone_resource_group_name
: specifies the resource group name of the Azure Public DNS zone used by the application routing add-on.grafana_admin_user_object_id
: specifies the object id of the Azure Managed Grafana administrator user account.vnet_integration_enabled
: specifies whether API Server VNet Integration is enabled.openai_deployments
: specifies the list of the Azure OpenAI Service models to create. This sample needs aGPT
model for chat completions and an embeddings model.
NOTE
We suggest reading sensitive configuration data such as passwords or SSH keys from a pre-existing Azure Key Vault resource. For more information, see Referencing Azure Key Vault secrets in Terraform. Before proceeding, also make sure to run theregister-preview-features.sh
Bash script in theterraform
folder to register any preview feature used by the AKS cluster.
OpenAI Terraform Module
The following table contains the code from the openai.tf
Terraform module used to deploy the Azure OpenAI Service.
Azure Cognitive Services use custom subdomain names for each resource created through the Azure portal, Azure Cloud Shell, Azure CLI, Bicep, Azure Resource Manager (ARM), or Terraform. Unlike regional endpoints, which were common for all customers in a specific Azure region, custom subdomain names are unique to the resource. Custom subdomain names are required to enable features like Microsoft Entra ID for authentication. In our case, we need to specify a custom subdomain for our Azure OpenAI Service as our chatbot application will use an Microsoft Entra ID security token to access it. By default, the main.tf
module sets the value of the custom_subdomain_name
parameter to the lowercase name of the Azure OpenAI resource. For more information on custom subdomains, see Custom subdomain names for Cognitive Services.
This terraform module allows you to pass an array containing the definition of one or more model deployments in the deployments
parameter. For more information on model deployments, see Create a resource and deploy a model using Azure OpenAI. As an alternative, you can use the Terraform module for deploying Azure OpenAI Service. to deploy an Azure OpenAI Service.
AKS Terraform Module
The following table shows the code of the Terraform module used to deploy the Azure Kubernetes Service (AKS) cluster with a user-assigned managed identity. For more information on the azurerm_kubernetes_cluster
resource, see Terraform documentation.
This module allows to deploy an Azure Kubernetes Service cluster with the following extensions and features:
- API Server VNET Integration allows you to enable network communication between the API server and the cluster nodes without requiring a private link or tunnel. AKS clusters with API Server VNET integration provide a series of advantages, for example, they can have public network access or private cluster mode enabled or disabled without redeploying the cluster. For more information, see Create an Azure Kubernetes Service cluster with API Server VNet Integration.
- Azure NAT Gateway to manage outbound connections initiated by AKS-hosted workloads.
- Event-driven Autoscaling (KEDA) add-on is a single-purpose and lightweight component that strives to make application autoscaling simple and is a CNCF Incubation project.
- Vertical Pod Autoscaling allows you to automatically sets resource requests and limits on containers per workload based on past usage. VPA makes certain pods are scheduled onto nodes that have the required CPU and memory resources. For more information, see Kubernetes Vertical Pod Autoscaling.
- Azure Key Vault Provider for Secrets Store CSI Driver provides a variety of methods of identity-based access to your Azure Key Vault.
- Image Cleaner to clean up stale images on your Azure Kubernetes Service cluster.
- Application routing add-on: this addon allows to deploy one or more instances of a managed NGINX ingress controller. The managed NGINX ingress controller deployed via the application routing add-on exposes many metrics for requests, the nginx process, and the controller that can be helpful in analyzing the performance and usage of your application. For more information, see Monitor the ingress-nginx controller metrics in the application routing add-on with Prometheus in Grafana.
NOTE
You can deploy an AKS resource as a public cluster with API Server VNet Integration enabled. During the installation, you can use Terraform modules that make use of the Helm, Kubectl, and Kubernetes Terraform Providers to install packages and create Kubernetes entities. Once the installation is complete, you can turn the cluster to private.
Private Endpoint and Private DNS Zone Terraform Modules
The main.tf
module creates Azure Private Endpoints and Azure Private DNDS Zones for each of the following resources:
- Azure OpenAI Service
- Azure Container Registry
- Azure Key Vault
- Azure Storage Account
In particular, it creates an Azure Private Endpoint and Azure Private DNDS Zone to the Azure OpenAI Service as shown in the following code snippet:
Below you can read the code of the private_dns_zone
and private_endpoint
modules used, respectively, to create the Azure Private Endpoints and Azure Private DNDS Zones.
Workload User-Defined Managed Identity
The following code snippet from the main.tf
Terraform module creates the user-defined managed identity used by the chatbot to acquire a security token from Microsoft Entra ID via Microsoft Entra Workload ID.
The above code snippet performs the following steps:
- Creates a new user-defined managed identity.
- Assign the new managed identity to the Cognitive Services User role with the resource group as a scope.
- Federate the managed identity with the service account used by the chatbot. The following information is necessary to create the federated identity credentials:
- The Kubernetes service account name.
- The Kubernetes namespace that will host the chatbot application.
- The URL of the OpenID Connect (OIDC) token issuer endpoint for Microsoft Entra Workload ID
For more information, see the following resources:
- How to configure Azure OpenAI Service with managed identities
- Use Microsoft Entra Workload ID with Azure Kubernetes Service (AKS)
Validate the deployment
Open the Azure Portal, and navigate to the resource group. Open the Azure Open AI Service resource, navigate to Keys and Endpoint
, and check that the endpoint contains a custom subdomain rather than the regional Cognitive Services endpoint.
Open to the <Prefix>WorkloadManagedIdentity
managed identity, navigate to the Federated credentials
, and verify that the federated identity credentials for the chainlit-sa
service account were created correctly, as shown in the following picture.
Use Microsoft Entra Workload ID with Azure Kubernetes Service (AKS)
Workloads deployed on an Azure Kubernetes Services (AKS) cluster require Microsoft Entra ID application credentials or managed identities to access Microsoft Entra ID protected resources, such as Azure Key Vault and Microsoft Graph. Microsoft Entra Workload ID integrates with the capabilities native to Kubernetes to federate with external identity providers.
Microsoft Entra Workload ID uses Service Account Token Volume Projection to enable pods to use a Kubernetes service account. When enabled, the AKS OIDC Issuer issues a service account security token to a workload and OIDC federation enables the application to access Azure resources securely with Microsoft Entra ID based on annotated service accounts.
Microsoft Entra Workload ID works well with the Azure Identity client libraries and the Microsoft Authentication Library (MSAL) collection if you use a registered application instead of a managed identity. Your workload can use any of these libraries to seamlessly authenticate and access Azure cloud resources. For more information, see the following resources:
- Azure Workload Identity open-source project
- Use an Microsoft Entra Workload ID on Azure Kubernetes Service (AKS
- Deploy and configure workload identity on an Azure Kubernetes Service (AKS) cluster
- Modernize application authentication with workload identity sidecar
- Tutorial: Use a workload identity with an application on Azure Kubernetes Service (AKS)
- Workload identity federation
- Use Microsoft Entra Workload ID for Kubernetes with a User-Assigned Managed Identity
- Use Microsoft Entra Workload ID for Kubernetes with an Microsoft Entra ID registered application
- Azure Managed Identities with Workload Identity Federation
- Microsoft Entra Workload ID federation with Kubernetes
- Microsoft Entra ID Workload Identity Federation with external OIDC Identy Providers
- Minimal Microsoft Entra Workload ID federation
Kubernetes Terraform module
This sample makes use of the kubernetes
of the Helm, Kubectl, and Kubernetes Terraform Providers to install packages and create Kubernetes entities:
- NGINX Ingress Controller
- Cert-Manager
- Prometheus: the AKS cluster is configured to collect metrics to the Azure Monitor workspace and Azure Managed Grafana. Nonetheless, the kube-prometheus-stack Helm chart is used to install Prometheus and Grafana on the AKS cluster.
- Workload namespace and service account: the Kubectl Terraform Provider and Kubernetes Terraform Provider are used to create the namespace and service account used by the chat applications.
- Azure Monitor ConfigMaps for Azure Monitor managed service for Prometheus
- Cluster Issuer used by the
cert-manager
.
The following table contains the Terraform module used to install the NGINX Ingress Controller:
Instead, the following module is used to create the workload namespace and service account:
In particular, the kubectl_manifest
resource makes use of variables to set the following service account annotations and labels necessary to Microsoft Entra Workload ID. For more information, see Service account labels and annotations.
Simple Chat Application
The Simple Chat Application is a large language model-based chatbot that allows users to submit general-purpose questions to a GPT model, which generates and streams back human-like and engaging conversational responses. The following picture shows the welcome screen of the chat application.
You can modify the welcome screen in markdown by editing the chainlit.md
file at the project's root. If you do not want a welcome screen, leave the file empty. The following picture shows what happens when a user submits a new message in the chat.
Chainlit can render messages in markdown format as shown by the following prompt:
Chainlit also provides classes to support the following elements:
- Audio: The
Audio
class allows you to display an audio player for a specific audio file in the chatbot user interface. You must provide either a URL or a path or content bytes. - Avatar: The
Avatar
class allows you to display an avatar image next to a message instead of the author's name. You need to send the element once. Next,, if an avatar's name matches an author's name, the avatar will be automatically displayed. You must provide either a URL or a path or content bytes. - File: The
File
class allows you to display a button that lets users download the content of the file. You must provide either a URL or a path or content bytes. - Image: The
Image
class is designed to create and handle image elements to be sent and displayed in the chatbot user interface. You must provide either a URL or a path or content bytes. - Pdf: The
Pdf
class allows you to display a PDF hosted remotely or locally in the chatbot UI. This class either takes a URL of a PDF hosted online or the path of a local PDF. - Pyplot: The
Pyplot
class allows you to display a Matplotlib pyplot chart in the chatbot UI. This class takes a pyplot figure. - TaskList: The
TaskList
class allows you to display a task list next to the chatbot UI. - Text: The
Text
class allows you to display a text element in the chatbot UI. This class takes a string and creates a text element that can be sent to the UI. It supports the markdown syntax for formatting text. You must provide either a URL or a path or content bytes.
You can click the user icon on the UI to access the chat settings and choose, for example, between the light and dark theme.
The application is built in Python. Let's take a look at the individual parts of the application code. In the following section, the Python code starts by importing the necessary packages/modules.
These are the libraries used by the chat application:
os
: This module provides a way of interacting with the operating system, enabling the code to access environment variables, file paths, etc.sys
: This module provides access to some variables used or maintained by the interpreter and functions that interact with the interpreter.openai
: The OpenAI Python library provides convenient access to the OpenAI API from applications written in Python. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the OpenAI API. You can find usage examples for the OpenAI Python library in our API reference and the OpenAI Cookbook.logging
: This module provides flexible logging of messages.chainlit as cl
: This imports the Chainlit library and aliases it ascl
. Chainlit is used to create the UI of the application.from azure.identity import DefaultAzureCredential, get_bearer_token_provider
: when theopenai_type
property value isazure_ad,
aDefaultAzureCredential
object from the Azure Identity client library for Python is used to acquire security token from the Microsoft Entra ID using the credentials of the user-defined managed identity federated with the service account.load_dotenv
anddotenv_values
fromdotenv
: Python-dotenv reads key-value pairs from a.env
file and can set them as environment variables. It helps in the development of applications following the 12-factor principles.
The requirements.txt
file under the src
folder contains the list of packages used by the chat applications. You can restore these packages in your environment using the following command:
Next, the code reads the value of the environment variables used to initialize Azure OpenAI objects. In addition, it creates a token provider for Azure OpenAI.
Here's a brief explanation of each variable and related environment variable:
temperature
: A float value representing the temperature for Create chat completion method of the OpenAI API. It is fetched from the environment variables with a default value of 0.9.api_base
: The base URL for the OpenAI API.api_key
: The API key for the OpenAI API. The value of this variable can be null when using a user-assigned managed identity to acquire a security token to access Azure OpenAI.api_type
: A string representing the type of the OpenAI API.api_version
: A string representing the version of the OpenAI API.engine
: The engine used for OpenAI API calls.model
: The model used for OpenAI API calls.system_content
: The content of the system message used for OpenAI API calls.max_retries
: The maximum number of retries for OpenAI API calls.timeout
: The timeout in seconds.
In the next section, the code creates the AsyncAzureOpenAI
client object used by the application to communicate with the Azure OpenAI Service instance. When the api_type
is equal to azure
, the code initializes the object with the API key. Otherwise, it initializes the azure_ad_token_provider
property to the token provider created earlier. Then the code creates a logger.
The backoff time is calculated using the backoff_in_seconds
and attempt
variables. It follows the formula backoff_in_seconds * 2 ** attempt + random.uniform(0, 1)
. This formula increases the backoff time exponentially with each attempt and adds a random value between 0 and 1 to avoid synchronized retries.
Next, the code defines a function called start_chat
that is used to initialize the UI when the user connects to the application or clicks the New Chat
button.
Here is a brief explanation of the function steps:
@cl.on_chat_start
: The on_chat_start decorator registers a callback functionstart_chat()
to be called when the Chainlit chat starts. It is used to set up the chat and send avatars for the Chatbot, Error, and User participants in the chat.cl.Avatar()
: the Avatar class allows you to display an avatar image next to a message instead of the author name. You need to send the element once. Next if the name of an avatar matches the name of an author, the avatar will be automatically displayed. You must provide either a URL or a path or content bytes.cl.user_session.set()
: This API call sets a value in the user_session dictionary. In this case, it initializes themessage_history
in the user's session with a system content message, which indicates the start of the chat.
Finally, the application defines the method called whenever the user sends a new message in the chat.
Here is a detailed explanation of the function steps:
@cl.on_message
: The on_message decorator registers a callback functionmain(message: str)
to be called when the user submits a new message in the chat. It is the main function responsible for handling the chat logic.cl.user_session.get()
: This API call retrieves a value from the user's session data stored in the user_session dictionary. In this case, it fetches themessage_history
from the user's session to maintain the chat history.message_history.append()
: This API call appends a new message to themessage_history
list. It is used to add the user's message and the assistant's response to the chat history.cl.Message()
: This API call creates a Chainlit Message object. TheMessage
class is designed to send, stream, edit, or remove messages in the chatbot user interface. In this sample, theMessage
object is used to stream the OpenAI response in the chat.msg.stream_token()
: The stream_token method of the Message class streams a token to the response message. It is used to send the response from the OpenAI Chat API in chunks to ensure real-time streaming in the chat.await openai.chat.completions.create()
: This API call sends a message to the OpenAI Chat API in an asynchronous mode and streams the response. It uses the providedmessage_history
as context for generating the assistant's response.
Below, you can read the complete code of the application.
You can run the application locally using the following command. The -w
flag` indicates auto-reload whenever we make changes live in our application code.
Documents QA Chat
The Documents QA Chat application allows users to submit up to 10 .pdf
and .docx
documents. The application processes the uploaded documents to create vector embeddings. These embeddings are stored in ChromaDB vector database for efficient retrieval. Users can pose questions about the uploaded documents and view the Chain of Thought, enabling easy exploration of the reasoning process. The completion message contains links to the text chunks in the documents that were used as a source for the response. The following picture shows the chat application interface. As you can see, you can click the Browse
button and choose up to 10 .pdf
and .docx
documents to upload. Alternatively, you can just drag and drop the files over the control area.
After uploading the documents, the application creates and stores embeddings to ChromaDB vector database. During the phase, the UI shows a message Processing <file-1>, <file-2>...
, as shown in the following picture:
When the code finished creating embeddings, the UI is ready to receive user's questions:
As your chat application grows in complexity, understanding the individual steps for generating a specific answer can become challenging. To solve this issue, Chainlit allows you to easily explore the reasoning process right from the user interface using the Chain of Thought. If you are using the LangChain integration, every intermediary step is automatically sent and displayed in the Chainlit UI just clicking and expanding the steps, as shown in the following picture:
To see the text chunks that were used by the large language model to originate the response, you can click the sources links, as shown in the following picture:
In the Chain of Thought, below each message, you can find an edit
button, as a bug icon, if that message was generated by a prompt. Clicking on it opens the Prompt Playground dialog which allows you to modify and iterate on the prompt as needed.
As shown in the following picture, you can click and edit the value of the highlighted variables in the user prompt:
You can then click and edit the user question.
Then, you can click the submit button to test the effect of your changes, as shown in the following picture.
Let's take a look at the individual parts of the application code. In the following section, the Python code starts by importing the necessary packages/modules.
These are the libraries used by the chat application:
os
: This module provides a way of interacting with the operating system, enabling the code to access environment variables, file paths, etc.sys
: This module provides access to some variables used or maintained by the interpreter and functions that interact with the interpreter.time
: This module provides various time-related functions for time manipulation and measurement.openai
: the OpenAI Python library provides convenient access to the OpenAI API from applications written in the Python language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses, which makes it compatible with a wide range of versions of the OpenAI API. You can find usage examples for the OpenAI Python library in our API reference and the OpenAI Cookbook.logging
: This module provides flexible logging of messages.chainlit as cl
: This imports the Chainlit library and aliases it ascl.
Chainlit is used to create the UI of the application.AzureChatOpenAI
fromchainlit.playground.config import
: you need to importAzureChatOpenAI
fromchainlit.playground.config
to use the Chainlit Playground.DefaultAzureCredential
fromazure.identity
: when theopenai_type
property value isazure_ad
, aDefaultAzureCredential
object from the Azure Identity client library for Python - version 1.13.0 is used to acquire security token from the Microsoft Entra ID using the credentials of the user-defined managed identity, whose client ID is defined in theAZURE_CLIENT_ID
environment variable.load_dotenv
anddotenv_values
fromdotenv
: Python-dotenv reads key-value pairs from a.env
file and can set them as environment variables. It helps in the development of applications following the 12-factor principles.langchain
: Large language models (LLMs) are emerging as a transformative technology, enabling developers to build applications that they previously could not. However, using these LLMs in isolation is often insufficient for creating a truly powerful app - the real power comes when you can combine them with other sources of computation or knowledge. LangChain library aims to assist in the development of those types of applications.
The requirements.txt
file under the src
folder contains the list of packages used by the chat applications. You can restore these packages in your environment using the following command:
Next, the code reads environment variables and configures the OpenAI settings.
Here's a brief explanation of each variable and related environment variable:
temperature
: A float value representing the temperature for Create chat completion method of the OpenAI API. It is fetched from the environment variables with a default value of 0.9.api_base
: The base URL for the OpenAI API.api_key
: The API key for the OpenAI API. The value of this variable can be null when using a user-assigned managed identity to acquire a security token to access Azure OpenAI.api_type
: A string representing the type of the OpenAI API.api_version
: A string representing the version of the OpenAI API.chat_completion_deployment
: the name of the Azure OpenAI GPT model for chat completion.embeddings_deployment
: the name of the Azure OpenAI deployment for embeddings.model
: The model used for chat completion calls (e.g,gpt-35-turbo-16k
).max_size_mb
: the maximum size for the uploaded documents.max_files
: the maximum number of documents that can be uploaded.text_splitter_chunk_size
: the maximum chunk size used by theRecursiveCharacterTextSplitter
object.text_splitter_chunk_overlap
: the maximum chunk overlap used by theRecursiveCharacterTextSplitter
object.embeddings_chunk_size
: the maximum chunk size used by theOpenAIEmbeddings
object.max_retries
: The maximum number of retries for OpenAI API calls.retry_min_seconds
: the minimum number of seconds before a retry.retry_max_seconds
: the maximum number of seconds before a retry.timeout
: The timeout in seconds.system_template
: The content of the system message used for OpenAI API calls.
Next, the code defines a function called start_chat
that is used to initialize the when the user connects to the application or clicks the New Chat
button.
Here is a brief explanation of the function steps:
@cl.on_chat_start
: The on_chat_start decorator registers a callback functionstart_chat()
to be called when the Chainlit chat starts. It is used to set up the chat and send avatars for the Chatbot, Error, and User participants in the chat.cl.Avatar()
: the Avatar class allows you to display an avatar image next to a message instead of the author name. You need to send the element once. Next if the name of an avatar matches the name of an author, the avatar will be automatically displayed. You must provide either a URL or a path or content bytes.
The following code is used to initialize the large language model (LLM) chain used to reply to questions on the content of the uploaded documents.
The AskFileMessage API call prompts the user to upload up to a specified number of .pdf
or .docx
files. The uploaded files are stored in the files
variable. The process continues until the user uploads files. For more information, see AskFileMessage.
The following code processes each uploaded file by extracting its content.
- The text content of each file is stored in the list
all_texts
. - This code performs text processing and chunking. It checks the file extension to read the file content accordingly, depending on if it's a
.pdf
or a.docx
document. - The text content is split into smaller chunks using the RecursiveCharacterTextSplitter LangChain object.
- Metadata is created for each chunk and stored in the
metadatas
list.
The next piece of code performs the following steps:
- It creates an AzureOpenAIEmbeddings configured to use the embeddings model in the Azure OpenAI Service to create embeddings from text chunks.
- It creates a ChromaDB vector database using the
OpenAIEmbeddings
object, the text chunks list, and the metadata list. - It creates an AzureChatOpenAI LangChain object based on the GPR model hosted in Azure OpenAI Service.
- It creates a chain using the RetrievalQAWithSourcesChain.from_chain_type API call uses previously created models and stores them as retrievers.
- It stores the metadata and text chunks in the user session using the
cl.user_session.set()
API call. - It creates a message to inform the user that the files are ready for queries, and finally returns the
chain
. - The
cl.user_session.set("chain", chain)
call stores the LLM chain in the user_session dictionary for later use.
The next section create the LangChain LLM chain.
The following code handles the communication with the OpenAI API and incorporates retrying logic in case the API calls fail due to specific errors.
@cl.on_message
: The on_message decorator registers a callback functionmain(message: str)
to be called when the user submits a new message in the chat. It is the main function responsible for handling the chat logic.cl.user_session.get("chain")
: this call retrieves the LLM chain from the user_session dictionary.cl.AsyncLangchainCallbackHandler
: this call creates a LangChain callback handler.await chain.acall
: The asynchronous call to the RetrievalQAWithSourcesChain.acall executes the LLM chain with the user message as an input.
The code below extracts the answers and sources from the API response and formats them to be sent as a message.
- The
answer
andsources
are obtained from theresponse
dictionary. - The sources are then processed to find corresponding texts in the user session metadata (
metadatas
) and createsource_elements
usingcl.Text()
. cl.Message().send()
: the Message API creates and displays a message containing the answer and sources, if available.- The last command sets the
AZURE_OPENAI_API_KEY
environment variable to a security key to access Azure OpenAI returned by the token provider. This key is used by the Chainlit playground.
Below, you can read the complete code of the application.
You can run the application locally using the following command. The -w
flag` indicates auto-reload whenever we make changes live in our application code.
Build Docker Images
You can use the src/01-build-docker-images.sh
Bash script to build the Docker container image for each container app.
Before running any script in the src
folder, make sure to customize the value of the variables inside the 00-variables.sh
file located in the same folder. This file is embedded in all the scripts and contains the following variables:
The Dockerfile
under the src
folder is parametric and can be used to build the container images for both chat applications.
Test applications locally
You can use the src/02-run-docker-container.sh
Bash script to test the containers for the sender
, processor
, and receiver
applications.
Push Docker containers to the Azure Container Registry
You can use the src/03-push-docker-image.sh
Bash script to push the Docker container images for the sender
, processor
, and receiver
applications to the Azure Container Registry (ACR).
Deployment Scripts
If you deployed the Azure infrastructure using the Terraform modules provided with this sample, you only need to deploy the application using the following scripts and YAML templates in the scripts
folder.
Scripts:
09-deploy-apps.sh
10-configure-dns.sh
YAML manifests:
configMap.yml
deployment.yml
ingress.yml
service.yml
If you instead want to deploy the application in your AKS cluster, make sure to run all of the scripts in order.
The 09-deploy-apps.sh
script creates the configmap, deployment, service, and ingress Kubernetes objects for the chat
and docs
applications. This script makes use of the yq tool to customize the manifests with the value of the variables defined in the 00-variables.sh
file. This tool is a lightweight and portable command-line YAML, JSON and XML processor that uses jq like syntax but works with YAML files as well as json, xml, properties, csv and tsv. It doesn't yet support everything jq does - but it does support the most common operations and functions, and more is being added continuously.
The 10-configure-dns.sh
script creates an A record in the Azure Public DNS Zone to expose the chat
and docs
applications via a given subdomain (e.g., https://chat.contoso.com
).
YAML manifests
Below you can read the YAML manifests used to deploy the chat
chatbot to AKS. For brevity, I will cover only the installation of this application, but you can find all the YAML manifests in the companion GitHub repository. The chat-configmap.yml
defines a value for the environment variables passed to the application container. The configmap does not define any environment variable for the OpenAI key as the container.
These are the parameters defined by the configmap:
TEMPERATURE
: the temperature used by the OpenAI API to generate the response.AZURE_OPENAI_TYPE
: specifyazure
if you want to let the application use the API key to authenticate against OpenAI. In this case, make sure to provide the Key in theAZURE_OPENAI_KEY
environment variable. If you want to authenticate using an Microsoft Entra ID security token, you need to specifyazure_ad
as a value. In this case, don't need to provide any value in theAZURE_OPENAI_KEY
environment variable.AZURE_OPENAI_BASE
: the URL of your Azure OpenAI resource. If you use the API key to authenticate against OpenAI, you can specify the regional endpoint of your Azure OpenAI Service (e.g., https://eastus.api.cognitive.microsoft.com/). If you instead plan to use Microsoft Entra ID security tokens for authentication, you need to deploy your Azure OpenAI Service with a subdomain and specify the resource-specific endpoint url (e.g., https://myopenai.openai.azure.com/).AZURE_OPENAI_KEY
: the key of your Azure OpenAI resource. If you setAZURE_OPENAI_TYPE
toazure_ad
you can leave this parameter empty.AZURE_OPENAI_VERSION
: A string representing the version of the OpenAI API.AZURE_OPENAI_DEPLOYMENT
: the name of the ChatGPT deployment used by your Azure OpenAI resource, for examplegpt-35-turbo
.AZURE_OPENAI_MODEL
: the name of the ChatGPT model used by your Azure OpenAI resource, for examplegpt-35-turbo
.AZURE_OPENAI_SYSTEM_MESSAGE
: The content of the system message used for OpenAI API calls. You can use it to describe the assistant's personality.
The chat-deployment.yml
manifest is used create a Kubernetes deployment that defines the application pods to create. azure.workload.identity/use label is required in the pod template spec. Only pods with this label will be mutated by the azure-workload-identity mutating admission webhook to inject the Azure specific environment variables and the projected service account token volume.
The application is exposed using a ClusterIP
Kubernetes service.
The ingress.yml
manifest defines a Kubernetes ingress object used to expose the service via the NGINX Ingress Controller. This project deploys a managed NGINX Ingress Controller using the application routing add-on and an unmanaged instance of the NGINX Ingress Controller using the Helm Terrafom Provider and related chart. The Terraform module creates two clusterissuer
objects, one for the managed and one for the unmanaged version of the NGINX Ingress Controller. You can run the following command to see the two ingress classes:
Executing the command will produce a result as follows:
Run the following command to retrieve the cluster issuers used by the cert-manager
:
The above command should return a result as follows:
The chat-ingress
contains the code of the ingress object used to expose the chat
application. This version of the ingress makes use of the unmanaged instance of the NGINX Ingress Controller.
This version of the ingress makes use of the managed instance of the NGINX Ingress Controller installed by the application routing addon.
The ingress object defines the following annotations:
- cert-manager.io/cluster-issuer: specifies the name of a cert-manager.io ClusterIssuer to acquire the certificate required for this Ingress. It does not matter which namespace your Ingress resides, as ClusterIssuers are non-namespaced resources. In this sample, the cert-manager is instructed to use the
letsencrypt-nginx
ClusterIssuer that you can create using the06-create-cluster-issuer.sh
script. - cert-manager.io/acme-challenge-type: specifies the challend type.
- nginx.ingress.kubernetes.io/affinity: you need to specify this annotation to
cookie
as Chainlit uses WebSockets and requires cookie-based affinity - nginx.ingress.kubernetes.io/proxy-connect-timeout: specifies the connection timeout in seconds.
- nginx.ingress.kubernetes.io/proxy-send-timeout: specifies the send timeout in seconds.
- nginx.ingress.kubernetes.io/proxy-read-timeout: specifies the read timeout in seconds.
- nginx.ingress.kubernetes.io/proxy-next-upstream-timeout: specifies the next upstream timeout in seconds.
- nginx.ingress.kubernetes.io/enable-cors: to enable Cross-Origin Resource Sharing (CORS) in an Ingress rule, set this annotation to
true
. - nginx.ingress.kubernetes.io/cors-allow-origin: controls what's the accepted Origin for CORS.
- nginx.ingress.kubernetes.io/cors-allow-credentials: controls if credentials can be passed during CORS operations.
Clean up resources
You can delete the resource group using the following Azure CLI command when you no longer need the resources you created. This will remove all the Azure resources.
Alternatively, you can use the following PowerShell cmdlet to delete the resource group and all the Azure resources.