Blog Post Generator Using LangGraph and LangChain
We will build a simple blog post generator using LangGraph, a powerful framework for structuring multi-step workflows in LangChain.
In this tutorial, Our system will have two agents:
- Title Generator Agent – Generates a blog post title based on a given topic.
- Content Generator Agent – Generates the blog post content based on the generated title.
We will use OpenAI's GPT-4o model (or Groq LLM if needed) to generate the responses.
Setting Up the Environment
Before we begin, ensure you have the required dependencies installed:
pip install langchain langchain_openai langgraph python-dotenvThen, set up your API keys by creating a .env file:
GROQ_API_KEY=your_groq_api_key
OPENAI_API_KEY=your_openai_api_keyNow, let's import the required modules and load the environment variables.
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o") # Using GPT-4o for better responsesDefining the Message State
LangGraph uses a message-based state to pass data between nodes. We define a MessagesState dictionary that will hold a list of messages.
from langgraph.graph import MessagesState
from typing_extensions import TypedDict
from langchain_core.messages import AnyMessage
from typing import Annotated
from langgraph.graph.message import add_messages
class MessagesState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]1. Title Generator Agent
This agent takes the user-provided blog topic and generates a blog title.
from langchain_core.messages import HumanMessage, SystemMessage
# System message for Title Generator
title_generator = SystemMessage(
content="You are a helpful assistant tasked with generating a Title for the Blog Heading that I provide."
)
def title_generator_agent(state: MessagesState):
return {"messages": [llm.invoke([title_generator] + state["messages"])]}2. Blog Content Generator Agent
This agent takes the generated blog title and creates blog content based on it.
# System message for Blog Content Generator
blog_generator = SystemMessage(
content="You are a helpful assistant tasked with generating Blog Content for the Blog Title."
)
def blog_generator_agent(state: MessagesState):
return {"messages": [llm.invoke([blog_generator] + state["messages"])]}Building the Workflow with LangGraph
Now, we use LangGraph to define the workflow:
- The process starts with the Title Generator.
- The generated title is passed to the Blog Generator.
- The blog generator creates content based on the title.
from langgraph.graph import START, StateGraph, END
from IPython.display import Image, display
# Initialize the graph
builder = StateGraph(MessagesState)
# Define nodes (agents)
builder.add_node("Title Generator", title_generator_agent)
builder.add_node("Blog Generator", blog_generator_agent)
# Define edges (flow)
builder.add_edge(START, "Title Generator")
builder.add_edge("Title Generator", "Blog Generator")
builder.add_edge("Blog Generator", END)
# Compile the graph
graph = builder.compile()
# Visualizing the workflow
display(Image(graph.get_graph().draw_mermaid_png()))Conclusion
We successfully built a blog post generator using LangGraph and LangChain. This modular design allows us to extend the system by adding more content refinement steps or integrating with external knowledge sources. Now -
- Enhance the title generation logic with better prompt tuning.
- Expand the blog generator to include structured sections (Introduction, Body, Conclusion).
- Implement SEO optimization for better readability.