Create a generative AI chat app
In this exercise, you use the Azure AI Foundry SDK to create a simple chat app that connects to a project and chats with a language model.
This exercise takes approximately 30 minutes.
Create an Azure AI Foundry project
Let’s start by creating an Azure AI Foundry project.
-
In a web browser, open the Azure AI Foundry portal at
https://ai.azure.com
and sign in using your Azure credentials. Close any tips or quick start panes that are opened the first time you sign in, and if necessary use the Azure AI Foundry logo at the top left to navigate to the home page, which looks similar to the following image: - In the home page, select + Create project.
- In the Create a project wizard, enter a suitable project name for (for example,
my-ai-project
) then review the Azure resources that will be automatically created to support your project. - Select Customize and specify the following settings for your hub:
- Hub name: A unique name - for example
my-ai-hub
- Subscription: Your Azure subscription
- Resource group: Create a new resource group with a unique name (for example,
my-ai-resources
), or select an existing one - Location: Select Help me choose and then select gpt-4 in the Location helper window and use the recommended region*
- Connect Azure AI Services or Azure OpenAI: Create a new AI Services resource with an appropriate name (for example,
my-ai-services
) or use an existing one - Connect Azure AI Search: Skip connecting
* Azure OpenAI resources are constrained at the tenant level by regional quotas. In the event of a quota limit being reached later in the exercise, there’s a possibility you may need to create another resource in a different region.
- Hub name: A unique name - for example
- Select Next and review your configuration. Then select Create and wait for the process to complete.
-
When your project is created, close any tips that are displayed and review the project page in Azure AI Foundry portal, which should look similar to the following image:
Deploy a generative AI model
Now you’re ready to deploy a generative AI language model to support your chat application. In this example, you’ll use the Microsoft Phi-4 model; but the principles are the same for any model.
- In the toolbar at the top right of your Azure AI Foundry project page, use the Preview features icon to enable the Deploy models to Azure AI model inference service feature. This feature ensures your model deployment is available to the Azure AI Inference service, which you’ll use in your application code.
- In the pane on the left for your project, in the My assets section, select the Models + endpoints page.
- In the Models + endpoints page, in the Model deployments tab, in the + Deploy model menu, select Deploy base model.
- Search for the Phi-4 model in the list, and then select and confirm it.
- Agree to the license agreement if prompted, and then deploy the model with the following settings by selecting Customize in the deployment details:
- Deployment name: A unique name for your model deployment - for example
phi-4-model
(remember the name you assign, you’ll need it later) - Deployment type: Global standard
- Deployment details: Use the default settings
- Deployment name: A unique name for your model deployment - for example
- Wait for the deployment provisioning state to be Completed.
Create a client application to chat with the model
Now that you have deployed a model, you can use the Azure AI Foundry SDK to develop an application that chats with it.
Tip: You can choose to develop your solution using Python or Microsoft C#. Follow the instructions in the appropriate section for your chosen language.
Prepare the application configuration
- In the Azure AI Foundry portal, view the Overview page for your project.
- In the Project details area, note the Project connection string. You’ll use this connection string to connect to your project in a client application.
- Open a new browser tab (keeping the Azure AI Foundry portal open in the existing tab). Then in the new tab, browse to the Azure portal at
https://portal.azure.com
; signing in with your Azure credentials if prompted. -
Use the [>_] button to the right of the search bar at the top of the page to create a new Cloud Shell in the Azure portal, selecting a PowerShell environment. The cloud shell provides a command line interface in a pane at the bottom of the Azure portal.
Note: If you have previously created a cloud shell that uses a Bash environment, switch it to PowerShell.
-
In the cloud shell toolbar, in the Settings menu, select Go to Classic version (this is required to use the code editor).
Tip: As you paste commands into the cloudshell, the ouput may take up a large amount of the screen buffer. You can clear the screen by entering the
cls
command to make it easier to focus on each task. -
In the PowerShell pane, enter the following commands to clone the GitHub repo for this exercise:
rm -r mslearn-ai-foundry -f git clone https://github.com/microsoftlearning/mslearn-ai-studio mslearn-ai-foundry
Note: Follow the steps for your chosen programming language.
-
After the repo has been cloned, navigate to the folder containing the chat application code files:
Python
cd mslearn-ai-foundry/labfiles/chat-app/python
C#
cd mslearn-ai-foundry/labfiles/chat-app/c-sharp
-
In the cloud shell command line pane, enter the following command to install the libraries you’ll use:
Python
pip install python-dotenv azure-identity azure-ai-projects azure-ai-inference
C#
dotnet add package Azure.AI.Inference dotnet add package Azure.AI.Projects --prerelease dotnet add package Azure.Identity
-
Enter the following command to edit the configuration file that has been provided:
Python
code .env
C#
code appsettings.json
The file is opened in a code editor.
- In the code file, replace the your_project_endpoint placeholder with the connection string for your project (copied from the project Overview page in the Azure AI Foundry portal), and the your_model_deployment placeholder with the name you assigned to your Phi-4 model deployment.
- After you’ve replaced the placeholders, use the CTRL+S command to save your changes and then use the CTRL+Q command to close the code editor while keeping the cloud shell command line open.
Write code to connect to your project and chat with your model
Tip: As you add code, be sure to maintain the correct indentation.
-
Enter the following command to edit the code file that has been provided:
Python
code chat-app.py
C#
code Program.cs
-
In the code file, note the existing statements that have been added at the top of the file to import the necessary SDK namespaces. Then, under the comment Add references, add the following code to reference the namespaces in the libraries you installed previously:
Python
from dotenv import load_dotenv from azure.identity import DefaultAzureCredential from azure.ai.projects import AIProjectClient
C#
using Azure.Identity; using Azure.AI.Projects; using Azure.AI.Inference;
- In the main function, under the comment Get configuration settings, note that the code loads the project connection string and model deployment name values you defined in the configuration file.
-
Under the comment Initialize the project client, add the following code to connect to your Azure AI Foundry project using the Azure credentials you are currently signed in with:
Python
projectClient = AIProjectClient.from_connection_string( conn_str=project_connection, credential=DefaultAzureCredential())
C#
var projectClient = new AIProjectClient(project_connection, new DefaultAzureCredential());
-
Under the comment Get a chat client, add the following code to create a client object for chatting with a model:
Python
chat = projectClient.inference.get_chat_completions_client()
C#
ChatCompletionsClient chat = projectClient.GetChatCompletionsClient();
-
Note that the code includes a loop to allow a user to input a prompt until they enter “quit”. Then in the loop section, under the comment Get a chat completion, add the following code to submit the prompt and retrieve the completion from your model:
Python
response = chat.complete( model=model_deployment, messages=[ {"role": "system", "content": "You are a helpful AI assistant that answers questions."}, {"role": "user", "content": input_text}, ], ) print(response.choices[0].message.content)
C#
var requestOptions = new ChatCompletionsOptions() { Model = model_deployment, Messages = { new ChatRequestSystemMessage("You are a helpful AI assistant that answers questions."), new ChatRequestUserMessage(input_text), } }; Response<ChatCompletions> response = chat.Complete(requestOptions); Console.WriteLine(response.Value.Content);
- Use the CTRL+S command to save your changes to the code file and then use the CTRL+Q command to close the code editor while keeping the cloud shell command line open.
Run the chat application
-
In the cloud shell command line pane, enter the following command to run the app:
Python
python chat-app.py
C#
dotnet run
- When prompted, enter a question, such as
What is the fastest animal on Earth?
and review the response from your generative AI model. - Try a few more questions. When you’re finished, enter
quit
to exit the program.
Summary
In this exercise, you used the Azure AI Foundry SDK to create a client application for a generative AI model that you deployed in an Azure AI Foundry project.
Clean up
If you’ve finished exploring Azure AI Foundry portal, you should delete the resources you have created in this exercise to avoid incurring unnecessary Azure costs.
- Return to the browser tab containing the Azure portal (or re-open the Azure portal at
https://portal.azure.com
in a new browser tab) and view the contents of the resource group where you deployed the resources used in this exercise. - On the toolbar, select Delete resource group.
- Enter the resource group name and confirm that you want to delete it.