44# license information.
55# --------------------------------------------------------------------------
66"""
7- Azure OpenAI Chat Application using Azure App Configuration.
7+ Azure AI Foundry Chat Application using Azure App Configuration.
88This 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
1212import os
13- from azure .identity import DefaultAzureCredential , get_bearer_token_provider
13+ from azure .identity import DefaultAzureCredential
1414from 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
1818APP_CONFIG_ENDPOINT_KEY = "AZURE_APPCONFIGURATION_ENDPOINT"
1919
20-
2120# Initialize CREDENTIAL
2221CREDENTIAL = 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
122104if __name__ == "__main__" :
0 commit comments