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 both in Azure Commercial and Azure Government.
Simple.Chat_.AI-Powered.Chat.-.v0.190.mp4
Important Change:
- Azure OpenAI and Bing Search configuration is now done via the Admin Settings page within the application, rather than being stored directly in environment variables (
.env
file). This allows for easier updates and toggling of features like GPT, embeddings, web search (Bing), and image generation at runtime.
- Chat with AI: Interact with an AI model based on OpenAI's GPT.
- RAG with Hybrid Search: Upload documents and perform hybrid searches, retrieving relevant information from your files.
- Document Management: Upload, store, and manage multiple versions of documents (personal or group-level).
- Group Management: Create and join groups to share access to group-specific documents (RBAC).
- Ephemeral (Single-Convo) Documents: Upload temporary documents available only during the current chat session.
- Azure Cosmos DB: Stores conversations, documents, and metadata.
- Azure Cognitive Search: Facilitates efficient search and retrieval of document data.
- Azure Document Intelligence: Extracts data from various document formats, including PDFs, Word documents, and images.
- Optional Bing Web Search: Toggle web-search-based augmentation from the Admin Settings.
- Optional Image Generation: Toggle image generation with Azure OpenAI from the Admin Settings.
- Authentication & RBAC: Secured via Azure Active Directory (AAD) integration, using MSAL (Microsoft Authentication Library), plus group-based access control and app roles.
-
Admin Settings UI
- Configure Azure OpenAI GPT, Embeddings, Image Generation, and Bing Search settings directly through an in-app interface (rather than
.env
). - Choose between key-based or managed identity authentication for GPT, Embeddings, and Image Generation.
- Dynamically switch models/deployments without redeploying the app.
- Configure Azure OpenAI GPT, Embeddings, Image Generation, and Bing Search settings directly through an in-app interface (rather than
-
Multiple Roles & Group Permissions
- Roles include
Owner
,Admin
,DocumentManager
, andUser
. - Group Owners/Admins can invite or remove members, manage documents, and set “active workspace” for group-based search.
- Roles include
-
One-Click Switching of Active Group
- Users in multiple groups can quickly switch their active group to see group-specific documents and chat references.
-
Ephemeral Document Upload
- Upload a file for a single conversation. The file is not saved in Azure Cognitive Search; instead, it is only used for the session’s RAG context.
-
Inline File Previews in Chat
- Files attached to a conversation can be previewed directly from the chat, with text or data displayed in a pop-up.
-
Optional Bing Web Search
- Administrators can enable or disable web search. When enabled, the user can toggle “Search the Web” while chatting to incorporate Bing results.
-
Optional Image Generation
- Users can toggle an “Image” button to create images via Azure OpenAI (e.g., DALL·E) when configured in Admin Settings.
-
App Roles & Enterprise Application
- Provides a robust way to control user access at scale.
- Admins can assign roles to new users or entire Azure AD groups.
- Flask (Python): Web framework for handling requests and rendering web pages.
- Azure OpenAI: Used for generating AI responses and creating document embeddings for RAG.
- Azure Cosmos DB: For storing conversations, documents, and metadata.
- Azure Cognitive Search: Enables document retrieval based on AI-embedded vectors.
- Azure Document Intelligence: Extracts text from uploaded documents in various formats.
- MSAL: Handles authentication with Azure Active Directory (AAD).
The Simple Chat Application uses Azure AI Search to store user (personal) and group documents. You’ll create two indexes:
- User Index
- Group Index
Both schemas are found in the artifacts/
folder (user-index.json
and group-index.json
).
📁 SimpleChat
└── 📁 artifacts
└── user-index.json
└── group-index.json
-
Access the Azure Portal
- Go to the Azure Portal.
- In the search bar, search for "Azure Cognitive Search" and select your Azure AI Search resource.
-
Navigate to Indexes
- In the left-hand menu, select Indexes under Search Management.
- Click on + Add Index from JSON to create a new index.
-
Create Index from JSON
- Open
user-index.json
in your local editor. Copy its JSON and paste into the Azure portal’s Add Index from JSON screen. - Do the same for
group-index.json
.
- Open
-
Verify Index Creation
- After creation, you should see
simplechat-user-index
andsimplechat-group-index
listed under Indexes.
- After creation, you should see
The application secures access using Azure Active Directory. Users log in with their organizational credentials. Access is further refined with roles (Owner
, Admin
, DocumentManager
, User
) assigned in your tenant’s Enterprise Applications.
-
Enable App Service Authentication
- In the App Service → Authentication blade, add Microsoft as an identity provider, linking to your Azure AD app registration.
- Require authentication so only logged-in users can access the app.
-
App Registration
- In Azure AD → App Registrations, find your registration (e.g.,
my-webapp-simplechat
). - Configure Redirect URIs (e.g.,
https://my-webapp.azurewebsites.net/getAToken
) and permissions. - Grant admin consent if needed (e.g.,
User.Read
,User.ReadBasic.All
, etc.).
- In Azure AD → App Registrations, find your registration (e.g.,
-
Assign Users in Enterprise Applications
- Under Enterprise Applications → Users and groups, assign users or groups to the app, specifying the appropriate role.
Your application is authorized to call APIs when granted permissions by users or administrators. Below are the currently configured permissions for Microsoft Graph in this application.
API / Permission Name | Type | Description |
---|---|---|
Delegated | View users' email address | |
offline_access | Delegated | Maintain access to data you have given it access to |
openid | Delegated | Sign users in |
People.Read.All | Delegated | Read all users' relevant people lists |
profile | Delegated | View users' basic profile |
User.Read | Delegated | Sign in and read user profile |
User.ReadBasic.All | Delegated | Read all users' basic profiles |
For the permissions that require admin consent, ensure that an administrator grants consent by:
- Navigating to Azure Portal > Azure Active Directory.
- Selecting App registrations and locating your registered application.
- Clicking on API permissions.
- Selecting Grant admin consent for [your tenant].
- Confirming the operation.
If your application requires further permissions:
- Click + Add a permission.
- Select Microsoft Graph or another API.
- Choose either Delegated permissions (acting on behalf of the user) or Application permissions (acting as a service).
- Select the required permissions and Add them.
- If admin consent is required, follow the Granting Admin Consent steps above.
By ensuring the correct permissions and admin consent, your application can securely interact with Microsoft Graph APIs while respecting user and security policies.
Description: App roles are custom roles to assign permissions to users or apps. The application defines and publishes these app roles, which are then interpreted as permissions during authorization.
Display Name | Description | Allowed Member Types | Value | State |
---|---|---|---|---|
Admins | Manage the application | Users/Groups | Admin | Enabled |
Users | Normal user | Users/Groups | User | Enabled |
To allow users to sign in to your application and automatically be assigned the correct role (Admin or User), you must add these users in the Enterprise application that is associated with your Registered app in Azure Active Directory:
-
Go to Azure Active Directory
- In the Azure Portal, go to Azure Active Directory from the main menu.
-
Select ‘Enterprise applications’
- Under the Manage section in Azure AD, choose Enterprise applications.
-
Locate Your Application
- Find and select the Enterprise application that was automatically created when you registered your app (the name should match or be closely related to the registered app’s name).
-
Go to ‘Users and groups’
- Under Manage for that Enterprise application, select Users and groups to manage role assignments.
-
Click on ‘Add user/group’
- Here, you can choose to add either individual users or entire Azure AD groups to the application.
-
Assign the Appropriate Role
- When adding users or groups, you will see the available app roles (e.g., Admins, Users).
- Select the relevant role to ensure the user or group is granted the correct permissions.
-
Save Your Changes
- Confirm your assignments and click Assign (or Save) to finalize.
-
Verification
- Once a user is assigned, they can sign in and be granted the permissions associated with their role in your application.
While Azure OpenAI (GPT, Embeddings, Image Gen) and Bing Search are now configured via the in-app Admin Settings, you still need some basic environment variables for the rest of the services. These are typically set in the Azure Portal under Configuration or uploaded via a .env
file.
-
Modify
example.env
- Rename
example.env
to.env
. - Update placeholders for Azure Cosmos DB, Azure Cognitive Search, Azure Document Intelligence, and AAD values.
- Omit any direct references to Azure OpenAI or Bing Search here, since these are now set in the admin UI.
- Rename
-
Upload
.env
to Azure App Service- In VS Code, use "Azure App Service: Upload Local Settings" or manually copy the env keys into App Service → Configuration.
Note: Keep secrets out of source control. Use Azure Key Vault or the App Service Settings blade to store any credentials for production scenarios.
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.
- Navigate to your App Service in the Azure Portal.
- Go to Settings > Configuration.
- Click on the Application settings tab.
- Click Advanced edit.
- Copy and paste the JSON configuration below into the Advanced Edit window.
- Click OK, then Save to apply the changes.
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": "<your-authentication-secret>", "slotSetting": true },
{ "name": "WEBSITE_AUTH_AAD_ALLOWED_TENANTS", "value": "<your-allowed-tenant-id>", "slotSetting": false },
{ "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", "value": "true", "slotSetting": false },
{ "name": "WEBSITE_HTTPLOGGING_RETENTION_DAYS", "value": "7", "slotSetting": false },
{ "name": "APPINSIGHTS_INSTRUMENTATIONKEY", "value": "<your-instrumentation-key>", "slotSetting": false },
{ "name": "APPLICATIONINSIGHTS_CONNECTION_STRING", "value": "InstrumentationKey=<your-instrumentation-key>;IngestionEndpoint=<your-ingestion-endpoint>;LiveEndpoint=<your-live-endpoint>;ApplicationId=<your-application-id>", "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": "APPINSIGHTS_PROFILERFEATURE_VERSION", "value": "1.0.0", "slotSetting": false },
{ "name": "APPINSIGHTS_SNAPSHOTFEATURE_VERSION", "value": "1.0.0", "slotSetting": false },
{ "name": "SnapshotDebugger_EXTENSION_VERSION", "value": "disabled", "slotSetting": false },
{ "name": "InstrumentationEngine_EXTENSION_VERSION", "value": "disabled", "slotSetting": false },
{ "name": "XDT_MicrosoftApplicationInsights_BaseExtensions", "value": "disabled", "slotSetting": false },
{ "name": "XDT_MicrosoftApplicationInsights_PreemptSdk", "value": "disabled", "slotSetting": false },
{ "name": "AZURE_COSMOS_ENDPOINT", "value": "<your-cosmosdb-endpoint>", "slotSetting": false },
{ "name": "AZURE_COSMOS_KEY", "value": "<your-cosmosdb-key>", "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_COSMOS_LOGS_CONTAINER_NAME", "value": "logs", "slotSetting": false },
{ "name": "AZURE_AI_SEARCH_ENDPOINT", "value": "<your-ai-search-endpoint>", "slotSetting": false },
{ "name": "AZURE_AI_SEARCH_KEY", "value": "<your-ai-search-key>", "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": "<your-document-intelligence-endpoint>", "slotSetting": false },
{ "name": "AZURE_DOCUMENT_INTELLIGENCE_KEY", "value": "<your-document-intelligence-key>", "slotSetting": false },
{ "name": "BING_SEARCH_ENDPOINT", "value": "https://api.bing.microsoft.com/", "slotSetting": false },
{ "name": "CLIENT_ID", "value": "<your-client-id>", "slotSetting": false },
{ "name": "TENANT_ID", "value": "<your-tenant-id>", "slotSetting": false },
{ "name": "SECRET_KEY", "value": "<your-32-character-secret>", "slotSetting": false }
]
- The
slotSetting
flag is set totrue
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.
-
Clone the Repo
git clone https://github.com/your-repo/SimpleChat.git cd SimpleChat
-
Install Dependencies
pip install -r requirements.txt
- Deploy to Azure App Service You can use Azure CLI or VS Code deployment.
- Install the Azure Tools VS Code Extension.
- Sign in to your Azure account in VS Code.
- Right-click your project folder → Deploy to Web App.
- Select or create an Azure App Service.
- Wait for deployment to complete.
- Upload your
.env
or configure application settings in the Azure Portal.
-
Locally (for testing):
flask run
Then open http://localhost:5000 in your browser.
-
Azure: Once deployed, open your
https://<app_name>.azurewebsites.net
.
After deployment and login (with a role of Admin or Owner), navigate to Admin Settings
in the navigation bar:
- General: Set application title, toggle show/hide logo, customize the landing page text.
- GPT: Provide the Azure OpenAI GPT endpoint, choose between “Key” or “Managed Identity,” and select your model deployment.
- Embeddings: Provide the Azure OpenAI Embedding endpoint and select the embedding model deployment.
- Image Generation (optional): Enable to add an “Image” button in chat for AI image generation.
- Web Search (Bing): Toggle to enable or disable web-based augmentation with Bing Search.
- External APIs (optional): If you have custom chunking or embedding endpoints, set them here.
- Other: Additional limits (max file size, conversation history limit, default system prompt, etc.).
Changes are stored in your Azure Cosmos DB’s configuration container. Once saved, the new settings are applied almost immediately, without editing .env
.
For deployments in Azure Government, ensure that the endpoints for Azure Cosmos DB, Azure Cognitive Search, Azure Document Intelligence, etc., use the .azure.us
suffix (or region-specific endpoints).
-
Login: Users must log in via Azure Active Directory.
-
Chat: Start a conversation with the AI or retrieve previous conversations.
-
Upload Documents
(Personal or Group):
- Personal documents are indexed in
simplechat-user-index
. - Group documents are indexed in
simplechat-group-index
and only visible to group members.
- Personal documents are indexed in
-
Toggle Hybrid Search: Optionally switch on the “Search Documents” button to retrieve context from your docs.
-
Upload Ephemeral Documents: Files that live for one conversation only (not in Cognitive Search).
-
Bing Web Search (optional): Toggle “Search the Web” for external augmentation.
-
Image Generation (optional): Enable “Image” mode to generate images via Azure OpenAI.
-
Groups
:
- Create or join existing groups; each group has an owner and optional admins.
- Switch to the “active group” to see that group’s documents.
- Login via Azure AD → The user is assigned a role.
- Choose Group: If applicable, pick or set an active group.
- Chat: Compose messages in the chat UI.
- Attach Docs: Upload personal or group docs to store or ephemeral docs for a single conversation.
- Hybrid Search: Enable searching your personal or group docs for context.
- Review Past Chats: The user can revisit conversation history stored in Azure Cosmos DB.