The Simple Chat Application is designed to enable users to interact with a generative AI model via a web-based chat interface. It supports Retrieval-Augmented Generation (RAG), allowing users to enhance the AI’s responses with custom data by uploading documents. The application uses inline temporary file storage for short-term processing and Azure AI Search for long-term document retrieval and storage, enabling efficient hybrid searches. The application is built to run on Azure App Service, tailored for use in Azure Government, but it also works seamlessly in Azure Commercial environments.
https://github.com/user-attachments/assets/b79bea89-e24a-461b-bf75-31c79f1b18bf
The Simple Chat Application utilizes Azure AI Search for document retrieval. The user-index.json
file contains the schema for creating the search index that will store user documents and metadata. Here's how to initialize the Azure AI Search index directly through the Azure portal using the user-index.json
file.
Access the Azure Portal:
Navigate to Indexes:
Create Index from user-index.json
:
In the Add Index from JSON screen, you'll need to manually enter the index schema. Open the user-index.json
file from the artifacts/
folder in your project to view the schema, which includes fields, types, and configurations.
📁 SimpleChat
└── 📁 artifacts
└── user-index.json
Copy and Paste from user-index.json
file into the open menu on the right
Verify Index Creation:
simplechat-user-index
).To secure access to the Simple Chat Application, we configure authentication using Azure Active Directory (Azure AD). The app is registered in Microsoft Entra ID (formerly Azure AD) to allow users to log in using their organizational credentials. Below are the steps for setting up authentication in Azure App Service and configuring the Azure AD App Registration for the application.
Authentication is enabled directly in the Azure App Service that hosts the application, ensuring that only authenticated users can access the app.
Access the Azure Portal:
Add a Provider (Microsoft):
Edit Authentication Settings:
az-webapp-demo-chat
00000000-0000-0000-0000-000000000000
MICROSOFT_PROVIDER_AUTHENTICATION_SECRET
https://sts.windows.net/00000000-0000-0000-0000-000000000000/v2.0
api://000000000-0000-0000-0000-000000000000
The App Registration is where you define how users will authenticate against Azure AD when accessing your application.
Navigate to App Registrations:
az-webapp-demo-chat
.Set Redirect URIs: Under the Authentication tab in the app registration, configure the following URIs:
https://az-webapp-demo-chat.azurewebsites.us/getAToken
This URI is used to handle the OAuth 2.0 authorization code flow and retrieve the authentication token.
https://az-webapp-demo-chat.azurewebsites.us/logout
This URL is used to log out users from the application and Azure AD session.
Configure API Permissions:
Client Secret:
MICROSOFT_PROVIDER_AUTHENTICATION_SECRET
in your app's environment variables.Once authentication is set up, you can configure access controls using Azure AD’s Enterprise Applications. This allows you to manage which users or groups can sign in to your application.
Navigate to Enterprise Applications:
Select the Application:
azgov-webapp-demo-chat-az
).Assign Users and Groups:
Verify Access:
By following these steps, you'll ensure that your Simple Chat Application is secure and accessible only to authorized users via Azure AD.
.env
FileThe application relies on several environment variables for proper configuration. These variables are defined in the .env
file. Follow the steps below to modify the example.env
file and upload it to your Azure App Service.
example.env
FileRename the example.env
file to .env
:
example.env
to .env
.Open the .env
file in your text editor (e.g., VS Code) and replace the placeholder values with your specific configuration details. Here’s an example:
# General Application Settings
SCM_DO_BUILD_DURING_DEPLOYMENT="true"
WEBSITE_HTTPLOGGING_RETENTION_DAYS="7"
FLASK_KEY="<your-flask-secret-key>"
# Application Insights
APPLICATIONINSIGHTS_CONNECTION_STRING="InstrumentationKey=<your-instrumentation-key>;EndpointSuffix=<your-endpoint-suffix>;IngestionEndpoint=<your-ingestion-endpoint>;AADAudience=<your-aad-audience>;ApplicationId=<your-application-id>"
ApplicationInsightsAgent_EXTENSION_VERSION="~3"
APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED="true"
XDT_MicrosoftApplicationInsights_Mode="default"
# Azure OpenAI
AZURE_OPENAI_API_TYPE="azure"
AZURE_OPENAI_KEY="<your-openai-api-key>"
AZURE_OPENAI_ENDPOINT="<your-openai-endpoint>"
AZURE_OPENAI_API_VERSION="2024-02-15-preview"
AZURE_OPENAI_LLM_MODEL="gpt-4o"
AZURE_OPENAI_EMBEDDING_MODEL="text-embedding-ada-002"
# Azure Cosmos DB
AZURE_COSMOS_ENDPOINT="<your-cosmosdb-endpoint>"
AZURE_COSMOS_KEY="<your-cosmosdb-key>"
AZURE_COSMOS_DB_NAME="SimpleChat"
AZURE_COSMOS_DOCUMENTS_CONTAINER_NAME="documents"
AZURE_COSMOS_CONVERSATIONS_CONTAINER_NAME="conversations"
# Azure AI Search
AZURE_AI_SEARCH_ENDPOINT="<your-ai-search-endpoint>"
AZURE_AI_SEARCH_KEY="<your-ai-search-key>"
AZURE_AI_SEARCH_USER_INDEX="simplechat-user-index"
AZURE_AI_SEARCH_GROUP_INDEX="simplechat-group-index"
# Azure Document Intelligence
AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT="<your-document-intelligence-endpoint>"
AZURE_DOCUMENT_INTELLIGENCE_KEY="<your-document-intelligence-key>"
# Azure AD Authentication
WEBSITE_AUTH_AAD_ALLOWED_TENANTS="<your-allowed-tenant-id>"
MICROSOFT_PROVIDER_AUTHENTICATION_SECRET="<your-authentication-secret>"
CLIENT_ID="<your-client-id>"
TENANT_ID="<your-tenant-id>"
Save your changes.
.env
File to Azure App ServiceTo upload the .env
file to your Azure App Service, follow these instructions:
Open Command Palette in VS Code:
Ctrl + Shift + P
(or Cmd + Shift + P
on macOS) to open the Command Palette.Search for Upload Command:
Select the .env
File:
.env
file you modified in the previous step.Choose Your Azure Subscription:
Select Your App Service:
.env
file and automatically set the environment variables.If you prefer, you can update your environment variables directly in the Azure Portal using the Advanced Edit feature. This method allows you to paste a JSON configuration, which can be especially useful for bulk updates or when setting up a new environment.
[!NOTE]
Replace the placeholder values (e.g.,
MICROSOFT_PROVIDER_AUTHENTICATION_SECRET
,FLASK_KEY
, etc.) with your actual configuration values.
[
{ "name": "MICROSOFT_PROVIDER_AUTHENTICATION_SECRET", "value": "", "slotSetting": true },
{ "name": "WEBSITE_AUTH_AAD_ALLOWED_TENANTS", "value": "", "slotSetting": false },
{ "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", "value": "true", "slotSetting": false },
{ "name": "WEBSITE_HTTPLOGGING_RETENTION_DAYS", "value": "7", "slotSetting": false },
{ "name": "FLASK_KEY", "value": "", "slotSetting": false },
{ "name": "APPLICATIONINSIGHTS_CONNECTION_STRING", "value": "InstrumentationKey=;EndpointSuffix=;IngestionEndpoint=;AADAudience=;ApplicationId=", "slotSetting": false },
{ "name": "ApplicationInsightsAgent_EXTENSION_VERSION", "value": "~3", "slotSetting": false },
{ "name": "APPLICATIONINSIGHTSAGENT_EXTENSION_ENABLED", "value": "true", "slotSetting": false },
{ "name": "XDT_MicrosoftApplicationInsights_Mode", "value": "default", "slotSetting": false },
{ "name": "AZURE_OPENAI_API_TYPE", "value": "azure", "slotSetting": false },
{ "name": "AZURE_OPENAI_KEY", "value": "", "slotSetting": false },
{ "name": "AZURE_OPENAI_ENDPOINT", "value": "", "slotSetting": false },
{ "name": "AZURE_OPENAI_API_VERSION", "value": "2024-02-15-preview", "slotSetting": false },
{ "name": "AZURE_OPENAI_LLM_MODEL", "value": "gpt-4o", "slotSetting": false },
{ "name": "AZURE_OPENAI_EMBEDDING_MODEL", "value": "text-embedding-ada-002", "slotSetting": false },
{ "name": "AZURE_COSMOS_ENDPOINT", "value": "", "slotSetting": false },
{ "name": "AZURE_COSMOS_KEY", "value": "", "slotSetting": false },
{ "name": "AZURE_COSMOS_DB_NAME", "value": "SimpleChat", "slotSetting": false },
{ "name": "AZURE_COSMOS_DOCUMENTS_CONTAINER_NAME", "value": "documents", "slotSetting": false },
{ "name": "AZURE_COSMOS_CONVERSATIONS_CONTAINER_NAME", "value": "conversations", "slotSetting": false },
{ "name": "AZURE_AI_SEARCH_ENDPOINT", "value": "", "slotSetting": false },
{ "name": "AZURE_AI_SEARCH_KEY", "value": "", "slotSetting": false },
{ "name": "AZURE_AI_SEARCH_USER_INDEX", "value": "simplechat-user-index", "slotSetting": false },
{ "name": "AZURE_AI_SEARCH_GROUP_INDEX", "value": "simplechat-group-index", "slotSetting": false },
{ "name": "AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT", "value": "", "slotSetting": false },
{ "name": "AZURE_DOCUMENT_INTELLIGENCE_KEY", "value": "", "slotSetting": false },
{ "name": "CLIENT_ID", "value": "", "slotSetting": false },
{ "name": "TENANT_ID", "value": "", "slotSetting": false }
]
slotSetting
flag is set to true
for sensitive or environment-specific variables (e.g., secrets). This ensures that these variables are not affected by swapping deployment slots (e.g., staging and production).By using the Advanced Edit function and pasting this JSON, you can easily manage and update your environment variables in a single step.
.env
file have been added correctly.<your-flask-secret-key>
, <your-instrumentation-key>
, <your-endpoint-suffix>
, etc., with the appropriate values for your environment.AZURE_OPENAI_ENDPOINT
, AZURE_COSMOS_ENDPOINT
, AZURE_AI_SEARCH_ENDPOINT
, and other endpoints, depending on the region (e.g., *.azure.us
for Azure Government and *.azure.com
for Azure Commercial).This generalization allows you to switch between environments by simply updating the placeholders.
.env
file, you will need to re-upload it to Azure App Service..env
files for different environments (e.g., development, staging, production) to manage configuration settings effectively.By following these steps, your environment variables will be configured correctly, ensuring the application functions as expected in your Azure App Service environment.
Clone the repository to your local environment:
git clone https://github.com/your-repo/SimpleChat.git
cd SimpleChat
Install the required dependencies:
pip install -r requirements.txt
Deploy the application to Azure App Service using your preferred method (Azure CLI, Visual Studio Code, etc.). Ensure that the environment variables are properly configured in the Azure environment.
Locally: You can run the application locally for testing:
flask run
On Azure: After deployment, navigate to the Azure App Service URL to interact with the application.
For deployments in Azure Government, ensure that the endpoints for Azure OpenAI, Azure Cosmos DB, Azure Cognitive Search, and Azure Document Intelligence are set to the correct .azure.us
suffix.
The Simple Chat Application provides a streamlined user experience for interacting with an AI-powered chat system, enhanced by document retrieval through Azure AI Search. Below is an overview of the typical workflow a user will follow when interacting with the application.
To access the application's features, users must log in using their Azure Active Directory (Azure AD) credentials.
After logging in, users can start a conversation with the AI directly from the chat interface.
To enhance AI responses or perform document-specific searches, users can upload their own documents into the system.
With documents uploaded, users can enhance the AI's responses by utilizing the hybrid search functionality.
Users can view and manage their uploaded documents through the document management interface.
All user conversations are stored in Azure Cosmos DB, allowing users to revisit previous chats.
This project is licensed under the MIT License. See the LICENSE file for details.