-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathapp.py
More file actions
106 lines (83 loc) · 3.51 KB
/
app.py
File metadata and controls
106 lines (83 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
Azure AI Foundry Chat Application using Azure App Configuration.
This script demonstrates how to create a chat application that uses Azure App Configuration
to manage settings and Azure AI Foundry to power chat interactions.
"""
import os
from azure.identity import DefaultAzureCredential
from azure.appconfiguration.provider import load, SettingSelector, WatchKey
from azure.ai.projects import AIProjectClient
from models import AzureAIFoundryConfiguration, ChatCompletionConfiguration
APP_CONFIG_ENDPOINT_KEY = "AZURE_APPCONFIGURATION_ENDPOINT"
# Initialize CREDENTIAL
CREDENTIAL = DefaultAzureCredential()
APPCONFIG = None
CHAT_COMPLETION_CONFIG = None
def main():
global APPCONFIG
app_config_endpoint = os.environ.get(APP_CONFIG_ENDPOINT_KEY)
if not app_config_endpoint:
raise ValueError(f"The environment variable '{APP_CONFIG_ENDPOINT_KEY}' is not set or is empty.")
# Load configuration
APPCONFIG = load(
endpoint=app_config_endpoint,
selects=[SettingSelector(key_filter="ChatApp:*")],
credential=CREDENTIAL,
trim_prefixes=["ChatApp:"],
refresh_on=[WatchKey(key="ChatApp:Sentinel")],
on_refresh_success=configure_app,
)
configure_app()
azure_foundry_config = AzureAIFoundryConfiguration(
endpoint=APPCONFIG.get("AzureAIFoundry:Endpoint", "")
)
project_client = create_project_client(azure_foundry_config)
openai_client = project_client.get_openai_client()
chat_conversation = []
print("Chat started! What's on your mind?")
while True:
# Refresh the configuration from Azure App Configuration
APPCONFIG.refresh()
# Get user input
user_input = input("You: ")
# Exit if user input is empty
if not user_input.strip():
print("Exiting chat. Goodbye!")
break
# Add user message to chat conversation
chat_conversation.append({"role": "user", "content": user_input})
if not CHAT_COMPLETION_CONFIG.messages:
CHAT_COMPLETION_CONFIG.messages = []
chat_messages = list(CHAT_COMPLETION_CONFIG.messages)
chat_messages.extend(chat_conversation)
# Get AI response and add it to chat conversation
response = openai_client.chat.completions.create(
model=CHAT_COMPLETION_CONFIG.model,
messages=chat_messages,
max_completion_tokens=CHAT_COMPLETION_CONFIG.max_completion_tokens,
)
ai_response = response.choices[0].message.content
chat_conversation.append({"role": "assistant", "content": ai_response})
print(f"AI: {ai_response}")
def configure_app():
"""
Configure the chat application with settings from Azure App Configuration.
"""
global CHAT_COMPLETION_CONFIG
# Configure chat completion with AI configuration
CHAT_COMPLETION_CONFIG = ChatCompletionConfiguration(**APPCONFIG["ChatCompletion"])
def create_project_client(config: AzureAIFoundryConfiguration) -> AIProjectClient:
"""
Create an AIProjectClient using the configuration from Azure App Configuration.
"""
return AIProjectClient(
endpoint=config.endpoint,
credential=CREDENTIAL,
)
if __name__ == "__main__":
main()