Okay.
This is the single most important moment of the entire lab.
So it's the chat function, and I've spaced it out a bit to really explain what's going on.
It's a more powerful chat function that includes the calling of a tool.
It starts exactly the same way as it did before.
We create some messages.
Role of system.
The content is the system prompt.
We add in the history from before.
Role of user.
The content is the user's message.
And now do a pretty standard trick of looping again and again and again until a variable called done
gets set to true.
So it's going to keep going until done is true.
What is it going to keep doing?
It's going to call open AI.
And this is the usual thing OpenAI.
Create we're familiar with that.
We pass in the model.
We pass in the messages this set of of messages.
But look we also pass in Tools.
Tools is that JSON blob, that structure of JSON, which is going to be sent to the LM saying, these
are all the things you can do.
This is the description of everything you can do.
All right.
And back it comes into response.
And what comes in response might just be the output to go to the user, or it might be a response that
indicates that tools need to be called.
So that is what we do next.
We look at response choices.
And this tells us whether the LM is finished and it's done or whether it wants to call a tool.
So if the finish reason is tool calls, it wants to call a tool.
Then we we look at the message itself, we pluck out the tool calls from it.
And this this is where we call that function that we just wrote, that clever function, that sneaky
function.
Right here it is.
Remember it handle tool calls.
This is the one that is the glorified if statement, the one that has the fancy if statement.
But there's also this more silly version here that just has an actual if statement.
But it's going to call this.
So back we go.
So again if it wants to call tools then we do that.
We handle the tool calls.
We call the tools.
And then into the set of messages.
We put what it wanted to do and the results of doing it.
And then we will loop back and call a second time.
And we will keep doing this until the finish.
Reason is not tool calls.
It does not want to call tools with whatever, it's just responded, in which case we return the content
because that is going to be the actual response to the user.
I hope that made sense.
If not, go through it slowly, carefully until it does.
This is this is pivotal.
This is how the whole thing hangs together.
Now, I know what you're thinking.
You're thinking it's all very well showing us all this code.
But does it actually work?
Well, let's find out.
Let's run the code.
Here we have it.
Let's say hi there.
Hello.
How can I assist you today?
What's your job?
I'm currently the co-founder and CTO of Nebula.
Very nice.
Uh, do you have a patent?
Yes I do.
Very nice.
Uh, who is your favorite musician?
That's what I want to know.
I hope you, uh, heard that success.
So it says.
I'm not sure about my favorite musician.
I appreciate a variety.
If you have a favorite, I'd love to hear about it.
It called the tool.
You heard my phone.
Go.
We have success.
The agent was able to take that JSON.
It knew that this was a tool that it could call.
It was able to respond with the finish.
Reason was was tool calls.
And that caused us to to interpret that to make the call to R our function handle tool calls, which
had our fancy code that ended up calling this function that ended up calling pushover, that then sent
the push notification to my phone and it all hung together.
All right, let's keep going.
I'd like to get in touch.
That's great to hear.
Please share your email address.
I'm at Edward donna.com.
I know you're expecting it.
Now.
The, uh, the magic's worn off.
You're not impressed anymore?
Well, I think that's really cool.
So it has indeed, of course, notified me about my own email address, which is great.
And so it shows you that it does hold together.
Uh, and you are indeed able to have these interactions and get it to text you.
Okay.
That's great.
I hope you enjoyed it.
But next it's all about doing this for you and actually deploying this live to production so that you
can serve your own avatar on your personal website.
So if you've followed everything so far, then many congratulations.
If you haven't, then also congratulations because it gives you this great opportunity to go through
work through this until you do.
And if I can help.
Email me or contact me anytime.
LinkedIn with me.
Message me so that I can help you out.
So what I want to do now is show you how you can deploy this application in production for yourself,
so that you can have this as your virtual resume.
Surely this is the future of resumes.
No longer will we have profiles or CVS resumes where you list out your skills and experience, but rather
you'll have a chat bot that people can interact with to learn about your career.
And what better way to highlight your your AI abilities and your abilities to work with Agentic AI than
to have an agentic solution up on your website that will allow you to interact with people and talk
about your career.
So let me show you some steps to deploying this.
So the first thing to do is to put the code into a Python module.
And I have done that right here for you in App.py.
And you'll see that this has all of the stuff you would expect.
It's got the same information in here, but organized into nice little functions at the top of the functions
that cover the tools that we call.
And then there is a class me as in me or or you.
And that's something which has the handle tool call that we know so well that's right here.
And it has the system prompt and this loop that we just went through, this chat loop that has the while
not done it calls open I create, it passes in the tools.
I don't think I mentioned this actually, so I'm pleased.
I pause for a minute here because I do want to show this one more time.
We call chat completions create.
We select the model, we pass in the messages, and we pass in the tools JSON, the description of the
tools that it can call.
And with what comes back, we take choices zero.
And we look at the finish reason back in the response.
And we see whether or not that is tool calls.
And if so, then we loop through to make sure that we run all of the tools before calling it again to
get the final response.
So this is so critical, this this chat with its loop that I do encourage you to take a look at it.
And now at the bottom here we have the Gradio code.
We've got it set up here so that if this is just run as it is then it will create a gradio chat interface
with showing me chat and with the messages and it will launch it.
So what I can do now, in fact, is I can bring up a terminal which you may remember.
You press control and the backwards tick mark.
I can go into the week one folder.
Here we are and I can now type.
Remember you don't type Python.
You type, you run to run something.
In this way you've run app.py.
And if I run that then it should come up.
So this link.
Now on this link I can press control and click it to bring it up.
Up it comes.
Let me make that a bit bigger for you.
This is now an app running on my box and I can say hi there.
Hello.
How can I assist you and, and uh, do you have a patent and so on and there we go.
Okay.
So so that works going back here, what we now want to do is look at actually deploying this.
So there are various ways you can deploy an application like this, but one of them that's particularly
simple and elegant is brought to you by the lovely AI company Hugging Face, which also happens to be
the company that owns Gradio.
That beautiful, framework hugging face has something called hugging face spaces, and hugging face
spaces is a way that you can deploy things really simply.
And then if you wish, you can also embed them in your own home page.
So it gives you a really nice, simple way to do it.
So this is how you do it.
You first have to go to Hugging Faces website and set up your own account if you don't already have
one.
So you may already have a hugging face account, but if not, you need to set one up.
Once you've done with that, you go into the foundations folder in a terminal and you run this command
Gradio deploy.
It's then going to ask you a series of questions, a bunch of simple questions to which I've got the
answers you have to give right here.
And I'll go through and do it right now.
But I'm not going to do it properly, because one of the questions it's going to ask you about some
secrets that you'll say yes, and you're going to provide your OpenAI API key so that it can use that
in the deployment.
And you can also give your pushover tokens as well.
And then I have got the rest of the instructions right there for you.
And once that's done, it will be deployed into a hugging face space.
And you can choose whether that is public or private.
You can have it be private if you don't want other people using your key.
Although as I say, it's super cheap, but people would have to.
It would have to go viral for you to have any kind of significant spend.
And of course, make sure that your OpenAI account, you never want to have that auto refilling so that
if that ever were to happen, if your app did go viral, then you would be protected.
And anyway, OpenAI has limits, as you probably know.
So, uh, let's go and actually do this.
Let's run Gradio deploy.
So I'm going to bring up the terminal now, and then we will go and do this ourselves together.