|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for |
| 4 | +# license information. |
| 5 | +# -------------------------------------------------------------------------- |
| 6 | +""" |
| 7 | +Azure OpenAI Chat Application using Azure App Configuration. |
| 8 | +This script demonstrates how to create a chat application that uses Azure App Configuration |
| 9 | +to manage settings and Azure OpenAI to power chat interactions. |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +from azure.identity import DefaultAzureCredential, get_bearer_token_provider |
| 14 | +from azure.appconfiguration.provider import load, SettingSelector, WatchKey |
| 15 | +from openai import AzureOpenAI |
| 16 | +from models import AzureOpenAIConfiguration, ChatCompletionConfiguration |
| 17 | + |
| 18 | +APP_CONFIG_ENDPOINT_KEY = "AZURE_APPCONFIGURATION_ENDPOINT" |
| 19 | + |
| 20 | + |
| 21 | +# Initialize CREDENTIAL |
| 22 | +CREDENTIAL = DefaultAzureCredential() |
| 23 | + |
| 24 | +APPCONFIG = None |
| 25 | +CHAT_COMPLETION_CONFIG = None |
| 26 | + |
| 27 | + |
| 28 | +def main(): |
| 29 | + global APPCONFIG |
| 30 | + app_config_endpoint = os.environ.get(APP_CONFIG_ENDPOINT_KEY) |
| 31 | + if not app_config_endpoint: |
| 32 | + raise ValueError(f"The environment variable '{APP_CONFIG_ENDPOINT_KEY}' is not set or is empty.") |
| 33 | + |
| 34 | + # Load configuration |
| 35 | + APPCONFIG = load( |
| 36 | + endpoint=app_config_endpoint, |
| 37 | + selects=[SettingSelector(key_filter="ChatApp:*")], |
| 38 | + credential=CREDENTIAL, |
| 39 | + keyvault_credential=CREDENTIAL, |
| 40 | + trim_prefixes=["ChatApp:"], |
| 41 | + refresh_on=[WatchKey(key="ChatApp:Sentinel")], |
| 42 | + on_refresh_success=configure_app, |
| 43 | + ) |
| 44 | + configure_app() |
| 45 | + |
| 46 | + azure_openai_config = AzureOpenAIConfiguration( |
| 47 | + api_key=APPCONFIG.get("AzureOpenAI:ApiKey", ""), |
| 48 | + endpoint=APPCONFIG.get("AzureOpenAI:Endpoint", ""), |
| 49 | + deployment_name=APPCONFIG.get("AzureOpenAI:DeploymentName", ""), |
| 50 | + api_version=APPCONFIG.get("AzureOpenAI:ApiVersion", ""), |
| 51 | + ) |
| 52 | + azure_client = create_azure_openai_client(azure_openai_config) |
| 53 | + |
| 54 | + chat_conversation = [] |
| 55 | + |
| 56 | + print("Chat started! What's on your mind?") |
| 57 | + |
| 58 | + while True: |
| 59 | + # Refresh the configuration from Azure App Configuration |
| 60 | + APPCONFIG.refresh() |
| 61 | + |
| 62 | + # Get user input |
| 63 | + user_input = input("You: ") |
| 64 | + |
| 65 | + # Exit if user input is empty |
| 66 | + if not user_input.strip(): |
| 67 | + print("Exiting chat. Goodbye!") |
| 68 | + break |
| 69 | + |
| 70 | + # Add user message to chat conversation |
| 71 | + chat_conversation.append({"role": "user", "content": user_input}) |
| 72 | + |
| 73 | + chat_messages = list(CHAT_COMPLETION_CONFIG.messages) |
| 74 | + chat_messages.extend(chat_conversation) |
| 75 | + |
| 76 | + # Get AI response and add it to chat conversation |
| 77 | + response = azure_client.chat.completions.create( |
| 78 | + model=azure_openai_config.deployment_name, |
| 79 | + messages=chat_messages, |
| 80 | + max_tokens=CHAT_COMPLETION_CONFIG.max_tokens, |
| 81 | + temperature=CHAT_COMPLETION_CONFIG.temperature, |
| 82 | + top_p=CHAT_COMPLETION_CONFIG.top_p, |
| 83 | + ) |
| 84 | + |
| 85 | + ai_response = response.choices[0].message.content |
| 86 | + chat_conversation .append({"role": "assistant", "content": ai_response}) |
| 87 | + print(f"AI: {ai_response}") |
| 88 | + |
| 89 | + |
| 90 | +def configure_app(): |
| 91 | + """ |
| 92 | + Configure the chat application with settings from Azure App Configuration. |
| 93 | + """ |
| 94 | + global CHAT_COMPLETION_CONFIG |
| 95 | + # Configure chat completion with AI configuration |
| 96 | + CHAT_COMPLETION_CONFIG = ChatCompletionConfiguration(**APPCONFIG["ChatCompletion"]) |
| 97 | + |
| 98 | + |
| 99 | +def create_azure_openai_client(azure_openai_config: AzureOpenAIConfiguration) -> AzureOpenAI: |
| 100 | + """ |
| 101 | + Create an Azure OpenAI client using the configuration from Azure App Configuration. |
| 102 | + """ |
| 103 | + if azure_openai_config.api_key: |
| 104 | + return AzureOpenAI( |
| 105 | + azure_endpoint=azure_openai_config.endpoint, |
| 106 | + api_key=azure_openai_config.api_key, |
| 107 | + api_version=azure_openai_config.api_version, |
| 108 | + azure_deployment=azure_openai_config.deployment_name, |
| 109 | + ) |
| 110 | + else: |
| 111 | + return AzureOpenAI( |
| 112 | + azure_endpoint=azure_openai_config.endpoint, |
| 113 | + azure_ad_token_provider=get_bearer_token_provider( |
| 114 | + CREDENTIAL, |
| 115 | + "https://cognitiveservices.azure.com/.default", |
| 116 | + ), |
| 117 | + api_version=azure_openai_config.api_version, |
| 118 | + azure_deployment=azure_openai_config.deployment_name, |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments