Well, the first thing you're going to notice is that I don't have a notebook open for you.
And that's because this time we're just going to be looking at code.
Uh, no notebooks for this recording.
For this video, we are going to look at the actual finally, the actual agent framework itself that
we're writing that we've, that we've built, that we're going to run it in.
And it sounds like something so fancy, but in fact, it's going to be incredibly simple.
And it's the reason why I didn't want to use an off the shelf kind of package, because I just wanted
to show you that there's nothing more than connecting together, different llms and bits of Python code
to carry out a more advanced function.
Now, typically when you talk about these sorts of agent frameworks, what you're looking for is you're
looking at something that can handle the database, uh, database connectivity, in our case to our
Chrome data store memory.
So we're going to want some sense of persistent memory.
Um, that can that can survive between different chats or of different instances, so we'll understand
how memory is going to work.
And also stuff like logging I mentioned is sort of good, good practice to be able to understand what's
happened in your environment.
And then there's also going to be some user interface concerns that we're going to want to build a Gradio
UI that's going to be able to use this framework.
So it's going to have to to handle that.
But that last one we're going to get to in the next set of series.
Uh, so for now we're going to look at our, at our framework.
Just before that, a couple of things to point out.
First of them, when we looked in our agents folder, we've looked at each of these different classes.
That represents a different unit of work.
And you'll notice that each of them were subclasses of agent.
Uh, and you may have wondered, what is that?
What is this agent that he's subclassing is there must be all sorts of functionality in there.
Well, actually, no, there's very little functionality in this superclass.
Uh, it is actually just an abstract class.
Uh, which is able to log a message.
And the way that it logs a message is it does logging.info.
I imagine you're familiar.
You've used Python logging, probably.
And so you're pretty.
Familiar that logging.info logs, uh, to to to wherever you set up a logger for, um.
And.
The one thing that I'm doing that's a bit different here, um, is that I'm going to.
Add in the name of the subclass of the agent.
That's that's, uh, that's.
Actually, uh, sending this log message and we're going to give each one a color.
So that we can see, uh, what which agent is doing what?
And for example, if I go back to, uh, or if I go to the specialist agent, you can see that it's
given itself a name.
Of specialist agent.
And its color is red, so that when it logs, it's going to.
Appear in red.
And you can see that you might have noticed that I didn't do.
Logging.info I did self dot log.
And that's so that I can call the log message in the superclass.
So you know, there's really nothing fancy to this agent superclass.
It just meant that we could log consistently.
You could equally well have just included that function in the different classes.
Nothing particularly special about the agent.
All right.
One other bit of groundwork to do is to show you this JSON file called memory JSON.
And it is a simple piece of JSON.
And if it didn't exist then then you'll see it will it will create it manually.
Let's open it with the editor, not with the fancy one, but with the normal editor.
And this is what it looks like.
It is a list.
You can see it's got a list on the outside of it, a list of JSON blobs.
And each JSON blob is something which may look familiar to you.
Do you recognize what it is?
Yes, it's of course, an opportunity object turned into JSON.
It's got a deal which has a product description, price and URL, and then it has the estimate that's
come from the model and it has a discount, which is quite simply this number minus this number.
Oops.
That number.
Ha.
Uh, it's how much is this being offered at a discount to what we think it's really worth?
So this memory has three deals that have been surfaced.
Uh, now, storing it in a JSON file like this is perhaps not the most industrial strength.
If that went into a database, then it's something that could handle multiple of these agent frameworks
running.
But we only have one agent framework that will only write to this memory, uh, once when it does its
work.
So that wasn't strictly necessary, but of course it could go in Chrome easily.
We've got a database hanging around already, so you could certainly take that as an exercise if you
wish.
Um, that would certainly be a good, good production improvement to make.
But anyway, the time has come to look at the actual, uh, the, the framework itself, the agent framework.
Uh, so let's do that right away.
Um, and I want to make sure I bring up the right class so that I don't give too much away of what is
still to come.
Here it is.
Deal agent framework.
Uh, okay.
So the, uh, agent framework, um, is something which, first of all, it sets up logging.
This is something which is standard Python stuff that will make sure that when someone does logging.info,
um, it will get sent to standard out and it has some, some structure to it.
So this is a sort of standard, uh, um, boilerplate Python stuff to make sure we log properly.
And then this is the deal agent framework.
It's got coded in it, the database, the thing that I accidentally left off a couple of times, uh,
so that's coded in here along with the name of the memory file.
When it starts up, it will begin logging.
It will make a log message itself.
It will load the env.
This is something that we, of course, always do in all of our Jupyter notebooks.
So this needs to do it now.
It will, it will create or it will access the database.
Um, it will read in memory from that JSON file.
Uh, it will get the products collection and it will then set the planning agent, uh, initializing
it with the collection that it needs to look at.
And then it will say, I'm ready.
This this method here read memory does exactly what it says on the tin.
It, uh, you can see I haven't hard coded the name of the file.
Well done, well done.
Me?
Uh, so it loads this in from JSON.
If it doesn't already exist, then it just returns an empty one, which is completely fine, because
there's also a function.
A method here writes memory, which writes out these opportunities to, uh, the the memory file.
There's a log, uh, a method here to send a log message with its own agent framework tag.
Then there's a run method that actually kicks off the planning agent.
Uh, and uh, once it gets back a result, it says it calls self dot planner.
Plan that.
Of course, that's the big moment.
That's when we we actually set this thing going.
And look it passes in its memory.
It passes in the memory of opportunities that it knows about.
So in that goes uh, and then it logs the results.
And if the result wasn't none, remember that the planner might return none if it doesn't find anything
new.
But if it was a real result, then it adds it to the memory and writes the memory and returns the result.
And that is it.
This last piece here is coming up in the UI to the next time, but for now, that is the end of the
framework.
So how do we run this framework?
Well, we run it from the command line.
This is now for reals.
This is no longer this is all Python code.
We're outside the Jupyter world and we're using this interface for convenience.
Really.
But you could be using VSCode or something like that.
The way to run a terminal is we press the plus button here.
We can also do it in JupyterLab and we can open a new terminal like this.
This is a you can also, of course be doing this however you like to do it.
Um, it could be an anaconda prompt or it could be you could be using.
If you're using a mac, then you can just bring up a new terminal window.
Um, but I am first going to have to activate my conda environment.
Um, so I do conda activate LMS, uh, which will mean that as you're now very familiar, uh, my environment
changes from base to LMS because I'm now in that Anaconda environment, and I really think that's it.
I should now be able to say Python deal agent framework.
Spell it right.
Dot py.
And that should really be it.
There is one more thing I need to show you before I do that though.
I almost forgot just as well.
It's just this very last few lines here.
You might have been wondering what's going to happen if I do that, if I don't show you this.
So the very end, we of course, make it clear that if this is being run from the command line so that
this file is main, then we instantiate, we create a new instance of deal agent framework.
And we call this run method that we just looked at.
That's the little missing link there just to explain okay okay.
With that.
Now I should be able to run this.
Take a deep breath.
Let's do it.
A long pause while it thinks, huh?
All right.
Let's see what's going on.
The agent framework says it's initializing.
The planning agent is initializing scanner.
Agent becomes ready.
The ensemble agent gets ready.
So then it's a bit hard to see this.
Read the specialist agent, which is connecting to modal, gets ready.
And then the frontier model gets ready.
It has to load in that sentence transformer.
You can see it's happening there.
And then the random forest agent gets ready as well.
And that's done.
The ensemble is ready, the messenger is ready.
And everything in blue.
Here it says Agent Framework is ready.
So it went through instantiating and creating all of our agents and setting them all up.
They've loaded their model weights that they've the single one the specialist model has connected to
modal.
And they have now, um, all prepared themselves for action.
Model weights have been read in and it's kicked off a run.
The scanner agent you can see here, uh, received there's a there's an error there.
I'll improve that.
It's not printing the actual number of results.
It's printing needs an F before it.
Uh, I will have fixed that before you see it.
Uh, so the scanner agent is making that call.
Um, and now the planning agent is pricing up the deal, and it's now sitting with modal while my machine
warms up.
Uh, but as they say on those cooking shows, uh, if you know what I mean, uh, you have those cooking
shows when they put the thing in the oven and they say, and here's the one that I put in earlier,
and they pull that out of the oven.
I always think that's a devious trick when they do that.
But you're going to have to trust me that I did just do this.
I just kicked it off a few minutes ago and it ran over here, and that's where I left it, and it finished
up its thing.
And you can see that basically it for each of the five deals that returned, it made a series of calls
to all of the different models.
You can read through each of this.
The ensemble model returns a number.
Um, so we should we should work back.
First of all, the specialist model calculates a number and then the frontier model calculates the number
with the Rag lookup and then the random forest model.
And then the ensemble model.
And that goes back to the planning agent.
And so you can read through all of this.
And at the end of this it completed and it sent a push notification.
And I did indeed get a push notification about this particular deal that it found, which was a certified
refurbished laptop boasting a 13th generation i5, which the estimate was $925 for that, which was
a very healthy discount to what it was going for at $560.
So there we have it.
That was the run of our agent framework running.
And yeah, it's incredibly satisfying to see that happening.
I love absolutely love watching it and could watch it all day.
I have to say it's it's hugely enjoyable.
Look, I didn't even need to put that to bring that one out of the oven, because the one that we put
in the oven is now cooking up.
You can see once modal is warmed up, it's quite fast in how it turns around the different things.
So it's going through there.
It's come up with something.
And of course, what it's proposing is different to what it proposed a moment ago.
And I just got the notification on my phone and no doubt on my watch.
So we just watched we just saw the full framework running and me getting the notification.
And you can read through this trace and see how each of the agents are collaborating to solve this problem.
Um, and so all that remains for this is to make it really autonomous so that it's running all the time.
And while we're doing that, we might as well slap on a great UI because we know how to use gradio and
we know how good it is.
And all of that is what we're going to do tomorrow.
But for now, I'm going to let you, of course, enjoy this.
I do hope that you're running this a few times because it's so satisfying.
Honestly, I love it.
Hope you're finding just as much enjoyment as me, and I will see you for the slides to wrap up the
day.