68  
dotnet
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Jan 28, 2023

Adding ChatGPT to your .NET 7 application

Автор:
Rokas
Источник:
Просмотров:
3319

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

Похожее
Aug 22, 2021
The following are a set of best practices for using the HttpClient object in .NET Core when communicating with web APIs. Note that probably not all practices would be recommended in all situations. There can always be good reasons to...
Dec 25, 2023
Author: Albert Starreveld
DAPR is an abbreviation for Distributed APplication Runtime. As the name implies, it’s useful for container-based, distributed architectures. It makes life easier when it comes to: Service Discovery. When you deploy multiple instances of your application, then how do you...
May 31, 2023
Author: Yohan Malshika
Understanding uow to use And and Or operators with Expression Trees As a C# developer, you may have come across scenarios where you need to build complex logical expressions dynamically based on user input or other dynamic factors. In such...
Sep 9
Author: Stefan Đokić
Delve into securing .NET REST APIs against cyber threats with a focus on JWT, OAuth, SSL/TLS, and role-based authorization. This guide emphasizes for real-time monitoring and security assessments, ensuring your API's integrity and user data protection. Introduction In the digital...
Написать сообщение
Тип
Почта
Имя
*Сообщение
RSS