|
| 1 | +--- |
| 2 | +title: How to use Agent Framework in a Python app with Azure App Configuration |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn how to use Agent Framework in a Python app with Azure App Configuration. |
| 5 | +ms.service: azure-app-configuration |
| 6 | +author: MaryanneNjeri |
| 7 | +ms.author: mgichohi |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 11/10/2025 |
| 10 | +ms.update-cycle: 180-days |
| 11 | +ms.collection: ce-skilling-ai-copilot |
| 12 | +--- |
| 13 | + |
| 14 | +# Use Agent Framework in a Python app with Azure App Configuration |
| 15 | + |
| 16 | +In this guide, you build an AI agent chat application using Azure App Configuration to load agent YAML specifications that define AI agent behavior, prompts and model configurations. |
| 17 | + |
| 18 | +The full sample source code is available in the [Azure App Configuration GitHub repository](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/ChatAgent). |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- Create an _Azure AI project_ in Microsoft Foundry and configure the _example agent settings_ discussed in the [Get started](./howto-ai-agent-config.md#example-agent-settings) section. |
| 23 | +- Python 3.10 or later - for information on setting up Python on Windows, see the [Python on Windows documentation](/windows/python/). |
| 24 | + |
| 25 | +## Console application |
| 26 | + |
| 27 | +In this section, you create a console application and load the agent YAML specification from your App Configuration store. |
| 28 | + |
| 29 | +1. Create a new folder for your project. In the new folder, install the following packages by using the `pip install` command: |
| 30 | + |
| 31 | + ```console |
| 32 | + pip install azure-appconfiguration-provider |
| 33 | + pip install agent-framework-declarative --pre |
| 34 | + pip install azure-identity |
| 35 | + ``` |
| 36 | + |
| 37 | +1. Create a new file called _app.py_, and add the following import statements: |
| 38 | + |
| 39 | + ```python |
| 40 | + import asyncio |
| 41 | + import os |
| 42 | + from agent_framework.declarative import AgentFactory |
| 43 | + from azure.identity import DefaultAzureCredential |
| 44 | + from azure.appconfiguration.provider import load |
| 45 | + ``` |
| 46 | + |
| 47 | +1. You can connect to Azure App Configuration using either Microsoft Entra ID (recommended) or a connection string. In this example, you use Microsoft Entra ID with `DefaultAzureCredential` to authenticate to your App Configuration store. Follow these [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign the **App Configuration Data Reader** role to the identity represented by `DefaultAzureCredential`. Be sure to allow sufficient time for the permission to propagate before running your application. |
| 48 | + |
| 49 | + ```python |
| 50 | + async def main(): |
| 51 | + endpoint = os.environ["AZURE_APPCONFIGURATION_ENDPOINT"] |
| 52 | + |
| 53 | + # Connect to Azure App Configuration using Microsoft Entra ID. |
| 54 | + credential = DefaultAzureCredential() |
| 55 | + |
| 56 | + config = load(endpoint=endpoint, credential=credential) |
| 57 | + ``` |
| 58 | + |
| 59 | +1. Update the code in _app.py_ to retrieve the agent specification from the configuration, create the agent from the YAML spec and handle user interaction: |
| 60 | + |
| 61 | + ```python |
| 62 | + async def main(): |
| 63 | + endpoint = os.environ["AZURE_APPCONFIGURATION_ENDPOINT"] |
| 64 | + |
| 65 | + # Connect to Azure App Configuration using Microsoft Entra ID. |
| 66 | + credential = DefaultAzureCredential() |
| 67 | + |
| 68 | + config = load(endpoint=endpoint, credential=credential) |
| 69 | + |
| 70 | + agent_spec = config["ChatAgent:Spec"] |
| 71 | + |
| 72 | + agent = AgentFactory(client_kwargs={"credential": credential, "project_endpoint": config["ChatAgent:ProjectEndpoint"]}).create_agent_from_yaml(agent_spec) |
| 73 | + |
| 74 | + while True: |
| 75 | + print("How can I help? (type 'quit' to exit)") |
| 76 | + |
| 77 | + user_input = input("User: ") |
| 78 | + |
| 79 | + if user_input.lower() in ['quit', 'exit', 'bye']: |
| 80 | + break |
| 81 | + |
| 82 | + response = await agent.run(user_input) |
| 83 | + print("Agent response: ", response.text) |
| 84 | + input("Press enter to continue...") |
| 85 | + |
| 86 | + print("Exiting... Goodbye...") |
| 87 | + |
| 88 | + if __name__ == "__main__": |
| 89 | + asyncio.run(main()) |
| 90 | + ``` |
| 91 | + |
| 92 | +1. After completing the previous steps, your _app.py_ file should now contain the complete implementation as shown below: |
| 93 | + ```python |
| 94 | + import asyncio |
| 95 | + import os |
| 96 | + from agent_framework.declarative import AgentFactory |
| 97 | + from azure.identity import DefaultAzureCredential |
| 98 | + from azure.appconfiguration.provider import load |
| 99 | + |
| 100 | + async def main(): |
| 101 | + endpoint = os.environ["AZURE_APPCONFIGURATION_ENDPOINT"] |
| 102 | + |
| 103 | + # Connect to Azure App Configuration using Microsoft Entra ID. |
| 104 | + credential = DefaultAzureCredential() |
| 105 | + |
| 106 | + config = load(endpoint=endpoint, credential=credential) |
| 107 | + |
| 108 | + agent_spec = config["ChatAgent:Spec"] |
| 109 | + |
| 110 | + agent = AgentFactory(client_kwargs={"credential": credential, "project_endpoint": config["ChatAgent:ProjectEndpoint"]}).create_agent_from_yaml(agent_spec) |
| 111 | + |
| 112 | + while True: |
| 113 | + print("How can I help? (type 'quit' to exit)") |
| 114 | + |
| 115 | + user_input = input("User: ") |
| 116 | + |
| 117 | + if user_input.lower() in ['quit', 'exit', 'bye']: |
| 118 | + break |
| 119 | + |
| 120 | + response = await agent.run(user_input) |
| 121 | + print("Agent response: ", response.text) |
| 122 | + input("Press enter to continue...") |
| 123 | + |
| 124 | + print("Exiting... Goodbye...") |
| 125 | + |
| 126 | + if __name__ == "__main__": |
| 127 | + asyncio.run(main()) |
| 128 | + ``` |
| 129 | + |
| 130 | +## Build and run the app |
| 131 | + |
| 132 | +1. Set the environment variable named **AZURE_APPCONFIGURATION_ENDPOINT** to the endpoint of your App Configuration store found under the *Overview* of your store in the Azure portal. |
| 133 | + |
| 134 | + If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect: |
| 135 | + |
| 136 | + ```cmd |
| 137 | + setx AZURE_APPCONFIGURATION_ENDPOINT "<endpoint-of-your-app-configuration-store>" |
| 138 | + ``` |
| 139 | + |
| 140 | + If you use PowerShell, run the following command: |
| 141 | + ```powershell |
| 142 | + $Env:AZURE_APPCONFIGURATION_ENDPOINT="<endpoint-of-your-app-configuration-store>" |
| 143 | + ``` |
| 144 | + |
| 145 | + If you use macOS or Linux, run the following command: |
| 146 | + ```bash |
| 147 | + export AZURE_APPCONFIGURATION_ENDPOINT='<endpoint-of-your-app-configuration-store>' |
| 148 | + ``` |
| 149 | + |
| 150 | +1. After the environment variable is properly set, run the following command to run the app locally: |
| 151 | + |
| 152 | + ```console |
| 153 | + python app.py |
| 154 | + ``` |
| 155 | + |
| 156 | +1. Type the message "What is the weather today in Seattle?" when prompted with "How can I help?" and then press the Enter key. |
| 157 | + |
| 158 | + ```Output |
| 159 | + How can I help? (type 'quit' to exit) |
| 160 | + User: What is the weather today in Seattle? |
| 161 | + Agent response: Today in Seattle, expect steady rain throughout the day with patchy fog, and a high temperature around 57°F (14°C). |
| 162 | + Winds are from the south-southwest at 14 to 17 mph, with gusts as high as 29 mph. |
| 163 | + Flood and wind advisories are in effect due to ongoing heavy rain and saturated conditions. |
| 164 | + Rain is likely to continue into the night, with a low near 49°F. |
| 165 | + Please stay aware of weather alerts if you are traveling or in low-lying areas [National Weather Service Seattle](https://forecast.weather.gov/zipcity.php?inputstring=Seattle%2CWA) [The Weather Channel Seattle Forecast](https://weather.com/weather/today/l/Seattle+Washington?canonicalCityId=1138ce33fd1be51ab7db675c0da0a27c). |
| 166 | + Press enter to continue... |
| 167 | + ``` |
| 168 | + |
| 169 | +## Next steps |
| 170 | + |
| 171 | +To learn how to use Chat completion configuration in your application, continue to this tutorial. |
| 172 | + |
| 173 | +> [!div class="nextstepaction"] |
| 174 | +> [Chat completion configuration](./howto-chat-completion-config.md) |
0 commit comments