|
15 | 15 | from openai import AzureOpenAI |
16 | 16 | from models import AzureOpenAIConfiguration, ChatCompletionConfiguration |
17 | 17 |
|
18 | | - |
19 | 18 | APP_CONFIG_ENDPOINT_KEY = "AZURE_APPCONFIGURATION_ENDPOINT" |
20 | 19 | CHAT_APP_KEY = "ChatApp:" |
21 | 20 | AZURE_OPENAI_KEY = "AzureOpenAI:" |
22 | 21 | CHAT_COMPLETION_KEY = "ChatCompletion" |
23 | 22 | BEARER_SCOPE = "https://cognitiveservices.azure.com/.default" |
24 | 23 |
|
25 | 24 |
|
26 | | -class ChatApp: |
27 | | - """ |
28 | | - Chat Application using Azure OpenAI and Azure App Configuration. |
29 | | - """ |
| 25 | +# Initialize CREDENTIAL and config |
| 26 | +CREDENTIAL = DefaultAzureCredential() |
30 | 27 |
|
31 | | - def __init__(self): |
32 | | - # Initialize credential and config |
33 | | - self._credential = DefaultAzureCredential() |
34 | | - |
35 | | - # Initialize chat state |
36 | | - self._chat_messages = [] |
37 | | - self._chat_conversation = [] |
38 | | - |
39 | | - # Load configuration |
40 | | - self._appconfig = self._load_config() |
41 | | - self.configure_app() |
42 | | - |
43 | | - def _get_app_config_endpoint(self): |
44 | | - app_config_endpoint = os.environ.get(APP_CONFIG_ENDPOINT_KEY) |
45 | | - if not app_config_endpoint: |
46 | | - raise ValueError(f"The environment variable '{APP_CONFIG_ENDPOINT_KEY}' is not set or is empty.") |
47 | | - return app_config_endpoint |
48 | | - |
49 | | - def _load_config(self): |
50 | | - return load( |
51 | | - endpoint=self._get_app_config_endpoint(), |
52 | | - selects=[SettingSelector(key_filter=f"{CHAT_APP_KEY}*")], |
53 | | - credential=self._credential, |
54 | | - keyvault_credential=self._credential, |
55 | | - trim_prefixes=[CHAT_APP_KEY], |
56 | | - refresh_on=[WatchKey(key=f"{CHAT_APP_KEY}{CHAT_COMPLETION_KEY}")], |
57 | | - on_refresh_success=self.configure_app, |
58 | | - ) |
| 28 | +# Initialize chat state |
| 29 | +APPCONFIG = None |
| 30 | +CHAT_COMPLETION_CONFIG = None |
59 | 31 |
|
60 | | - def configure_app(self): |
61 | | - """ |
62 | | - Configure the chat application with settings from Azure App Configuration. |
63 | | - """ |
64 | | - self.azure_openai_config = self._extract_openai_config() |
65 | | - self.azure_client = self._create_ai_client() |
66 | | - |
67 | | - # Configure chat completion with AI configuration |
68 | | - self.chat_completion_config = ChatCompletionConfiguration(**self._appconfig[CHAT_COMPLETION_KEY]) |
69 | | - self._chat_messages = self.chat_completion_config.messages |
70 | | - |
71 | | - def _create_ai_client(self) -> AzureOpenAI: |
72 | | - # Create an Azure OpenAI client |
73 | | - if self.azure_openai_config.api_key: |
74 | | - return AzureOpenAI( |
75 | | - azure_endpoint=self.azure_openai_config.endpoint, |
76 | | - api_key=self.azure_openai_config.api_key, |
77 | | - api_version=self.azure_openai_config.api_version, |
78 | | - azure_deployment=self._appconfig.get(f"{AZURE_OPENAI_KEY}DeploymentName", ""), |
79 | | - ) |
80 | | - else: |
81 | | - return AzureOpenAI( |
82 | | - azure_endpoint=self.azure_openai_config.endpoint, |
83 | | - azure_ad_token_provider=get_bearer_token_provider(self._credential, BEARER_SCOPE), |
84 | | - api_version=self.azure_openai_config.api_version, |
85 | | - azure_deployment=self._appconfig.get(f"{AZURE_OPENAI_KEY}DeploymentName", ""), |
86 | | - ) |
87 | | - |
88 | | - def _extract_openai_config(self) -> AzureOpenAIConfiguration: |
89 | | - """ |
90 | | - Extract Azure OpenAI configuration from the configuration data. |
91 | | -
|
92 | | - :param config_data: The configuration data from Azure App Configuration |
93 | | - :return: An AzureOpenAIConfiguration object |
94 | | - """ |
95 | | - return AzureOpenAIConfiguration( |
96 | | - api_key=self._appconfig.get(f"{AZURE_OPENAI_KEY}ApiKey", ""), |
97 | | - endpoint=self._appconfig.get(f"{AZURE_OPENAI_KEY}Endpoint", ""), |
98 | | - api_version=self._appconfig.get(f"{AZURE_OPENAI_KEY}ApiVersion", "2023-05-15"), |
99 | | - ) |
100 | 32 |
|
101 | | - def ask(self): |
102 | | - """ |
103 | | - Ask a question to the chat application. |
104 | | - """ |
| 33 | +def main(): |
| 34 | + global APPCONFIG |
| 35 | + app_config_endpoint = os.environ.get(APP_CONFIG_ENDPOINT_KEY) |
| 36 | + if not app_config_endpoint: |
| 37 | + raise ValueError(f"The environment variable '{APP_CONFIG_ENDPOINT_KEY}' is not set or is empty.") |
| 38 | + |
| 39 | + # Load configuration |
| 40 | + APPCONFIG = load( |
| 41 | + endpoint=app_config_endpoint, |
| 42 | + selects=[SettingSelector(key_filter=f"{CHAT_APP_KEY}*")], |
| 43 | + credential=CREDENTIAL, |
| 44 | + keyvault_credential=CREDENTIAL, |
| 45 | + trim_prefixes=[CHAT_APP_KEY], |
| 46 | + refresh_on=[WatchKey(key=f"{CHAT_APP_KEY}{CHAT_COMPLETION_KEY}")], |
| 47 | + on_refresh_success=configure_app, |
| 48 | + ) |
| 49 | + configure_app() |
| 50 | + |
| 51 | + print("Chat started! What's on your mind?") |
| 52 | + |
| 53 | + azure_openai_config = AzureOpenAIConfiguration( |
| 54 | + api_key=APPCONFIG.get(f"{AZURE_OPENAI_KEY}ApiKey", ""), |
| 55 | + endpoint=APPCONFIG.get(f"{AZURE_OPENAI_KEY}Endpoint", ""), |
| 56 | + deployment_name=APPCONFIG.get(f"{AZURE_OPENAI_KEY}DeploymentName", ""), |
| 57 | + api_version=APPCONFIG.get(f"{AZURE_OPENAI_KEY}ApiVersion", ""), |
| 58 | + ) |
| 59 | + azure_client = create_ai_client(azure_openai_config) |
| 60 | + |
| 61 | + chat_messages = [] |
| 62 | + |
| 63 | + while True: |
105 | 64 | # Refresh the configuration from Azure App Configuration |
106 | | - self._appconfig.refresh() |
| 65 | + APPCONFIG.refresh() |
107 | 66 |
|
108 | 67 | # Get user input |
109 | 68 | user_input = input("You: ") |
110 | 69 |
|
111 | 70 | # Exit if user input is empty |
112 | 71 | if not user_input.strip(): |
113 | 72 | print("Exiting chat. Goodbye!") |
114 | | - return False # Stop the conversation |
| 73 | + break |
115 | 74 |
|
116 | 75 | # Add user message to chat conversation |
117 | | - self._chat_conversation.append({"role": "user", "content": user_input}) |
| 76 | + chat_messages.append({"role": "user", "content": user_input}) |
118 | 77 |
|
119 | | - # Get latest system message from AI configuration |
120 | | - self._chat_messages.extend(self._chat_conversation) |
| 78 | + chat_conversation = CHAT_COMPLETION_CONFIG.messages |
| 79 | + chat_conversation.extend(chat_messages) |
121 | 80 |
|
122 | 81 | # Get AI response and add it to chat conversation |
123 | | - response = self.azure_client.chat.completions.create( |
124 | | - model=self.azure_openai_config.deployment_name, |
125 | | - messages=self._chat_messages, |
126 | | - max_tokens=self.chat_completion_config.max_tokens, |
127 | | - temperature=self.chat_completion_config.temperature, |
128 | | - top_p=self.chat_completion_config.top_p, |
| 82 | + response = azure_client.chat.completions.create( |
| 83 | + model=azure_openai_config.deployment_name, |
| 84 | + messages=chat_conversation, |
| 85 | + max_tokens=CHAT_COMPLETION_CONFIG.max_tokens, |
| 86 | + temperature=CHAT_COMPLETION_CONFIG.temperature, |
| 87 | + top_p=CHAT_COMPLETION_CONFIG.top_p, |
129 | 88 | ) |
130 | 89 |
|
131 | 90 | ai_response = response.choices[0].message.content |
132 | | - self._chat_conversation.append({"role": "assistant", "content": ai_response}) |
| 91 | + chat_messages.append({"role": "assistant", "content": ai_response}) |
133 | 92 | print(f"AI: {ai_response}") |
134 | | - return True |
135 | 93 |
|
136 | 94 |
|
137 | | -def main(): |
138 | | - chat_app = ChatApp() |
| 95 | +def configure_app(): |
| 96 | + """ |
| 97 | + Configure the chat application with settings from Azure App Configuration. |
| 98 | + """ |
| 99 | + global CHAT_COMPLETION_CONFIG |
| 100 | + # Configure chat completion with AI configuration |
| 101 | + CHAT_COMPLETION_CONFIG = ChatCompletionConfiguration(**APPCONFIG[CHAT_COMPLETION_KEY]) |
139 | 102 |
|
140 | | - print("Chat started! What's on your mind?") |
141 | 103 |
|
142 | | - continue_chat = True |
143 | | - while continue_chat: |
144 | | - continue_chat = chat_app.ask() |
| 104 | +def create_ai_client(azure_open_ai_config: AzureOpenAIConfiguration) -> AzureOpenAI: |
| 105 | + """ |
| 106 | + Create an Azure OpenAI client using the configuration from Azure App Configuration. |
| 107 | + """ |
| 108 | + if azure_open_ai_config.api_key: |
| 109 | + return AzureOpenAI( |
| 110 | + azure_endpoint=azure_open_ai_config.endpoint, |
| 111 | + api_key=azure_open_ai_config.api_key, |
| 112 | + api_version=azure_open_ai_config.api_version, |
| 113 | + azure_deployment=azure_open_ai_config.deployment_name, |
| 114 | + ) |
| 115 | + else: |
| 116 | + return AzureOpenAI( |
| 117 | + azure_endpoint=azure_open_ai_config.endpoint, |
| 118 | + azure_ad_token_provider=get_bearer_token_provider( |
| 119 | + CREDENTIAL, |
| 120 | + BEARER_SCOPE, |
| 121 | + ), |
| 122 | + api_version=azure_open_ai_config.api_version, |
| 123 | + azure_deployment=APPCONFIG.get(f"{AZURE_OPENAI_KEY}DeploymentName", ""), |
| 124 | + ) |
145 | 125 |
|
146 | 126 |
|
147 | 127 | if __name__ == "__main__": |
|
0 commit comments