Skip to content
CBT Nuggets
DemoBook a Demo

Building a Generative AI-Integrated Chatbot

This skill guides learners through building a simple AI chatbot using Java and the OpenAI API. It covers essential concepts such as creating and managing API keys, making network requests, and parsing JSON responses. The course emphasizes the importance of using interfaces to maintain flexibility and avoid vendor lock-in, allowing for easy integration with other AI models like Claude or Gemini. By the end, learners will have a functional chatbot capable of interacting with AI models directly, bypassing traditional interfaces like ChatGPT.

Full skill from Java SE 11. Preview the IT training 23,000+ organizations trust.

50m

Skill 34 of 35 in Java SE 11

Creating and Using an API Key

Welcome to another skill where we'll be pulling together several different topics we've covered in this course to build a real-world application. In this skill, we'll be using network requests to interact with the OpenAI API and create a simple AI chatbot. In this first video, we see how to create an API key and access it from within our Java programs.

Knowledge Check

API keys are perfectly safe to share with others

Creating the Request and Response Classes

Now that we have an API key set up, let's create some new classes that will help us work with the requests we'll be sending the the API, as well as the responses that it will be sending back.

Knowledge Check

Which of the following is not a valid value for the "role" of a message when working with the OpenAI API?

Creating an API Client Class

Next, let's start creating the class that will represent the main functionality of our chatbot - the OpenAIClient class. As you'll see in this video, adding an interface in between this class and the rest of our app will make it much easier to swap out should we want to switch to another provider such as Claude or Gemini.

Knowledge Check

Encapsulating the conversation management logic inside our OpenAIClient class makes the class ________ flexible, and _______ difficult to use

Communicating with the OpenAI API

Finally, let's implement the method that will really make things happen. In this video, we see how to actually make a request to the OpenAI API and process the response into something our program can use.

Knowledge Check

The "Authorization" header typically begins with the word _________ when using api keys

Challenge & Solution: Bringing It All Together

Now it's time for a challenge! In this challenge, your task is to create a main method that brings together everything we've created. Watch the video for more information.

And now that you've attempted the challenge, I'll show you how to solve it.

Knowledge Check

If we forget to add new messages to the OpenAIClient's conversation list, the API will still keep track of the conversation for us.

View Transcript

Creating and Using an API Key

0:00Hi, Sean here and welcome to another skill where we're going to be building a

0:04project

0:05using what we've learned in this course.

0:07And in this skill in particular, we're going to be building our own generative

0:13AI chat

0:13bot.

0:14You might also say like, "Gend rid of AI agent," although a very simple one, "

0:20using

0:20the concepts that we've learned as well as a few additional ones."

0:25All right.

0:26So the basic idea here is we're going to create a simple Java program that will

0:31communicate

0:32with models like the chat GPT models, for example, in the same way as how you

0:39're able

0:40to communicate with them through just the chat GPT interface, right?

0:44So if you open up chat GPT, let's just open this up right here.

0:49If you open up chat GPT and type in some sort of prompt, like create a recipe

0:54that uses

0:55bacon and maple syrup, right?

0:58Something like that.

0:59And then you hit enter, then what happens is that prompt is sent to the model

1:06behind the

1:07scenes, the model generates a response, and that response is displayed to us.

1:12So kind of the cool thing is that the chat GPT interface here, right, this user

1:19interface

1:20is actually a separate thing from the models that are completing the

1:25conversation, right,

1:27or responding to our prompts.

1:30And what this means is that we can basically create our own programs that use

1:36the models

1:37directly instead of interacting with the models through this chat GPT interface

1:42, right?

1:42That might not make a whole lot of sense yet, but this actually allows you to

1:46do some very

1:47interesting things.

1:48So for example, if you wanted to write a Java program to generate, let's say,

1:52you know,

1:53a hundred articles on different topics, right?

1:57Well, if you were going to do that manually, right, if you were going to

1:59generate a hundred

2:00articles on different topics manually, you'd have to actually copy and paste

2:05the same prompt

2:06and then change it slightly into chat GPT a hundred times.

2:11Whereas if you're, you know, if you're able to write a Java program that can do

2:15this on

2:15your behalf, all you'd have to do is give it like a list of the topics you want

2:19to generate

2:21articles for, and it would communicate with chat GPT's models for you

2:26automatically.

2:27All right.

2:28So hopefully that all makes sense, but don't worry too much about this.

2:31The main focus here is on using the concepts that we've learned in Java.

2:35And in particular, in this skill, we're going to be using the idea of network

2:40requests and

2:41also parsing JSON to make all of this happen.

2:45All right.

2:46So let's get started here.

2:48What we're going to do first is we're going to set up an account on platform.

2:54openai.com,

2:55right?

2:56This is openai is the company that maintains chat GPT.

3:01And so this, you know, platform.openai.com is basically the portal for

3:07developers to use

3:09when they want to start integrating the functionality that these models provide

3:14into our applications.

3:15All right.

3:16So you are going to need to create an account here.

3:19And once you've done that, you're going to go to dashboard and click on API

3:24keys over

3:25here on the left hand side.

3:27So an API key, you can think of this as like a password that your app is going

3:32to use in

3:32order to actually use some of the APIs that are more locked down.

3:38Right.

3:39We saw, we saw in a previous skill when we were learning the basics of making

3:45network

3:45requests that there are some APIs out there that are just totally free, right?

3:51They're not only are they totally free, but like you can use them whenever and

3:56wherever

3:56you want like that JSON placeholder URL that we were using earlier.

4:01But what you'll typically find is that with more valuable APIs like this one

4:06here, right,

4:07APIs that provide some very valuable functionality, you're going to have to get

4:13an API key.

4:14And in many cases, you're going to have to pay for them as well.

4:17Now, I do believe that with the open AI API, you have to activate billing.

4:22However, at the time of recording, once you do that, they give you something

4:26like it's

4:27like $100 in free credits, which should be more than enough to complete this

4:32project.

4:33So I'll leave that up to you.

4:34It's fairly straightforward.

4:37You just need to go into billing in your profile over here and activate it,

4:40right?

4:41You might need to put in a credit card number.

4:42But again, once you do that, they typically give you free credits to use so

4:47that you can

4:47actually test it out before spending any money.

4:51It's also not very expensive either, right?

4:53Like each request is going to cost you maybe fractions of a cent.

4:58So as long as you're careful not to put this in like an infinite loop, right,

5:02and make

5:03requests for like hours on end, it shouldn't really be a problem, right?

5:08It's again, I think it's worth it to just play around with these APIs and see

5:12what they

5:13can do.

5:14But anyway, let's get started here by creating a new secret key.

5:19And you can call this secret key, whatever you want.

5:22So I'm going to call this key something like Java SE 11 demo.

5:28All right.

5:29You can call it whatever you want.

5:31It's really just a memo to for you to remember what it's for.

5:35And I'm going to click create secret key.

5:38And as you'll see, that will generate this big sort of random string in this

5:44little text

5:44box.

5:45And we're going to want to copy that, right?

5:48Now I mentioned earlier that this is kind of like your app's password that it

5:53can use

5:53to actually access the API, right?

5:57The API will say, sorry, you're not allowed to do that if we don't include this

6:01, this

6:02API key.

6:03So you do need to keep this secret, right?

6:06You don't want to commit this to a GitHub repo.

6:08You don't want to, you know, I'm showing it to you here in this video, but I'm

6:12going

6:13to deactivate it as soon as I'm done recording so that by the time you see it,

6:17the API key

6:18won't be working anymore.

6:19All right.

6:20Cool.

6:21So anyway, let's click copy here.

6:24And that's going to copy that API key.

6:26And there's a number of things that you can do with this.

6:29But for our purposes here, what we're going to do is we're going to set this as

6:35an environment

6:36variable, which means that it's going to be defined sort of like locally on our

6:41computer,

6:42right?

6:43So it's not going to actually be in our code base, which makes it safe to still

6:49safe to,

6:50you know, commit this code to GitHub, things like that.

6:53All right.

6:54So here's what this is going to look like.

6:56What you're going to want to do first of all is you're going to want to open up

7:00a terminal

7:01and most IDEs have an integrated terminal here.

7:04In the case of IntelliJ, it's just right over here on the left hand side.

7:07I'm going to click on that.

7:09And once you see this little, you know, dollar sign thing, or you may see

7:13something slightly

7:14different there depending on your operating system, what we're going to do is

7:17we're going

7:18to say export, open AI API key equals, and then in quotation marks, I'm going

7:24to paste

7:25my API key.

7:26So you can see the full thing there.

7:28Again, I'm going to deactivate this, which is the only reason I'm showing it to

7:30you right

7:31now.

7:32These are you want to keep these very private, just as a reminder.

7:36And I'm going to hit enter.

7:38Now this is the this is what you do for Mac OS, as well as for Linux.

7:45If you're on Windows and using something like PowerShell, the command, I

7:48believe, is set

7:49X and then it's open API API key, and then you just leave a space and then you

7:54put your

7:55API key there like that.

7:58So anyway, that is how we set an environment variable.

8:03And an environment variable is basically just a value that our program can

8:07access that

8:08doesn't form part of the program or our file system itself, right?

8:13So we could put this in a file if we wanted to, but then we would have to go

8:16through the

8:17extra trouble of making sure that we don't commit that file to GitHub or share

8:21it with

8:21people that shouldn't have access to this API key.

8:24All right.

8:26So what we're going to do here is, first of all, I've created a new Maven

8:30project, which

8:31I've called gen AI chatbot.

8:34So I recommend that you do the same.

8:35Feel free to go back to the skill where we talk about adding third party

8:39libraries.

8:40If you don't remember how to do that.

8:43So you just create a new project and select Maven as the, as the build tool.

8:47And once you've done that, we have an empty project now.

8:50So we're going to create a new Java class where we're going to test out that

8:54API key

8:55that we just set up to make sure that it's working.

8:57And this will also be a great opportunity for me to show you how to access, you

9:01know, environment

9:02variables in Java programs.

9:04So we're going to call this something like this, this will be the main file

9:08that we're

9:08going to be writing our code in.

9:10So we'll just call it main.

9:13And inside here, we're going to add a main method, of course.

9:16So we'll say public static, void, main string, ARGS.

9:23And inside here, now what we're going to do is we're going to say, first of all

9:28, the

9:28way that you access an environment variable is pretty straightforward.

9:33We're just going to say string API key equals system dot get end.

9:40And then we need the name of the environment variable, which the name we gave

9:44it here was

9:45open AI API key.

9:48So that's what we're going to use there.

9:50And now just to make sure this is working, let's print this out, we're going to

9:52say system

9:53dot out dot print ln.

9:55And we're going to print out the API key like so.

9:58So let's try running this now.

10:00And if all goes well, you should see that big long random string printed out to

10:04the console,

10:05like what I have here.

10:06Now if you don't see that, then that may be because you closed your ID or like

10:12loaded

10:13a new project and you just need to run that export command that I showed you in

10:17the console

10:18a little bit earlier.

10:19So anyway, once you have that, then you should be good to go.

10:24And the next thing we're going to do is we're going to actually start building

10:27out our app.

10:28[BLANK_AUDIO]

Creating the Request and Response Classes

0:00All right, so now that we've created our project and set it up with an API key

0:05for the open AI API, the next thing that we're going to do is since we'll be

0:10using, you know, since we'll be actually making network requests to the open AI

0:15API and having to deal with that is parse the JSON responses that it's going to

0:22be giving us back.

0:24We're going to create two classes that will represent the data that we're going

0:28to be working with here, right? So we're going to create our first class, which

0:33will be called open AI request.

0:38All right.

0:39And we'll create another one here.

0:41There we go.

0:42New Java class, which will be called open AI response.

0:47So the basic idea here is that these two classes are going to represent the

0:52data that we're sending to the open AI API and the data that we're getting back

0:57from the open AI API, respectively.

1:00Okay.

1:01Now, in addition to this, there's a very important concept in working with the

1:07open AI API of something called a message.

1:11So we're going to create a class for that as well. And this message class is,

1:16and you could call it open AI message if you wanted to. I'm just going to call

1:20it message.

1:21This message class basically only has two pieces of information.

1:25And one is going to be, we're going to say private string role.

1:31And we're going to have one called private string content.

1:35Now, here's how this idea of a message works when working with the open AI API.

1:40And this is very similar for other options out there like Gemini, or if you're

1:44working with the anthropic API, which is the company that makes Claude.

1:50Basically, what we're going to be doing here is we're going to be using the

1:54role and content to specify individual messages in a conversation, what those

2:00messages contain, and who actually sent them. So for role, for example, there

2:07are actually three individual options here.

2:11The first one is user. This means that it's something that we sent to the API.

2:18Right. So that would be the equivalent of like, you know, I'll just write you.

2:23Right.

2:24The second one is going to be a message that the model has created for us.

2:31Right. And this, this role is actually called assistant. Okay. So that will be

2:37the model.

2:39Right. So, you know, if you're picturing like a text message conversation, or

2:43like an iMessage conversation, this would be the blue messages and this would

2:48be the gray messages on the other side. Right.

2:51Cool. So there's actually a third option for role, which might seem a little

2:56bit strange, but this is referred to as the developer role.

3:01You might see this called like the system role or something like that in other

3:05situations. But basically, this is where we can specify custom instructions for

3:10the model that have sort of like a higher priority than user messages.

3:16So this is where if you're trying to create a chatbot that's going to be a

3:19little bit more robust, this is where you can tell the chatbot, Hey, don't talk

3:23about politics.

3:25Don't talk about these topics. Don't agree to send the user free products. That

3:30sort of thing. Right.

3:32So we might take a look at that in a little bit more detail later. But anyway,

3:35that's the role. And then the content is just the text that each message

3:40contains.

3:42Right. So if we enter a prompt that says create a recipe that contains bacon

3:46and maple syrup again, then that's what the content would be for that message.

3:54All right. Cool. So anyway, now that we've created this message class and we

3:58can just leave that there. Oh, except we should probably also get or also add

4:02getters.

4:03So we'll just say generate getters for both of these things here. We're going

4:06to say, okay. And now that we've done that, we can go back to open AI request

4:11and open AI response.

4:13So starting off with open AI request, the only real responsibility of this,

4:19this class is to represent the data that the open AI API is going to be

4:25expecting from our program.

4:28So this includes the model. So we'll say private final string model. All right.

4:35That's going to be a string representing the model that we want to use.

4:39So GPT for Oh, GPT for Oh, mini GPT five. Those are what we have currently,

4:44although obviously in the future, there are going to be other options.

4:49So that's the model. And the second one here is just going to be a private

4:54final list of type message.

4:57That will be the messages that the request contains. Now, it might confuse you

5:02here a little bit that messages is a list instead of just an individual message

5:07.

5:08Because right when you interact with chat GPT, you're used to just typing in a

5:11single message and hitting enter.

5:13Well, the funny part about working with the open AI API is that in many cases,

5:19our program is actually the one that's going to have to keep track of and

5:24manage the messages in the conversation, which is something that's just taken

5:29care of by the platform when you use chat GPT.

5:34It takes care of storing all your messages. But all that to say that every time

5:39we make a request to the open API API, we have to include the messages from the

5:46entire conversation here as a list.

5:50All right. That's just how the open API API works because it's what's known as

5:53memory lists, right? It doesn't remember the previous requests we've made,

5:59which is a very common thing with API's just allows them to be much more

6:02performant.

6:03And kind of puts the burden of responsibility on the developer instead of on

6:09the API. All right. So now that we have the model and the messages, the next

6:13thing we're going to do is we're going to say public open AI request.

6:19All right. This is just going to take string model as an argument and list

6:26message as an argument as well. And it's just going to say this dot model

6:30equals model.

6:31And this dot messages equals messages. Okay. And now that we've done that, the

6:36next thing that we're going to do is we're going to actually go back to the

6:40message class.

6:41And since we'll need to create these messages ourselves in some cases, we're

6:44going to add a constructor to it. So here's what that's going to look like. We

6:48're just going to say generate constructor.

6:51And it'll include both these things here, the role and the content. And there

6:55we have it. So let's go back to our open AI request. And actually that's it,

7:00right? We don't need to do anything more for this class at the moment.

7:04So the next thing, let's head over to open AI response. And this one's a little

7:08bit trickier because we are going to need to create this directly from the JSON

7:13string that we get back from the, from the open AI API.

7:18The actual structure of the JSON that we're going to get back from the open AI

7:22API is going to look like this. First thing is it's going to have a property

7:28called choices.

7:30This confuses people a lot. Basically, you know, sometimes when you're working

7:34with chat GPT, it'll give you like two options and it'll say which response do

7:38you prefer.

7:39That's what this choices thing represents. And that's actually a list. All

7:43right, the JSON array, you would call it. And inside there, it's going to

7:47contain a number of, well, usually only, usually it only contains one JSON

7:53object.

7:55And this JSON object contains a property called message. And then that message

8:02contains the role and content that we just talked about previously, right?

8:10So anyway, with that structure in mind, that's why we're going to have to

8:14define this open AI response class like we're going to.

8:18All right, there is a fair, there's a fair bit of nesting here. So the first

8:22thing we're going to need to do is we're going to say public list of type

8:26choice called choices.

8:28Now we haven't defined a choice class yet. And because this is such a specific

8:33thing to this response, what we're actually going to do is just create a static

8:37class. This is an option for us here.

8:40So we're going to say public static class choice. All right. And then what we

8:47're going to do is we're going to say public message message as its member

8:51variable.

8:53And oops, I wanted to actually set these to private. I don't know why I was

8:56typing public there. So we're going to say private message message.

9:00And we're going to say private list choices. So we'll say import class there.

9:05And now that we've done that, that should be all we need in order to start

9:09making requests to the open API API and actually be able to do something with

9:15the data that we get back.

9:16So that's what we're going to do in the next video.

Creating an API Client Class

0:00All right, so at this point, what we've done is we've created classes that

0:04represent the

0:05request we're going to be sending to the OpenAI API, the response that we're

0:10going to be receiving

0:11from the OpenAI API, and of course, the message that represents the data that's

0:17really going

0:18to be included in both of those communications.

0:21So the next thing that we're going to do is we're going to kind of start to tie

0:25these

0:25together by creating a new class called OpenAI Client, which is really going to

0:32be the main

0:33way that are, you know, the core part of our program, this main method is going

0:39to use

0:39these things.

0:40All right.

0:41So here's what we're going to do.

0:43We're going to create a new class.

0:44So let's go into Java, new Java class, and we're going to call this one OpenAI

0:49Client.

0:50All right.

0:51And in order to make this thing a little bit more interchangeable, what we're

0:54going to

0:54do is we're actually going to create a interface here.

0:58So let's select interface, and we're going to call this interface chatbot.

1:03All right.

1:04Or actually, I don't really like that name.

1:06I was pretty confident about that name until I typed it out.

1:08Then I thought again, let's call this something like generative AI client.

1:16All right.

1:17So the reason that we're doing this, and we've seen numerous examples of this

1:22in other

1:22examples that we've, you know, other projects that we've worked on in this

1:26course.

1:27But the reason that we're doing this is so that this OpenAI client is

1:31interchangeable

1:32to the rest of our program, right?

1:34We don't want to get vendor locked with OpenAI, if let's say they raise their

1:39prices or we

1:40find another option, right?

1:43We want to be able to swap that out as quickly and easily as possible.

1:46So you know, by having this generative AI client interface, this leaves us open

1:51to create

1:51like a cloud client, a Gemini client, a llama client, really whatever, whatever

1:58other tool

1:59we want to use for this same purpose instead.

2:03Okay.

2:04So for this interface, what we're going to do is we're just going to define a

2:07single

2:08method on it.

2:09We're going to call this method string get response.

2:13And what this is going to do is this is going to take a new message as an

2:19argument.

2:20So we're going to say string user message.

2:24And this is going to be communicating across, you know, across the internet.

2:29So obviously it has the potential to throw an IO exception.

2:32We're actually going to say throws exception because there's a few other things

2:35that might

2:35happen there that we're just going to kind of like brush under the rug for now.

2:40This isn't a good practice, by the way, but since the focus here is doing the

2:43generative

2:44AI thing and communicating with this API instead of handling exceptions, I'm

2:48going to leave

2:48that one up to you.

2:50All right.

2:52So now that we've done that, what we're going to do is we're going to go back

2:55to the open

2:55AI client.

2:56And really the first thing to notice here is you may have been wondering if you

3:02were kind

3:02of thinking ahead a little bit, you may have been wondering whether this get

3:07response method

3:07that we just defined was going to take, you know, a list of messages, right,

3:12this message

3:13type that we defined up here, or a list of strings or a single string or a

3:18single message.

3:19There's really lots of ways that you could do this.

3:22And it kind of depends on how much functionality you want to encapsulate in the

3:29client classes

3:30that are going to implement this generative AI client interface.

3:34So here's what I mean by this, right?

3:36Let's just picture our interface, right?

3:38So this is our generative AI client interface.

3:40And right now we only have one specific class, which is our open AI client

3:48class.

3:49And I've already discussed here that, you know, in order to interact with these

3:55APIs,

3:56in order to interact with the open AI API, for example, our program is the one

4:00that has

4:00to manage those conversations, right?

4:02So we basically have to keep track of all of the messages that are in that

4:06conversation

4:07and then actually send that to the open AI server from inside one of our, you

4:14know,

4:15probably inside this open AI client class.

4:18Now what I meant when I said that we have to decide where to draw the line

4:23functionality

4:24wise is if we have the rest of our program right here, right, which is just our

4:29, well,

4:30I'll just call that main for now.

4:32Well, all of that management, right, managing the entire conversation and

4:37keeping track

4:38of the different, you know, messages that have been sent and received, we can

4:41either

4:42put that in here, right, inside main, and here, let me actually just write

4:45something

4:46here.

4:47So we'll say something like, well, say something like convo management,

4:52something like that.

4:55And again, we can either put that inside the main portion of our program or

4:59inside the

5:00open AI client portion of our program.

5:02And there are advantages and disadvantages to each really for our purposes

5:06right now,

5:07the big advantage of putting it in the open AI client class, which is what we

5:11're going

5:12to do is that that will allow us to reuse that functionality, right?

5:18That'll allow us to easily create a chat bot in many different situations

5:22without having

5:23to worry about any of that conversation management.

5:26And if we want to create like a new conversation, all we have to do is create a

5:29new instance

5:30of this open AI client.

5:32Okay.

5:33Now, if we were to put it in the main portion of our program, that would give

5:36us a lot more

5:37flexibility.

5:38All right.

5:39So there are certain situations where you might want to just create an entire

5:43conversation

5:44from scratch with responses and, you know, prompts.

5:49There are situations where you might want to do that.

5:52And, you know, by putting this conversation management logic inside the main

5:56portion of

5:56our application, that would make our program a lot more flexible for those

6:01kinds of situations.

6:03So anyway, all of that to say that we are going to be putting this conversation

6:07management

6:08inside open AI client.

6:11And that's why this get response thing only has a single user message argument,

6:16right?

6:17That's all we want it to have because it's going to take care of keeping track

6:21of the

6:21response and, you know, just kind of organizing the conversations behind the

6:26scenes.

6:27So with all that said, let's go back to our open AI client class.

6:32We're going to add implements generative AI client here.

6:37We're going to add that method that it's going to need here.

6:39So we'll say, generate and we'll say implement methods and select get response.

6:45That's the only one here that we have to implement.

6:48And now what we're going to do, right, let's, let's kind of take a step back

6:53and think

6:53about how we want this thing to work.

6:55I mentioned that this is going to have to keep track of all of the messages

6:59that are

6:59going back and forth.

7:00So we're going to add a list as, you know, just a member variable here of this

7:08message

7:09class that we define.

7:10So here's what we're going to do.

7:11We're going to say list of type message.

7:13Let's import list into this class.

7:16And now we're going to say messages are here better yet.

7:19We'll call it conversation.

7:21And we're going to say equals new ArrayList and we're just going to leave it

7:25empty at

7:25first.

7:26Okay.

7:27So now that we've done that, there's a few other things we're going to want to

7:31do, you

7:31know, doing things like setting up an HTTP client to send requests is a good

7:34idea to do

7:35here.

7:36So we're going to say something like, actually, what we'll do, we'll start off

7:39by saying private,

7:41static, final, string API URL, right?

7:45This is going to be the URL of the open AI API.

7:50And I'm going to just copy this and paste this in here.

7:53Feel free to copy that.

7:55It's just API open AI comm slash V one slash chat slash completions.

8:00And now that we've done that and, oops, I put that in single quotes.

8:03Don't know why I did that.

8:04Let's try that again.

8:05We're going to swap that out with double quotes.

8:07And that's just going to make it easier to refer to the API URL.

8:11And if that API URL ever changes, as these URLs tend to do, all we would have

8:16to do is

8:17change that in this one place instead of having to, you know, make changes

8:21throughout

8:22this class.

8:23Awesome.

8:24So now that we have the API key, we're going to say private final HTTP client

8:29as well.

8:30We'll just call that HTTP client like so.

8:34And then we're going to say private final Gson.

8:40And we'll call that Gson.

8:41And we don't have Gson added to our project yet.

8:44So let's just do that real quick.

8:45You probably remember how to do this.

8:46We're just going to say, going to palm dot XML, and we need to add some

8:51dependencies

8:52to this project.

8:53So I'm just going to copy and paste that right here, like so.

8:57And if you click on this little refresh icon, or if you just wait for a minute

9:02or two,

9:03your IDE will install it.

9:04And if you go back to open AI client, you should see now that you have the

9:07option to

9:08import Gson.

9:10So there we go.

9:11All right.

9:12So let's add a constructor to our class now.

9:15What we're going to do here is we're going to say public open AI client.

9:21And this is going to take the API key as a string.

9:25All right.

9:27And then what we're going to do is we're going to say this API key.

9:31Oops.

9:32And I actually didn't add API key here.

9:34I forgot to do that.

9:35So let's just go up here.

9:36I'm going to say private final string API key.

9:41All right.

9:42And now that we've done that, we're going to say API key equals API key, this

9:48dot HTTP

9:49client, that's going to equal HTTP client dot new HTTP client, just creating a

9:54new client

9:55there like we saw how to do in previous skills.

9:58And then we're going to say this dot Gson equals new Gson.

10:01All right.

10:02So in other words, we're just setting these things up initially when we create

10:07the new

10:07client in the first place, which means that when we call get response, we're

10:12not going

10:13to have to do all of those things.

10:14That just saves us a little bit of time in there.

Communicating with the OpenAI API

0:00So now that we've done all of that, the last thing we need to do here is we

0:04need to implement this get response method.

0:07And here's what that's going to look like.

0:10So you can see that the default here just has return empty string. We're going

0:13to delete that.

0:14And we're going to create a new open AI request.

0:18Okay.

0:19And we're going to call this request body.

0:22And we're going to say equals new open AI request.

0:25And as the arguments here, we're going to specify the model we want.

0:29We'll just do GPT 40 mini.

0:33You can feel free to take a look at the open AI API reference if you want to

0:38see what other models there are and what the pricing for those models is.

0:42But I'm just going to use GPT 40 mini for now.

0:44I'm going to say list dot of.

0:47And here's what we want to do.

0:48We want to include all the messages from this conversation in this request.

0:54So we basically have to take this conversation list, add a new message with

0:59whatever string the user entered into it.

1:02And then what we're going to do is we're going to just send that over to the

1:06API.

1:07So first of all, right, before we even create that request body, we'll come

1:10back to that in a minute.

1:12We're going to say conversation dot add.

1:15And we're going to add a new message instance with the role of user, right,

1:23because this is a message that the user is actually sending to the API.

1:27Right.

1:28The other options, as you may recall, are assistant and either system or

1:32developer, depending on which API and the version you're using.

1:35So we're going to say user message.

1:38And that is adding a new message onto our conversation list. So we just need to

1:44convert this conversation now into a new list.

1:49So we just need to include this conversation with this new request and actually

1:53this list of we don't need that because we've already got a list.

1:57We've already got our conversation in list form.

1:59So we can actually just say this conversation right inside of there and that

2:03will be fine.

2:05Awesome.

2:06So now that we have the request body, here's what we're going to do.

2:10We're going to say string JSON request.

2:14And we're going to use Gson here.

2:16So we'll say Gson dot two, JSON.

2:19And we're going to pass request body there that will just take all of the

2:22member variables that are in there, right, role content.

2:27And well, it's in a list.

2:29So it'll be a JSON.

2:30JSON array is what they're called and that's going to turn it into something

2:35that we can send with the request.

2:38So here's what we're going to do next.

2:39We're going to say HTTP request and create an actual HTTP request object that

2:44sort of wraps this data up.

2:47So here's what that's going to look like.

2:49We're going to say HTTP request equals HTTP request dot new builder.

2:56You may recall this from previous skills and we're going to say first of all

3:00dot URI.

3:01This is where we want to send the request to.

3:03So we'll say URI dot create and we're going to say API URL as the URL for that.

3:12Let's also import the URI class.

3:15All right, then we're going to set something called headers.

3:18These are basically just extra information that's attached to a request that,

3:23you know, let's the server know what type of data you're going to do.

3:25So what type of data we're expecting.

3:27This is where we also include our API key to let the server know that, you know

3:32, what account this is this request is actually coming from and should be billed

3:36to.

3:37All right, so we're going to say dot header.

3:39We're going to say content type and the value for this one is going to be

3:44application slash JSON.

3:47All right, basically again telling the server that it should expect JSON data

3:50here.

3:51We're going to add another header by saying dot header again.

3:54This one is going to be an authorization header and for this one, all we need

4:00to do is say bearer.

4:02This is just something that every authorization header begins with when you're

4:07using API keys.

4:08And then we're going to add the API key onto the end like so.

4:12All right, this is going to be a post request.

4:15And inside here we're going to say HTTP dot body publishers dot of string.

4:23And we're going to pass JSON request to it.

4:26Don't worry too much about that one.

4:27That's just how we add that extra data, right, the conversation to this request

4:31.

4:32And finally we're going to say dot build and that will basically convert all of

4:37that into an instance of this HTTP request class.

4:42Awesome.

4:43So now we have our request.

4:45Let's send this thing.

4:47So we're going to be expecting an HTTP response back from it.

4:51We're going to say HTTP response of type string.

4:55All right, let's make sure we import that class here as well.

4:59There we go.

5:00And we're going to say response.

5:02And now we're going to say equals HTTP client dot send.

5:07We're going to include the request as the first argument as the second argument

5:10.

5:11We're going to say HTTP response dot body handlers dot of string.

5:19Oops, let me try that again.

5:20There we go.

5:21And now that we've done that, we're going to basically convert this into an

5:27open AI response, right, an instance of that class that we got.

5:30So here's what this is going to look like.

5:32We're going to say open AI response.

5:35And then we're going to say equals Gson dot from JSON.

5:40We're going to have the response body in there by saying response dot body.

5:45And then we're going to pass open AI response dot class as the second argument.

5:50And this just takes the data that we got back in this response here, or sorry,

5:55in this response here and converts it to an instance of this class.

5:58So once we've done that, we'll be able to access the choices and the messages

6:03inside each one.

6:05So here's what we're going to do next.

6:08We're just going to say if AI response is not null and if AI response is or AI

6:19response rather dot choices dot is empty, right?

6:24We're checking if it's not empty there using this little not symbol here.

6:29And let's actually change this to get choices, right?

6:32We need to make it so that we can access that.

6:35So we'll go into open AI response.

6:37We're going to add a getter for this one.

6:38So we'll say generate.

6:40We're going to add a getter for choices.

6:43And that should be all we need there.

6:45So, oh, and oops, I wrote open AI with a lowercase i.

6:50So let's just fix that real quick.

6:52We spelled it with a capital I.

6:54So we need to be consistent about that, of course.

6:56All right.

6:57So everything seems to be going well now.

6:59So for that if statement, what we're going to do is, you know, if that's there,

7:04what we're going to want to do is if that's there, we're just going to say

7:08return AI response.

7:10All right, dot choices or dot get choices rather dot get index zero.

7:17So we're getting the first choice there.

7:22That's just, you know, kind of the default way of working with this API because

7:24we just assume that there's one choice and ignore the others.

7:27Right.

7:28Then we're going to say dot get message.

7:31And then we're going to say dot get content.

7:34Okay.

7:35Now there are a few things we need to go in and fix there.

7:37So let's go into open AI response.

7:40We're going to add a getter for this message thing here.

7:42So we'll say generate getter for message.

7:46All right.

7:47And inside message, oh, we already have get content.

7:50So we should be all good there.

7:52So let's go back to open AI client.

7:54There's no red there anymore.

7:55And basically if the response is null or if there aren't any choices, we're

8:00going to want to just respond with some sort of default message like, you know,

8:05chat GPT or the Google Gemini interface tends to do.

8:09We're just going to say return.

8:11We'll say, sorry, I didn't get that.

8:16And then we'll say something like please try again later.

8:21All right.

8:22Not very helpful, but that'll just kind of provide a backup there.

8:25Awesome.

8:26So now that we've done that, all we have left to do is go into our main method

8:32and actually bring all of this together.

8:36And that is actually something that I'm going to leave up to you as a challenge

8:39.

8:40So head to the next video and I'll describe the challenging more detail there.

Challenge & Solution: Bringing It All Together

0:00All right, well now that we've finished up our open AI client class, it's time

0:04for you to do a challenge.

0:05And in this challenge, your task is going to be quite simply to implement the

0:09main method in this main class

0:11so that it will actually make use of this open AI client chatbot and allow the

0:17user to have a conversation with it.

0:20All right, so the basic idea here is you're just going to create an infinite

0:24loop, right?

0:25So you can just, you know, in there you're just going to want to say like, "

0:28wild true" and you're just going to keep getting input from the user.

0:33Do be careful with that wild true loop when you're sending a request to the

0:37open AI API, of course.

0:39But you're just going to keep getting input from the user and using this get

0:44Response method that we implemented

0:47in order to get the, you know, get the open AI API's response and you're going

0:53to display that to the user

0:55and maybe give the user a keyword like exit or something like that that they

0:59can enter in order to leave the app.

1:03Okay, now before we do that, there's two things I wanted to show you that are

1:07going to make this a much more pleasant experience.

1:10The first thing is you may have noticed that we forgot to add the model's

1:16response to the conversation, right?

1:19So our open AI client isn't really doing very well in its responsibilities of

1:23conversation management.

1:25So all we have to do here is just say this.conversation.add and we're going to

1:31add the new message by saying,

1:34I'm just going to copy that here and here. Let's make this a little bit neater

1:39by saying message new message equals

1:42and then I'm going to just print that out like so and we're going to say

1:46conversation add new message

1:48and last but not least, we can change this to new message dot get content.

1:55All right, cool. So that's the first thing that you needed to change.

1:59The second thing that you needed to change is something with the IDE itself.

2:05Now you may have realized this kind of depends on your operating system and

2:09stuff like that.

2:09So you may or may not run into this problem, but if you run into the problem of

2:13it not finding the API key that we set in the terminal,

2:17another thing to try, which is a little bit more robust and the way that I

2:21would recommend doing it in, you know, in the real world,

2:23is if you go into run and then go to edit configurations, I've created this

2:29application configuration.

2:30You can do that just by saying plus and then application and all you have to do

2:35is give it a name.

2:36So I called mine open AI for build and run. You just need to select the main

2:41class, right?

2:42The class with the main method in it and then for the environment variables,

2:48what you need to do is you just need to set.

2:51Let's just scroll back here. Almost there. There we go.

2:55You just need to add something here that says open AI API key equals and then

3:00paste your open AI key like so.

3:02And then when you say, okay, you just need to make sure that that's selected

3:05from this drop down when you run your app.

3:08And that's again, a little bit more robust than just setting it through the

3:11terminal.

3:12So anyway, that is your challenge is to create the main method that brings all

3:16of this together.

3:17So feel free to give this maybe five to 10 minutes to complete. And once you've

3:20done that,

3:21you can move on to the next video where I'll walk you through the solution.

3:24So best of luck and I'll see you in the next video.

Challenge & Solution: Bringing It All Together

0:00All right, well, hopefully you gave the challenge a try.

0:02Let's take a look at the solution.

0:04So the first thing that I did is I opened up main.java.

0:08And since we already had the API key,

0:10creating a new instance of this open AI client

0:14should have been pretty easy to do.

0:16So here's what I did.

0:18I just underneath where we set up the API key,

0:21and I'm gonna remove that system.out.printerlin there,

0:24'cause we don't actually want to print that out

0:25to the console each and every time we run the program.

0:29What we're gonna do is we're gonna start off

0:31by creating a scanner.

0:33So we'll say scanner scan equals,

0:36or here I'll just call it scanner,

0:38equals new scanner system.in.

0:41And now that we have the API key,

0:45we're gonna create a new chatbot

0:47by saying generative AI client.

0:51We'll just call this chatbot equals new open AI client.

0:56And we're gonna pass the API key like so.

1:00All right, so again, if we wanted to switch this out

1:02to use something else like the Claude API

1:07or the Gemini API,

1:08all we'd have to do is create another API here

1:10that implements the same interface.

1:13Okay, so now that we have our chatbot,

1:16what we're gonna do is we're gonna start off

1:17by printing out a message to the console.

1:20You didn't have to do this, but I thought it was,

1:23you know, a pretty good thing to start off with.

1:26I said chatbot is ready.

1:28And then I said type exit to quit, okay.

1:33Cool, so just kind of initial thing

1:37giving the user some instructions

1:39on how to interact with this thing.

1:40All right, so what we're gonna do next

1:41is we're just gonna create an infinite loop

1:44that we'll only exit out of

1:46when the user types in the word exit, okay?

1:49So here's what we're gonna do.

1:50We're gonna say system.out.print.

1:55All right, and we're going to just say you, right?

1:59This is where we're gonna have the user

2:01enter in their message.

2:04And we're going to get their next prompt

2:06by saying string user input equals scanner.nextline.

2:11And now that we have the user's input,

2:16what we're gonna do is we're gonna check to see

2:18whether it equals exit or not.

2:20All right, so we'll say if,

2:23then we'll say user input.equalsignorecase exit.

2:28Well, in that case, what we're gonna do

2:32is we're just gonna say break

2:33and that will sort of pop the user out of this while loop.

2:37All right, that's how the program will end.

2:40So the next thing that we're gonna do

2:42is we're just gonna add a try catch block here

2:44because again, that here, let's just go back here, right?

2:49This getResponse method throws an exception.

2:52All right, so we'll have to catch that.

2:55And inside here, we're gonna say string response equals chatbot.getResponse.

3:00And then we're just gonna say user input.

3:04So we're passing that new prompt through

3:07to the chatbot that we created.

3:11And now we're just gonna say system.out.println.

3:16And we're gonna say bot

3:17and we're going to add that response onto there like so.

3:22And you'll see that there's a little red underline here.

3:25That's because we need to catch any exceptions that occur.

3:28So we're just gonna say catch exception e.

3:30And in this case, we're just gonna say e.printstacktrace

3:34that will just kind of print out whatever happened.

3:36And then we'll say something like system.out.println.

3:41And we'll just say error communicating with the API.

3:46All right, cool.

3:49So anyway, that's all we really need to do.

3:52The last thing is outside this while loop,

3:54we're just gonna call scanner.close.

3:56And that should be all we need to do.

3:59So let's just give this thing a try.

4:01We're going to run our file.

4:04If all goes well, we should see this come up

4:06and say that the chatbot's ready.

4:08So let's try just entering in a prompt

4:11like you'd enter into chat GPT, all right?

4:13And here I'm gonna zoom in on that a little bit

4:14so that we can see it.

4:16So let's say something like tell me three fun facts

4:22about coffee.

4:24All right, and if we hit enter,

4:26what we're gonna see is it does take a minute or two

4:29to finish because, well, it's kind of a slow,

4:32computationally intensive process behind the scenes.

4:35But anyway, we see that sure enough,

4:36we get three fun facts about coffee.

4:38And the cool thing about this is

4:40that because we're keeping track of the conversation,

4:44we can ask it to expand on a specific part of this.

4:48So we could say something like tell me more

4:51about the second point, right?

4:54And because the API is gonna have access

4:58to our previous messages, right?

5:01And the previous messages that it sent us,

5:04because we're including those in our request,

5:06it will know what we're talking about, right?

5:08If you were to remove that, here,

5:11let's just take a look at this.

5:12If you were to go into OpenAI client

5:14and remove the parts where we're actually adding this

5:16onto the conversation list,

5:19then what you would find is it would have sort of

5:20like short-term memory loss where it would say,

5:23like the second point of what?

5:25I don't know what you're talking about, right?

5:27But in this case, because we've added that,

5:29what we're gonna see is if we say, tell me more

5:30about the second point, it'll know what we're referring to.

5:34And what we should see is that it should tell us,

5:37in this case, more about the coffee plant's berries.

5:39So sure enough, yep, coffee cherries and beans.

5:42And it, that's the response that we got.

5:46So anyway, congratulations on finishing this challenge.

5:49And congratulations on building

5:51a generative AI-enabled app in Java.

Team training path

Turn this skill into assignable team training

This free skill is a preview of the courses your team can assign, track, and report on with CBT Nuggets.

What's next?

Ready to keep going?

For your team

Bring this training to your team

See how CBT Nuggets helps IT teams close skills gaps, hit compliance targets, and prove training ROI.

Book a Demo
Just need Java SE 11?

Learning on your own? Browse individual plans ($49/month, billed annually)

Not ready to buy?
with no purchase required. Already have an account?
Book a Demo