And in this video, you're going to learn how you can automatically write blog posts using AI and ChatGPT.
We're going to use Lang Chain, and we're going to also break this down into several steps to improve
our outputs.
Firstly, you're going to need to install a couple of different packages, mainly lang chain lang and
OpenAI lang chain community.
And then we're going to import a bunch of packages here.
You can see that we're also using Pydantic.
And if you're using the Lang chain version of that you can also use V1.
Or you could add that to your imports at the top here with pedantic as well.
Pedantic is now supported on version two for Lang Chain, which is great.
And we're also going to now set up a chat model using GPT four.
So notice how we've set up a pedantic model here that we're looking to extract.
We have an outline sections key which is a list of strings.
And in the description of that field we have the sections of the blog post outline.
And if the point is a nested point, then add a number to the start of it.
We then set up a system mock system message prompt template and tell the alarm that it's a helpful system
that's going to be writing blog post outlines.
The outline must be incredibly long, extensive, and detailed, and you are writing an outline on the
topic of and put a topic there.
Also, notice that we make a chat prompt template from the system message, and also we're using the
model with structured output and passing in our Pydantic model.
And this is getting piped the prompt.
So the prompt gets piped into the model.
And then the model is then using this with structured output to know that it needs to produce these
sections, which we defined earlier.
This is our blog post outline for our blog post generation system prompt.
We're going to say that because we're not going to generate the entire blog post all in one go, we're
going to say you are a helpful assistant to write blog posts.
The blog post must be incredibly long and extensive and detailed.
Here is the article topic.
You can say, here are the last three sections of the article that have been generated.
Here are the next three sections of the article that will be generated.
You must render the article in a structured markdown format and also you must produce the content.
Never include the section headings as these are added later.
And then we just tell it to produce the current section content.
And then we have the same kind of thing here.
But notice that we're instead of using a Pydantic model, we're putting the prompt into the model and
then into a structured output parser, which basically means that will be getting back a string.
And you'll see why that's important in the moment.
But to start with, we just do our blog post outline, uh, runnable, which will produce, as you can
see here, this sections class.
And then we can go into the dot outline sections.
And you can see we've got all the various sections here which are a list of strings.
And the ones that are in the numbers here are the the sort of nested ones that you can see.
Now that's great.
And we know that that works.
But one thing we want to store is maybe like the last three sections of content.
And this is important because we want to make sure that the LM isn't repeating itself from between sections,
as we're doing a for loop here.
So we're going to store the history and we're going to enumerate over the outline sections.
And you can see that becomes the index.
And we have the current section there.
We're also going to go get the previous sections and the previous content for those sections.
And then we're also going to get the next sections.
Just so the LM is aware about what sections are going to be coming up next in the blog post, we're
then going to invoke our blog post Generation Runnable Chain, and we're going to provide in here the
topic and also the previous article sections and the next three article sections.
We're then going to add that to the history with the current section and the section content.
And then we're going to generate a message called generated section.
And then at the end of that we're going to take everything that's in the history and just join it together
with a new line.
And then you'll start to see this is kind of how it would work.
So if we're only generating three sections, then you know, this, this is how it would, would work.
And then you can see this is the full blog post that it decided to generate.
So the key points here are at the moment oh doesn't have for all users, doesn't provide the best generation
in terms of an entire blog post in one go.
So we've broken the problem down and decomposed it into two steps, the first step being a blog post
outline, and the second step being that blog post generation on a section by section basis.
The important thing that we've also done is we've included a couple of sections to help this chain have
a bit more continuity.
So this bit here, um, the previous article sections, hopefully that's going to really showcase the
lamb, uh, what has happened in the last three sections so that the lamb has, you know, provides
a better blog post and it's not going to be repeating itself.
Cool.
All right.
I'll see you in the next one.