Bald Bearded Builder
copilot-ai/building-ai-with-net-chatgpt-4o-dall-e-3-and-whisper

Building AI with .NET: ChatGPT 4o, Dall-E 3 & Whisper

A workplace ban on LINQ and unit tests prompts a critique of blanket restrictions, hard-coded SQL, and development rules rooted in past failures.

Loads from YouTube when you press play

Transcript 4 topics

OpenAI SDK overview

0:00

You know, I remember about five, ten years ago when no matter what app you were building, the new craze was, well, have you put it in containers? Are you running it in Kubernetes? Is it in Docker? Everything was like, oh, we'll cure everything with Docker. And now we're talking about AI the same way.

No matter what you're building, have you added AI to it? And if you're not wanting to learn all the things about how to build your own model, how to fine tune your models, all that kind of stuff, you just wanna use an API, well, OpenAI provides a great resource for you. And they've had a really good SDK for dot net for a while. It's version one dot something or other, and it works fine. But now they're releasing a two point o version and it's a big upgrade.

It's a lot more features and we're gonna cover some of those. Let's let's see what we can do with it real quick. I'm gonna dot net new console And let's see. Load that up. Alright.

I'm gonna clear that out just for grins and giggles. Okay. And then I'll open folder, and we are in the like and comment, please. That seems appropriate. And it should be pretty empty.

Okay. So we'll have our program CS that we're used to with this to hello world. We'll get that out of there. I wanna do a few things though. Like, for instance, how can I make a chatbot or assistant?

Maybe I wanna create images using Dolly three or maybe even transcribe a video just like this one, using Whisper models. So let's try them all. First, we'll go with the good old fashioned, like, chatbot kind of thing. Before I start making changes, I know I'm gonna need an OpenAI API key. And I already grabbed that.

So what I'm gonna do is I'm gonna create a new file. I'm gonna call it constants. Cs. And I'm just going to paste in some code. And yeah.

Yeah. So it's just got a public static open API open AI API key. Learn to use your words. And there's the API key. And I know what you're thinking.

Oh, dude. You just showed us your API key. Well, don't worry. I'm a little smarter than that. I mean, I may not look it, but I am.

I'll go delete this before this video comes out. But it'll allow me to do it without having to copy in that string every time. So now we're gonna need the OpenAI SDK. And you know what? Let me open a browser to Nougat.

You can see it's it's out there. 1 eleven zero, we just learned, is the version number, but that's not what we want. We've been there. We've done that. Show me some other versions. Oh, look.

A beta five came out. Okay. So that's what we want really. So I'm gonna use this nice copy feature. Come over here, and I'm gonna paste that in like a boss, and let's add that package.

Alright. Got it.

Building a streaming chatbot

2:49

So now I'm gonna minimize that. Now we wanna build something. What do we wanna do? Okay. So let's first create, a chatbot.

And what I'd really love to do is to be able to add this to my terminal. So I could just say whatever we call it. Let's let's call it how, like, from 2001 Space Odyssey. So, like, how how and then like a prompt. What's the circumference of the earth?

But if I type that in, that's actually gonna get submitted as different args. So we'll need to concatenate all those. So let's start with that. Let's say, var prompt equals args dot aggregate, x and y, we'll just say, for grins and giggles. And then let's put those together.

Look at you IntelliSense. You did great on our Copilot. Alright. So we've got our prompt, and now we're gonna want the OpenAI chat client. Now if you're using, like, version one of the SDK, this is a difference in the two.

They initially had one client to rule them all. It's an OpenAI something client, API client, whatever. And from that, you would call like dot transcription or dot chat completion or dot whatever. In this case, they've broken it apart into different clients based on what you want to accomplish. So we're going use the chat client.

So we'll say, var open AI equals new chat client. There it is. And then we had that constants file, so we'll say constants dot OpenAI API key like a boss. Now what we're gonna do is we need to make sure we provided a prompt. So we'll say, oh, if we go in if we do a while instead of an if here, then we could just keep asking ourselves questions.

Let's do that. While prompt, and maybe the last prompt would say like no. So while prompt does not equal no and string dot is null or empty prompt or actually no, not that. Yeah. Yeah.

Yeah. While we have a prompt and it's not no, what we're gonna do is it's kind of giving me some some Copilot stuff for the old version, but we don't want to do this. We want to say, var updates equals, openai. Complete chat streaming async and give it the prompt. And there's a reason I'm doing that.

Like, if we look back at that, there's a complete chat, a complete chat async, a complete chat streaming, and a complete chat streaming async. Now the streaming versus streaming async or chat versus chat async is just the difference in sync versus async. But what's the difference in complete chat and complete streaming? Well, the difference is how often it sends you back information. So if I ask it a question, it could think and come up with couple paragraphs of a response and then just dump me all that paragraph of response or two paragraphs response.

But if I'm building an app that's gonna be interactive with a user, I don't wanna wait to get, like, two paragraphs back. Wouldn't it be great to, as it thinks up fragments or sentences, have it just send me those as it's coming along? And then at the end of it, I'll have my two paragraphs, but I can start showing data for the user quicker. That's where streaming comes in. With those updates, we can, foreach those and get those fragments a little faster, be a little more responsive to our users.

You don't have to do that. It's just what I'm doing. That IntelliSense that that Copilot junk there was all wrong. Good day to you, sir. Okay.

We're gonna get an update in and then for each of the parts of that. So we're gonna have, like, var message in update dot I think it's content part, content update. There we go. For each of those, we don't wanna write line because we're just getting a fragment back. So we're gonna console write that message.

So as those fragments come back, we'll write them back out and then we can say, you know what? Just to keep the conversation going, let's go console dot write line. Let's give it like a little new line and say anything else? Anything else? Anything else was was very Alabama of me.

And then we'll just say prompt console read line. Yes. We should be able to just run this now And looking at it, we get our prompt out. We had to stringify it, kinda get it in like that. Then we create a new chat client.

Yeah. Yeah. This looks oh, no. It's not golden. I'll tell you why.

You used to do this in a different place, but in the new two point o version, you don't you have to specify the model when you instantiate the client. So I need to tell it what model. I want GPT dash four o. Let's get fancy with this thing and use up all my credits. Yeah.

And now we can run this. So let's give it a go. Bring up the terminal. Let's say dot net run. How many hours in a day?

That's a really simple question. Hopefully, it doesn't take long to bring it back. The day is traditionally divided in twenty four hours. Its division is based on their observation. Blah blah blah blah blah blah.

But did you notice how it wrote that out, like, really quick? Like, it it it didn't just plop the text down. It gave you, like, a little bit and wrote it. That's what that streaming gave us. Is there anything else?

Yes. Am I handsome? Can't physically see you, but well, you know, I'm gonna take that as a yes. Anyway, no. Nothing else.

And because we had that if prompt does not equal no, it exits out. That works like a champ. Pretty cool. Pretty simple to write. You can see where I mean, you're not gonna probably do this in a console app, but you can see how quick and painless it is to kinda put that into your API or maybe some kind of native app, your your WinUIs or WPF apps.

Generating DALL·E images

8:59

So now let's take it somewhere else. Like, let's create images, maybe using Dolly three. I'm not gonna need all of this rigmarole because I'm just gonna do it once. I won't need that using chat because we're not gonna use it and see this is where it changes. We're not gonna want a chat client anymore because we're not doing chat.

I believe it's an image client. Yeah. Look at that. Look at me remembering things. Same format, though.

We need to put in our model, which we're gonna want DALL E, three. And then same deal. We'll do our constant with the API key that, sorry, you can't use by the time this comes out. And then before we call to get a result, let's create what what it calls an image generation option. We'll say, image request equals new image request image request image generation options.

There we go. And it's gonna give us some bad examples. Thanks a lot, Copilot. You didn't help at all. I've already got some of these, so let me just paste them in.

Quality is gonna be high. The size there's only certain sizes you can use with DALL E three. If you don't if you use the wrong one, it'll throw up at you and say, hey. Can you use this, this, or that? And you just try again.

Vivid. And then the response format, give us the URI. Another option would be to, like, give us the bites of the image itself. I did play around with that. It's it's great.

It's you could use it and then save the file, but it's just as easy to me to get just the URI. And the one thing I tried with the bytes was I wanted to use ASCII art to, like, make it draw the image in the terminal. I thought that was kinda cool, but that is way outside the bounds of this video, so I'm gonna skip over all that. Now, I mean, if you're if you're saying, hey. But, Mike, I think that's really cool too.

I'd love to see that. Well, then in that case, you need to join our Discord because I'm in there all the time. We're actually streaming on Twitch every day of the week building this stuff live. So hop in there with us and say, hey, Mike. Give me that code that does the ASCII art.

I'll say, sure. Here it is. Anyway, let's move on and do the actual request. Let's say var response, let's say, equals await OpenAI dot generate image async, and we're gonna give it the prompt. And oh, look at it.

The the stinking Copilot's helping me out here. And then we'll just do a console. WriteLine with response dot I think it's value. Yeah. Yeah.

There it is. And then it's like Yuri or something like that. Imagery. There we go. We'll just write that out, and then we just click on it and see what it does.

Yeah. I think we're good to go here. So let's run this one now. Bring it up. Let's say oh, no.

Dot net. Run. What should our prompt be? A cat riding a T Rex while holding a flag and wearing a red bandana. That's very, very in-depth.

Let's see how it does. Hopefully it'll just write out our URL if everything works correctly. We'll click on it and see what happens. It's probably right now laughing that someone would actually ask this to be honest And I get it. I get it.

And there we go. There's our URL. Let me click on that. Yeah. I trust it.

Open it up. What's the worst that can happen? There it is. Our cat riding a T Rex holding a flag. It's not really holding a flag.

He's got a pole there like a flagpole. He is wearing a red bandana. So well done you, Dolly. Okay. So we got that one done.

Look at us making progress here. So you can see like this stuff is really pretty straightforward. There's very little content here that I have to write to do this. So integrating this into my existing apps is gonna be pretty quick.

So let's get rid of all that. Let's, keep a prompt because we're gonna wanna keep doing that.

Transcribing audio with Whisper

12:56

But now, what if we wanna use these really cool Whisper models for speech to text? So we're gonna want not an image client, but an, you got it, audio client. I like the way they've done this and kept it simple. Same format, we're gonna want the model which I think let's use whisper one. And then again give it our, constants learn how to spell.

There we go. Okay. So now let's define some options for it. Let's say options equals new audio. I think it's audio transcription options.

Yeah. Yeah. There it is. And what you look like? Oh, look at me going all JavaScript y on you.

The only options I really want are, the response format because I don't want just the the text like you could do that. Right? But I want SRT format. I think that's like a little easier for me to read And honestly, if I'm like transcribing audio to upload to YouTube or any other social platform, I want the SRT format so that it can be timed correctly. Right?

So let's do that. And then we'll say var response equals await openai. Transcribe audio async. Perfect. Give it our prompt, which in this case is just gonna be a file name.

Pass in our options. All of this is golden. And then let's console. This this stinking Copilot is kinda good, but kinda bad dot value dot text like a boss. Okay.

Is that it? Yeah. We got our API key. We got a WhisperOne. We got our options of SRT.

Now we need some audio. Well, luckily luckily, one thing I have is audio and video. So I've got a test dot m p four up there. I'm just gonna run against that. What does that test m p four look like?

Well, luckily for you, I've got it pulled up. Let's see if you can hear it. I'm wearing pants. I wore pants for this. Throw out that follow.

Okay. That's an unfortunate clip for me to choose, but what's done is done. Let's dot net run, and then pass it in let's say what was it test m p four I think and let's see if it comes up with our SCT SRT learn how to speak. Look at that I'm wearing pants. I wore pants for this.

Throw out that follow. Well, I am wearing pants, so felt yay. Almost got you. Anyway, so just like that, we've done the image generation, we've done transcription, and we've done the chat client. You can see this is a big upgrade from if you haven't got experience with one you don't understand what a big upgrade it is.

Trust me. Just trust me. Would anybody with a bald head, bearded face who writes C sharp ever tell a lie? I don't think so. Anyway, try out this SDK for yourself.

I'm really curious if you're using OpenAI in your projects today, or are you building your own models? Are you using AI in some different way? I would love to hear about this stuff. Let me know in the comments. I'll catch you next time.

Sign in to join in. Reading needs nothing.