WEBVTT

00:00.320 --> 00:05.690
And in this video, you're going to learn how you can automatically write blog posts using AI and ChatGPT.

00:05.930 --> 00:10.490
We're going to use Lang Chain, and we're going to also break this down into several steps to improve

00:10.490 --> 00:11.570
our outputs.

00:11.690 --> 00:16.460
Firstly, you're going to need to install a couple of different packages, mainly lang chain lang and

00:16.460 --> 00:18.740
OpenAI lang chain community.

00:18.740 --> 00:21.980
And then we're going to import a bunch of packages here.

00:21.980 --> 00:24.560
You can see that we're also using Pydantic.

00:24.560 --> 00:28.460
And if you're using the Lang chain version of that you can also use V1.

00:28.460 --> 00:32.900
Or you could add that to your imports at the top here with pedantic as well.

00:32.900 --> 00:36.830
Pedantic is now supported on version two for Lang Chain, which is great.

00:36.830 --> 00:41.150
And we're also going to now set up a chat model using GPT four.

00:41.270 --> 00:46.250
So notice how we've set up a pedantic model here that we're looking to extract.

00:46.250 --> 00:50.150
We have an outline sections key which is a list of strings.

00:50.150 --> 00:53.690
And in the description of that field we have the sections of the blog post outline.

00:53.690 --> 00:57.670
And if the point is a nested point, then add a number to the start of it.

00:57.820 --> 01:04.570
We then set up a system mock system message prompt template and tell the alarm that it's a helpful system

01:04.570 --> 01:07.030
that's going to be writing blog post outlines.

01:07.060 --> 01:10.990
The outline must be incredibly long, extensive, and detailed, and you are writing an outline on the

01:10.990 --> 01:13.210
topic of and put a topic there.

01:13.540 --> 01:18.370
Also, notice that we make a chat prompt template from the system message, and also we're using the

01:18.370 --> 01:22.780
model with structured output and passing in our Pydantic model.

01:22.780 --> 01:24.940
And this is getting piped the prompt.

01:24.940 --> 01:26.830
So the prompt gets piped into the model.

01:26.830 --> 01:31.450
And then the model is then using this with structured output to know that it needs to produce these

01:31.450 --> 01:33.700
sections, which we defined earlier.

01:34.150 --> 01:37.750
This is our blog post outline for our blog post generation system prompt.

01:37.750 --> 01:42.070
We're going to say that because we're not going to generate the entire blog post all in one go, we're

01:42.070 --> 01:44.530
going to say you are a helpful assistant to write blog posts.

01:44.530 --> 01:47.170
The blog post must be incredibly long and extensive and detailed.

01:47.200 --> 01:48.790
Here is the article topic.

01:48.820 --> 01:52.510
You can say, here are the last three sections of the article that have been generated.

01:52.510 --> 01:55.900
Here are the next three sections of the article that will be generated.

01:55.900 --> 02:02.590
You must render the article in a structured markdown format and also you must produce the content.

02:02.620 --> 02:05.320
Never include the section headings as these are added later.

02:05.320 --> 02:08.020
And then we just tell it to produce the current section content.

02:08.020 --> 02:10.030
And then we have the same kind of thing here.

02:10.030 --> 02:16.210
But notice that we're instead of using a Pydantic model, we're putting the prompt into the model and

02:16.210 --> 02:22.600
then into a structured output parser, which basically means that will be getting back a string.

02:22.600 --> 02:24.520
And you'll see why that's important in the moment.

02:24.520 --> 02:31.540
But to start with, we just do our blog post outline, uh, runnable, which will produce, as you can

02:31.540 --> 02:33.400
see here, this sections class.

02:33.400 --> 02:36.840
And then we can go into the dot outline sections.

02:36.840 --> 02:40.020
And you can see we've got all the various sections here which are a list of strings.

02:40.020 --> 02:45.570
And the ones that are in the numbers here are the the sort of nested ones that you can see.

02:45.570 --> 02:47.370
Now that's great.

02:47.370 --> 02:48.420
And we know that that works.

02:48.420 --> 02:52.440
But one thing we want to store is maybe like the last three sections of content.

02:52.440 --> 02:58.290
And this is important because we want to make sure that the LM isn't repeating itself from between sections,

02:58.290 --> 02:59.610
as we're doing a for loop here.

02:59.610 --> 03:05.280
So we're going to store the history and we're going to enumerate over the outline sections.

03:05.280 --> 03:07.170
And you can see that becomes the index.

03:07.170 --> 03:09.030
And we have the current section there.

03:09.030 --> 03:13.650
We're also going to go get the previous sections and the previous content for those sections.

03:13.650 --> 03:15.660
And then we're also going to get the next sections.

03:15.660 --> 03:21.270
Just so the LM is aware about what sections are going to be coming up next in the blog post, we're

03:21.270 --> 03:28.270
then going to invoke our blog post Generation Runnable Chain, and we're going to provide in here the

03:28.270 --> 03:34.150
topic and also the previous article sections and the next three article sections.

03:34.150 --> 03:37.750
We're then going to add that to the history with the current section and the section content.

03:37.750 --> 03:40.600
And then we're going to generate a message called generated section.

03:40.600 --> 03:44.470
And then at the end of that we're going to take everything that's in the history and just join it together

03:44.470 --> 03:45.340
with a new line.

03:45.340 --> 03:47.710
And then you'll start to see this is kind of how it would work.

03:47.710 --> 03:52.590
So if we're only generating three sections, then you know, this, this is how it would, would work.

03:52.590 --> 03:55.770
And then you can see this is the full blog post that it decided to generate.

03:55.770 --> 04:03.510
So the key points here are at the moment oh doesn't have for all users, doesn't provide the best generation

04:03.510 --> 04:05.640
in terms of an entire blog post in one go.

04:05.640 --> 04:10.830
So we've broken the problem down and decomposed it into two steps, the first step being a blog post

04:10.830 --> 04:16.640
outline, and the second step being that blog post generation on a section by section basis.

04:16.670 --> 04:22.310
The important thing that we've also done is we've included a couple of sections to help this chain have

04:22.310 --> 04:23.540
a bit more continuity.

04:23.540 --> 04:29.150
So this bit here, um, the previous article sections, hopefully that's going to really showcase the

04:29.150 --> 04:34.760
lamb, uh, what has happened in the last three sections so that the lamb has, you know, provides

04:34.760 --> 04:38.000
a better blog post and it's not going to be repeating itself.

04:38.000 --> 04:38.540
Cool.

04:38.540 --> 04:38.960
All right.

04:38.960 --> 04:39.770
I'll see you in the next one.
