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