Skip to content

Commit 295c876

Browse files
committed
review changes
1 parent 6cf8451 commit 295c876

4 files changed

Lines changed: 91 additions & 99 deletions

File tree

examples/Python/ChatApp/app.py

Lines changed: 76 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -15,133 +15,113 @@
1515
from openai import AzureOpenAI
1616
from models import AzureOpenAIConfiguration, ChatCompletionConfiguration
1717

18-
1918
APP_CONFIG_ENDPOINT_KEY = "AZURE_APPCONFIGURATION_ENDPOINT"
2019
CHAT_APP_KEY = "ChatApp:"
2120
AZURE_OPENAI_KEY = "AzureOpenAI:"
2221
CHAT_COMPLETION_KEY = "ChatCompletion"
2322
BEARER_SCOPE = "https://cognitiveservices.azure.com/.default"
2423

2524

26-
class ChatApp:
27-
"""
28-
Chat Application using Azure OpenAI and Azure App Configuration.
29-
"""
25+
# Initialize CREDENTIAL and config
26+
CREDENTIAL = DefaultAzureCredential()
3027

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
5931

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-
)
10032

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:
10564
# Refresh the configuration from Azure App Configuration
106-
self._appconfig.refresh()
65+
APPCONFIG.refresh()
10766

10867
# Get user input
10968
user_input = input("You: ")
11069

11170
# Exit if user input is empty
11271
if not user_input.strip():
11372
print("Exiting chat. Goodbye!")
114-
return False # Stop the conversation
73+
break
11574

11675
# 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})
11877

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)
12180

12281
# 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,
12988
)
13089

13190
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})
13392
print(f"AI: {ai_response}")
134-
return True
13593

13694

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])
139102

140-
print("Chat started! What's on your mind?")
141103

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+
)
145125

146126

147127
if __name__ == "__main__":

examples/Python/ChatApp/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class ChatCompletionConfiguration:
3232
Maps to configuration values with keys 'model', 'messages', 'max_tokens', 'temperature', and 'top_p'.
3333
"""
3434

35+
max_tokens: int
36+
temperature: float
37+
top_p: float
3538
model: Optional[str] = None
3639
messages: Optional[List[Dict[str, str]]] = None
37-
max_tokens: int = 1024
38-
temperature: float = 0.7
39-
top_p: float = 0.95
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[tool.black]
2+
line-length = 120
3+
target-version = ['py38']
4+
include = '\.pyi?$'
5+
6+
[tool.pylint.format]
7+
max-line-length = 120
8+

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[tool.black]
2+
line-length = 120
3+
target-version = ['py38']
4+
include = '\.pyi?$'

0 commit comments

Comments
 (0)