Skip to content

Commit c0bb7c1

Browse files
authored
Updating Python Chat App (#1137)
* Updating Chat App * Update app.py
1 parent f06a3c2 commit c0bb7c1

4 files changed

Lines changed: 36 additions & 64 deletions

File tree

examples/Python/ChatApp/README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
# Azure App Configuration - Python ChatApp Sample
22

3-
This sample demonstrates using Azure App Configuration to configure Azure OpenAI settings for a chat application built with Python.
3+
This sample demonstrates using Azure App Configuration to configure Azure AI Foundry settings for a chat application built with Python.
44

55
## Features
66

7-
- Integrates with Azure OpenAI for chat completions
7+
- Integrates with Azure AI Foundry for chat completions
88
- Dynamically refreshes configuration from Azure App Configuration
99

1010
## Prerequisites
1111

12-
- Python 3.8 or later
12+
- Python 3.9 or later
1313
- An Azure subscription with access to:
1414
- Azure App Configuration service
15-
- Azure OpenAI service
15+
- Azure AI Foundry project
1616
- Required environment variables:
1717
- `AZURE_APPCONFIGURATION_ENDPOINT`: Endpoint URL of your Azure App Configuration instance
18-
- `AZURE_OPENAI_API_KEY`: API key for Azure OpenAI (optional if stored in Azure App Configuration)
1918

2019
## Setup
2120

@@ -29,15 +28,9 @@ This sample demonstrates using Azure App Configuration to configure Azure OpenAI
2928
1. Configure your Azure App Configuration store with these settings:
3029

3130
```console
32-
ChatApp:AzureOpenAI:Endpoint - Your Azure OpenAI endpoint URL
33-
ChatApp:AzureOpenAI:DeploymentName - Your Azure OpenAI deployment name
34-
ChatApp:AzureOpenAI:ApiVersion - API version for Azure OpenAI (e.g., "2023-05-15")
35-
ChatApp:AzureOpenAI:ApiKey - Your Azure OpenAI API key (Optional only required when not using AAD, preferably as a Key Vault reference)
36-
ChatApp:Model - An AI configuration entry containing the following settings:
37-
- model - Model name (e.g., "gpt-35-turbo")
38-
- max_tokens - Maximum tokens for completion (e.g., 1000)
39-
- temperature - Temperature parameter (e.g., 0.7)
40-
- top_p - Top p parameter (e.g., 0.95)
31+
ChatApp:AzureAIFoundry:Endpoint - Your Azure AI Foundry project endpoint URL
32+
ChatApp:ChatCompletion - An AI configuration entry containing the following settings:
33+
- model - Model name (e.g., "gpt-5")
4134
- messages - An array of messages with role and content for each message
4235
ChatApp:Sentinel - A sentinel key to trigger configuration refreshes
4336
```
@@ -46,7 +39,6 @@ This sample demonstrates using Azure App Configuration to configure Azure OpenAI
4639

4740
```bash
4841
export AZURE_APPCONFIGURATION_ENDPOINT="https://your-appconfig.azconfig.io"
49-
export AZURE_OPENAI_API_KEY="your-openai-api-key" # Optional if stored in Azure App Configuration
5042
```
5143

5244
## Running the Application

examples/Python/ChatApp/app.py

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
"""
7-
Azure OpenAI Chat Application using Azure App Configuration.
7+
Azure AI Foundry Chat Application using Azure App Configuration.
88
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.
9+
to manage settings and Azure AI Foundry to power chat interactions.
1010
"""
1111

1212
import os
13-
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
13+
from azure.identity import DefaultAzureCredential
1414
from azure.appconfiguration.provider import load, SettingSelector, WatchKey
15-
from openai import AzureOpenAI
16-
from models import AzureOpenAIConfiguration, ChatCompletionConfiguration
15+
from azure.ai.inference import ChatCompletionsClient
16+
from models import AzureAIFoundryConfiguration, ChatCompletionConfiguration
1717

1818
APP_CONFIG_ENDPOINT_KEY = "AZURE_APPCONFIGURATION_ENDPOINT"
1919

20-
2120
# Initialize CREDENTIAL
2221
CREDENTIAL = DefaultAzureCredential()
2322

@@ -36,22 +35,18 @@ def main():
3635
endpoint=app_config_endpoint,
3736
selects=[SettingSelector(key_filter="ChatApp:*")],
3837
credential=CREDENTIAL,
39-
keyvault_credential=CREDENTIAL,
4038
trim_prefixes=["ChatApp:"],
4139
refresh_on=[WatchKey(key="ChatApp:Sentinel")],
4240
on_refresh_success=configure_app,
4341
)
4442
configure_app()
4543

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", ""),
44+
azure_foundry_config = AzureAIFoundryConfiguration(
45+
endpoint=APPCONFIG.get("AzureAIFoundry:Endpoint", "")
5146
)
52-
azure_client = create_azure_openai_client(azure_openai_config)
47+
chat_client = create_chat_client(azure_foundry_config)
5348

54-
chat_conversation = []
49+
chat_conversation = []
5550

5651
print("Chat started! What's on your mind?")
5752

@@ -70,20 +65,19 @@ def main():
7065
# Add user message to chat conversation
7166
chat_conversation.append({"role": "user", "content": user_input})
7267

68+
if not CHAT_COMPLETION_CONFIG.messages:
69+
CHAT_COMPLETION_CONFIG.messages = []
7370
chat_messages = list(CHAT_COMPLETION_CONFIG.messages)
7471
chat_messages.extend(chat_conversation)
7572

7673
# Get AI response and add it to chat conversation
77-
response = azure_client.chat.completions.create(
78-
model=azure_openai_config.deployment_name,
74+
response = chat_client.complete(
75+
model=CHAT_COMPLETION_CONFIG.model,
7976
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,
8377
)
8478

8579
ai_response = response.choices[0].message.content
86-
chat_conversation .append({"role": "assistant", "content": ai_response})
80+
chat_conversation.append({"role": "assistant", "content": ai_response})
8781
print(f"AI: {ai_response}")
8882

8983

@@ -96,27 +90,15 @@ def configure_app():
9690
CHAT_COMPLETION_CONFIG = ChatCompletionConfiguration(**APPCONFIG["ChatCompletion"])
9791

9892

99-
def create_azure_openai_client(azure_openai_config: AzureOpenAIConfiguration) -> AzureOpenAI:
93+
def create_chat_client(config: AzureAIFoundryConfiguration) -> ChatCompletionsClient:
10094
"""
101-
Create an Azure OpenAI client using the configuration from Azure App Configuration.
95+
Create a ChatCompletionsClient using the configuration from Azure App Configuration.
10296
"""
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-
)
97+
return ChatCompletionsClient(
98+
endpoint=config.endpoint,
99+
credential=CREDENTIAL,
100+
credential_scopes=["https://cognitiveservices.azure.com/.default"],
101+
)
120102

121103

122104
if __name__ == "__main__":

examples/Python/ChatApp/models.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,19 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
"""
7-
Model classes for Azure OpenAI Chat Application.
7+
Model classes for Azure AI Foundry Chat Application.
88
"""
99
from dataclasses import dataclass
1010
from typing import List, Optional, Dict
1111

1212

1313
@dataclass
14-
class AzureOpenAIConfiguration:
14+
class AzureAIFoundryConfiguration:
1515
"""
16-
Represents the configuration for Azure OpenAI service.
16+
Represents the configuration for Azure AI Foundry service.
1717
"""
1818

19-
api_key: str
2019
endpoint: str
21-
deployment_name: str
22-
api_version: Optional[str] = None
2320

2421

2522
@dataclass
@@ -28,8 +25,9 @@ class ChatCompletionConfiguration:
2825
Represents the configuration for an AI model including messages and parameters.
2926
"""
3027

31-
max_tokens: int
32-
temperature: float
33-
top_p: float
3428
model: Optional[str] = None
29+
max_completion_tokens: Optional[int] = None
30+
reasoning_effort: Optional[str] = None
31+
verbosity: Optional[str] = None
32+
stream: Optional[bool] = None
3533
messages: Optional[List[Dict[str, str]]] = None
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
azure-identity
22
azure-appconfiguration-provider<3.0.0
3-
openai
3+
azure-ai-inference

0 commit comments

Comments
 (0)