Building Your Own Chatbot using Azure OpenAI Capabilities
I am Sarita , Microsoft Learn Student Ambassador. In this guide, we’ll walk you through the steps to set up a chatbot using Open AI’s GPT-4o model that leverages Azure’s advanced language models. At the end, I have provided with a link to the repository to integrate the chatbot in your very own website.
Building Your Own Chatbot with Azure OpenAI Capabilities
What is Azure Open AI Service ?
“Azure OpenAI Service provides REST API access to OpenAI’s powerful language models including GPT-4o, GPT-4o mini, GPT-4 Turbo with Vision, GPT-4, GPT-3.5-Turbo, and Embeddings model series. These models can be easily adapted to your specific task including but not limited to content generation, summarization, image understanding, semantic search, and natural language to code translation. Users can access the service through REST APIs, Python SDK, or our web-based interface in the Azure OpenAI Studio.” – Microsoft Learn
Prerequisites
An Azure Account with Active Azure Subscription.
Account on Azure Open AI Studio
Understanding Key Terms
Before diving into the setup, it’s essential to familiarize yourself with some key terms:
Playground: A user-friendly interface that allows you to explore, prototype, and test AI assistants without any coding requirements. It’s perfect for iterating and experimenting with new ideas.
Azure AI Services: A suite of tools and services that leverage artificial intelligence to help you build intelligent applications.
Resource Group: A container that holds related Azure resources for management and billing.
Keys and Endpoints: Security credentials and URLs that allow your applications to access Azure services.
Model Deployment: The process of configuring and launching a specific AI model for use.
System Message: Instructions provided to the AI model to guide its behaviour and response context.
Prompt: The input you give to the AI model to elicit a specific response.
Token : Tokens can be thought of as pieces of words. Before the API processes the request, the input is broken down into tokens. These tokens are not cut up exactly where the words start or end – tokens can include trailing spaces and even sub-words. Here are some helpful rules of thumb for understanding tokens in terms of lengths: 1 token ~= 4 chars in English,1 token ~= ¾ words ,100 tokens ~= 75 words.
Selecting the Right Model on Right Pricing
Models
Description
GPT-4o & GPT-4o mini & GPT-4 Turbo
The latest most capable Azure OpenAI models with multimodal versions, which can accept both text and images as input.
A set of models that improve on GPT-3.5 and can understand and generate natural language and code.
A set of models that improve on GPT-3 and can understand and generate natural language and code.
A set of models that can convert text into numerical vector form to facilitate text similarity.
A series of models that can generate original images from natural language.
A series of models in preview that can transcribe and translate speech to text.
Text to speech (Preview)
A series of models in preview that can synthesize text to speech.
– Microsoft learn Open AI service
Azure OpenAI Service offers two main pricing models for using its AI tools: Standard (On-Demand) and Provisioned Throughput Units (PTUs). Let’s understand both one by one:
Standard (On-Demand) Pricing:
You only pay for what you use.
The cost is calculated based on the number of tokens processed, where tokens represent chunks of text (input and output).
For each 1,000 tokens, there is a specific cost. For example, the more complex the model (like GPT-4), the higher the token cost.
This model is flexible and works well if you’re unsure how much you’ll use the service or if you don’t need continuous access.
Provisioned Throughput Units (PTUs):
This model offers consistent performance with less waiting time and guarantees throughput.
You pay a fixed hourly rate per PTU, so costs are predictable.
There are additional costs for input and output tokens, but since PTUs provide faster and more stable access, this model is ideal for larger-scale usage or scenarios where latency matters.
PTUs also have minimum scaling requirements (meaning you must buy a certain number of units to use the service at this level).
Comparison:
Standard (On-Demand): Pay only for tokens, ideal for small or variable usage.
PTU: Fixed hourly rate + token costs, better for stable and high-volume tasks.
Example (Why use GPT-4o as a student):
GPT-4o has a lower cost per token compared to other models like GPT-4-Turbo. This makes it cost-effective for students or small-scale users, and it doesn’t include Price per PTU per Hour, unlike GPT-4-Turbo.
Let’s Get Started
Step 1: Create an Azure and Azure OpenAI Account –
To begin, you’ll need to set up an Azure account if you don’t already have one. Visit the Azure and Azure Open AI websites to get started.
Step 2: Create a New Resource Group
Search for “Resource Group” in the Azure search box.
Click on “Create” to start a new Resource Group.
Fill in the required details:
Subscription : Choose your Azure subscription.
Resource Group Name : Give your group a unique name.
Location : Select the Azure region that suits you.
Click “Review + Create” and then “Create”.
Wait a few minutes for the resource group to be established.
Step 3: Create a New Azure OpenAI Resource
Search for “Azure OpenAI” in the Azure search box.
Click on “Create” from the menu bar.
Fill in the following details:
Subscription : Select your subscription.
Resource Group : Choose the resource group you just created.
Region : Specify the region.
Name : Assign a name to your OpenAI resource.
Pricing Tier : Select an appropriate pricing tier.
Click “Next Network” and select ‘All networks, including the internet, can access this resource’.
Proceed by clicking “Next Tags”.
Finally, click “Create” and then “Submit”.
Wait a few minutes for the resource to be set up.
Step 4: Go to Azure AI Studio
In your Azure dashboard, navigate to Projects Overview and select ‘Chat under Project Playground’.
Configure your Assistant:
Role : Assign a role to your bot, from the drop down menu.
System Message : Provide directives on how the assistant should behave. For example, you could instruct it to adopt the persona of Shakespeare, using rich metaphors and poetic language.
Examples : Provide user input and expected assistant output, to help your bot learn and respond better.
Enter a test question for the chatbot: “Write a message to invite friends to my birthday party”. This will help you evaluate your chatbot.
Select “Deploy to a web app” from “Deploy to” dropdown.
Choose “Create a new web app”
Fill the required information and enable the code history in the web app.
Hit “Deploy”.
Step 5: Congratulations!
You have successfully created your assistant! Now, to integrate the chatbot into your website, you can use Python and Flask to connect with the frontend. Here’s how to proceed:
Retrieve Keys and Endpoints: Use the keys and endpoints from the Azure resource you created earlier. Store these securely in a `.env` file.
Choose Your Code Editor: Open your preferred code editor to begin coding.
Refer to Repositories: Check out my repository to know the code.
Conclusion
With these steps, you now have a fully functional chatbot powered by Azure OpenAI capabilities. The possibilities are endless, and you can further customize and enhance your assistant as needed. Whether for personal use or business applications, leveraging Azure’s robust AI services opens a world of innovation. Happy coding!
Resources
GenAI for Beginners
What is Azure AI Studio ?
Fundamentals of Azure OpenAI services
Getting started with Azure OpenAI Assistants
Azure Samples – OpenAI
Azure OpenAI Services
Microsoft Tech Community – Latest Blogs –Read More