Welcome back to making chatbots.
Let's keep going.
So for the next part we're going to beef up the system message to something a bit more interesting.
System message.
You're a helpful assistant in a clothes store.
You should try to gently encourage the customer to try items on sale.
Hats off 60% off.
Most other items are 50% off.
For example, if the customer says I'm looking to buy a hat, you could reply, wonderful!
We have lots of hats, including several part of our sales event.
Encourage the customer to buy hats if they're unsure what to get.
So what you're seeing here is a few things going on in this system prompt.
We've got some facts that are being provided about the sales, the hats and other items.
Um, you've got an example, an example of if the customer says this, you could say that.
And that example both is a way to establish tone and style.
Um, and it's also a way to introduce more facts about hats, uh, into the conversation.
So this is all example of one shot prompting.
And you could argue Multi-shot prompting.
So we're giving it a few different sort of nuances of how to reply.
Um, and building that into the system message.
There are other ways of doing it that we'll talk about, or at least at least another way of doing it.
But this is one very effective way.
So we we add in that system message, and we're now going to have a chat with the with with the chat
bot.
So again we write our method of our generator chat um which takes a message and history because that's
what Gradio wants to call us with.
Um, and we first convert that into the format that OpenAI expects, um, by building the usual list
that you're familiar with.
I should also mention, I don't know if I mentioned this last time, that at the end here we have to
add in, of course, into that list the latest message that the user is sending that gets added to the
bottom as role user content.
And that message.
Then of course, we make a call that at this point, you have ingrained in your deepest memory the create
call and we have stream is true and we stream back results.
So here we go.
We'll bring that up.
We'll bring it up in a separate window again.
Why not.
And let's talk to our shopping assistant.
Hi there.
Welcome to our store.
How can I assist you today?
Are you looking for anything specific?
Say, uh, I'd like to buy some shoes.
Great.
We have a lovely selection.
While you're browsing, I want to mention we have a fantastic sale going on.
Most items are 50% off.
If you're open to it, we have some stylish hats that are 60% off.
They might be the perfect complement to your new shoes.
Would you like to take a look at both?
So I want to point out that that it's obviously figured out.
It's got the knowledge that we supplied to it in the system prompt.
But you hopefully will also notice that the sort of enthusiastic, effusive style that I used in that
system prompt has rubbed off in a way that it's communicating in this kind of, uh, very, uh, amiable
fashion.
Um, and that's a big part of this kind of one shot or multi-shot prompting when you set the tone,
give examples of how it should reply.
Uh, so let's keep going.
Let's take that system message and and add in.
If the customer asks for shoes, you could respond that shoes are not on sale.
Let's say should respond.
Should respond that shoes are not on sale today.
But remind the customer to look at hats.
So let's try this again.
Let's see how this does.
Uh, it's got another fact to add.
Uh, you could argue that that's.
Yeah, that's like another, uh, multi-shot prompt.
Let's say.
Hi there.
Are you looking.
I'd like to buy some shoes.
That sounds great.
I should mention shoes aren't on sale today.
But while you're here, have you thought about checking out our hats?
You've got to feel sorry for this poor customer who's going to get repeatedly pitched hats.
Um, so you can see again how it's established, um, that shoes aren't on sale, but that hats very
much are.
And that is an example, then, of Multi-shot prompting, uh, in that we're giving it more examples
to learn from.
Um, so another thing that we can do that is interesting, um, is that whenever you've seen these constructions
so far, you've always seen us beginning with the system message.
The system messages come at the top.
But in fact, with the OpenAI call, there's you're not constrained to have the system message at the
top.
You can add in more system messages as you go.
Um, and so for example, one of the things we can do, um, uh, and let me apologize for some very
hacky code here that I will not recommend, but it's here to show the example.
It's not the way that you should do it in practice, but what we could do is when we're building this
chat generator, we could put in here if this current message that the user is sending us contains the
word belt.
And you can see in a rather unawesome way, I've just looked for the string belt.
Of course, I should be testing whether it's the full word, and I should be thinking about uppercase,
lowercase and so on.
I'm not doing any of that, which is very naughty of me, but it shows the point.
So if belt is in the word message, it's going to add into this set of messages.
Another system message saying for added context, the store does not sell belts, but be sure to point
out items on sale so that will be then added in to the prompt if the user asks for a belt.
So let's give that a try and bring this up here.
Hi there.
Welcome to the store.
I can assist you.
I'd like to buy a belt.
I'm sorry.
We don't carry belts.
However, we have fantastic items, including hats.
60% off.
So there you go.
There you go.
It's, um.
Uh, definitely pays attention.
You can see that when the system message is added in as another row in this, uh, in this list of messages,
it pays attention to it.
And that gives us the opportunity to add context into the conversation.
And this is a, uh, whilst it is, of course, uh, very kludgy code to be detecting a word, a substring
like that, you can imagine that you could beef this up to be a bit more robust.
You could properly you could have a little dictionary which looks for particular words.
And when it finds them, it could use them to then enrich the the context in the right way.
So it gives you a little, a little ability to be looking things up and adding them into the context.
Now, you may be familiar with with some things about Rag, and you may be aware that that is a lot
of what rag is about.
Rag is about finding extra information that's relevant to the prompt, and adding it in to the context
of the message that gets sent to the LM.
Now, of course, Rag does that in a much more sophisticated and intelligent way than this hokey piece
of code right here.
But you can think of this as a as a light, a baby version of rag, and as an exercise for you.
You can certainly beef this up a bit, and at the very least, use regex to make it look for a particular
word.
Maybe have a little dictionary that has the the words, the different items in the store together with
their price so that you could add that in as a system message to give it more context.
You could try that out and see that you could build a chatbot that actually knows about the prices of
the goods in its store, and that would be pretty cool.
So that wraps up this particular experiment.
I do just want to mention, I alluded earlier to the fact that there are other ways of doing Multi-shot
prompting other than shoving it in the system prompt, and the other way is that you can have a user
assistant, user assistant set of messages that hasn't actually happened.
You can have a fictitious exchange between the user and the assistant that you include in the conversation
before the current conversation, and use that as a way to prime the LM with similar conversations,
so that it gets a sense of how it's responded to other questions.
You can use that again, both to train it on style and also to supply extra facts.
So there could have been an earlier interaction when there had been a question about a belt, and the
assistant had already replied that there are no belts in the store, and it would have learnt from that.
So either technique, they have pros and cons, whether you supply it in system prompts or whether you
give example user assistant interactions for it to have as part of the input context for it to to be
able to absorb.
Um, and my ask to you is to try them both out.
So update this so that it uses user assistant interactions instead of a system prompt and see how that
works.
See if you think you get a better or a worse clothes store assistant.
And then also make this change to make this a whole lot more robust, have a dictionary of different
items in the store, look up their prices or their sale amounts, and then add that as context into
the conversation so that the assistant responds with some expertise and have fun doing it, and I will
see you for the next video to wrap up this day.