agent.pymarketingLangchainv1.0.0
LangChain: Content Generator
Generates blog outlines and drafts using your existing content as style reference.
Setup time: ~10 min
Model: GPT-4o
Cost: ~$0.15/day
Last updated: Mar 16, 2026
Template
agent.py
# Install: pip install langchain langchain-openai langchain-community chromadb
from langchain_community.document_loaders import DirectoryLoader, TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import os
# --- Config ---
CONTENT_DIR = "./published-content"
CHROMA_DIR = "./chroma_content"
# --- Load existing content for style reference ---
loader = DirectoryLoader(CONTENT_DIR, glob="**/*.md", loader_cls=TextLoader)
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=200)
chunks = splitter.split_documents(docs)
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings, persist_directory=CHROMA_DIR)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
llm = ChatOpenAI(model="gpt-4o", temperature=0.5)
# --- Outline generator ---
outline_prompt = ChatPromptTemplate.from_messages([
("system", """You are a content strategist. Generate blog post outlines that match the style of existing content. Use the reference material for tone and structure.
Reference content:
{context}"""),
("human", """Create a detailed blog outline for:
Topic: {topic}
Target audience: {audience}
Goal: {goal}
Include: title, intro hook, 4-6 sections with subpoints, conclusion, and a CTA."""),
])
outline_chain = outline_prompt | llm | StrOutputParser()
def generate_outline(topic: str, audience: str = "developers", goal: str = "educate") -> str:
context_docs = retriever.invoke(topic)
context = "\n---\n".join(d.page_content for d in context_docs)
return outline_chain.invoke({
"context": context,
"topic": topic,
"audience": audience,
"goal": goal,
})
if __name__ == "__main__":
topic = input("Blog topic: ")
audience = input("Target audience (default: developers): ") or "developers"
goal = input("Goal (educate/convert/engage): ") or "educate"
print("\n--- Outline ---\n")
print(generate_outline(topic, audience, goal))Setup
- 1
Copy the agent.py content above.
- 2
Create a Python virtual environment and install dependencies.
- 3
Set your OPENAI_API_KEY environment variable.
- 4
Run: python agent.py
Run with LangChain
This is a Python script using LangChain. Set up a virtual environment and install dependencies.
# 1. Create a virtual environment
python -m venv venv && source venv/bin/activate
# 2. Install dependencies
pip install langchain langchain-openai langchain-community chromadb
# 3. Set your API key
export OPENAI_API_KEY="sk-..."
# 4. Save the agent.py from above and run it
python agent.pyVersion History
v1.0.0Initial releaseMar 16, 2026
Framework
LangchainRequirements
Python 3.10+
OpenAI API key
Estimated cost
~$0.15/day
on GPT-4o model
File type
agent.py
Version
v1.0.0
Updated Mar 16, 2026
You might also like
HEARTBEAT.md
Social Media Monitor
Track mentions of your brand across platforms. Daily digest of what people are saying.
~$0.20/day · ~10 min setup
HEARTBEAT.md
Content Scheduler
Draft social posts from your content calendar. Queue them for review.
~$0.30/day · ~10 min setup
HEARTBEAT.md
SEO Tracker
Monitor keyword rankings and page performance weekly.
~$0.10/day · ~10 min setup