How to connect to ChatGPT with C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this video I will show you how you use the chat gbt model engine to generate Instagram posts you could also tell it to make Facebook posts or any other social media I will show you how you create a Blazer server app where you create some input and then it output some text back to you so that you can review it and change it so that it fit your post even better what I will not show you in this video is how you automate it completely so you know actually using C sharp to put it out on Instagram and I will not do that because Facebook which is the owner of Instagram they require a lot of documentation and what kind of app you use to actually get access to the API and I actually don't think that they want automated Instagram or Facebook posts and I also don't recommend that you just use the post that it gives you in return just without reviewing it at first and maybe edit something in it if there's something that is not true in it or or if there's something that you just don't need to be in the post but it is a very good way to generate posts with hashtags because it's good at generating hashtags so you don't have to find out what hashtags you want in the post so let's get started we will create a Blazer server app and I'll just hit next and it doesn't matter what it's called so I'll just hit next again and we will use the.net6 framework so I'll just hit create so actually before we start I want to tell that I have an article about this in my blog I will link it in the description where you can actually go and copy and paste all the code in this video I will just show you how it actually works because that's not really described so well in the article but the first thing I will do is to go to the program.cs because I know that we need to add in a builder service and I'll put it just beneath the add Razer pages and it's here you need to type Builder dot Services dot add HT HTTP client and what that do is that it enables the HTTP clients so that we can make some API calls so now when that is set up let's go to the pages and to our index because I want to create the app in the index so let's go and get rid of these two lines and we could call this for Generate Instagram posts this is just an idea but you can really use it for everything that you want and in just a moment you will see why and how it's possible but let's first provide the HTML that we need so the first thing is this input field and what we will do is that we want to bind it to a variable that we're going to create that is has the name topic so that's just an input field where we can input a topic and the next thing we want to create is a button and when you click this we want to run this method called generate Instagram post and then we just give it some text and then the final HTML that we want to make is just a headline where we say Instagram post because just been these are we going to create a paragraph where we get a response and of course that response will be what the chat gbt engine called DaVinci zero zero three is going to generate for us and then we just have some styling so that it looks a bit nicer so the next thing is that we want to include some libraries that we're going to use and in our case we want to use the system.net.http we also want to use the Newton soft Json that we have to install as a nougat package then we want to use the system.txt the system.net.http.headers and finally we want to inject the HTTP client so that we can actually create the API call so next let's go and create our code section and just so that we can get rid of the newton-soft error here let's go and right click the project and say manage new kit packages go to browse and then we wanna you actually want to type in newtonsoft.json but I have it right here and I want to say install and as you can see now it's installed so let's just close this and let's insert the two variables that we need there's the topic and the response so for this little project it's okay to just say string topic and then set it equal to an empty string and now to the actually fun part where we make the method called generate Instagram post so we say private async and it's a task and then we want to generate Instagram post so now I just copy and pasted the first two lines to our method the first one is this API key where we need to go to chatgbt's website or actually open ai's website to get this API key then we want to save it in this API key variable and we want to put it as a barrel token in our header request so to get the API key we want to go to open api's website and on the website they have this called API and from here we actually just want to log in and I just use Google so I'll just continue with Google and show you when I'm done if you don't have a user you can just create one it is free and you get a 18 trial that you can use for three months and when only generating small amounts of text at a time then it's it's actually pretty much so you might be okay with 18 the first three months of course depending on how much you actually want to generate from it but now let's go to our profile image up here where it says personal and go to view API keys and as you can see I already have two API keys and we cannot see it again when we have generated it so for this project I just want to hit create new secret key and it's okay that you see it because I will just delete it after this video so go and copy this say okay and let's go back to the code where we want to paste it in to our API key variable so now we can go and create the Json object that we want to send to the engine and it looks like this the Json content I have called the variable and what we want to send with it is the prompt we want to send a model which model we want to connect to and at last we want to tell it how many tokens we want to use on this request and I actually think I just want to scroll in a bit so you can see a bit better maybe but inside our prompt we want to give it a command or what it should give an answer for or two so in this case my idea was that we have the input field where it will bind to the topic variable so whatever topic we put in here it will just put it down to the request text that we sent to the DaVinci model you can see the latest model right now at the time and which is also the most expensive one to make requests to but also the best should be this text and then Dash DaVinci Dash 0 0 3 and finally we have the max token where we put it to thousand tokens and what the open AI tell us is that we should think of these tokens as words but then again it depends on which engine you're contacting because as you can see here they have if you send thousand tokens it will be on the DaVinci engine it will be 0.02 and if you use it on this error with this which is the fastest model then it will be much cheaper so I think it can be a little bit complex too actually calculate what it will cost but as we go to the account I have you can see how much I have spent I have spent 0.22 and that is for request of some days and the day I used it the most was the 9th of January where I used 0.11 and I actually think I used it a lot that time so it's really not that expensive to make the request but let's go back and write our code finished so the last part is to actually make the API request so we use the HTTP and post sync and we're going to do it to this URL and basically what we are going to say here is that we're going to put our object and send it to the API and then we want to say that we want to use the encoding in the utf-8 and that is a Json string that we're going to send to the server and then it will response and we will get some context out of it and then we want to put it to our own data variable and just deserialize the response that we get and put that text into our response which is just this variable where we're going to bind it to our Blazer page or actually just display it in our Blazer page and of course this is just a very simple example on how you can connect to the model to get some response there's really no error handling in this so if we send an empty string I actually don't know what it's going to do but let's try and run this application so now this is our page and we want to insert a topic in here so let's say that we want an Instagram post about out factorial and let's say generate Instagram post and as you can see now it says it's time for some Victoria fun and it will use some hashtags on it and if we go back to the code you can of course formulate this as you want so let's say that we want a Facebook post instead then we can just say it should give us a Facebook post and let's save this and how to reload the page go back update the page and let's say again tutorial and now it's not Instagram but it's a Facebook post and of course there's not a big difference but maybe just some fewer hashtags but what you can do is that you can put this in a text area instead so that you can actually edit the response and then you could just copy and paste it to the post that you want to post so go ahead and be creative with it because this is the way you're going to connect to the chat gbt model in C sharp you can use play so you can use asp.nico it doesn't really matter and I also want to create a video soon about how you do it in Python so if that video is out you can see it in the car right now and if you like the video please like it and subscribe to my channel but let's just go and be creative with this it is a very nice tool that we actually never had before just remember to always read what is returning so that you don't end up in some bad situations but let's just have a nice day bye
Info
Channel: ZetBit
Views: 5,838
Rating: undefined out of 5
Keywords: ChatGPT C#, ChatGPT C# API, chatgpt explained, chatgpt coding, artificial intelligence, chatgpt tutorial, how to use chat gpt, chat gpt tutorial, chatgpt programming, open ai, chat gpt prompts, chat gpt
Id: GanrpgoeHr8
Channel Id: undefined
Length: 10min 9sec (609 seconds)
Published: Tue Jan 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.