|
| 1 | +--- |
| 2 | +title: Quickstart for using chat completion configuration in a Python app |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn to implement chat completion configuration in your Python application using Azure App Configuration. |
| 5 | +services: azure-app-configuration |
| 6 | +author: mrm9084 |
| 7 | +ms.service: azure-app-configuration |
| 8 | +ms.devlang: python |
| 9 | +ms.custom: devx-track-python, mode-other |
| 10 | +ms.topic: quickstart |
| 11 | +ms.tgt_pltfrm: Python |
| 12 | +ms.date: 03/11/2026 |
| 13 | +ms.update-cycle: 180-days |
| 14 | +ms.author: mametcal |
| 15 | +ms.collection: ce-skilling-ai-copilot |
| 16 | +--- |
| 17 | + |
| 18 | +# Use chat completion configuration in a Python console app |
| 19 | + |
| 20 | +In this guide, you build an AI chat application and iterate on the prompt using chat completion configuration dynamically loaded from Azure App Configuration. |
| 21 | + |
| 22 | +The full sample source code is available in the [Azure App Configuration GitHub repository](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/ChatApp). |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- Complete the tutorial to [Create a chat completion configuration](./howto-chat-completion-config.md#create-a-chat-completion-configuration). |
| 27 | +- Python 3.9 or later - for information on setting up Python on Windows, see the [Python on Windows documentation](/windows/python/). |
| 28 | + |
| 29 | +## Create a console app |
| 30 | + |
| 31 | +1. Create a new directory for your project and navigate to it: |
| 32 | + |
| 33 | + ```bash |
| 34 | + mkdir chatapp-quickstart |
| 35 | + cd chatapp-quickstart |
| 36 | + ``` |
| 37 | + |
| 38 | +1. Install the required Python packages: |
| 39 | + |
| 40 | + ```console |
| 41 | + pip install azure-appconfiguration-provider |
| 42 | + pip install azure-identity |
| 43 | + pip install azure-ai-projects |
| 44 | + ``` |
| 45 | + |
| 46 | +1. Create a file named `app.py` and add the following import statements: |
| 47 | + |
| 48 | + ```python |
| 49 | + import os |
| 50 | + from azure.appconfiguration.provider import load, SettingSelector, WatchKey |
| 51 | + from azure.identity import DefaultAzureCredential |
| 52 | + from azure.ai.projects import AIProjectClient |
| 53 | + ``` |
| 54 | + |
| 55 | +1. Create a function to load configuration from Azure App Configuration. |
| 56 | + |
| 57 | + You can connect to 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. |
| 58 | + |
| 59 | + ```python |
| 60 | + credential = DefaultAzureCredential() |
| 61 | +
|
| 62 | + def load_azure_app_configuration(): |
| 63 | + endpoint = os.environ.get("AZURE_APPCONFIGURATION_ENDPOINT") |
| 64 | + if not endpoint: |
| 65 | + raise ValueError("AZURE_APPCONFIGURATION_ENDPOINT environment variable is not set") |
| 66 | +
|
| 67 | + config = load( |
| 68 | + endpoint=endpoint, |
| 69 | + credential=credential, |
| 70 | + # Load all keys that start with "ChatApp:" and have no label |
| 71 | + selectors=[SettingSelector(key_filter="ChatApp:*")], |
| 72 | + trim_prefixes=["ChatApp:"], |
| 73 | + # Reload configuration if the ChatCompletion key has changed. |
| 74 | + # Use the default refresh interval of 30 seconds. It can be overridden via refresh_interval. |
| 75 | + refresh_on=[WatchKey("ChatApp:ChatCompletion")], |
| 76 | + ) |
| 77 | + return config |
| 78 | + ``` |
| 79 | +
|
| 80 | +1. Create a function to get AI responses from the chat client: |
| 81 | +
|
| 82 | + ```python |
| 83 | + def get_ai_response(client, config, chat_conversation): |
| 84 | + chat_completion_config = config["ChatCompletion"] |
| 85 | + messages = [] |
| 86 | +
|
| 87 | + # Add configured messages (system, user, assistant) |
| 88 | + for msg in chat_completion_config["messages"]: |
| 89 | + messages.append({"role": msg["role"], "content": msg["content"]}) |
| 90 | +
|
| 91 | + # Add the chat conversation history |
| 92 | + messages.extend(chat_conversation) |
| 93 | +
|
| 94 | + # Create chat completion |
| 95 | + response = client.complete( |
| 96 | + model=chat_completion_config["model"], |
| 97 | + messages=messages, |
| 98 | + ) |
| 99 | + return response.choices[0].message.content |
| 100 | + ``` |
| 101 | +
|
| 102 | +1. Create the main function that configures the chat client and runs the chat loop. |
| 103 | +
|
| 104 | + Create an instance of the `AIProjectClient` to connect to your Azure AI Foundry project. You use `DefaultAzureCredential` to authenticate. Assign the **Cognitive Services OpenAI User** role to the identity represented by `DefaultAzureCredential`. For detailed steps, refer to the [Role-based access control for Azure OpenAI service](/azure/ai-services/openai/how-to/role-based-access-control) guide. Be sure to allow sufficient time for the permission to propagate before running your application. |
| 105 | +
|
| 106 | + ```python |
| 107 | + def main(): |
| 108 | + config = load_azure_app_configuration() |
| 109 | +
|
| 110 | + # Create a project client using Microsoft Entra ID |
| 111 | + project_client = AIProjectClient( |
| 112 | + endpoint=config["AzureAIFoundry:Endpoint"], |
| 113 | + credential=credential, |
| 114 | + ) |
| 115 | + openai_client = project_client.get_openai_client() |
| 116 | +
|
| 117 | + # Initialize chat conversation |
| 118 | + chat_conversation = [] |
| 119 | + print("Chat started! What's on your mind?") |
| 120 | +
|
| 121 | + while True: |
| 122 | + # Refresh the configuration from Azure App Configuration |
| 123 | + config.refresh() |
| 124 | +
|
| 125 | + # Get user input |
| 126 | + user_input = input("You: ") |
| 127 | +
|
| 128 | + # Exit if user input is empty |
| 129 | + if not user_input: |
| 130 | + print("Exiting Chat. Goodbye!") |
| 131 | + break |
| 132 | +
|
| 133 | + # Add user message to chat conversation |
| 134 | + chat_conversation.append({"role": "user", "content": user_input}) |
| 135 | +
|
| 136 | + # Get AI response and add it to chat conversation |
| 137 | + response = get_ai_response(openai_client, config, chat_conversation) |
| 138 | + print(f"AI: {response}\n") |
| 139 | + chat_conversation.append({"role": "assistant", "content": response}) |
| 140 | +
|
| 141 | + if __name__ == "__main__": |
| 142 | + main() |
| 143 | + ``` |
| 144 | +
|
| 145 | +1. After completing the previous steps, your `app.py` file should now contain the complete implementation as shown below: |
| 146 | +
|
| 147 | + ```python |
| 148 | + import os |
| 149 | + from azure.appconfiguration.provider import load, SettingSelector, WatchKey |
| 150 | + from azure.identity import DefaultAzureCredential |
| 151 | + from azure.ai.projects import AIProjectClient |
| 152 | +
|
| 153 | + credential = DefaultAzureCredential() |
| 154 | +
|
| 155 | +
|
| 156 | + def load_azure_app_configuration(): |
| 157 | + endpoint = os.environ.get("AZURE_APPCONFIGURATION_ENDPOINT") |
| 158 | + if not endpoint: |
| 159 | + raise ValueError("AZURE_APPCONFIGURATION_ENDPOINT environment variable is not set") |
| 160 | +
|
| 161 | + config = load( |
| 162 | + endpoint=endpoint, |
| 163 | + credential=credential, |
| 164 | + # Load all keys that start with "ChatApp:" and have no label |
| 165 | + selectors=[SettingSelector(key_filter="ChatApp:*")], |
| 166 | + trim_prefixes=["ChatApp:"], |
| 167 | + # Reload configuration if the ChatCompletion key has changed. |
| 168 | + # Use the default refresh interval of 30 seconds. It can be overridden via refresh_interval. |
| 169 | + refresh_on=[WatchKey("ChatApp:ChatCompletion")], |
| 170 | + ) |
| 171 | + return config |
| 172 | +
|
| 173 | +
|
| 174 | + def get_ai_response(client, config, chat_conversation): |
| 175 | + chat_completion_config = config["ChatCompletion"] |
| 176 | + messages = [] |
| 177 | +
|
| 178 | + # Add configured messages (system, user, assistant) |
| 179 | + for msg in chat_completion_config["messages"]: |
| 180 | + messages.append({"role": msg["role"], "content": msg["content"]}) |
| 181 | +
|
| 182 | + # Add the chat conversation history |
| 183 | + messages.extend(chat_conversation) |
| 184 | +
|
| 185 | + # Create chat completion |
| 186 | + response = client.chat.completions.create( |
| 187 | + model=chat_completion_config["model"], |
| 188 | + messages=messages, |
| 189 | + max_completion_tokens=chat_completion_config["max_completion_tokens"], |
| 190 | + ) |
| 191 | + return response.choices[0].message.content |
| 192 | +
|
| 193 | +
|
| 194 | + def main(): |
| 195 | + config = load_azure_app_configuration() |
| 196 | +
|
| 197 | + # Create a project client using Microsoft Entra ID |
| 198 | + project_client = AIProjectClient( |
| 199 | + endpoint=config["AzureAIFoundry:Endpoint"], |
| 200 | + credential=credential, |
| 201 | + ) |
| 202 | + openai_client = project_client.get_openai_client() |
| 203 | +
|
| 204 | + # Initialize chat conversation |
| 205 | + chat_conversation = [] |
| 206 | + print("Chat started! What's on your mind?") |
| 207 | +
|
| 208 | + while True: |
| 209 | + # Refresh the configuration from Azure App Configuration |
| 210 | + config.refresh() |
| 211 | +
|
| 212 | + # Get user input |
| 213 | + user_input = input("You: ") |
| 214 | +
|
| 215 | + # Exit if user input is empty |
| 216 | + if not user_input: |
| 217 | + print("Exiting Chat. Goodbye!") |
| 218 | + break |
| 219 | +
|
| 220 | + # Add user message to chat conversation |
| 221 | + chat_conversation.append({"role": "user", "content": user_input}) |
| 222 | +
|
| 223 | + # Get AI response and add it to chat conversation |
| 224 | + response = get_ai_response(openai_client, config, chat_conversation) |
| 225 | + print(f"AI: {response}\n") |
| 226 | + chat_conversation.append({"role": "assistant", "content": response}) |
| 227 | +
|
| 228 | +
|
| 229 | + if __name__ == "__main__": |
| 230 | + main() |
| 231 | + ``` |
| 232 | +
|
| 233 | +## Build and run the app |
| 234 | +
|
| 235 | +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. |
| 236 | +
|
| 237 | + If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect: |
| 238 | +
|
| 239 | + ```cmd |
| 240 | + setx AZURE_APPCONFIGURATION_ENDPOINT "<endpoint-of-your-app-configuration-store>" |
| 241 | + ``` |
| 242 | +
|
| 243 | + If you use PowerShell, run the following command: |
| 244 | + ```powershell |
| 245 | + $Env:AZURE_APPCONFIGURATION_ENDPOINT = "<endpoint-of-your-app-configuration-store>" |
| 246 | + ``` |
| 247 | +
|
| 248 | + If you use macOS or Linux, run the following command: |
| 249 | + ```bash |
| 250 | + export AZURE_APPCONFIGURATION_ENDPOINT='<endpoint-of-your-app-configuration-store>' |
| 251 | + ``` |
| 252 | +
|
| 253 | +1. After the environment variable is properly set, run the following command to run your app: |
| 254 | + ```console |
| 255 | + python app.py |
| 256 | + ``` |
| 257 | +
|
| 258 | +1. Type the message "What is your name?" when prompted with "You:" and then press the Enter key. |
| 259 | +
|
| 260 | + ```Output |
| 261 | + Chat started! What's on your mind? |
| 262 | + You: What is your name? |
| 263 | + AI: I'm your helpful assistant! I don't have a personal name, but you can call me whatever you'd like. |
| 264 | + 😊 Do you have a name in mind? |
| 265 | + ``` |
| 266 | +
|
| 267 | +1. In Azure portal, select the App Configuration store instance that you created. From the **Operations** menu, select **Configuration explorer** and select the **ChatApp:ChatCompletion** key. Update the value of the Messages property: |
| 268 | + - Role: **system** |
| 269 | + - Content: "You are a pirate and your name is Eddy." |
| 270 | +
|
| 271 | +1. Type the same message when prompted with "You:". Be sure to wait a few moments for the refresh interval to elapse, and then press the Enter key to see the updated AI response in the output. |
| 272 | +
|
| 273 | + ```Output |
| 274 | + Chat started! What's on your mind? |
| 275 | + You: What is your name? |
| 276 | + AI: I'm your helpful assistant! I don't have a personal name, but you can call me whatever you'd like. |
| 277 | + 😊 Do you have a name in mind? |
| 278 | +
|
| 279 | + You: What is your name? |
| 280 | + AI: Arrr, matey! Me name be Eddy, the most fearsome pirate to ever sail the seven seas! |
| 281 | + What be yer name, landlubber? |
| 282 | + ``` |
0 commit comments