1111# Get Azure App Configuration endpoint from environment variable
1212ENDPOINT = os .environ .get ("AZURE_APPCONFIG_ENDPOINT" )
1313if not ENDPOINT :
14- raise ValueError ("The environment variable 'AZURE_APPCONFIG_ENDPOINT' is not set or is empty." )
14+ raise ValueError (
15+ "The environment variable 'AZURE_APPCONFIG_ENDPOINT' is not set or is empty."
16+ )
1517
1618# Initialize Azure credentials
1719credential = DefaultAzureCredential ()
2931 trim_prefixes = ["ChatApp:" ],
3032)
3133
32- T = TypeVar ('T' )
34+ T = TypeVar ("T" )
35+
3336
3437# Get OpenAI configuration
3538def get_openai_client ():
3639 """Create and return an Azure OpenAI client"""
3740 endpoint = config .get ("AzureOpenAI:Endpoint" )
3841 # Get API key from App Configuration or fall back to environment variable
3942 api_key = config .get ("AzureOpenAI:ApiKey" , os .environ .get ("AZURE_OPENAI_API_KEY" ))
40- api_version = config .get ("AzureOpenAI:ApiVersion" , "2023-05-15" ) # Read API version from config or use default
41-
43+ api_version = config .get (
44+ "AzureOpenAI:ApiVersion" , "2023-05-15"
45+ ) # Read API version from config or use default
46+
4247 # For DefaultAzureCredential auth if no API key is available
4348 if not api_key :
4449 token_provider = get_bearer_token_provider (
4550 DefaultAzureCredential (), "https://cognitiveservices.azure.com/.default"
4651 )
4752 return AzureOpenAI (
48- azure_endpoint = endpoint ,
53+ azure_endpoint = endpoint ,
4954 api_version = api_version ,
50- azure_ad_token_provider = token_provider
55+ azure_ad_token_provider = token_provider ,
5156 )
52-
57+
5358 # For API key auth
5459 return AzureOpenAI (
55- azure_endpoint = endpoint ,
56- api_key = api_key ,
57- api_version = api_version
60+ azure_endpoint = endpoint , api_key = api_key , api_version = api_version
5861 )
5962
63+
6064def get_chat_messages (messages : List [Message ]):
6165 """Convert from model Message objects to OpenAI messages format"""
6266 return [{"role" : msg .role , "content" : msg .content } for msg in messages ]
6367
68+
6469def main ():
6570 """Main entry point for the console app"""
6671 # Get OpenAI client
6772 client = get_openai_client ()
68-
73+
6974 # Get deployment name
7075 deployment_name = config .get ("AzureOpenAI:DeploymentName" )
71-
76+
7277 # Initialize conversation history with the configuration messages
7378 conversation_history = []
7479 first_run = True
75-
80+
7681 print ("Chat Application - type 'exit' to quit\n " )
77-
82+
7883 while True :
7984 # Refresh configuration from Azure App Configuration
8085 config .refresh ()
81-
86+
8287 # Get model configuration using data binding
8388 model_config = ModelConfiguration .from_dict (config .get ("Model" ))
84-
89+
8590 # On first run, initialize conversation history with configuration messages
8691 # and display the initial messages
8792 if first_run :
8893 conversation_history = model_config .messages .copy ()
8994 for msg in conversation_history :
9095 print (f"{ msg .role } : { msg .content } " )
9196 first_run = False
92-
97+
9398 # Get chat messages for the API
9499 messages = get_chat_messages (conversation_history )
95-
100+
96101 # Get response from OpenAI
97102 response = client .chat .completions .create (
98103 model = deployment_name ,
99104 messages = messages ,
100105 max_tokens = model_config .max_tokens ,
101106 temperature = model_config .temperature ,
102- top_p = model_config .top_p
107+ top_p = model_config .top_p ,
103108 )
104-
109+
105110 # Extract assistant message
106111 assistant_message = response .choices [0 ].message .content
107-
112+
108113 # Display the response
109114 print (f"assistant: { assistant_message } " )
110-
115+
111116 # Add assistant response to conversation history
112- conversation_history .append (Message (role = "assistant" , content = assistant_message ))
113-
117+ conversation_history .append (
118+ Message (role = "assistant" , content = assistant_message )
119+ )
120+
114121 # Get user input for the next message
115122 print ("\n user: " , end = "" )
116123 user_input = input ().strip ()
117-
124+
118125 # Check if user wants to exit
119- if user_input .lower () == ' exit' :
126+ if user_input .lower () == " exit" :
120127 print ("Exiting application..." )
121128 break
122-
129+
123130 # Add user input to conversation history
124131 conversation_history .append (Message (role = "user" , content = user_input ))
125132
126- if __name__ == '__main__' :
127- main ()
133+
134+ if __name__ == "__main__" :
135+ main ()
0 commit comments