112  
csharp
 
RU EN

Adding ChatGPT to your .NET 7 application

Автор:
Rokas
Источник:
Просмотров:
4503
Adding ChatGPT to your .NET 7 application favorites 0

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

Файлы

Похожее
5 февраля 2023 г.
Автор: Юрий Панчул
Вчера у меня в ленте на фейсбуке (жаль что в закрытом посте) проявился еще один аспект ChatGPT, жуть которого я не вполне осознавал. Представьте, что ваш менеджер присылает вам емейл: "я вот тут сгенерил с помощью ChatGPT код на языке...
Jan 13, 2023
Author: Jaydeep Patil
We are going to discuss the Unit of Work design pattern with the help of a generic repository and step-by-step implementation using .NET Core 6 Web API. Agenda Repository Pattern Unit of Work Step-by-step Implementation Prerequisites Visual Studio 2022 SQL...
Dec 1, 2023
Author: Rico Fritzsche
The flight monitor case study: Applying Vertical Slices in a real-world scenario In 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...
Aug 18, 2023
Author: Alex Maher
C# Data Structures Handbook! Every decision in programming is a silent influencer of performance and clarity. In C#, one such vital choice is selecting the right data structure. Data structures are foundational pillars. These structures are where data lives, breathes,...
Написать сообщение
Тип
Почта
Имя
*Сообщение
RSS
Если вам понравился этот сайт и вы хотите меня поддержать, вы можете
Soft skills: 18 самых важных навыков, которыми должен владеть каждый работник
Остановись, мгновенье. Медленное программирование — тренд для уставших разработчиков
Вопросы с собеседований, которые означают не то, что вы думаете
Найм программистов. Советы от программиста
Мои 7 правил при собеседовании разработчиков
Типичные взаимные блокировки в MS SQL и способы борьбы с ними
Рассуждение на тему, какую базу данных выбирать
Как сломался рынок найма и кто теперь на самом деле нас нанимает
Тестирование PRTG Network Monitor и сравнение с Zabbix
Идентификация, Аутентификация, Авторизация. В чем же разница?
Boosty
Donate to support the project
GitHub account
GitHub profile