WEBVTT

00:00.480 --> 00:06.400
And so this is the moment now when we make the transition from being a scientist to an engineer.

00:06.440 --> 00:08.040
We've done our experimentation.

00:08.040 --> 00:10.640
We've explored some agent configurations.

00:10.640 --> 00:18.160
We've we've figured out that this combination of researcher and the trader makes sense and is working

00:18.160 --> 00:19.120
well for us.

00:19.240 --> 00:23.240
And now we're going to move to building our Python module.

00:23.360 --> 00:24.480
So I want to take you there.

00:24.480 --> 00:27.520
It's called traders Pi and it is quite long.

00:27.600 --> 00:28.440
There's quite a lot here.

00:28.440 --> 00:34.200
But it is just everything we've just done in that lab turned into a module.

00:34.360 --> 00:36.720
There's, uh, there's nothing else to it.

00:36.720 --> 00:38.440
So you already know this stuff.

00:38.640 --> 00:41.440
Um, it's just presented differently.

00:41.640 --> 00:45.560
Uh, and it's one of the reasons, again, it would be a lot to take on board to go through all this

00:45.560 --> 00:46.520
code for nothing.

00:46.520 --> 00:50.880
But if you've built up to it in a lab, then you're ready to construct this.

00:51.360 --> 00:54.080
So this is this is the there's imports.

00:54.080 --> 00:55.480
There's load dot env at the top.

00:55.480 --> 00:56.080
Of course.

00:56.200 --> 01:00.040
Now this here is one thing I need to draw your attention to, because it's the one thing that you might

01:00.040 --> 01:01.320
need to come in and change.

01:01.520 --> 01:08.600
If you're using the paid market data MCP server, then you need to turn this to being your directory

01:08.600 --> 01:09.560
where that's running.

01:09.720 --> 01:13.040
And sorry to give you something to come in and do in the middle of this code.

01:13.040 --> 01:15.880
But but that's just the one thing that is coded there.

01:15.880 --> 01:21.720
And I don't know where you will have git cloned that repo, so I can't speculate on that for you.

01:21.720 --> 01:23.400
So I'll let I'll let you do that.

01:23.720 --> 01:28.640
Um, and then basically this makes a determination whether you're going free or not based on if you've

01:28.640 --> 01:34.480
got an API key for the for the paid product in your EMV, then it assumes you want to use that.

01:34.480 --> 01:37.680
So only set that API key if you actually want it to use it.

01:37.760 --> 01:40.720
Uh, and again, you know, it's it's $0.01 for every lookup.

01:40.720 --> 01:43.400
So just just be aware of that.

01:44.040 --> 01:47.360
So we're also going to get the deep seq API key and the Google API key.

01:47.360 --> 01:53.640
Because in time we're going to be using a combination of open AI and deep SEQ and Gemini together to

01:53.680 --> 01:54.400
do this.

01:54.400 --> 01:55.800
But you don't need to do that.

01:55.840 --> 01:58.320
And for now it's we're not we're not going to touch this today.

01:58.320 --> 01:59.400
That will be for tomorrow.

01:59.560 --> 02:03.040
Um, and I've got the base URLs coded here so that we can use it.

02:03.360 --> 02:05.520
And Max turns is something I'm using.

02:05.720 --> 02:10.960
Uh, you may remember this from, from when we used landgraaf that Landgraf would, would error out

02:10.960 --> 02:13.150
if it took more than 25 turns.

02:13.150 --> 02:14.950
So this is something to control.

02:15.270 --> 02:15.550
How?

02:15.710 --> 02:20.110
How deep down, the agent interactions will go before it will throw an error and say.

02:20.150 --> 02:21.350
Okay, enough is enough.

02:21.950 --> 02:22.590
All right.

02:23.470 --> 02:30.470
So the way with OpenAI agents SDK that you specify, if you want to use a different client to swap out

02:30.470 --> 02:37.230
from OpenAI is this way you create an async OpenAI instance like this.

02:37.270 --> 02:41.630
And so this is showing the Gemini client as an example.

02:41.870 --> 02:52.030
And we will uh, use that potentially in this case for, for uh, Gemini Flash right there.

02:52.630 --> 02:54.790
And somewhere there should be a deep seat client too.

02:54.790 --> 02:55.550
I don't see that there.

02:55.550 --> 02:56.990
So we should put that in right now.

02:57.470 --> 03:00.470
Let's copy this and make a deep neck client as well.

03:02.190 --> 03:03.270
Deep seek.

03:05.630 --> 03:06.390
Client.

03:07.710 --> 03:11.150
Uh, and this should be, of course, the deep seek base URL.

03:11.390 --> 03:14.190
And that should be at cursor has helped us out there.

03:14.190 --> 03:15.270
That is perfect.

03:15.310 --> 03:16.110
Okay, good.

03:16.110 --> 03:17.670
We now have our clients.

03:18.510 --> 03:19.150
All right.

03:19.150 --> 03:26.070
And then this next part right here is simply setting up the MCP servers, just as we did in the lab.

03:26.390 --> 03:28.990
Uh, the trader MCP server parameters.

03:28.990 --> 03:34.950
It's going to use accounts and it's going to use memory if we're using free market data.

03:34.950 --> 03:40.310
If we're not if we're using paid market data, then we're also going to add in the paid MCP server.

03:40.830 --> 03:47.390
And otherwise if we're using the free one then we will just we will collect the tools later.

03:48.070 --> 03:55.750
And then for the researcher we're going to use the MCP server for fetch and the MCP server for brave.

03:56.150 --> 03:58.110
And that is set up our params.

03:58.110 --> 04:04.790
We've now organized the params for our MCP servers into different lists, and we're ready to get going

04:04.790 --> 04:05.670
with the code.

04:05.710 --> 04:07.590
All right, so now we've set up our params.

04:07.590 --> 04:12.110
We now have three utility functions before we get to the meat of the module.

04:12.310 --> 04:14.590
First of all getmodel very simple.

04:14.590 --> 04:21.590
You pass in a model name and it's going to return one of these OpenAI chat completions models or string

04:21.700 --> 04:23.700
if it's if it's just GPT four or mini.

04:23.820 --> 04:29.460
So so if you pass in deep chat, it's going to create one of these guys using the deep sea client.

04:29.860 --> 04:33.620
If it's Gemini, it will create one of them using the Gemini client.

04:33.660 --> 04:37.100
Otherwise just the model name just to simplify some of our code later.

04:37.660 --> 04:39.980
And then this is Get researcher.

04:39.980 --> 04:42.020
This is exactly the same as we had before.

04:42.060 --> 04:44.860
This gets our researcher agent.

04:45.060 --> 04:51.060
It's got the system prompt for the researcher uh including the current date just as we said.

04:51.300 --> 04:57.180
And then it creates the agent and we pass in the MCP servers, just as we did before.

04:57.220 --> 05:00.540
We populate them and we return the researcher.

05:00.740 --> 05:08.660
And then the final utility is to wrap this in a tool, return the researcher as a tool, because we're

05:08.660 --> 05:11.660
going to use it in the request response kind of mode.

05:11.980 --> 05:18.900
Um, and so for this we this just calls the get researcher function and then returns that as tool.

05:19.100 --> 05:23.940
As tool being the way that you convert, uh, the, the agent into a tool.

05:24.420 --> 05:29.450
And we give it a name researcher and we give the description of the tool just as we did before.

05:29.690 --> 05:32.450
And that is the end of our utilities.

05:32.450 --> 05:35.770
We're in to creating our class trader.

05:35.810 --> 05:37.250
Okay, here we go.

05:37.250 --> 05:38.810
So a trader has a name.

05:39.210 --> 05:40.970
It has an underlying agent.

05:41.010 --> 05:43.050
It has a model name we'll set it up with.

05:43.330 --> 05:49.770
And then it has something that that it has an account that will populate when we read its account details.

05:49.970 --> 05:53.090
We store its MCP servers right here.

05:53.090 --> 05:56.770
And then a combination of the MCP servers of both.

05:56.770 --> 06:03.610
So this here we're actually instantiating, we're creating an MCP server studio instance for each of

06:03.610 --> 06:07.010
the different parameters uh, by iterating through them.

06:07.010 --> 06:10.890
So they are held at the instance of trader okay.

06:11.490 --> 06:21.210
And then we have here something which is going to, uh, set the system prompt for our trader agent

06:21.410 --> 06:25.810
and it says you are its name, a trader on the stock market.

06:25.810 --> 06:27.490
Your account is under your name.

06:27.730 --> 06:34.170
And now this this stuff is basically the same as what we had in the lab with one tiny difference, which

06:34.170 --> 06:34.890
is that I'm.

06:34.890 --> 06:39.850
I'm saying that you actively manage your portfolio according to your strategy, but I'm not actually

06:39.850 --> 06:43.690
getting it to put its strategy in the system prompt.

06:43.930 --> 06:45.130
And so it's not going to do that.

06:45.130 --> 06:47.090
It's going to put that in the user prompt.

06:47.290 --> 06:49.530
And why is that you may ask.

06:49.770 --> 06:56.970
Well that's because I have wild plans in the next uh, time in the in tomorrow, we might update this

06:56.970 --> 07:02.410
so that it can actually change its strategy as part of what it does, because I think that would be

07:02.410 --> 07:06.890
really cool if it has the ability to actually change its own strategy.

07:07.130 --> 07:09.970
Uh, we'll, we'll see if, uh, if we want to do that.

07:10.290 --> 07:12.330
Um, but, um, yeah.

07:12.330 --> 07:20.770
So, so, um, there's then get tools, which is simply going to, uh, populate the tools that this

07:20.770 --> 07:21.210
has.

07:21.450 --> 07:28.970
And if you're using free market data, then this is where it's going to to, uh, use the MCP client

07:28.970 --> 07:35.170
that I wrote and then use that to build some tools that we can call directly from our LM.

07:35.850 --> 07:41.930
The Researcher is getting that, using that utility function to get a tool that wraps our researcher

07:41.970 --> 07:42.570
agent.

07:42.970 --> 07:45.090
And then we return those tools.

07:46.290 --> 07:52.970
This this function connect to MCP servers iterates through all of our MCP servers and calls connect.

07:53.890 --> 07:54.650
All right.

07:54.690 --> 07:57.250
And then init agent does those.

07:57.290 --> 07:58.650
The connection I just said.

07:59.170 --> 08:02.850
And then it gets the instructions gets the tools.

08:02.850 --> 08:08.210
And then it creates the underlying agent that it stores in the in the instance variable agent.

08:08.410 --> 08:10.170
And it is an agent with its name.

08:10.490 --> 08:15.410
It could be Alice, it could be whatever with its instructions with the underlying model.

08:15.650 --> 08:23.010
And this is where it calls that utility function to, to to uh to get model given its name, the tools

08:23.210 --> 08:26.370
and the MCP servers are all set up.

08:26.370 --> 08:32.850
So now the underlying agent instance variable will now be populated with an agent.

08:33.570 --> 08:39.010
And it gets the account to the starting position and it returns the agent.

08:39.050 --> 08:39.690
Okay.

08:40.330 --> 08:41.250
So we're almost there.

08:41.250 --> 08:43.130
We're almost ready for action.
