🔧 Step 1. Set Up Azure OpenAI
First, you’ll need an Azure account. Sign up here if you don’t already have one.
✅ Create an Azure OpenAI Resource
- Go to the Azure Portal
- Search for Azure OpenAI in the Marketplace.
- Click Create, select your Subscription, Resource Group, and Region.
- Agree to Microsoft’s responsible AI use terms and deploy.
✅ Deploy a GPT Model
Once the resource is live:
- Go to the Deployments tab.
- Deploy a model like
gpt-3.5-turbo
or gpt-4
.
🔑 Step 2. Get Your API Credentials
After deployment:
- Go to the Keys and Endpoint section.
- Copy your:
You’ll use these to authenticate your requests.
⚙️ Step 3. Create the AI Agent Logic with Azure Functions
Let’s use Azure Functions (serverless backend) to handle user queries.
🧪 Sample Azure Function (Node.js)
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
module.exports = async function (context, req) {
const client = new OpenAIClient(
"https://<your-resource-name>.openai.azure.com/",
new AzureKeyCredential("<your-api-key>")
);
const deploymentId = "<your-deployment-id>";
const userInput = req.body.message;
const response = await client.getChatCompletions(deploymentId, [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: userInput }
]);
context.res = {
body: { response: response.choices[0].message.content }
};
};
You can deploy this function using VS Code or the Azure CLI.
💬 Step 4. Add Chatbot Capabilities with Azure Bot Service
Want to turn your AI into a full chatbot?
- Create a Bot Channels Registration in Azure.
- Point it to your Azure Function endpoint.
- Connect it to platforms like:
Microsoft Teams
Telegram
Web Chat (embed on your site)
This gives your AI agent a voice on your favorite platforms.
🖥️ Step 5. Integrate with Your Frontend
Use a simple fetch()
or axios
call from your frontend to send queries to the Azure Function.
const sendQuery = async (message) => {
const res = await fetch("/api/ai-agent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message }),
});
const data = await res.json();
console.log(data.response);
};
Now you have a live AI assistant in your web or mobile app.
🔐 Step 6. Secure Your AI Agent
Don’t forget to secure it:
- Use Azure API Management to expose your API safely.
- Store secrets in Azure Key Vault.
- Enable CORS, rate limiting, and auth tokens as needed.
🎯 Final Output Example
Here’s the kind of JSON response your AI agent can return.
{
"response": "Sure! I can help you track your order. Please provide your order ID."
}
🧩 Wrap Up
Creating an AI agent on Azure is surprisingly straightforward. With a few clicks and some code, you can build a highly intelligent assistant for websites, apps, or chat platforms.