How to Integrate Azure OpenAI Service with ASP.NET Core Web API


Integrating Azure OpenAI Service with an ASP.NET application allows you to build web-based solutions powered by OpenAI's GPT models (like GPT-3 or GPT-4) using Azure’s API. Below is an end-to-end guide on how to build an ASP.NET Core Web API application that interacts with the Azure OpenAI Service.


Prerequisites

  1. Azure Subscription: You need an Azure account to create an OpenAI resource.
  2. Azure OpenAI Resource: Create an OpenAI resource in Azure and get your API key and endpoint.
  3. .NET SDK: Install the .NET SDK.
  4. NuGet Packages: You’ll need the following NuGet packages:
    • Microsoft.Extensions.Configuration
    • Newtonsoft.Json
    • Microsoft.AspNetCore.Mvc.NewtonsoftJson

Step 1: Create an ASP.NET Core Web API Project

Create a new ASP.NET Core Web API project:

dotnet new webapi -n AzureOpenAIServiceApi
cd AzureOpenAIServiceApi

Step 2: Install NuGet Packages

Install the necessary NuGet packages:

dotnet add package Newtonsoft.Json
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Json

Step 3: Add Configuration for Azure OpenAI in appsettings.json

In your appsettings.json, add the configuration for your Azure OpenAI service, including the API Key, Endpoint, Deployment Name, and API Version.

{
  "AzureOpenAI": {
    "Endpoint": "https://your-resource-name.openai.azure.com",
    "ApiKey": "your-api-key",
    "DeploymentName": "your-deployment-name",
    "ApiVersion": "2023-03-15-preview"
  }
}

Step 4: Create a Service to Call Azure OpenAI

Create a service class (OpenAIService.cs) that will handle communication with the Azure OpenAI API.

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

public class OpenAIService
{
    private readonly string _endpoint;
    private readonly string _apiKey;
    private readonly string _deploymentName;
    private readonly string _apiVersion;

    public OpenAIService(IConfiguration configuration)
    {
        var azureOpenAIConfig = configuration.GetSection("AzureOpenAI");
        _endpoint = azureOpenAIConfig["Endpoint"];
        _apiKey = azureOpenAIConfig["ApiKey"];
        _deploymentName = azureOpenAIConfig["DeploymentName"];
        _apiVersion = azureOpenAIConfig["ApiVersion"];
    }

    public async Task<string> GetOpenAIResponseAsync(string prompt)
    {
        using (var httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Add("api-key", _apiKey);

            var requestUri = $"{_endpoint}/openai/deployments/{_deploymentName}/completions?api-version={_apiVersion}";

            var requestBody = new
            {
                prompt = prompt,
                max_tokens = 100,
                temperature = 0.7
            };

            var content = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");

            var response = await httpClient.PostAsync(requestUri, content);
            var responseBody = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception($"Error calling Azure OpenAI: {responseBody}");
            }

            dynamic jsonResponse = JsonConvert.DeserializeObject(responseBody);
            return jsonResponse.choices[0].text;
        }
    }
}

Step 5: Register the Service in Startup.cs or Program.cs

If you're using .NET 6 or later, register the OpenAIService in Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers()
    .AddNewtonsoftJson();

builder.Services.AddSingleton<OpenAIService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseAuthorization();

app.MapControllers();

app.Run();

Step 6: Create a Controller to Interact with OpenAI

Create a new controller (OpenAIController.cs) that will expose an endpoint to interact with the OpenAI service.

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

[Route("api/[controller]")]
[ApiController]
public class OpenAIController : ControllerBase
{
    private readonly OpenAIService _openAIService;

    public OpenAIController(OpenAIService openAIService)
    {
        _openAIService = openAIService;
    }

    [HttpPost("ask")]
    public async Task<IActionResult> AskOpenAI([FromBody] string prompt)
    {
        if (string.IsNullOrEmpty(prompt))
        {
            return BadRequest("Prompt cannot be empty.");
        }

        try
        {
            var response = await _openAIService.GetOpenAIResponseAsync(prompt);
            return Ok(response);
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Error: {ex.Message}");
        }
    }
}

Step 7: Run the Application

Build and run the ASP.NET Core Web API application:

dotnet run

Step 8: Test the API

Use a tool like Postman or cURL to test the API endpoint.

POST Request:

  • URL: https://localhost:5001/api/openai/ask
  • Body: { "prompt": "What is Azure OpenAI?" }

Response:

{
  "choices": [
    {
      "text": "Azure OpenAI is a service provided by Microsoft that allows developers to access OpenAI's powerful GPT models to generate text and perform natural language processing tasks."
    }
  ]
}

Step 9: (Optional) Deploy to Azure

Once the application is ready, you can deploy it to Azure App Service for cloud hosting:

  1. Publish from Visual Studio: Right-click the project > Publish > Azure.
  2. Azure CLI Deployment: Use dotnet publish and deploy to an App Service.

You now have a basic ASP.NET Core Web API application integrated with Azure OpenAI Service, capable of processing natural language prompts using the OpenAI models.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete