How to Use ChatGPT API in ASP.NET MVC - A Step-by-Step Guide with Source Code

Category > ASP.NET MVC || Published on : Tuesday, February 28, 2023 || Views: 1713 || ChatGPT API ASP.NET MVC Chatbot development


Here Pawan Kumar will explain how to use ChatGPT API in ASP.NET MVC and build your own chatbot. Follow our step-by-step guide with source code and create a conversational experience for your users.

Step 1: Sign up for OpenAI API access

Before you can use ChatGPT API, you will need to sign up for access to OpenAI API. Follow the instructions on the OpenAI website to sign up for an API key.

Step 2: Install OpenAI NuGet Package

Once you have signed up for OpenAI API access, you can install the OpenAI NuGet package. Open the Package Manager Console in Visual Studio and type the following command:

Install-Package OpenAI

This will install the OpenAI NuGet package into your project.

Step 3: Set up a controller action

In your ASP.NET MVC project, create a controller action that will handle the request to the ChatGPT API. In this example, we will create a controller named "ChatController" with an action named "GetResponse". The action will take a string parameter named "message" that represents the user's input.

using System.Web.Mvc;
using OpenAI;

namespace ChatBot.Controllers
{
    public class ChatController : Controller
    {
        public ActionResult GetResponse(string message)
        {
            var openAI = new OpenAI(apiKey: "your api key");
            var prompt = message + "\nAI:";
            var response = openAI.Completions.Create(
                engine: "davinci",
                prompt: prompt,
                maxTokens: 150,
                n: 1,
                stop: "\n",
                temperature: 0.7
            );

            var result = response.Choices[0].Text.Trim();
            return Content(result);
        }
    }
}

In this code, we create a new instance of the OpenAI class and pass in our API key. We then create a prompt variable that contains the user's message followed by the text "AI:". This is the prompt that will be sent to the ChatGPT API.

We then call the Create method on the Completions property of the openAI object. This method takes several parameters:

  • engine: The name of the GPT engine to use. We are using the "davinci" engine, which is the most advanced and capable engine.
  • prompt: The prompt to send to the API.
  • maxTokens: The maximum number of tokens to generate in the response.
  • n: The number of responses to generate.
  • stop: The token at which generation is stopped.
  • temperature: The sampling temperature to use when generating the response.

The response from the API is then stored in the response variable. We extract the generated text from the Choices property of the response object and return it as the result of the action.

Step 4: Add a view

Finally, add a view that will display the response from the ChatGPT API. In this example, we will create a view named "Index.cshtml" in the "Views/Chat" folder. The view will contain a simple form that allows the user to enter a message and a div that will display the response from the API.

@{
    ViewBag.Title = "Chat";
}

<h2>Chat</h2>

<form>
    <div class="form-group">
        <label for="message">Message:</label>
        <input type="text" class="form-control" id="message">
    </div>
    <button type="submit" class="btn btn-primary">Send</button>
</form>

<hr />

<div id="response"></div>