WEBVTT

00:00.450 --> 00:03.930
-: Okay, I'm gonna teach you how to use Instructor.

00:03.930 --> 00:05.700
It's a really lightweight package.

00:05.700 --> 00:09.300
If you've tried LangChain and really hated it,

00:09.300 --> 00:11.220
fallen in and out of love with it

00:11.220 --> 00:13.710
'cause it was too complicated, and too much overhead,

00:13.710 --> 00:17.070
Instructor is really, it's something you can't argue with

00:17.070 --> 00:19.590
because adds a minimum amount of overhead.

00:19.590 --> 00:23.190
It's basically the same as using the OpenAI API,

00:23.190 --> 00:25.170
except it gives you a lot of the benefits

00:25.170 --> 00:26.400
that you get with LangChain.

00:26.400 --> 00:28.710
You can drop in different LLMs,

00:28.710 --> 00:32.160
you can get automatic retries, and do a lot of cool stuff.

00:32.160 --> 00:36.270
First you wanna open install Instructor, really simple.

00:36.270 --> 00:37.680
And then this is how you use it.

00:37.680 --> 00:40.470
So you can see that this is exactly the same

00:40.470 --> 00:42.450
as the OpenAI API.

00:42.450 --> 00:45.210
And one thing that is a little bit different

00:45.210 --> 00:47.970
with the Instructor is that you pass it a model,

00:47.970 --> 00:49.410
a Pydantic model.

00:49.410 --> 00:52.920
So if you know anything about object-oriented programming

00:52.920 --> 00:56.010
or types or whatever, you probably familiar with this.

00:56.010 --> 00:57.930
But even if you aren't, it's pretty straightforward.

00:57.930 --> 01:01.530
You basically just make a structure, like a class,

01:01.530 --> 01:05.310
that tells the model what type of response you want.

01:05.310 --> 01:09.210
So inherits from the base model, which is from Pydantic.

01:09.210 --> 01:11.640
And then we've called it user info.

01:11.640 --> 01:14.970
And we just say that user info has a name and an age.

01:14.970 --> 01:16.980
And it's a string and an integer.

01:16.980 --> 01:19.170
And so that's all the information we have to give it.

01:19.170 --> 01:22.110
And we create a client with OpenAI

01:22.110 --> 01:23.910
and then we pass in that response model.

01:23.910 --> 01:27.060
What Instructor does under the hood is it feeds that

01:27.060 --> 01:28.740
into the system instructions.

01:28.740 --> 01:33.740
And that way we get back what the actual Pydantic model

01:33.840 --> 01:34.800
wants you to give back.

01:34.800 --> 01:37.980
If I run this, then we're gonna get back

01:37.980 --> 01:39.570
this user info model.

01:39.570 --> 01:42.240
You can see I just said user_info.name.

01:42.240 --> 01:43.447
It is John Doe.

01:43.447 --> 01:46.440
User_info.age is 30, and it's pulled that

01:46.440 --> 01:48.360
from the messages here.

01:48.360 --> 01:52.353
And this is great for image for, sorry, retake.

01:53.280 --> 01:56.610
This is great for if you want to extract information

01:56.610 --> 01:58.200
from large amounts of text.

01:58.200 --> 02:00.210
This is just a short thing here,

02:00.210 --> 02:02.520
but we could get quite a lot of information.

02:02.520 --> 02:03.540
And it's also great

02:03.540 --> 02:05.970
because you don't have to remember, okay,

02:05.970 --> 02:08.280
hold on, what was the thing again?

02:08.280 --> 02:11.400
How do we get the responses back

02:11.400 --> 02:13.020
and like how do we pass the response?

02:13.020 --> 02:15.120
And if it's JSON, what happens if it comes back

02:15.120 --> 02:16.200
and it's wrong?

02:16.200 --> 02:17.130
This is really good.

02:17.130 --> 02:19.800
And you can do automatic retries based on if it comes back

02:19.800 --> 02:20.850
the wrong thing.

02:20.850 --> 02:24.210
You can also drop in other LLMs.

02:24.210 --> 02:25.860
They're basically only line you have to change

02:25.860 --> 02:30.300
is like from anthropic if you want to drop in Claude.

02:30.300 --> 02:32.490
And they have all of that in those tutorials.

02:32.490 --> 02:34.350
And there's one other really cool thing

02:34.350 --> 02:37.140
I wanted to show you, which is streaming.

02:37.140 --> 02:41.820
So it can actually bring back structured data in midstream.

02:41.820 --> 02:44.700
So that doesn't make any sense to you probably,

02:44.700 --> 02:46.050
because you've never seen it before.

02:46.050 --> 02:48.300
But let me show you a demo, it's pretty cool.

02:48.300 --> 02:51.150
So here's a big textbook about some meeting.

02:51.150 --> 02:52.650
And we have some information there.

02:52.650 --> 02:55.830
Who are the people, what emails do we have,

02:55.830 --> 02:56.850
and things like that.

02:56.850 --> 02:58.200
And we set up a few different things.

02:58.200 --> 03:02.070
So we set up a user model instead of a meeting info model,

03:02.070 --> 03:05.190
which has a date, location, budget, deadline, et cetera.

03:05.190 --> 03:08.130
And then we have, we just create our extraction stream.

03:08.130 --> 03:10.320
And the only difference is we just say

03:10.320 --> 03:14.640
stream equals true, and we're giving it a partial.

03:14.640 --> 03:16.593
So it's saying we're getting back,

03:18.000 --> 03:21.990
so here's this partial, we're getting back a stream.

03:21.990 --> 03:24.300
And the stream is gonna be meeting info.

03:24.300 --> 03:26.940
And the meeting info has a list of users

03:26.940 --> 03:28.260
as well as the date location.

03:28.260 --> 03:30.750
And then each user will have a name, email, Twitter.

03:30.750 --> 03:31.860
Okay, we're gonna run this

03:31.860 --> 03:33.990
and we're gonna see this update in real time.

03:33.990 --> 03:36.570
So it's found John Smith and his email.

03:36.570 --> 03:38.370
It's found Jane Doe and her email,

03:38.370 --> 03:40.830
it's found Alice Johnson and her email.

03:40.830 --> 03:42.660
And it's starting to fill in these other things as well.

03:42.660 --> 03:43.493
And now it's done.

03:43.493 --> 03:45.690
Now you can see that this is really powerful

03:45.690 --> 03:49.020
because if you have a prompt that's pulling back

03:49.020 --> 03:51.420
and streaming back a bunch of structured data,

03:51.420 --> 03:53.580
you can actually act on that structured data

03:53.580 --> 03:54.990
before it's even ready.

03:54.990 --> 03:57.330
Which I think is really helpful.

03:57.330 --> 04:00.540
All right, one other thing is retry logic.

04:00.540 --> 04:03.570
And so, this is an example from the documentation.

04:03.570 --> 04:06.579
If you add like a field validator here,

04:06.579 --> 04:09.990
we're gonna do this just specifically to cause and error.

04:09.990 --> 04:12.270
And this is the field validator of name,

04:12.270 --> 04:15.390
and we're saying that name needs to be uppercase.

04:15.390 --> 04:17.400
So we have this here and it's gonna pass all that

04:17.400 --> 04:18.960
into the prompt as well.

04:18.960 --> 04:20.250
But we're gonna validate that.

04:20.250 --> 04:23.190
And we're using Tenacity here for retrying.

04:23.190 --> 04:27.210
Instructor has a max retries equals thing here.

04:27.210 --> 04:28.800
So we can just say, okay,

04:28.800 --> 04:30.900
we wanna retry three times, or whatever.

04:30.900 --> 04:33.540
And then we're just adding Tenacity into here

04:33.540 --> 04:34.830
to manage that for us.

04:34.830 --> 04:37.950
So, it's gonna print something before,

04:37.950 --> 04:39.720
it's gonna print something afterwards.

04:39.720 --> 04:42.900
And then it's gonna stop after three attempts.

04:42.900 --> 04:45.540
And I'm just gonna run this, and it's gonna extract

04:45.540 --> 04:48.690
the name John, but it's not gonna be all in uppercase.

04:48.690 --> 04:52.500
And we can assert here that it's not actually John, right?

04:52.500 --> 04:56.220
And you can see it tries one time, it tries another,

04:56.220 --> 04:59.790
it tries another time saying it's not passing the assertion.

04:59.790 --> 05:02.400
So that is really interesting.

05:02.400 --> 05:04.590
And this is a really helpful pattern.

05:04.590 --> 05:07.080
But Instructor makes that a little bit easier.

05:07.080 --> 05:09.510
All right, so yeah, hopefully that's useful for you.

05:09.510 --> 05:10.740
I really like this library.

05:10.740 --> 05:11.970
It's quite lightweight.

05:11.970 --> 05:13.530
We can get a lot of power out of it

05:13.530 --> 05:15.540
and a lot of the benefit that you would have

05:15.540 --> 05:16.950
from using LangChain,

05:16.950 --> 05:19.353
but without having to retool everything.
