WEBVTT

00:00.160 --> 00:04.640
Welcome back to cursor, where we are ready to go with OpenAI agents SDK.

00:04.960 --> 00:08.240
So you'll see that we have the folders on the left here, the directories.

00:08.240 --> 00:13.680
And we are now going into number two OpenAI and into lab number one.

00:13.840 --> 00:14.760
Here it is.

00:15.440 --> 00:18.080
First look at the OpenAI agents SDK.

00:18.440 --> 00:21.400
I've included a link to the docs for OpenAI.

00:21.520 --> 00:28.080
And I will say that it does almost concern me showing you these docs because they're so clearly written

00:28.080 --> 00:31.400
and so easy that I rather wonder what my job is in all this.

00:31.640 --> 00:34.920
I will say that for the next few weeks, I think I definitely have a job.

00:34.920 --> 00:39.080
It gets a lot harder, but for this one you'd be doing well just to read the docs and it'll be very

00:39.080 --> 00:39.760
clear to you.

00:39.760 --> 00:42.120
So anyway, I'll keep going regardless.

00:42.400 --> 00:44.480
So let's do some imports to get us started.

00:44.480 --> 00:49.360
So we will say from dot env import load dot env.

00:49.520 --> 00:50.680
We know we got to do that.

00:51.000 --> 00:55.320
And now we're going to to do some imports from OpenAI's package.

00:55.480 --> 00:57.760
And their package is called agents.

00:58.080 --> 01:00.840
And from that package we are going to import agent.

01:01.160 --> 01:03.240
We're going to import this thing called runner.

01:03.920 --> 01:06.060
And we're going to import this thing called trace.

01:06.260 --> 01:10.180
These are the three things I told you about that we were going to use today.

01:10.700 --> 01:13.380
And the OpenAI package is called agents.

01:13.380 --> 01:14.900
That's that's the one I do.

01:14.940 --> 01:20.460
I do sometimes think that it's a bit strange that they use such a generic name for it, because I imagine

01:20.460 --> 01:24.260
a lot of people use the name agents for, for their own packages.

01:24.420 --> 01:28.660
I often do, and so you have to then start messing around with, with different kinds of imports.

01:28.660 --> 01:29.620
But still they did.

01:29.660 --> 01:30.500
Its called agents.

01:30.500 --> 01:35.380
And this is, this is how we import these three key things agent runner and Trace.

01:35.900 --> 01:36.300
All right.

01:36.300 --> 01:37.860
So the usual starting point.

01:37.900 --> 01:39.980
Hopefully this is second nature to you.

01:40.020 --> 01:46.860
We need to run load dot env with override is true to bring in our environment variables they are in

01:47.300 --> 01:47.620
okay.

01:47.660 --> 01:49.500
We are now going to make an agent.

01:49.500 --> 01:53.620
We're going to create our first agent, our first agent from a framework of this course.

01:53.780 --> 02:01.740
And so we're going to have an agent and say agent equals it's going to be an instance of the class agent.

02:01.740 --> 02:05.700
So we're creating an instance of agent OpenAI's class.

02:05.700 --> 02:06.980
Now it has some parameters.

02:06.980 --> 02:08.580
We need to use some arguments.

02:08.580 --> 02:10.470
And one of them is name.

02:10.470 --> 02:13.430
We give it a name and we're going to call it the jokester.

02:14.070 --> 02:16.750
And then we give it something called instructions.

02:16.950 --> 02:18.830
I like the way this is just filled in.

02:18.830 --> 02:21.310
I'll just press tab and put me out of my misery.

02:21.430 --> 02:24.070
So we pass in some instructions.

02:24.150 --> 02:30.030
The instructions are basically the system prompt for this call to LM.

02:30.350 --> 02:36.430
So each of these agents you can think of as being an LM with one particular system prompt designed to

02:36.470 --> 02:38.830
organize it around one particular task.

02:39.070 --> 02:41.670
And so our system prompt called the instructions.

02:41.670 --> 02:44.030
In this case you are a joke teller.

02:44.030 --> 02:47.950
And you also pass in a model which in this case is GPT four mini.

02:48.190 --> 02:53.790
And you might be thinking in your mind a very good question, which is AI, does this mean that OpenAI

02:53.830 --> 02:57.870
agents SDK always uses OpenAI's models?

02:58.070 --> 03:02.350
You might, you know, that might sound like it's a sensible conclusion, but no, definitely not.

03:02.390 --> 03:10.070
The agents SDK is very much able to work with many models, as we will later in this week, explore

03:10.110 --> 03:13.950
using different models with the OpenAI Agent Agents SDK.

03:14.090 --> 03:15.210
It's not opinionated.

03:15.210 --> 03:19.970
You can use different models with this same framework, but by default, if you just pass in a model

03:20.010 --> 03:22.330
name, it assumes you mean OpenAI.

03:23.210 --> 03:25.730
Okay, so now we're going to execute this.

03:25.730 --> 03:28.010
And now we have our first agent.

03:28.050 --> 03:29.450
Let's just look at what it looks like.

03:30.290 --> 03:32.930
It is an agent with name jokester instructions.

03:32.930 --> 03:33.930
You're a joke teller.

03:33.930 --> 03:35.250
And it's got some things here.

03:35.250 --> 03:36.090
Hand offs.

03:36.090 --> 03:36.930
You remember.

03:36.970 --> 03:42.290
That was the concept that relates to the interactions between agents.

03:42.410 --> 03:47.730
It's got a model and it's got a bunch of settings and other things that you can, you can look at in

03:47.730 --> 03:50.170
your own time to be worth looking at.

03:50.170 --> 03:54.810
It has guardrails, input guardrails and output guardrails that we will look at later.

03:55.330 --> 03:55.890
Okay.

03:56.050 --> 04:01.010
So I mentioned that runner run is the way to run one of these things.

04:01.010 --> 04:06.290
So we can say perhaps results equals runner dot run.

04:06.450 --> 04:09.730
And when you run an agent you you pass in two things.

04:09.730 --> 04:13.170
You pass in the agent itself to be run by the runner.

04:13.170 --> 04:15.930
And you pass in what is essentially the user prompt.

04:15.930 --> 04:19.030
It is the message that you are sending the agent.

04:19.070 --> 04:24.950
Let's say tell a joke about autonomous agents.

04:25.230 --> 04:27.110
Autonomous AI agents.

04:27.550 --> 04:30.350
Okay, so now we are going to run this.

04:30.390 --> 04:31.470
It seems pretty simple.

04:31.630 --> 04:32.990
What do you think is going to happen?

04:32.990 --> 04:34.470
What's going to be in result?

04:34.830 --> 04:36.550
I will print results.

04:37.110 --> 04:38.470
Let's see what we get.

04:38.470 --> 04:40.310
If we do this and we print result.

04:40.590 --> 04:41.790
Let you have any ideas.

04:42.110 --> 04:42.830
Here it is.

04:43.230 --> 04:52.070
We get a coroutine object because runner run is an async method, which means an async function, which

04:52.070 --> 04:54.630
means that it returns a coroutine object.

04:54.630 --> 04:55.150
It's not.

04:55.190 --> 04:57.190
It's not a function, it's a coroutine.

04:57.550 --> 04:59.910
And so hopefully you're expecting that.

04:59.950 --> 05:02.030
Or you guessed the what.

05:02.030 --> 05:02.790
What does that mean?

05:02.830 --> 05:06.630
That means that we can't just call, run and run and expect that to work.

05:06.630 --> 05:08.470
We have to call await.

05:09.190 --> 05:13.310
And I'm going to I'm going to restart this kernel and start again from the top so that we don't have

05:13.310 --> 05:14.630
one hanging out there.

05:14.630 --> 05:15.670
Not not scheduled.

05:15.670 --> 05:16.710
We will come through.

05:17.150 --> 05:18.190
We will do this.

05:18.230 --> 05:20.430
We will create our agent.

05:21.190 --> 05:23.160
And there it is.

05:23.360 --> 05:26.200
And now we're going to say await.

05:27.000 --> 05:29.040
We're going to say results equals await.

05:29.040 --> 05:30.440
Runner dot run like that.

05:30.440 --> 05:36.480
And so now what we know is that runner dot run, that coroutine is going to be queued by the event loop.

05:36.480 --> 05:38.960
And when we call await it's going to execute it.

05:38.960 --> 05:41.760
And it will hold until it's completed execution.

05:41.760 --> 05:45.280
And the results will go into the variable result.

05:45.680 --> 05:50.880
And then we are going to print result dot final output.

05:51.520 --> 05:55.800
And this is now hopefully going to work and tell us a joke.

05:56.120 --> 05:57.000
See what happens.

05:57.600 --> 06:00.320
Why don't autonomous agents ever get lost.

06:00.320 --> 06:04.520
Because they are always following their own self driving instructions.

06:05.240 --> 06:12.600
Okay, so OpenAI is rather better at dealing with lightweight agents frameworks than it is at telling

06:12.600 --> 06:14.920
jokes, but we'll accept it.

06:15.200 --> 06:16.640
So it works.

06:16.680 --> 06:20.520
Now you can be forgiven for saying, all right, we haven't done anything particularly special here

06:20.520 --> 06:21.560
to do with agents.

06:21.560 --> 06:24.200
All we've done is have a system prompt and a user prompt.

06:24.200 --> 06:27.380
But never fear, there'll be plenty of time for that later in the week.

06:27.380 --> 06:30.740
What we're doing right now is just showing you the basic construct.

06:30.740 --> 06:35.220
And for my final part of this, I'm going to wrap this in a trace.

06:36.140 --> 06:39.820
So for trace you wrap this in a context manager.

06:39.820 --> 06:44.260
So I say with trace telling a joke.

06:44.980 --> 06:48.260
And then I just need to put that like that.

06:48.420 --> 06:51.740
If you're not familiar with context managers then then look in the the guides.

06:51.740 --> 06:53.940
But but I imagine that you are.

06:54.100 --> 07:00.860
And what this does is it's telling OpenAI that we would like it to record these agent interactions,

07:00.860 --> 07:05.380
which is only one, and record it under the kind of heading telling a joke.

07:05.420 --> 07:12.180
And this allows you to have sophisticated agent interactions with many different agent calls involved,

07:12.180 --> 07:17.700
and have it all packaged up and available in their monitoring tools under one kind of heading.

07:17.940 --> 07:19.260
So let's run this.

07:19.660 --> 07:20.780
We'll get another joke.

07:20.980 --> 07:23.740
Why did the autonomous agent break up with its human partner?

07:23.780 --> 07:26.380
It just couldn't handle the emotional bandwidth.

07:26.420 --> 07:26.980
Okay.

07:28.020 --> 07:30.420
I guess it's not got a whole lot to do with agents, except it's.

07:30.460 --> 07:31.870
Put it in there with the human partner.

07:31.870 --> 07:32.710
That's all right.

07:32.990 --> 07:34.430
I'll let you be the judge of it.

07:34.430 --> 07:37.270
But now we can go and look at this trace.

07:37.270 --> 07:39.830
If we go to platform Openai.com.

07:41.390 --> 07:42.310
Here it comes.

07:43.150 --> 07:44.070
Traces.

07:44.070 --> 07:49.070
It's actually you can just go to platform openai.com and then just click on traces in the left, and

07:49.070 --> 07:52.950
you'll see that telling a joke is the top trace up here.

07:53.110 --> 07:56.190
And if I go into this trace we will see.

07:56.190 --> 08:01.910
What happened is that there was one endpoint called the system instructions was you were a joke teller.

08:01.910 --> 08:05.150
That's the system message that was just called instructions.

08:05.470 --> 08:09.150
The user prompt was tell a joke about autonomous agents.

08:09.150 --> 08:12.070
And then this was what the assistant responded.

08:12.070 --> 08:19.350
So true to its word, it's giving us a clear trace of the single call to an LLM that happened as part

08:19.350 --> 08:24.430
of this gigantic workflow, but it's packaged it up under the heading Telling a Joke.

08:24.630 --> 08:28.630
And this has allowed us to come into traces and see this.

08:28.670 --> 08:32.630
And as you can imagine, it's not particularly profound with this very simple example.

08:32.630 --> 08:36.390
But there will come cases in the future when this is going to be invaluable.
