-: Okay, I'm gonna teach you how to use Instructor.
It's a really lightweight package.
If you've tried LangChain and really hated it,
fallen in and out of love with it
'cause it was too complicated, and too much overhead,
Instructor is really, it's something you can't argue with
because adds a minimum amount of overhead.
It's basically the same as using the OpenAI API,
except it gives you a lot of the benefits
that you get with LangChain.
You can drop in different LLMs,
you can get automatic retries, and do a lot of cool stuff.
First you wanna open install Instructor, really simple.
And then this is how you use it.
So you can see that this is exactly the same
as the OpenAI API.
And one thing that is a little bit different
with the Instructor is that you pass it a model,
a Pydantic model.
So if you know anything about object-oriented programming
or types or whatever, you probably familiar with this.
But even if you aren't, it's pretty straightforward.
You basically just make a structure, like a class,
that tells the model what type of response you want.
So inherits from the base model, which is from Pydantic.
And then we've called it user info.
And we just say that user info has a name and an age.
And it's a string and an integer.
And so that's all the information we have to give it.
And we create a client with OpenAI
and then we pass in that response model.
What Instructor does under the hood is it feeds that
into the system instructions.
And that way we get back what the actual Pydantic model
wants you to give back.
If I run this, then we're gonna get back
this user info model.
You can see I just said user_info.name.
It is John Doe.
User_info.age is 30, and it's pulled that
from the messages here.
And this is great for image for, sorry, retake.
This is great for if you want to extract information
from large amounts of text.
This is just a short thing here,
but we could get quite a lot of information.
And it's also great
because you don't have to remember, okay,
hold on, what was the thing again?
How do we get the responses back
and like how do we pass the response?
And if it's JSON, what happens if it comes back
and it's wrong?
This is really good.
And you can do automatic retries based on if it comes back
the wrong thing.
You can also drop in other LLMs.
They're basically only line you have to change
is like from anthropic if you want to drop in Claude.
And they have all of that in those tutorials.
And there's one other really cool thing
I wanted to show you, which is streaming.
So it can actually bring back structured data in midstream.
So that doesn't make any sense to you probably,
because you've never seen it before.
But let me show you a demo, it's pretty cool.
So here's a big textbook about some meeting.
And we have some information there.
Who are the people, what emails do we have,
and things like that.
And we set up a few different things.
So we set up a user model instead of a meeting info model,
which has a date, location, budget, deadline, et cetera.
And then we have, we just create our extraction stream.
And the only difference is we just say
stream equals true, and we're giving it a partial.
So it's saying we're getting back,
so here's this partial, we're getting back a stream.
And the stream is gonna be meeting info.
And the meeting info has a list of users
as well as the date location.
And then each user will have a name, email, Twitter.
Okay, we're gonna run this
and we're gonna see this update in real time.
So it's found John Smith and his email.
It's found Jane Doe and her email,
it's found Alice Johnson and her email.
And it's starting to fill in these other things as well.
And now it's done.
Now you can see that this is really powerful
because if you have a prompt that's pulling back
and streaming back a bunch of structured data,
you can actually act on that structured data
before it's even ready.
Which I think is really helpful.
All right, one other thing is retry logic.
And so, this is an example from the documentation.
If you add like a field validator here,
we're gonna do this just specifically to cause and error.
And this is the field validator of name,
and we're saying that name needs to be uppercase.
So we have this here and it's gonna pass all that
into the prompt as well.
But we're gonna validate that.
And we're using Tenacity here for retrying.
Instructor has a max retries equals thing here.
So we can just say, okay,
we wanna retry three times, or whatever.
And then we're just adding Tenacity into here
to manage that for us.
So, it's gonna print something before,
it's gonna print something afterwards.
And then it's gonna stop after three attempts.
And I'm just gonna run this, and it's gonna extract
the name John, but it's not gonna be all in uppercase.
And we can assert here that it's not actually John, right?
And you can see it tries one time, it tries another,
it tries another time saying it's not passing the assertion.
So that is really interesting.
And this is a really helpful pattern.
But Instructor makes that a little bit easier.
All right, so yeah, hopefully that's useful for you.
I really like this library.
It's quite lightweight.
We can get a lot of power out of it
and a lot of the benefit that you would have
from using LangChain,
but without having to retool everything.