Skip to content

Commit

Permalink
Follow-up fixes from async refactor (microsoft#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-widder authored Feb 2, 2024
1 parent 89f53f3 commit 16de7ec
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 68 deletions.
67 changes: 25 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ This repo contains sample code for a simple chat webapp that integrates with Azu

## 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: an existing Azure Cognitive Search resource and index.
- 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 (preview)
- AzureML index (preview)

## Deploy the app

Expand All @@ -21,7 +26,7 @@ Please see the [section below](#add-an-identity-provider) for important informat
### Deploy from your local machine

#### Local Setup: Basic Chat Experience
1. Update the environment variables listed in `app.py` as described in the [Environment variables](#environment-variables) section.
1. Copy `.env.sample` to a new file called `.env` and configure the settings as described in the [Environment variables](#environment-variables) section.

These variables are required:
- `AZURE_OPENAI_RESOURCE`
Expand All @@ -37,17 +42,17 @@ Please see the [section below](#add-an-identity-provider) for important informat

See the [documentation](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#example-response-2) for more information on these parameters.

2. Start the app with `start.cmd`. This will build the frontend, install backend dependencies, and then start the app.
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:5000.
3. You can see the local running app at http://127.0.0.1:50505.

#### Local Setup: Chat with your data (Preview)
[More information about Azure OpenAI on your data](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/concepts/use-your-data)

1. Update the `AZURE_OPENAI_*` environment variables as described above.
2. To connect to your data, you need to specify an Azure Cognitive Search index to use. You can [create this index yourself](https://learn.microsoft.com/en-us/azure/search/search-get-started-portal) or use the [Azure AI Studio](https://oai.azure.com/portal/chat) to create the index for you.

These variables are required when adding your data:
These variables are required when adding your data with Azure AI Search:
- `AZURE_SEARCH_SERVICE`
- `AZURE_SEARCH_INDEX`
- `AZURE_SEARCH_KEY`
Expand All @@ -67,8 +72,8 @@ Please see the [section below](#add-an-identity-provider) for important informat
- `AZURE_SEARCH_STRICTNESS`
- `AZURE_OPENAI_EMBEDDING_NAME`

3. Start the app with `start.cmd`. This will build the frontend, install backend dependencies, and then start the app.
4. You can see the local running app at http://127.0.0.1:5000.
3. 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`.
4. You can see the local running app at http://127.0.0.1:50505.

#### Local Setup: Enable Chat History
To enable chat history, you will need to set up CosmosDB resources. The ARM template in the `infrastructure` folder can be used to deploy an app service and a CosmosDB with the database and container configured. Then specify these additional environment variables:
Expand All @@ -77,7 +82,7 @@ To enable chat history, you will need to set up CosmosDB resources. The ARM temp
- `AZURE_COSMOSDB_CONVERSATIONS_CONTAINER`
- `AZURE_COSMOSDB_ACCOUNT_KEY`

As above, start the app with `start.cmd`, then visit the local running app at http://127.0.0.1:5000.
As above, start the app with `start.cmd`, then visit the local running app at http://127.0.0.1:50505. Or, just run the backend in debug mode using the VSCode debug configuration in `.vscode/launch.json`.

#### Local Setup: Enable Message Feedback
To enable message feedback, you will need to set up CosmosDB resources. Then specify these additional environment variable:
Expand Down Expand Up @@ -123,32 +128,10 @@ To add further access controls, update the logic in `getUserInfoList` in `fronte
Feel free to fork this repository and make your own modifications to the UX or backend logic. For example, you may want to change aspects of the chat display, or expose some of the settings in `app.py` in the UI for users to try out different behaviors.

### Scalability
For apps published with `az webapp up` or from the Azure AI Studio, you can increase your app's ability to handle concurrent requests from multiple users with the following steps:
1. Upgrade your App Service plan tier to a higher tier, for example tiers with more than one vCPU.

2. Configure the following app setting on your App Service in the Azure Portal:
- `PYTHON_ENABLE_GUNICORN_MULTIWORKERS`: true
This will default to use a default worker count of (2 * numCores) + 1 and thread count of 1.
If your App Service Plan has additional compute capacity and you want to increase the worker or thread count, you can figure these additional settings accordingly:
- `PYTHON_GUNICORN_CUSTOM_WORKER_NUM`
- `PYTHON_GUNICORN_CUSTOM_THREAD_NUM`
You can configure the number of threads and workers in `gunicorn.conf.py`. After making a change, redeploy your app using the commands listed above.

See the [Oryx documentation](https://github.com/microsoft/Oryx/blob/main/doc/configuration.md) for more details on these settings.

After adding the settings, be sure to save the configuration and then restart your app.

For apps published with `One click Azure deployment` you can increase your app's ability to handle concurrent requests from multiple users with the following steps:
1. Upgrade your App Service plan tier to a higher tier, for example tiers with more than one vCPU.

2. Configure the following app settings on your App Service in the Azure Portal:
- `UWSGI_PROCESSES`: 5 (may be higher or lower depending on your App Service Plan tier)
- `UWSGI_THREADS`: 5 (may be higher or lower depending on your App Service Plan tier)

After adding the settings, be sure to save the configuration and then restart your app.

> In case you build your own docker image based on the `WebApp.Dockerfile` and host it in a App Serice, you can also increase your app's ability to handle concurrent requests from multiple users with the above steps.
When you host your container not in an App Service you can add this seetings as container environment variable.

### Debugging your deployed app
First, add an environment variable on the app service resource called "DEBUG". Set this to "true".

Expand Down Expand Up @@ -208,24 +191,24 @@ We recommend keeping these best practices in mind:

## Environment variables

Note: settings starting with `AZURE_SEARCH` are only needed when using Azure OpenAI on your data. If not connecting to your data, you only need to specify `AZURE_OPENAI` settings.
Note: settings starting with `AZURE_SEARCH` are only needed when using Azure OpenAI on your data with Azure AI Search. If not connecting to your data, you only need to specify `AZURE_OPENAI` settings.

| App Setting | Value | Note |
| --- | --- | ------------- |
|AZURE_SEARCH_SERVICE||The name of your Azure Cognitive Search resource|
|AZURE_SEARCH_INDEX||The name of your Azure Cognitive Search Index|
|AZURE_SEARCH_KEY||An **admin key** for your Azure Cognitive Search resource|
|AZURE_SEARCH_SERVICE||The name of your Azure AI Search resource|
|AZURE_SEARCH_INDEX||The name of your Azure AI Search Index|
|AZURE_SEARCH_KEY||An **admin key** for your Azure AI Search resource|
|AZURE_SEARCH_USE_SEMANTIC_SEARCH|False|Whether or not to use semantic search|
|AZURE_SEARCH_QUERY_TYPE|simple|Query type: simple, semantic, vector, vectorSimpleHybrid, or vectorSemanticHybrid. Takes precedence over AZURE_SEARCH_USE_SEMANTIC_SEARCH|
|AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG||The name of the semantic search configuration to use if using semantic search.|
|AZURE_SEARCH_TOP_K|5|The number of documents to retrieve from Azure Cognitive Search.|
|AZURE_SEARCH_TOP_K|5|The number of documents to retrieve from Azure AI Search.|
|AZURE_SEARCH_ENABLE_IN_DOMAIN|True|Limits responses to only queries relating to your data.|
|AZURE_SEARCH_CONTENT_COLUMNS||List of fields in your Azure Cognitive Search index that contains the text content of your documents to use when formulating a bot response. Represent these as a string joined with "|", e.g. `"product_description|product_manual"`|
|AZURE_SEARCH_FILENAME_COLUMN|| Field from your Azure Cognitive Search index that gives a unique idenitfier of the source of your data to display in the UI.|
|AZURE_SEARCH_TITLE_COLUMN||Field from your Azure Cognitive Search index that gives a relevant title or header for your data content to display in the UI.|
|AZURE_SEARCH_URL_COLUMN||Field from your Azure Cognitive Search index that contains a URL for the document, e.g. an Azure Blob Storage URI. This value is not currently used.|
|AZURE_SEARCH_VECTOR_COLUMNS||List of fields in your Azure Cognitive Search index that contain vector embeddings of your documents to use when formulating a bot response. Represent these as a string joined with "|", e.g. `"product_description|product_manual"`|
|AZURE_SEARCH_PERMITTED_GROUPS_COLUMN||Field from your Azure Cognitive Search index that contains AAD group IDs that determine document-level access control.|
|AZURE_SEARCH_CONTENT_COLUMNS||List of fields in your Azure AI Search index that contains the text content of your documents to use when formulating a bot response. Represent these as a string joined with "|", e.g. `"product_description|product_manual"`|
|AZURE_SEARCH_FILENAME_COLUMN|| Field from your Azure AI Search index that gives a unique idenitfier of the source of your data to display in the UI.|
|AZURE_SEARCH_TITLE_COLUMN||Field from your Azure AI Search index that gives a relevant title or header for your data content to display in the UI.|
|AZURE_SEARCH_URL_COLUMN||Field from your Azure AI Search index that contains a URL for the document, e.g. an Azure Blob Storage URI. This value is not currently used.|
|AZURE_SEARCH_VECTOR_COLUMNS||List of fields in your Azure AI Search index that contain vector embeddings of your documents to use when formulating a bot response. Represent these as a string joined with "|", e.g. `"product_description|product_manual"`|
|AZURE_SEARCH_PERMITTED_GROUPS_COLUMN||Field from your Azure AI Search index that contains AAD group IDs that determine document-level access control.|
|AZURE_SEARCH_STRICTNESS|3|Integer from 1 to 5 specifying the strictness for the model limiting responses to your data.|
|AZURE_OPENAI_RESOURCE||the name of your Azure OpenAI resource|
|AZURE_OPENAI_MODEL||The name of your model deployment|
Expand Down
1 change: 1 addition & 0 deletions frontend/src/pages/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const Chat = () => {

useEffect(() => {
if (appStateContext?.state.isCosmosDBAvailable?.status !== CosmosDBStatus.Working
&& appStateContext?.state.isCosmosDBAvailable?.status !== CosmosDBStatus.NotConfigured
&& appStateContext?.state.chatHistoryLoadingState === ChatHistoryLoadingState.Fail
&& hideErrorDialog) {
let subtitle = `${appStateContext.state.isCosmosDBAvailable.status}. Please contact the site administrator.`
Expand Down
20 changes: 10 additions & 10 deletions start.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ if "%errorlevel%" neq "0" (
exit /B %errorlevel%
)

echo.
echo Starting backend
echo.
cd ..
start http://127.0.0.1:5000
call python ./app.py
if "%errorlevel%" neq "0" (
echo Failed to start backend
exit /B %errorlevel%
)
echo.
echo Starting backend
echo.
cd ..
start http://127.0.0.1:50505
call uvicorn app:app
if "%errorlevel%" neq "0" (
echo Failed to start backend
exit /B %errorlevel%
)
2 changes: 1 addition & 1 deletion start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cd ..
echo ""
echo "Starting backend"
echo ""
./.venv/bin/python -m flask run --port=5000 --host=127.0.0.1 --reload --debug
./.venv/bin/python -m quart run --port=50505 --host=127.0.0.1 --reload --debug
if [ $? -ne 0 ]; then
echo "Failed to start backend"
exit $?
Expand Down
Loading

0 comments on commit 16de7ec

Please sign in to comment.