Advertisement
Always will be ready notify the world about expectations as easy as possible: job change page

Adding ChatGPT to your .NET 7 application

Added:
Jan 28, 2023
Author:
Rokas
Source:
Views:
1105

Prerequisites: VSCode and .Net 7 installed. Don't need to copy the code, GitHub link provided in the end.

Open up a terminal in VSCode or any IDE of your choice, run:

dotnet new console

2 files will appear, default Program.cs and .csproj named according to your folder:

Now let's create a file called OpenAIResponse.cs and start filling in the objects.


Main response object


Text response and usage objects

We will use these objects to translate the open AI response to C# object oriented language.

Time to call the API!

Get your API Key from https://beta.openai.com/account/api-keys

Create new secret key, copy & paste it into the project.

Now let's set up the main function:


The entire function to call Open AI

Let's go through each part, firstly lets look at the function signature:


Function signature

Open AI requires a fairly large amount of settings to be specified in order to provide us with the expected result, we are passing them through the function signature, the comments explain each individual parameter.


Setup for the request

Then we have the setup for the request, essentially the request requires:

  • The API Key (used for authorization).
  • The Call URL (used to direct the request).
  • The Content (used to setup the parameters of the request).


Parse the response

After the setup we just send the request and read it into our prepared class.

Alright, everything is set up, let's call it! (Don't forget to set the API Key to yours)


The default playground settings

Now in your terminal just run:

dotnet run

And after inputting a question you should receive an answer.

You can find all the source files here: https://github.com/RokasMarcinkevicius/ChatGPT

Similar
5 февраля 2023 г.
Автор: Юрий Панчул
Вчера у меня в ленте на фейсбуке (жаль что в закрытом посте) проявился еще один аспект ChatGPT, жуть которого я не вполне осознавал. Представьте, что ваш менеджер присылает вам емейл: "я вот тут сгенерил с помощью ChatGPT код на языке...
Dec 1, 2023
Author: Rico Fritzsche
The Flight Monitor Case Study: Applying Vertical Slices in a Real-World ScenarioIn my last articles I talked about Vertical Slice Architecture and how to organize code in a non-technical way. I showed some simple examples of how to do this...
Dec 20, 2023
Author: Fiodar Sazanavets
You can run a single monolithic instance of a server application only if the number of clients accessing your application doesn’t exceed a couple of thousand. But what if you expect hundreds of thousands, or even millions, of clients to...
Sep 5, 2023
Author: Edson Moisinho
Simplifying Data Transport in C#.In modern C# development, data transport objects (DTOs) play a crucial role in exchanging information between different layers of an application, such as between a client and a server, and traditionally, developers have used classes to...
Send message
Email
Your name
*Message


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban