content_chain using LangChain. By the end, you will have created a workflow that:
- Generates an impactful blog post title
- Produces a detailed outline
- Writes a 200-word blog post
- Crafts a concise summary for social media
Table of Contents
- Prerequisites
- Imports
- Defining Individual Chains
- Composing the Content Chain
- Invoking the Chain
- Customizing Your LLMs
- References
Prerequisites
- Python 3.8+
- An active OpenAI API key
- The
langchain_coreandlangchain_openaipackages installed - Familiarity with basic LLM concepts
1. Imports
Begin by importing the necessary classes and functions:2. Defining Individual Chains
Each mini-chain handles one stage of the workflow. We parse the LLM output withStrOutputParser and wrap the result in a passthrough runnable to tag it in the next step.
Title Chain
Generates an engaging title from a raw topic.Outline Chain
Creates a detailed outline based on the generated title.Blog Chain
Builds a 200-word blog post using the outline.Summary Chain
Produces a concise, social-media-ready summary from the blog content.Each chain uses the same parser (
StrOutputParser) and can be extended with custom parsers or validators as needed.3. Composing the Content Chain
Link the mini-chains in sequence so the output of one stage feeds into the next:4. Invoking the Chain
Run the full workflow by providing a topic. The chain executes each stage in order:Large chains incur multiple API calls. Monitor your usage to avoid unexpected costs.
5. Customizing Your LLMs
You can assign different models to each stage to optimize for speed, cost, or quality. For example:| Stage | Task | Example Model |
|---|---|---|
| Title | Creative title generation | Gemini |
| Outline | Structured outline creation | GPT-3.5 |
| Blog | Long-form content (200 words) | GPT-4 |
| Summary | Short social-media summary | Claude |
ChatOpenAI():