An AI-powered blog writing system that uses web research (Tavily API), RAG (Retrieval-Augmented Generation) with LangChain, and OpenRouter for flexible LLM access to generate high-quality blog posts.
- π Web Research: Automatically researches topics using Tavily API
- π RAG System: Uses LangChain and FAISS for context-aware generation
- π€ Flexible AI Models: Access multiple LLMs via OpenRouter (including free models)
- π Quality Content: Generates 1000-1500 word blog posts with proper structure
- π Source Citations: Automatically includes source references
- π Observability: Built-in LangSmith integration for tracing and debugging
- Python 3.10 or higher
- OpenRouter API key (get it at https://openrouter.ai/)
- Tavily API key (get it at https://tavily.com/)
-
Navigate to the project directory:
cd "/media/mats/3c24094c-800b-4576-a390-d23a6d7a02291/workspace/test_ai_gen/blog writter"
-
Activate the virtual environment:
source venv/bin/activate -
Create a
.envfile in the project root:nano .env
-
Add your API keys to the
.envfile:OPENROUTER_API_KEY=your_openrouter_key_here TAVILY_API_KEY=your_tavily_key_here # Optional: LangSmith for observability and debugging LANGCHAIN_TRACING_V2=true LANGCHAIN_API_KEY=your_langsmith_api_key_here LANGCHAIN_PROJECT=blog-writer-agent
-
Activate the virtual environment:
source venv/bin/activate -
Edit the topic in
main.py:if __name__ == "__main__": generate_blog("Your Topic Here")
-
Run the script:
python main.py
-
Find your blog post in the
output/directory
Edit config.py to change the OpenRouter model:
# Free models (recommended for testing)
OPENROUTER_MODEL = "meta-llama/llama-3-70b-instruct"
# OPENROUTER_MODEL = "google/gemma-7b-it:free"
# OPENROUTER_MODEL = "mistralai/mistral-7b-instruct:free"
# Paid models (better quality)
# OPENROUTER_MODEL = "anthropic/claude-3.5-sonnet"
# OPENROUTER_MODEL = "openai/gpt-4-turbo"blog-writer-agent/
βββ venv/ # Virtual environment
βββ .env # API keys (create this)
βββ .gitignore
βββ requirements.txt # Dependencies
βββ config.py # Configuration
βββ main.py # Main script
βββ agents/
β βββ __init__.py
β βββ researcher.py # Tavily web search agent
β βββ rag_manager.py # RAG system with FAISS
β βββ writer.py # Blog post generation agent
βββ output/ # Generated blog posts
-
Research Phase: The Researcher Agent searches the web using Tavily API to gather relevant, up-to-date information about your topic.
-
RAG Building: The RAG Manager processes the research data, splits it into chunks, and stores it in an in-memory FAISS vector store with embeddings.
-
Context Retrieval: When generating content, the system retrieves the most relevant chunks from the vector store.
-
Content Generation: The Writer Agent uses an OpenRouter LLM with the retrieved context to generate a comprehensive, well-structured blog post.
-
Output: The final blog post is saved as a markdown file with source citations.
output/blog_The_Future_of_AI_in_Healthcare.md
The output includes:
- Properly formatted markdown
- Clear section headings
- 1000-1500 words of content
- Source citations at the end
Edit main.py to change the number of sources:
research_data = researcher.search(
f"Latest information about {topic}",
max_results=10 # Increase for more sources
)Modify the style parameter in main.py:
blog_post = writer.generate_blog_post(
topic=topic,
context_docs=context_docs,
style="technical" # Options: professional, casual, technical, etc.
)Edit agents/rag_manager.py:
chunk_size=1000, # Increase for larger chunks
chunk_overlap=200, # Adjust overlapMake sure you've created a .env file with both API keys.
Ensure you've activated the virtual environment:
source venv/bin/activateCheck your Tavily API key and internet connection.
Some OpenRouter models require credits. Check available free models at https://openrouter.ai/models
The system includes built-in LangSmith integration for comprehensive observability:
- Complete LLM Interaction Logs: Every prompt and response is automatically logged
- Agent Identification: See which agent (Writer, Planner, Scorer) made each call
- Session Tracking: Group related interactions by session ID
- Performance Metrics: Response times, token usage, and costs
- Error Tracking: Automatic error logging and debugging information
- Visual Interface: Web dashboard at https://smith.langchain.com
- Sign up for LangSmith (free tier available): https://smith.langchain.com
- Get your API key from the LangSmith dashboard
- Add to your
.envfile:LANGCHAIN_TRACING_V2=true LANGCHAIN_API_KEY=your_langsmith_api_key_here LANGCHAIN_PROJECT=blog-writer-agent
- After running the blog generator, check the console output for session information
- Visit https://smith.langchain.com to view detailed traces
- Filter by session ID to see all interactions for a specific run
- Analyze performance, debug issues, and optimize prompts
- Tavily API: Free tier includes 1,000 searches/month
- OpenRouter:
- Free models: $0
- Paid models: ~$0.01-0.10 per blog post (depending on model)
- LangSmith: Free tier includes 1,000 traces/month
MIT License - Feel free to modify and use as needed.
source venv/bin/activate