Introduction

Azure OpenAI provides powerful AI models that can be integrated into applications to generate human-like text, translate languages, and analyze sentiment. Azure Functions, a serverless compute service, allows developers to build event-driven applications with minimal infrastructure management. Combining Azure OpenAI with Azure Functions enables intelligent, scalable, and cost-effective AI-powered solutions.

Prerequisites

Before getting started, ensure you have the following:

Step 1. Setting Up Azure OpenAI

1. Create an Azure OpenAI Resource

2. Deploy a Model

Step 2. Creating an Azure Function using Visual Studio 2022

1. Open Visual Studio 2022

2. Configure the Function

3. Install Required Dependencies

Step 3. Implementing Azure OpenAI in the Function (Function1.cs).

using Azure;
using Azure.AI.OpenAI;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using OpenAI.Chat;

namespace AIDemo
{
    public class Function1
    {
        private readonly ILogger<Function1> _logger;

        public Function1(ILogger<Function1> logger)
        {
            _logger = logger;
        }

        [Function("Function1")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
        {
            _logger.LogInformation("Processing request with Azure OpenAI");

            string prompt = req.Query["prompt"];
            if (string.IsNullOrEmpty(prompt))
            {
                return new BadRequestObjectResult("Please provide a prompt");
            }

            var openAiApiKey = "YOUR OPENAI KEY";
            var openAiEndpoint ="YOUR OPENAI Key Endpoint";
            var deploymentName = "gpt-4";

            AzureOpenAIClient client = new(new Uri(openAiEndpoint), new AzureKeyCredential(openAiApiKey));
            ChatClient chatClient = client.GetChatClient(deploymentName);

            var requsetOptions = new ChatCompletionOptions()
            {
                MaxOutputTokenCount = 1000,
            };

            var messages = new List<ChatMessage>
            {
                new UserChatMessage(prompt)
            };

            ChatCompletion response = await chatClient.CompleteChatAsync(messages, requsetOptions);

            return new OkObjectResult(response.Content[0].Text);
        }
    }
}

Step 4. Running the Function Locally

1. Start the Function

2. Open the browser and add the local URL with prompt as a query parameter

Add local URL With prompt as query parameter

Step 5. Deploying to Azure

Publish from Visual Studio

Conclusion

Integrating Azure OpenAI with Azure Functions allows developers to build scalable, AI-powered applications with minimal infrastructure overhead. By leveraging serverless computing, businesses can reduce costs while enhancing application intelligence.