RepoMicrosoftMicrosoftpublished Apr 6, 2023seen 6d

microsoft/sample-app-aoai-chatGPT

Python

Open original ↗

Captured source

source ↗

microsoft/sample-app-aoai-chatGPT

Description: Sample code for a simple web chat experience through Azure OpenAI, including Azure OpenAI On Your Data.

Language: Python

License: MIT

Stars: 1923

Forks: 3080

Open issues: 12

Created: 2023-04-06T21:16:41Z

Pushed: 2026-06-20T03:59:06Z

Default branch: main

Fork: no

Archived: no

README:

[Preview] Sample Chat App with AOAI

This repo contains sample code for a simple chat webapp that integrates with Azure OpenAI. Note: some portions of the app use preview APIs.

Prerequisites

  • An existing Azure OpenAI resource and model deployment of a chat model (e.g. gpt-35-turbo-16k, gpt-4)
  • To use Azure OpenAI on your data, one of the following data sources:
  • Azure AI Search Index
  • Azure CosmosDB Mongo vCore vector index
  • Elasticsearch index (preview)
  • Pinecone index (private preview)
  • Azure SQL Server (private preview)
  • Mongo DB (preview)

Configure the app

Create a .env file for local development

Follow instructions below in the [app configuration](#app-settings) section to create a .env file for local development of your app. This file can be used as a reference to populate the app settings for your Azure App Service deployed webapp.

Create a JSON file for populating Azure App Service app settings

After creating your .env file, run one of the following commands in your preferred shell to create a JSON representation of your environment which is recognized by Azure App Service.

Powershell

Get-Content .env | ForEach-Object {
if ($_ -match "(?[A-Z_]+)=(?.*)") {
[PSCustomObject]@{
name = $matches["name"]
value = $matches["value"]
slotSetting = $false
}
}
} | ConvertTo-Json | Out-File -FilePath env.json

Bash

cat .env | jq -R '. | capture("(?[A-Z_]+)=(?.*)")' | jq -s '.[].slotSetting=false' > env.json

Deploy the app

Deploy with Azure Developer CLI

Please see [README_azd.md](./README_azd.md) for detailed instructions.

One click Azure deployment

![Deploy to Azure](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2Fmicrosoft%2Fsample-app-aoai-chatGPT%2Fmain%2Finfrastructure%2Fdeployment.json)

Click on the Deploy to Azure button and configure your settings in the Azure Portal as described in the [Environment variables](#environment-variables) section.

Please see the [section below](#add-an-identity-provider) for important information about adding authentication to your app.

Deploy from your local machine

1. Follow the steps below in the [app configuration](#app-settings) section to construct your .env file with the appropriate variables for your use case.

2. Start the app with start.cmd. This will build the frontend, install backend dependencies, and then start the app. Or, just run the backend in debug mode using the VSCode debug configuration in .vscode/launch.json.

3. You can see the local running app at http://127.0.0.1:50505.

Deploy with the Azure CLI

Create the Azure App Service

NOTE: If you've made code changes, be sure to build the app code with start.cmd or start.sh before you deploy, otherwise your changes will not be picked up. If you've updated any files in the frontend folder, make sure you see updates to the files in the static folder before you deploy.

You can use the Azure CLI to deploy the app from your local machine. Make sure you have version 2.48.1 or later.

If this is your first time deploying the app, you can use az webapp up. Run the following command from the root folder of the repo, updating the placeholder values to your desired app name, resource group, location, and subscription. You can also change the SKU if desired.

az webapp up --runtime PYTHON:3.11 --sku B1 --name --resource-group --location --subscription

Note: if using the Azure CLI version 2.62 or greater, you may also want to add the flag --track-status False to prevent the command from failing due to startup errors. Startup errors can be solved by following the instructions in the next section about [updating app configuration](#update-app-configuration).

Update app configuration

After creating your Azure App Service, follow these steps to update the configuration to allow your application to properly start up.

1. Set the app startup command

az webapp config set --startup-file "python3 -m gunicorn app:app" --name

2. Set WEBSITE_WEBDEPLOY_USE_SCM=false to allow local code deployment.

az webapp config appsettings set -g -n --settings WEBSITE_WEBDEPLOY_USE_SCM=false

3. Set all of your app settings in your local .env file at once by [creating a JSON representation](#create-a-json-file-for-populating-azure-app-service-app-settings) of the .env file, and then run the following command.

az webapp config appsettings set -g -n --settings "@env.json"

Update an existing app

Check the runtime stack for your app by viewing the app service resource in the Azure Portal. If it shows "Python - 3.10", use PYTHON:3.10 in the runtime argument below. If it shows "Python - 3.11", use PYTHON:3.11 in the runtime argument below.

Check the SKU in the same way. Use the abbreviated SKU name in the argument below, e.g. for "Basic (B1)" the SKU is B1.

Then, use these commands to deploy your local code to the existing app:

1. az webapp up --runtime --sku --name --resource-group 1. az webapp config set --startup-file "python3 -m gunicorn app:app" --name

Make sure that the app name and resource group match exactly for the app that was previously deployed.

Deployment will take several minutes. When it completes, you should be able to navigate to your app at {app-name}.azurewebsites.net.

Authentication

Add an identity provider

After deployment, you will need to add an identity provider to provide authentication support in your app. See this tutorial for more information.

If you don't add an identity provider, the chat functionality of your app will be blocked to prevent unauthorized access to your resources and data.

To remove this restriction, you can add AUTH_ENABLED=False to the environment variables. This will disable authentication and allow...

Excerpt shown — open the source for the full document.

Notability

notability 6.0/10

Sample app for Azure OpenAI ChatGPT with moderate traction.