ChatGPT In .NET 7 API And Testing With Postman

Introduction

Hello learners, in this article, I will explain how we can configure or use ChatGPT in .Net7 API and we are going to test it with Postman. Here I will use Visual Studio 2022 as an IDE and Postman for testing that API. If you are using VS Code, you can use Thunder client for the API testing. If you are a web developer, I hope you are familiar with Postman. If not, then don't worry we'll also discuss it. 

Thunder client

Chat GPT In .NET 7 API And Testing With Postman

Thunder client is an extension for API testing in VS Code. It will definitely increase your performance if you use VS Code for development. You don't need to switch between screens, you need to switch the tab, and you can test your API by clicking on a new request. It will provide most of the similar functionalities to the Postman. When I'm writing this article, Thunder Client has 1,727,761 downloads.

Postman

Postman is an API testing tool that is very helpful for rapid API testing for development. It will provide a lot more functionalities than any other API testing tool. You can pass almost every authentication parameter from Postman. You can group your requests based on their category and save them with the parameter so you can use them later. You don't need to re-enter the same parameters. You can download Postman from this link.

Chat GPT In .NET 7 API And Testing With Postman

Let's get back to our main topic. First, I want to explain What is chat GPT. Before I start, how to configure Chat GPT?

What is chat GPT?

OpenAI created ChatGPT, a conversational AI model. It is a subset of the GPT-3 transformer-based language paradigm (Generative Pretrained Transformer 3). It can create human-like text replies to natural language inquiries after being trained on a vast corpus of text data.

Chat GPT In .NET 7 API And Testing With Postman

ChatGPT is used to deliver a natural language interface to users in various applications, including chatbots, content production, and text completion tools. ChatGPT's adaptability makes it a powerful tool for various applications, including chatbots, content production, and text completion tools.ChatGPT facilitates smooth and intuitive communication between humans and technology by offering users a natural language interface. For example, a ChatGPT-powered customer service chatbot may respond quickly and accurately to respond to consumer inquiries, decreasing the need for human customer support employees. The model has been fine-tuned for text production tasks such as sentence and paragraph completion and creating full articles based on supplied prompts. You need to follow these steps to use chatGPT in your API.

Step 1

Open Visual Studio and click on the new project

Chat GPT In .NET 7 API And Testing With Postman

Step 2

Choose a project type as an ASP.NET Core Web API.

Chat GPT In .NET 7 API And Testing With Postman

Step 3

Then click on the Next button and configure your project with project detail and click on the Next button.

Chat GPT In .NET 7 API And Testing With Postman

Step 4

Choose the.NET7 as project target framework.NET7 is the latest version of .Net, which have Standard Team Support. Now click on the Create button.

Chat GPT In .NET 7 API And Testing With Postman

Step 5

We must add the OpenAI NuGet package to our newly created API project. Follow these steps to Add this package to your project.

  • Right-click on the project name and click on Manage NuGet Packages 

Chat GPT In .NET 7 API And Testing With Postman

  • Click on Browse Tab and search for OpenAI

Chat GPT In .NET 7 API And Testing With Postman

  • And click on the Install button.

Chat GPT In .NET 7 API And Testing With Postman

Step 6

Now, most things are done, but to access the OpenAI chatGPT, we need an API key to access ChatGPT. To get that Key, you need to follow these steps.

  • First, if you don't have an account in OpenAI, you need to signup and confirm your email.
  • Login to your account.
  • Click on your profile.
  • And click on View API keys or click on this link.

Chat GPT In .NET 7 API And Testing With Postman

  • And click on Create new secret Key.

Chat GPT In .NET 7 API And Testing With Postman

  • You'll get the following display with a new API key. You need to click on copy symbol. We'll use this Key later in our project.

 Chat GPT In .NET 7 API And Testing With Postman

Step 7

Now you can add a separate controller for making calls for ChatGPT, or you can use the default WeatherForecastController. I'm going to use the default WeatherForecastController. Now you need to add the following code to your controller.

[HttpGet]
[Route("askMe")]
public async Task < string > askMe(string question) {
    string apiKey = "*****Your API key past here**********";
    string answer = string.Empty;
    var openai = new OpenAIAPI(apiKey);
    CompletionRequest completion = new CompletionRequest();
    completion.Prompt = question;
    completion.MaxTokens = 4000;
    var result = openai.Completions.CreateCompletionAsync(completion);
    if (result != null) {
        foreach(var item in result.Result.Completions) {
            answer = item.Text;
        }
        return answer;
    } else {
        return "Explain more about your Query";
    }
}

In this code, we have created a call with Route askme. I have defined that API key in the string variable apiKey and a variable to hold the response from the API with the name answer. And we check for the result. If the result is not null, we return the response. In other cases, I'm returning a message to the user.

Let's check for the response. Open Postman, click on the new request, past your localhost and API URL, and send the request with your question. You'll get the response based on it. I ask to draw a rat, and this is the response. 

Chat GPT In .NET 7 API And Testing With Postman

Conclusion

In this article, we have created an API that returns the response based on your query. You can ask many questions from this API and get a response. Now you can use this call on any platform to get the response.