Latest Questions
  1. /
  2. Latest Articles
  3. /
  4. Code
  5. /
  6. TypeScript
  7. /
  8. Building OpenAI chat application with Angular, ASP .Net API, and...

Viewed 460 times

Building OpenAI chat application with Angular, ASP .Net API, and Azure

In this project I will show you how to build an OpenAI chat application using Angular, ASP .Net API, and Azure.


You can download or see the code at GitHub.

We will go over:

  • Setup needed on Azure
  • Code needed on the ASP .Net API
  • Code needed on the Angular front end

Azure OpenAI Setup

The first thing is to create the Azure OpenAI resource shown below.  It’s a resource not available by default in Azure and you have to apply for access.  Once Microsoft reviews your request, which takes a few days, you will be able to create the Azure OpenAI resource.  You can apply for access HERE.

Once the Azure OpenAI resource is created, go to the Model Deployment section to choose the OpenAI model that you want to use as shown below, Azure will take you to the Azure AI Studio where you can also test the chat functionality on their website.

Create the deployment by choosing the ChatGpt version you like as shown below.  At the time of publication I chose the gpt-35-turbo.

Once the model is created, note down the name of the deployment as shown below.  You will need the name of the deployment for building the API.

Go back to the Azure OpenAI resource and note down the EndPoint and Key needed for building the API as shown below.  You only need 1 key in the API.


ASP .Net API Setup

The code for getting the chat response from Azure OpenAI is shown below.  First create the OpenAIClient, followed by creating the ChatCompletionsOptions for preparing the query, which is the user chat input, then call the GetChatCompletionsAsync method to get the response from Azure OpenAI.  In the controller method we just return the content of the response from OpenAI.

As noted in the ReadMe.txt file, you will need 3 configurations below using the .net Secret Manager so that the configurations are not stored in plain text files.


For instructions on how to use the .net Secret Manager check out Securing ASP .Net Core configurations using the Secret Manager and Azure Key Vault.

Building Front End Chat UI using Angular

There are several properties needed for building the chat front end:

  1. The Chat window should stay static.  In another words, it should not expand or contract due the content inside the chat window and should stay at the same location.  When the text exceeds the window space it should allow scrolling.
  2. The order in which the chat messages are being displayed should go from bottom to top, with the latest user input or response starting from the very bottom.

For the Chat window to stay static and allow scrolling as the contents expand, declare a fixed position flexbox container (shown as the chat-container class in the code below), and the first flexbox item would just be another flexbox (shown as the messages class in the code below), which has the flex-direction as column-reverse for showing the items from bottom to top, the overflow-y as scroll for vertical scrolling, and overflow-x as hidden to hide the horizontal scrolling.

The user input and the OpenAI response need to be shown differently, which can be done by declaring 2 different classes, which are the bot-message class for showing the OpenAI response and the user-message class for showing the user input.  Notice the user-message class has align-self as flex-end so that it’s aligned to the right.

Below is the html:

Below is the scss:

Each of the message will have the message type of either 1 for user input or 2 for OpenAI response as shown in the html above.  The definition for the messages are shown below:

Below is the code of the component:

Next we need to show the latest message at the very bottom and push previous messages upwards.  Although the css definition will show the messages from bottom to top, but the order of the messages are from top to bottom which need to be reversed.  Also each call to the API is merely a request and a response, and the history of the chat will need to be kept.  These can be solved by declaring an array to store the chat history, and use the reverse method to return the chat history as an Observable.  The code for the chat service is shown below.

And that’s all, hope you will find this project useful.

Notify of
Inline Feedbacks
View all comments
About Author
© All Rights Reserved 2020
Would love your thoughts, please comment.x