View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Related Content
Transcript

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


HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening


Published

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts