Adding Chat GPT to your .NET 7 application
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.
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:
Let's go through each part, firstly lets look at the 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.
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)
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)
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
Let me know what other integrations you'd like to see!