-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (112 loc) · 4.71 KB
/
app.py
File metadata and controls
141 lines (112 loc) · 4.71 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
Azure App Configuration Chat Application using Azure OpenAI.
This module provides a simple chat application that uses configurations from Azure App Configuration
and connects to Azure OpenAI services for chat completions.
"""
import os
from typing import TypeVar, List
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from azure.appconfiguration.provider import load, SettingSelector
from openai import AzureOpenAI
from models import ModelConfiguration, Message
# Get Azure App Configuration endpoint from environment variable
ENDPOINT = os.environ.get("AZURE_APPCONFIG_ENDPOINT")
if not ENDPOINT:
raise ValueError(
"The environment variable 'AZURE_APPCONFIG_ENDPOINT' is not set or is empty."
)
# Initialize Azure credentials
credential = DefaultAzureCredential()
# Create selector for ChatApp configuration
chat_app_selector = SettingSelector(key_filter="ChatApp:*")
# Load configuration from Azure App Configuration
config = load(
endpoint=ENDPOINT,
selects=[chat_app_selector],
credential=credential,
keyvault_credential=credential, # Use the same credential for Key Vault references
trim_prefixes=["ChatApp:"],
)
T = TypeVar("T")
# Get OpenAI configuration
def get_openai_client():
"""Create and return an Azure OpenAI client"""
endpoint = config.get("AzureOpenAI:Endpoint")
# Get API key from App Configuration or fall back to environment variable
api_key = config.get("AzureOpenAI:ApiKey", os.environ.get("AZURE_OPENAI_API_KEY"))
api_version = config.get(
"AzureOpenAI:ApiVersion", "2023-05-15"
) # Read API version from config or use default
# For DefaultAzureCredential auth if no API key is available
if not api_key:
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
)
return AzureOpenAI(
azure_endpoint=endpoint,
api_version=api_version,
azure_ad_token_provider=token_provider,
)
# For API key auth
return AzureOpenAI(
azure_endpoint=endpoint, api_key=api_key, api_version=api_version
)
def get_chat_messages(messages: List[Message]):
"""Convert from model Message objects to OpenAI messages format"""
return [{"role": msg.role, "content": msg.content} for msg in messages]
def main():
"""Main entry point for the console app"""
# Get OpenAI client
client = get_openai_client()
# Get deployment name
deployment_name = config.get("AzureOpenAI:DeploymentName")
# Initialize conversation history with the configuration messages
conversation_history = []
first_run = True
print("Chat Application - type 'exit' to quit\n")
while True:
# Refresh configuration from Azure App Configuration
config.refresh()
# Get model configuration using data binding
model_config = ModelConfiguration.from_dict(config.get("Model"))
# On first run, initialize conversation history with configuration messages
# and display the initial messages
if first_run:
conversation_history = model_config.messages.copy()
for msg in conversation_history:
print(f"{msg.role}: {msg.content}")
first_run = False
# Get chat messages for the API
messages = get_chat_messages(conversation_history)
# Get response from OpenAI
response = client.chat.completions.create(
model=deployment_name,
messages=messages,
max_tokens=model_config.max_tokens,
temperature=model_config.temperature,
top_p=model_config.top_p,
)
# Extract assistant message
assistant_message = response.choices[0].message.content
# Display the response
print(f"assistant: {assistant_message}")
# Add assistant response to conversation history
conversation_history.append(
Message(role="assistant", content=assistant_message)
)
# Get user input for the next message
print("\nuser: ", end="")
user_input = input().strip()
# Check if user wants to exit
if user_input.lower() == "exit":
print("Exiting application...")
break
# Add user input to conversation history
conversation_history.append(Message(role="user", content=user_input))
if __name__ == "__main__":
main()