So, everyone's getting pretty excited about ChatGPT. At least I am. And don't worry,
if you're not getting excited by it, we'll be back with some more electronics videos
shortly. One of the issues with ChatGPT at the moment
is that there's no API for it. This makes it really hard to make your own chatbot on
top of it. However, the makers of ChatGPT, OpenAI,
do have APIs for the technologies that underlie ChatGPT, and it's surprisingly easy to build
on them and get something that's pretty close to the functionality of ChatGPT.
The first thing you'll need to do is create an account at beta.openai.com. So I've logged
into my OpenAI account, and we need to create an API key. To do this, we just click on our
little picture, and we go to View API Keys, and then we click Create New Secret Key. Now,
before you all jump into the comments, don't worry, I'm going to revoke this key before
I publish this video so it stays secret. We need to copy this key somewhere safe so we
don't lose it because you can't access it after you close this window. So we just click
the copy and then paste it somewhere safe. With our API key created, we can now make
our bot. Now, OpenAI have a whole bunch of examples that you can build your bot off, but
we're going to jump straight to the playground and build it from scratch. We've got a bunch
of parameters on this side of the screen. The first one is the model. Now, Text DaVinci
03 is the most capable model, so we're going to use this for our chatbot, but you can switch
to simpler models, but they may not be as effective. The next things we can do are change the
temperature. This controls the randomness. I'm going to move it down to 0.5 as this will
give our bot slightly more repeatable results. The maximum length is an interesting one.
This controls how much of a response you get from the bot. There is a trade-off here because
the size of this limits how much you can put into the prompt. If we have a longer response,
we have a shorter prompt, but I'm going to set this reasonably high so our bot can be
quite chatty. We're going to leave the stop sequence empty and we'll leave top P as
1. The frequency penalty and the presence penalty, you can try tweaking these to see
what you get. It's interesting to play around with these to see what the actual bot responds
with. For the start text and restart text, we'll leave that off, but we will turn on
show probabilities so we can see what the bot is trying to do. So the only thing we
need to do now is actually create the prompt that will make our thing behave like a chat
bot. So I thought it might be fun to try and create a bot that will help us make cocktails.
So I've generated some prompt. Let's have a look at what we've got. So these three
lines tell it that it's an expert in alcoholic beverages. It knows all about cocktails, wine,
spirits and beers, and it can provide advice on drink menus, cocktail ingredients, how
to make cocktails and anything else related to alcoholic drinks. So it should be quite
a helpful cocktail bot. Now the next line we add is going to try and keep our bot focused
on just answering questions about drinks. We're not trying to build a generic chat bot.
So we're basically telling it, if it doesn't know the answer to a question, just say, I'm
just a simple barman. I can't help with that. Now this next line, I added after a bit of
experimentation. I found that occasionally my bot would put URLs in, or it would refer
to blog posts that it just made up. So this tries to keep it just using knowledge it knows
itself. And then finally, we're going to be getting list of cocktails from this bot. So
it'd be nice if they were formatted nicely on individual lines with a dash and a space
in front of them. So let's give our bot a bit of a spin. We prompt it with questions
by prefixing them with human. So let's say, what are some cocktails I can make at home
easily? And then to get a response from our bot, we just give it the prompt AI colon. So
let's submit this and see what our bot says. So there we go. Here's a list of easy cocktails
we can make at home. So let's try a follow up question and see if our bot maintains some
context. So let's try asking, what are the ingredients for the cocktails? And once again,
we'll leave it with the AI colon. So it knows where to put its answer. So there we go. We
can see that it's using context for its previous answer in the next answer. So gin and tonic,
evidently, tonic water, lime juice and gin. So that's pretty good. Let's check that it
stays on message and doesn't try and answer questions that it shouldn't be able to. So
we'll ask it a question that should be out of scope of its knowledge. And there we go.
We get the correct answer. It's just a simple barman. It can't help us fix our car. So
let's delete all our questions. So we're just left with the prompt. Now what's really
nice is we can click this view code button and it tells us exactly how to call it. We
can ask for Python code. We can ask for Node.js code. We can even ask for command line code
using curl. So that's pretty good. But I've created a nice skeleton project that we can
just copy and paste our prompt into. So let's do that next. And we'll create a command line
app that acts as a chat box that will help you make fantastic cocktails. So I've cloned
the skeleton project from GitHub. And let's go through the read me so we get everything
set up. The first thing to do is to check we've got Python installed. So run Python
3 version and we can see we have Python 3.10.8. So that's great. Now we need to create a virtual
environment to install the dependencies. This is quite an important part of using Python.
You want to keep your dependencies local to your project and not contaminate your entire
system with them. So the first thing we'll do is create a virtual environment. And then
we'll activate it. And then we'll install the requirements. Now there's not many requirements.
This is a pretty simple project. We have the open AI library. So we can call the open AI
API. We're using dot EMV to load settings from a dot EMV file. And I'm using some coloring
on the terminal. So I can color the questions and the answers so we can read them easily.
So let's go back to the read me and see what we do next. So we need to copy the EMV dot
sample to dot EMV. So let's do that quickly. And then replace your API key with the API
key you created earlier. So I pasted that somewhere safe. Let me copy it out and paste
it in. So that's our dot EMV file created back in the read me. We now edit main dot PY
and replace the put the prompt here with our prompt. So we go to main dot PY and we'll
change this to our prompt. So we go back to the open AI playground, copy the prompt and
paste it in. So now we've done that. There's a few constants here. So we have the answer
sequence, which is AI colon, this prompts the bot to respond. And we have the question
sequence human colon, which is how the bot knows our questions. Then we have some constants
for the temperature, the max tokens, the frequency penalty and the presence penalty. Now this
is quite an interesting constant. We need to provide the bot context so it knows what
we've been talking about. But there's a limit on how many tokens the bot can produce and
how many it can receive. So if we look here, the maximum length is 4000 tokens. And this
is shared between the prompt and the completion. So if we have a 500 token completion, we've
only got 3500 tokens for the prompt. So we need to limit how much data we send up in
the prompt. So I'm limiting how much data by saying we'll only send up the previous
10 questions and answers in our prompt to the bot. This will give it some context, but
it won't give us context from right to the beginning of the conversation. I'm assuming
that if you've been chatting for a very long time, the subject will have changed by the
time you're asking new questions. So let's scroll down and have a look at the main code
and see what actually happens. It is actually really, really simple. So don't get worried.
All we have is a thing that keeps track of the previous questions and answers. That's
how we build our context. We then prompt the user for a question. And then we call the
getmoderation function. If there are moderation errors, then we say to the user, your question
didn't pass the moderation check. And they should probably go and recheck what they've
written to make sure it's not horrible or nasty. Now we can build the context. So we
know that the chat is getting the previous questions and answers. So all I'm doing is
taking the last max context questions and reconstructing a human AI response. And then
we add in the new question. And then we ask them for the response from the chat bot. Once
we've got the response, we just append the new question and answer to our previous questions
and answers. So it will be in the context for next time. So get response is really simple.
It's exactly the same as the code that the open AI gave us for calling its API. The getmoderation
is also very simple. We just pass in the question and it will return us a set of categories
that it thinks the question has failed on. It could be hate or hate and threatening,
self-harm, sexual, sexual with minors, violence or violence, graphic violence. So that lets
you check that your users aren't trying to make the bot do something horrible. So that's
pretty much it for the code. It is really, really simple. There's not many lines. There's
only 121 lines of code. And most of that is boilerplate code that OpenAI gave us. So let's
try our chat bot out and see if it works. So we just do Python main.py. So how about
cocktails for a fancy champagne bar? So there we go. We've got a list of cocktails that
we could serve at our champagne bar. Let's ask it for the ingredients. And there we go
as well. So it's using the context from our previous question and answer in the new answer
and it's telling us what we should put in our cocktails. So that's pretty cool. It's
working really well. And it can answer new questions. So I'm pretty happy with that.
Let's check the moderation. Now it's pretty horrible testing moderation because you have
to type things that you really don't like. I'm going to blow out what I'm typing, but
I'm sure you can use your own imagination for how to test moderation. So there we go.
Our question didn't pass the moderation text. So that's really great. It blocks anything
that would be really horrible and stop our chat bot from producing things that you wouldn't
like. So that's it really. It is that easy. There's nothing to it. So you can clone the
GitHub repo and build your own chat bot. It's great fun and it's really entertaining to
see what's possible and really push the limits of what you can do. So thanks for watching
and I'll see you in the next video.