⭐ If you like this sample, star it on GitHub — it helps a lot!
Overview • Get started • Run the sample • Resources • FAQ • Guidance
This sample shows how to deploy a secure Azure OpenAI infrastructure with reusable components to build a web UI with authentication. It provides a starting point for building secure AI chat applications, using RBAC permissions and OpenAI API SDKs with keyless (Entra) authentication. The backend resources are secured within an Azure Virtual Network, and the frontend is hosted on Azure Static Web Apps.
Building AI applications can be complex and time-consuming, but using accelerator components with Azure allows to greatly simplify the process. This template provides a starting point for building a secure UI with Azure OpenAI, using a keyless authentication mechanism and a virtual network to secure the backend resources. It also demonstrates how to set up user authentication and authorization with configurable providers with Azure Static Web Apps Easy Auth.
This application is made from multiple components:
-
Reusable and customizable web components built with Lit handling user authentication and providing an AI chat UI. The code is located in the
packages/ai-chat-components
folder. -
Example web app integrations of the web components, hosted on Azure Static Web Apps. There are example using static HTML, React, Angular, Vue and Svelte.
-
A serverless API built with Azure Functions and using OpenAI SDK to generate responses to the user chat queries. The code is located in the
packages/api
folder.
We use the HTTP protocol for AI chat apps to communicate between the web app and the API.
- Secure deployments: Uses Azure Managed Identity for keyless authentication and Azure Virtual Network to secure the backend resources.
- Reusable components: Provides reusable web components for building secure AI chat applications.
- Serverless Architecture: Utilizes Azure Functions and Azure Static Web Apps for a fully serverless deployment.
- Scalable and Cost-Effective: Leverages Azure's serverless offerings to provide a scalable and cost-effective solution.
- Local Development: Supports local development using Ollama for testing without any cloud costs.
There are multiple ways to get started with this project.
The quickest way is to use GitHub Codespaces that provides a preconfigured environment for you. Alternatively, you can set up your local environment following the instructions below.
Important
If you want to run this sample entirely locally using Ollama, you have to follow the instructions in the local environment section.
You need to install following tools to work on your local machine:
- Node.js LTS
- Azure Developer CLI
- Git
- PowerShell 7+ (for Windows users only)
- Important: Ensure you can run
pwsh.exe
from a PowerShell command. If this fails, you likely need to upgrade PowerShell. - Instead of Powershell, you can also use Git Bash or WSL to run the Azure Developer CLI commands.
- Important: Ensure you can run
Then you can get the project code:
- Fork the project to create your own copy of this repository.
- On your forked repository, select the Code button, then the Local tab, and copy the URL of your forked repository.
git clone <your-repo-url>
You can run this project directly in your browser by using GitHub Codespaces, which will open a web-based VS Code:
A similar option to Codespaces is VS Code Dev Containers, that will open the project in your local VS Code instance using the Dev Containers extension.
You will also need to have Docker installed on your machine to run the container.
There are multiple ways to run this sample: locally using Ollama or Azure OpenAI models for testing, or by deploying it to Azure.
- Azure account. If you're new to Azure, get an Azure account for free to get free Azure credits to get started. If you're a student, you can also get free credits with Azure for Students.
- Azure subscription with access enabled for the Azure OpenAI service. You can request access with this form.
- Azure account permissions:
- Your Azure account must have
Microsoft.Authorization/roleAssignments/write
permissions, such as Role Based Access Control Administrator, User Access Administrator, or Owner. If you don't have subscription-level permissions, you must be granted RBAC for an existing resource group and deploy to that existing group by running these commands:azd env set AZURE_RESOURCE_GROUP <name of existing resource group> azd env set AZURE_LOCATION <location of existing resource group>
- Your Azure account also needs
Microsoft.Resources/deployments/write
permissions on the subscription level.
- Your Azure account must have
See the cost estimation details for running this sample on Azure.
- Open a terminal and navigate to the root of the project.
- Authenticate with Azure by running
azd auth login
. - Run
azd up
to deploy the application to Azure. This will provision Azure resources, deploy this sample, and build the search index based on the files found in the./data
folder.- You will be prompted to select a base location for the resources. If you're unsure of which location to choose, select
eastus2
. - By default, the OpenAI resource will be deployed to
eastus2
. You can set a different location withazd env set AZURE_OPENAI_RESOURCE_GROUP_LOCATION <location>
. Currently only a short list of locations is accepted. That location list is based on the OpenAI model availability table and may become outdated as availability changes.
- You will be prompted to select a base location for the resources. If you're unsure of which location to choose, select
The deployment process will take a few minutes. Once it's done, you'll see the URL of the web app in the terminal.
You can now open the web app in your browser and start chatting with the bot.
By default, the sample deploys the static HTML version of the webapp. However, we provide example integrations of the UI web components with different web app frameworks:
If you want to switch the deployment to use any of these, edit the file azure.yaml
in the root of the project and changes the project path to the one you want to deploy for the webapp service:
services:
webapp:
# Change here to the path of the web app you want to deploy,
# for example, to deploy the React version of the web app
# change it to ./packages/webapp-react
project: ./packages/webapp-html
To clean up all the Azure resources created by this sample:
- Run
azd down --purge
- When asked if you are sure you want to continue, enter
y
The resource group and all the resources will be deleted.
If you have a machine with enough resources, you can run this sample entirely locally without using any cloud resources. To do that, you first have to install Ollama and then run the following commands to download the models on your machine:
ollama pull phi3
Note
The phi3
model with download a few gigabytes of data, so it can take some time depending on your internet connection.
After that you have to install the NPM dependencies:
npm install
Next, create a .env
file in the packages/api
folder with the following content:
OPENAI_BASE_URL=http://localhost:11434/v1
OPENAI_API_KEY=__dummy
OPENAI_MODEL_NAME=phi3
Then you can start the application by running the following command which will start the web app and the API locally:
npm start
You can now open the URL http://localhost:4280
in your browser, use the authentication emulator to connect to the web app, and start chatting with the bot.
First you need to provision the Azure resources needed to run the sample. Follow the instructions in the Deploy the sample to Azure section to deploy the sample to Azure, then you'll be able to run the sample locally using the deployed Azure resources.
Once your deployment is complete, you should see a .env
file in the packages/api
folder. This file contains the environment variables needed to run the application using Azure resources.
To run the sample, you can then use the same commands as for the Ollama setup. This will start the web app and the API locally:
npm start
Open the URL http://localhost:4280
in your browser, use the authentication emulator to connect to the web app, and start chatting with the bot.
Here are some resources to learn more about Azure OpenAI and related technologies:
- Serverless AI Chat sample
- Generative AI with JavaScript
- Generative AI For Beginners
- Azure OpenAI Service
- Chat + Enterprise data with Azure OpenAI and Azure AI Search
You can also find more Azure AI samples here.
You can find answers to frequently asked questions in the FAQ.
This template uses model gpt-4o-mini
which may not be available in all Azure regions. Check for up-to-date region availability and select a region during deployment accordingly.
We recommend using East US 2
if you're unsure of which region to choose.
This template has Managed Identity built in to eliminate the need for developers to manage these credentials. Applications can use managed identities to obtain Microsoft Entra tokens without having to handle any secrets in the code. Additionally, we're using Microsoft Security DevOps GitHub Action to scan the infrastructure-as-code files and generates a report containing any detected issues.
If you have any issue when running or deploying this sample, please check the troubleshooting guide. If you can't find a solution to your problem, please open an issue in this repository.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.