Great news, everybody. There's now an official ChatGPT API. We can use it to easily build our
own bespoke chatbot. There's even more great news. The new API is considerably cheaper than the
previous APIs. In our last version, we used the DaVinci model to make our own chatbot.
This model costs 2 cents for 1000 tokens. The new model only costs 0.2 cents for 1000 tokens.
That's one tenth the price. Amazing! It's absolutely incredible progress. OpenAI has done a
fantastic job optimizing the model and it now runs on just 10% of the original resources.
Let's head over to the playground at platform.openai.com and try out the new Chat API.
In the model drop-down, select Chat. The UI for the Chat model is slightly different. We now have
a new System section. This is where we'll fill in the instructions for how our chatbot should
behave. We'll use the same prompt as in our previous video. It's still worth watching that
as there's some useful information in it. Our first three lines try and guide the model into
giving the user relevant answers for our use case. We're going to make an AI cocktail assistant.
You are an AI assistant that is an expert in alcoholic beverages. You know about cocktails,
wines, spirits and beers. You can provide advice on drink menus, cocktail ingredients,
how to make cocktails and anything else related to alcoholic drinks. We want an AI assistant that
knows about drinks. Specifically, it's going to know all about cocktails and alcoholic beverages.
If you are unable to provide an answer to a question, please respond with the phrase,
I'm just a simple barman. I can't help with that. This next line tries to keep our chatbot
focused on its area of expertise. We don't want it answering any old random questions.
Please aim to be as helpful, creative and friendly as possible in all your responses.
We also want our chatbot to be friendly and helpful, so we tell it to behave in that way.
This should make it quite pleasant to interact with. Do not use any external URLs in your answers.
Do not refer to any blogs in your answers. Now, I added this line after a bit of experimenting
with the previous model. I found the assistant would often refer to external links or imaginary
blog posts when asked about cocktail recipes. This attempts to stop it from doing this.
Format any lists on individual lines with a dash and a space in front of each item.
Finally, let's get the output nicely formatted. We're likely to be asking for lists of cocktails
and ingredients, so it would be good to get it formatted nicely. This last line may be unnecessary
with the new chat model. I really recommend playing with the instructions to see how it behaves.
Let's try asking some questions of our new chatbot. Let's see what we can make at home.
That's a pretty good response. Some nice cocktails. Let's check to see if it maintains
context. What glasses do we need for the suggested cocktails?
This works really nicely as well. It knows the cocktails it suggested earlier
and gives us the required glasses. Now let's try going off pieced.
Well, it didn't try and answer the question, but it didn't use our canned phrase. Maybe we
need to do a bit of prompt engineering to make it behave better. So apart from that last bit,
it's great. This all works nicely in the playground. How do we convert this into code?
The only major change from our previous project is that before we had to send up the chat context
as part of the instructions for the model. We needed to format this as a series of messages
between the user and the assistant. It was pretty straightforward, but a bit clunky and prone to
error. With the new chat API, we've sent up the message context in a structured format.
There are three roles involved in the conversation. System, user and assistant.
As in the playground, the system role is used to help set the behaviour of the chatbot.
This is where you tell your bot what it can and can't do, and how it should respond to the user.
The user role contains questions from the end user, and the assistant role
is used to store the responses from the model. We send these up in the order they happened,
the system first, followed by alternating user questions and assistant responses.
As before, there is a maximum limit to how much chat history can be sent up.
I've taken the same approach as previously, where we throw away older questions and responses.
This does mean that after a while, our chatbot will forget earlier parts of the conversation.
This is probably quite appropriate for our example, given it's all about alcoholic drinks.
Remember kids, always drink responsibly. You'll need to get an API key to make the
sample code work. To do this, click on your picture in the top right and head over to the
View API keys. You can now click on Create New Secret Key. Make a copy of the Secret Key value
and put it somewhere safe. You'll need it later. Now clone the code from GitHub.
I've put a link to the repo in the description. With the code on your local machine,
you just need to make a couple of changes. Move or copy the.env.sample file to just a
file called.env. And now paste your API key over the placeholder text.
Open up main.py and replace the text that says put the prompt here with whatever prompt you want to
use. I've provided an example in the Read Me that you can use for testing. As I said before,
it's really worth experimenting with the prompt you use. This is where most of the chatbot
behaviour will come from, so it's the most important part of the whole system.
You can also tweak these other parameters to see what effect they have. There's documentation on
the OpenAI site that describes each one and gives suggested default values. And you can easily
play with these values in the playground to see what happens. The code really is very simple.
We just sit in the loop, prompting the user to ask a question. One thing that is optional,
but is highly recommended, is moderation. This is particularly important if you are planning to
open up your chatbot to the public. I know moderation can be a controversial subject
for some people, but it's worth bearing in mind that not everyone is willing or able to behave
acceptably. And the last thing you want to have is bad publicity. Just look at Microsoft's Bing
chatbot and see the kind of things it's come out with. It's quite dangerous. The moderation system
categorises content into several different categories. Hateful content that targets particular groups
of people. Hateful threatening content as above but involves violence or serious harm. Self-harm,
content that encourages or depicts acts of self-harm. Sexual, content that is meant to sexually arouse
or excite. Sexual involving minors. Anything sexual involving under 18 year olds. Violence,
any violent content. And graphic violence, any violent content that is very graphic.
If our question passes the moderation check, we send it off to the API to get a response.
This is really easy. We set up the message history starting with the system message containing our
prompt, and then following that with the previous questions from the user and responses from the
assistant. Finally, we stuck our new question at the end. All this is sent up to the chat API.
If everything works, then we get a response from the bot. In production code, you'll need to check
for errors here and fail gracefully. Back in the main loop, we add this new question and answer
to the chat history and then show the response to the user. That's it. It really is that simple.
Let's spin our code up and give it a go. We'll repeat our tests from the playground.
What are some nice cocktails I can make at home? Pretty good. And let's see what kind of glasses
we should use. What glasses do I need for the cocktails? Also great, our bot is maintaining
context. Let's try and get it to answer something that it shouldn't know about.
Tell me about life. So this is similar to the playground. It's given us a nice response,
but it's not really the response we asked for. I did try this again and got a more suitable
answer with a nice bit of banter. As I say, maybe we need to do a bit of prompt engineering to make
our bot behave more consistently. So that's it. It really is that easy to build your own chat bot
using the new APIs. And even better, it's not going to break the bank. Happy chatting and I'll see you
in the next video.