MENU: PREREQUISITES | DEVELOPMENT | DEPLOYMENT | TRACING AND MONITORING | DEVELOPMENT OPTIONS | SUPPORTING DOCUMENTATION | TROUBLESHOOTING
This project creates an Azure AI Foundry hub, project and connected resources including Azure AI Services, AI Search and more. It deploys an agent chat application to Azure Container Apps that uses Azure File Search with uploaded files and can provide citations.
If you do not have an Azure Subscription, you can sign up for a free Azure account and create an Azure Subscription.
NOTE! Whether you sign up for new account, or you are using an existing subsription, check that you have the necessary permissions:
- Your Azure account for the Azure Subscription that you are using must have
Microsoft.Authorization/roleAssignments/write
permissions, such as Role Based Access Control Administrator, User Access Administrator, or Owner. - Your Azure account also needs
Microsoft.Resources/deployments/write
permissions on the subscription level.
You can view the permissions for your account and subscription by going to Azure portal, clicking 'Subscriptions' under 'Navigation' and then choosing your subscription from the list. If you try to search for your subscription and it does not come up, make sure no filters are selected. After selecting your subscription, select 'Access control (IAM)' and you can see the roles that are assigned to your account for this subscription. If you want to see more information about the roles, you can go to the 'Role assignments' tab and search by your account name and then click the role you want to view more information about.
Make sure the following tools are installed:
- Azure Developer CLI (azd) Install or update to the latest version. Instructions can be found on the linked page.
- Python 3.9+
- Git
This step applies if you have an existing AI project resource, and you want to bring it into the solution. You can do that by setting the following environment variable:
azd env set AZURE_EXISTING_AIPROJECT_CONNECTION_STRING "<connection-string>"
You can find the connection string on the overview page of your Azure AI project.
This solution has been configured to use "gpt-4o-mini" model. If you do not have a deployment named "gpt-4o-mini" in your existing AI project, you should either create one in Azure AI Foundry or follow the steps in Customizing model deployments to specify a different model.
Download the project code:
git clone https://github.com/Azure-Samples/azureai-agents-chat.git
At this point you could make changes to the code if required. However, no changes are needed to deploy and test the app as shown in the next step.
For options on customizing the deployment to disable resources, change resource names, or customize the models, you can follow these steps in deployment customizations now.
If you want to personalize your agent, you can change the default configuration for your agent. This can include changing the model, adding tools, and uploading files to the agent. More information can be found in Customizing Model Deployments.
To change the model, set the following environment variables:
azd env set AZURE_AI_CHAT_DEPLOYMENT_NAME <MODEL_DEPLOYMENT_NAME>
azd env set AZURE_AI_CHAT_MODEL_NAME <MODEL_DEPLOYMENT_NAME>
To add tools, update the agents.yaml
file located in the repository.
# TODO: add this once it is configured
If you want to enable logging to a file, uncomment the following line in Dockerfile located in the src directory:
ENV APP_LOG_FILE=app.log
By default the file name app.log is used. You can provide your own file name by replacing app.log with the desired log file name.
NOTE! Any changes to the Dockerfile require a re-deployment in order for the changes to take effect.
Consider whether you want to have logging to file enabled after the R&D phase, when running in production.
To enable tracing to Azure Monitor, modify the value of ENABLE_AZURE_MONITOR_TRACING environment variable to true in Dockerfile found in src directory:
ENV ENABLE_AZURE_MONITOR_TRACING=true
Note that the optional App Insights resource is required for tracing to Azure Monitor (it is created by default).
To enable message contents to be included in the traces, set the following environment variable to true in the same Dockerfile. Note that the messages may contain personally identifiable information.
ENV AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED=true
You can optionally use a local development server to test app changes locally. Make sure you first deployed the app to Azure before running the development server.
-
Create a Python virtual environment and activate it.
On Windows:
py -3 -m venv .venv .venv\scripts\activate
On Linux:
python3 -m venv .venv source .venv/bin/activate
-
Navigate to the
src
directory:cd src
-
Install required Python packages:
python -m pip install -r requirements.txt
-
Run the local server:
python -m uvicorn "api.main:create_app" --factory --reload
-
Click 'http://127.0.0.1:8000' in the terminal, which should open a new tab in the browser. It may take a few moments for application startup to complete.
-
Enter your message in the box.
Once you've opened the project locally and made any desired adjustments, you can deploy it to Azure.
-
Login to Azure:
azd auth login
-
(Optional) If you would like to customize the deployment to disable resources, customize resource names, or customize the models, you can follow those steps now.
-
Provision and deploy all the resources:
azd up
It will prompt you to provide an
azd
environment name (like "azureaiapp"), select a subscription from your Azure account, and select a location which has quota for all the resources. Then it will provision the resources in your account and deploy the latest code. If you get an error or timeout with deployment, changing the location can help, as there may be availability constraints for the resources.NOTE! If you get authorization failed and/or permission related errors during the deployment, please refer to the Azure account requirements in the Prerequisites section.
-
When
azd
has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the app! 🎉You can view information about your deployment with:
azd show
-
If you make further modification to the app code, you can deploy the updated version with:
azd deploy
You can get more detailed output with the
--debug
parameter.azd deploy --debug
Check for any errors during the deployment, since updated app code will not get deployed if errors occur.
You can view console logs in Azure portal. You can get the link to the resource group with the azd tool:
azd show
Or if you want to navigate from the Azure portal main page, select your resource group from the 'Recent' list, or by clicking the 'Resource groups' and searching your resource group there.
After accessing you resource group in Azure portal, choose your container app from the list of resources. Then open 'Monitoring' and 'Log Stream'. Choose the 'Application' radio button to view application logs. You can choose between real-time and historical using the corresponding radio buttons. Note that it may take some time for the historical view to be updated with the latest logs.
If you enabled logging to a file, you can view the log file by choosing 'Console' under the 'Monitoring' (same location as above for the console traces), opening the default console and then for example running the following command (replace app.log with the actual name of your log file):
more app.log
You can view the App Insights tracing in Azure AI Foundry. Select your project on the Azure AI Foundry page and then click 'Tracing'.
As an alternative to local development environment, the following development environment options can be used.
GitHub Codespaces
You can run this template virtually by using GitHub Codespaces. The button will open a web-based VS Code instance in your browser:
-
Open the template (this may take several minutes):
-
Open a terminal window
-
Continue with the deploying steps
VS Code Dev Containers
A related option is VS Code Dev Containers, which will open the project in your local VS Code using the Dev Containers extension:
-
Start Docker Desktop (install it if not already installed Docker Desktop)
-
Open the project:
-
In the VS Code window that opens, once the project files show up (this may take several minutes), open a terminal window.
-
Continue with the deploying steps
Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. The majority of the Azure resources used in this infrastructure are on usage-based pricing tiers. However, Azure Container Registry has a fixed cost per registry per day.
You can try the Azure pricing calculator for the resources:
- Azure AI Foundry: Free tier. Pricing
- Azure AI Search: Standard tier, S1. Pricing is based on the number of documents and operations. Pricing
- Azure Storage Account: Standard tier, LRS. Pricing is based on storage and operations. Pricing
- Azure Key Vault: Standard tier. Pricing is based on the number of operations. Pricing
- Azure AI Services: S0 tier, defaults to gpt-4o-mini and text-embedding-ada-002 models. Pricing is based on token count. Pricing
- Azure Container App: Consumption tier with 0.5 CPU, 1GiB memory/storage. Pricing is based on resource allocation, and each month allows for a certain amount of free usage. Pricing
- Azure Container Registry: Basic tier. Pricing
- Log analytics: Pay-as-you-go tier. Costs based on data ingested. Pricing
azd down
.
This template uses Azure AI Foundry connections to communicate between resources, which stores keys in Azure Key Vault. This template also uses Managed Identity for local development and deployment.
To ensure continued best practices in your own repository, we recommend that anyone creating solutions based on our templates ensure that the Github secret scanning setting is enabled.
You may want to consider additional security measures, such as:
- Enabling Microsoft Defender for Cloud to secure your Azure resources.
- Protecting the Azure Container Apps instance with a firewall and/or Virtual Network.
This template creates everything you need to get started with Azure AI Foundry:
- AI Hub Resource
- AI Project
- Azure AI Service: Default models deployed are gpt-4o-mini and text-embedding-ada-002, but any Azure AI models can be specified per the documentation.
- AI Search Service (Optional, enabled by default)
The template also includes dependent resources required by all AI Hub resources:
- Storage Account
- Key Vault
- Application Insights (Optional, enabled by default)
- Container Registry (Optional, enabled by default)
- If you have an issue is with timeouts or provisioning resources, changing the location of your resource group can help, as there may be availability constrains for resources. Call
azd down
and remove your current resources, and delete the.azure
folder from your workspace. Then, callazd up
again and select a different region. - You may debug further using azd commands.
azd show
displays information abour your app and resources, andazd deploy --debug
enables debugging and logging while deploying the application's code to Azure. - Ensure that your az and azd tools are up to date.
- After fully deploying with azd, additional errors in the Azure Portal may indicate that your latest code has not been successfully deployed
- If your ACA does not boot up, it is possible that your deployment has failed. This could be due to quota constraints, permission issues, or resource availability. Check failures in the deployment and container app logs in the Azure Portal.
- Console traces in ACA can be found in the Azure Portal, but they may be unreliable. Use Python’s logging with INFO level, and adjust Azure HTTP logging to WARNING.
- Once your ACA is deployed, utilize the browser debugger (F12) and clear cache (CTRL+SHIFT+R). This can help debug the frontend for better traceability.
- If your agent is occasionally unresponsive, your model may have reached its rate limit. You can increase its quota by adjusting the bicep configuration or by editing the model in the Azure AI Foundry page for your project's model deployments.
- If your agent is crashing, confirm that you are using a model that you have deployed to your project.
- For easier agents configuration and streamlined integration with an existing assistants library, export the agent from Azure AI Foundry and implement a yaml-based configuration.
- This application is designed to serve multiple users on multiple browsers. This application uses cookies to ensure that the same thread is reused for conversations across multiple tabs in the same browser. If the browser is restarted, the old thread will continue to serve the user. However, if the application has a new agent after a server restart or a thread is deleted, a new thread will be created without requiring a browser refresh or signaling to the users.When users submit a message to the web server, the web server will create an agent, thread, and stream back a reply. The response contains
agent_id
andthread_id
in cookies. As a result, each subsequent message sent to the web server will also contain these IDs. As long as the same agent is being used in the system and the thread can be retrieved in the cookie, the same thread will be used to serve the users. - For document handling, use filename-based downloads to avoid storing files in dictionaries.
- Intermittent errors may arise when retrieving filenames for file IDs, which may be mitigated by using a single worker and fresh threads for each new assistant.
- File citation can be enhanced by automatically including filenames to reduce manual steps.