A comprehensive Python toolkit for working with AI language models through the OpenRouter API. Supports single prompt processing, multi-prompt chaining, file conversion (docx/pdf/epub), web interface, and advanced automation with pre/post processing scripts.
- Multiple Interfaces: CLI, Web UI, and programmatic access
- Prompt Chaining: Sequential prompt execution with restart/recovery capabilities
- Smart Recovery: Automatic restart from failed steps with status tracking
- 100+ AI Models: Support for all OpenRouter-compatible models
- Script Integration: Pre/post processing scripts at global and per-step levels
- File Conversion: Convert between markdown and docx, pdf, epub, and more
- Combine Outputs: Merge all step outputs into a single file
- Chapter Splitter: Split documents by chapters, prologues, and epilogues (supports markdown, plain text, Roman numerals)
- Advanced Configuration: YAML-based configuration with parameter overrides
- File Management: Automatic chunking, validation, and format handling
- Web Dashboard: Real-time monitoring and visual chain builder
- Direct File Loading: Upload and execute JSON prompts and YAML chains instantly
- No-Registry Execution: Run prompts and chains without permanent storage
# 1. Clone and install
git clone https://github.com/your-org/openrouter-interface.git
cd openrouter-interface
./install-global.sh
# 2. Set up API key
export OPENROUTER_API_KEY="your-api-key-here"
# 3. Run a single prompt
openrouter-runner -p prompts/example.json -i input.md -o output.md
# 4. Create and run a chain
openrouter-chain --create-sample
openrouter-chain -c sample_chain.yaml
# 5. Split a document by chapters
split-chapters book.md -o chapters/
# 6. Start web interface
openrouter-webInstall system-wide for use from anywhere:
git clone https://github.com/your-org/openrouter-interface.git
cd openrouter-interface
./install-global.sh
# Verify installation
openrouter-runner --help
openrouter-web --help
openrouter-chain --help
split-chapters --helpNote: The installer automatically detects and installs pandoc for file conversion features. If automatic installation fails, install manually from pandoc.org.
For development or isolated environments:
./install.sh web
source openrouter-venv/bin/activate
PYTHONPATH=src python3 -m openrouter_interface.cli --helpLinux
# Ubuntu/Debian
sudo apt update
sudo apt install python3 python3-pip git
# Clone and install
git clone https://github.com/your-org/openrouter-interface.git
cd openrouter-interface
./install-global.sh
# Set API key
echo 'export OPENROUTER_API_KEY="your-api-key-here"' >> ~/.bashrc
source ~/.bashrcmacOS
# Using Homebrew
brew install python git
# Clone and install
git clone https://github.com/your-org/openrouter-interface.git
cd openrouter-interface
./install-global.sh
# Set API key
echo 'export OPENROUTER_API_KEY="your-api-key-here"' >> ~/.zshrc
source ~/.zshrcWindows
# Install Python 3.8+ and Git
# Clone repository
git clone https://github.com/your-org/openrouter-interface.git
cd openrouter-interface
# Run PowerShell as administrator
powershell -ExecutionPolicy Bypass -File install-windows.ps1
# Set API key
setx OPENROUTER_API_KEY "your-api-key-here"Get your API key from OpenRouter and set it up:
# Method 1: Environment variable
export OPENROUTER_API_KEY="your-api-key-here"
# Method 2: Setup script
./setup-api-key.sh
# Method 3: Add to shell profile
echo 'export OPENROUTER_API_KEY="your-api-key-here"' >> ~/.bashrcExecute individual prompts against AI models:
# Basic usage
openrouter-runner -p prompts/analysis.json -i document.md -o result.md
# With model and parameter overrides
openrouter-runner -p prompts/creative.json -i input.md -o output.md \
--model "openai/gpt-4-turbo" \
--temperature 0.8 \
--max-tokens 25000
# Multiple prompts in sequence
openrouter-runner -p "prompts/grammar.json,prompts/style.json" \
-i draft.md -o final.mdLaunch the web dashboard for visual prompt management:
# Start with default settings
openrouter-web
# Custom port and debug mode
openrouter-web --port 8080 --debug
# With custom configuration
openrouter-web --config config/web_config.yamlWeb Features:
- File Upload: Drag-and-drop interface
- Model Selection: Choose from 100+ models
- Chain Builder: Visual chain creation
- Real-time Monitoring: Live progress tracking
- File Management: Browse intermediate results
- Load Prompt: Upload and execute JSON prompts instantly
- Load Chain: Upload and execute YAML chains directly
Access from the main page with the green "Load Prompt" button:
- Upload JSON Prompt: Select any JSON prompt file
- Preview: View prompt details (title, description, instructions, examples)
- Configure: Optionally upload YAML configuration for API parameters
- Input: Provide text or upload file
- Execute: Run immediately and view results in modal
# Example usage flow:
# 1. Click "Load Prompt" on main page
# 2. Upload: test_load_prompt.json
# 3. Configure: test_single_prompt_config.yaml (optional)
# 4. Input: "This is test content to analyze"
# 5. Execute and view resultsAccess from the Chain Runner page - navigates to dedicated full-page interface:
Dedicated Load Page (/chains/load)
- Navigate: Click "Load Chain" to go to comprehensive load interface
- Upload Options:
- File upload with drag-and-drop support
- Text area for direct YAML input
- Configuration-based upload with template selection
- Rich Configuration Analysis:
- Detailed breakdown showing prompts, models, and parameters
- Raw YAML preview with syntax highlighting
- Template usage detection and description
- Advanced Execution Options:
- Multiple input methods (text input, file upload, config-specified)
- Custom output filename overrides
- Debug mode toggle for detailed logging
- Validation checks before execution
- Template Management: Access to pre-built configuration templates
- Direct Execution: Start chains immediately from the load interface
YAML Editor Integration (/chains/yaml-editor)
- Professional code editor with full YAML syntax highlighting
- Five built-in templates: Basic, Advanced, Scripts, Multi-pass, Append
- Real-time validation with error detection and reporting
- Auto-save functionality and keyboard shortcuts (Ctrl+S, Ctrl+Shift+F)
- Light and dark themes for comfortable editing
- Direct execution: Save & Execute for immediate chain deployment
Enhanced Features:
- Configuration Validation: Pre-execution YAML and config validation
- Error Handling: Comprehensive error reporting with specific line numbers
- Template System: Quick-start templates for common workflow patterns
- Seamless Integration: Edit, validate, and execute in unified interface
# Complete workflow example:
# 1. Click "Load Chain" on /chains page → Full-page load interface
# 2. Upload: complex_chain.yaml → Rich analysis shows 5 prompts, 3 models
# 3. Preview: Raw YAML with syntax highlighting, template detection
# 4. Configure: Custom input method, debug mode enabled
# 5. Validate: Check for YAML errors and missing files
# 6. Execute: Start chain with comprehensive monitoring
# 7. Edit: Jump to YAML editor for quick modificationsChain multiple prompts for complex workflows:
# Create sample configuration
openrouter-chain --create-sample
# Run a chain
openrouter-chain -c chain_config.yaml
# With debug output
openrouter-chain -c chain_config.yaml --debugBasic Chain Configuration:
global_config:
model: "anthropic/claude-4-sonnet-20250522"
temperature: 0.7
input_file: document.md
output_file: processed_document.md
prompts:
prompt 1:
name: "grammar_check"
prompt_file: "prompts/grammar.json"
temperature: 0.2
prompt 2:
name: "style_improvement"
prompt_file: "prompts/style.json"
temperature: 0.8Chain Restart and Recovery:
OpenRouter Interface includes powerful restart functionality to recover from failures and save time/costs by avoiding re-execution of completed steps.
# Check execution status
openrouter-chain -c config.yaml --status-only
# Restart from failed steps automatically
openrouter-chain -c config.yaml --restart
# Force restart from specific step
openrouter-chain -c config.yaml --restart-from 4
# Clean status and start fresh
openrouter-chain -c config.yaml --clean-statusKey Benefits:
- Time Savings: Skip expensive LLM calls for completed steps
- Cost Efficiency: Avoid re-running successful operations
- Transparency:
.statusfiles show exactly what completed/failed - Flexible Recovery: Auto-detect restart points or force restart from any step
- Multi-file Support: Independent restart points per input file
Status File Example:
{
"chain_config": "my_chain.yaml",
"status": "failed",
"files": {
"chapter_21.md": {
"status": "failed",
"steps": {
"1": {"status": "completed", "name": "grammar_check", "time": 136.3},
"2": {"status": "completed", "name": "style_improvement", "time": 135.7},
"3": {"status": "failed", "name": "final_polish", "error": "API timeout"}
}
}
}
}When restarted, the chain will:
- Detect the previous failure at step 3
- Skip steps 1-2 (already completed)
- Resume from step 3 and continue to completion
- Track all new progress in the status file
Split documents into separate files by chapters, prologues, and epilogues:
# Basic usage - splits in same directory as input
split-chapters book.md
# Works with plain text files too
split-chapters book.txt -o chapters/
# Specify output directory
split-chapters book.md -o chapters/
# Dry run (preview without creating files)
split-chapters book.md --dry-run
# Overwrite existing files (default: skip existing)
split-chapters book.md --overwrite-existing
# Global command usage (after install-global.sh)
split-chapters --helpFeatures:
- Multiple Formats: Supports both markdown headers and plain text chapter markers
- Prologue and Epilogue: Detects and extracts Prologue/Prolog and Epilogue/Epilog sections
- Roman Numerals: Handles Roman numeral chapter numbers (I, II, III, IV, etc.)
- Smart Naming: Creates files as
prologue.md,chapter_1.md,epilogue.md, etc. - Title Stripping: Removes text after chapter number (e.g., "Chapter 1: The Beginning" becomes
chapter_1.md) - Duplicate Handling: Adds A, B, C suffixes for duplicate sections (
chapter_1_A.md,prologue_A.md) - Preamble Support: Saves content before first section to
preamble.md - Case Insensitive: Detects "chapter", "Chapter", "CHAPTER", etc.
Supported Chapter Formats:
# Markdown header formats:
# Prologue
# Chapter 1
# Chapter I: The Beginning
## Chapter 2 - The Middle
# Epilogue
# Plain text formats (no # prefix):
Prologue
Chapter 1
Chapter I: The Beginning
Chapter 2 - The Middle
Epilogue
Example Output:
Found Prologue at line 5 → prologue.md
→ Wrote 42 lines to prologue.md
Found Chapter 1 at line 50 → chapter_1.md
→ Wrote 1,245 lines to chapter_1.md
Found Chapter 2 at line 1,300 → chapter_2.md
→ Wrote 1,089 lines to chapter_2.md
Found Chapter 3 at line 2,400 → chapter_3.md
→ Wrote 1,367 lines to chapter_3.md
...
Found Chapter 15 at line 15,800 → chapter_15.md
→ Wrote 1,156 lines to chapter_15.md
Found Epilogue at line 17,000 → epilogue.md
→ Wrote 85 lines to epilogue.md
Writing preamble (342 lines) → preamble.md
============================================================
SUMMARY
============================================================
Input file: book.md
Output directory: /path/to/chapters/
Chapters written: 17
Chapter files written:
Prologue → prologue.md ( 42 lines)
Chapter 1 → chapter_1.md (1,245 lines)
Chapter 2 → chapter_2.md (1,089 lines)
...
Chapter 15 → chapter_15.md (1,156 lines)
Epilogue → epilogue.md ( 85 lines)
Use Cases:
- Book Processing: Split manuscripts for chapter-by-chapter editing (markdown or plain text)
- Chain Preparation: Prepare individual chapters for prompt chain processing
- Parallel Processing: Enable concurrent processing of multiple chapters
- Classic Literature: Process books with Roman numeral chapter numbering
- Plain Text Manuscripts: Work with unformatted text files without markdown
- Story Structure: Extract prologues, chapters, and epilogues separately
- Organization: Structure large documents for better version control
Integration with Chains:
# 1. Split book into chapters
split-chapters manuscript.md -o chapters/
# 2. Process each chapter with a chain
for chapter in chapters/chapter_*.md; do
openrouter-chain -c editing_chain.yaml --input "$chapter"
done
# 3. Combine results if needed
cat chapters/processed_*.md > final_manuscript.mdSupports 100+ models through OpenRouter:
- Anthropic: Claude 4, Claude 3.5 Sonnet, Claude Haiku
- OpenAI: GPT-4 Turbo, GPT-4o, GPT-3.5 Turbo
- Google: Gemini 2.0 Flash, Gemini 1.5 Pro
- Specialized: DeepSeek Coder, Llama models
- And many more...
Execute custom scripts before and after processing:
# Global scripts (run once per chain)
preprocessing:
script01: echo "Starting processing pipeline"
name01: "Initialize Pipeline"
script02: python3 scripts/validate_input.py
name02: "Validate Input"
postprocessing:
script01: python3 scripts/generate_report.py
name01: "Generate Report"
# Per-step scripts (run for each prompt)
prompts:
prompt 1:
prescript: "touch {input_file}.backup"
name: "content_processing"
prompt_file: "prompts/process.json"
postscript: "wc -l {output_file} > {output_file}.stats"Run prompts multiple times with iterative improvement:
prompts:
prompt 1:
name: "iterative_improvement"
prompt_file: "prompts/improve.json"
passes: 3 # Run 3 times
append: yes # Accumulate contentConvert between markdown and various document formats:
# Convert input docx to markdown before processing
input_convert:
enabled: true
from_format: docx # Auto-detects if not specified
# Convert final output to multiple formats
output_convert:
enabled: true
formats:
- docx
- pdf
- epub
# Per-step conversion for specific outputs
prompts:
prompt 2:
convert_output:
format: pdf
filename: step2_report.pdfSupported Formats:
- Input: docx, odt, rtf, html, epub, txt, pdf, latex, rst, org, mediawiki
- Output: docx, odt, rtf, html, epub, pdf, txt, latex, rst, org, mediawiki
Requirements:
pypandocPython package (auto-installed)pandocsystem binary (auto-installed by install scripts)- LaTeX for PDF generation (optional)
Merge all step outputs into a single file:
# Enable combine feature
combine: true
combined_file_name: all_steps_combined.md
prompts:
prompt 1:
name: "analysis"
prompt_file: "prompts/analyze.json"
prompt 2:
name: "enhancement"
prompt_file: "prompts/enhance.json"
prompt 3:
name: "finalization"
prompt_file: "prompts/finalize.json"
# Result: Creates markdown file with headers for each stepOutput Format:
# Combined Output - input_file.md
Generated: 2025-01-15 14:30:00
---
## Step 1: analysis
[output from step 1]
---
## Step 2: enhancement
[output from step 2]
---
## Step 3: finalization
[output from step 3]
---- Automatic Chunking: Handle large files automatically
- Intermediate Files: All step outputs preserved
- Size Validation: Detect processing issues
- Format Support: Markdown, text, JSON, YAML
# content_enhancement.yaml
global_config:
model: "anthropic/claude-4-sonnet-20250522"
temperature: 0.7
input_file: blog_draft.md
output_file: enhanced_blog.md
prompts:
prompt 1:
name: "grammar_foundation"
prompt_file: "prompts/grammar.json"
temperature: 0.2
prompt 2:
name: "engagement_boost"
prompt_file: "prompts/engagement.json"
temperature: 0.8
prompt 3:
name: "seo_optimization"
prompt_file: "prompts/seo.json"
temperature: 0.6# technical_docs.yaml
global_config:
model: "deepseek/deepseek-coder"
temperature: 0.3
preprocessing:
script01: python3 scripts/extract_code.py
name01: "Extract Code Blocks"
prompts:
prompt 1:
prescript: "python3 scripts/validate_syntax.py {input_file}"
name: "code_documentation"
prompt_file: "prompts/document_code.json"
postscript: "python3 scripts/test_examples.py {output_file}"
postprocessing:
script01: python3 scripts/generate_toc.py
name01: "Generate Table of Contents"# multi_file_processing.yaml
input_files:
- chapter1.md
- chapter2.md
- chapter3.md
output_pattern: "processed_{input_name}.md"
prompts:
prompt 1:
prescript: "echo 'Processing {input_file}' >> progress.log"
name: "content_enhancement"
prompt_file: "prompts/enhance.json"
postscript: "wc -w {output_file} >> word_counts.log"Process Word documents and export to multiple formats:
# document_conversion.yaml
input_file: manuscript.docx
output_file: processed_manuscript.md
# Convert input Word doc to markdown
input_convert:
enabled: true
from_format: docx
# Convert final output to multiple formats
output_convert:
enabled: true
formats:
- docx # Microsoft Word
- pdf # PDF document
- epub # eBook format
# Combine all steps for reference
combine: true
combined_file_name: processing_history.md
global_config:
model: "anthropic/claude-4-sonnet-20250522"
temperature: 0.7
prompts:
prompt 1:
name: "grammar_check"
prompt_file: "prompts/grammar.json"
temperature: 0.3
prompt 2:
name: "style_improvement"
prompt_file: "prompts/style.json"
temperature: 0.7
# Export this step as PDF report
convert_output:
format: pdf
filename: style_report.pdf
prompt 3:
name: "final_polish"
prompt_file: "prompts/polish.json"
temperature: 0.5
# Result: Creates processed_manuscript.md, .docx, .pdf, .epub
# Plus processing_history.md with all steps combined
# Plus style_report.pdf from step 2Process a book manuscript chapter by chapter:
# Step 1: Split manuscript into chapters
split-chapters manuscript.md -o manuscript_chapters/
# Step 2: Create a chapter processing chain
cat > chapter_editing.yaml << 'EOF'
global_config:
model: "anthropic/claude-4-sonnet-20250522"
temperature: 0.7
input_files:
- manuscript_chapters/chapter_1.md
- manuscript_chapters/chapter_2.md
- manuscript_chapters/chapter_3.md
# Add all chapters...
output_pattern: "edited_{input_name}.md"
prompts:
prompt 1:
name: "grammar_and_clarity"
prompt_file: "prompts/grammar.json"
temperature: 0.3
prompt 2:
name: "dialogue_enhancement"
prompt_file: "prompts/dialogue.json"
temperature: 0.7
prompt 3:
name: "pacing_review"
prompt_file: "prompts/pacing.json"
temperature: 0.6
passes: 2
EOF
# Step 3: Process all chapters
openrouter-chain -c chapter_editing.yaml
# Step 4: Combine edited chapters
cat manuscript_chapters/edited_chapter_*.md > final_manuscript.md
# Optional: Convert to multiple formats
openrouter-chain -c convert_final.yamlWith restart functionality:
# If processing fails on chapter 5
openrouter-chain -c chapter_editing.yaml --restart
# Check status of all chapters
openrouter-chain -c chapter_editing.yaml --status-only
# Restart specific chapter from step 2
openrouter-chain -c chapter_editing.yaml --restart-from 2// test_load_prompt.json
{
"title": "Content Quality Analysis",
"description": "Analyze content for clarity, coherence, and quality",
"persona": "You are a professional content analyst who provides clear, actionable feedback.",
"instructions": "Analyze the provided content for clarity, coherence, and overall quality. Provide specific recommendations for improvement.",
"review_criteria": "Evaluate based on: 1) Clarity of message, 2) Logical structure, 3) Grammar and style, 4) Overall effectiveness",
"output_format": "Provide a structured analysis with specific examples and recommendations."
}Web Usage:
- Click "Load Prompt" on main page
- Upload
test_load_prompt.json - Optionally upload
test_single_prompt_config.yamlfor custom API settings - Enter content: "This is my draft article about AI development..."
- Execute and view results instantly
# test_web_yaml_config.yaml
global_config:
model: "anthropic/claude-4-sonnet-20250522"
temperature: 0.7
max_tokens: 20000
input_file: test_input.md
output_file: test_web_output.md
preprocessing:
script01: echo "Starting web-loaded chain"
name01: "Initialize Chain"
prompts:
prompt 1:
name: "test_analysis"
prompt_file: "prompts/content_quality.json"
temperature: 0.3
prompt 2:
name: "final_polish"
prompt_file: "prompts/content_quality.json"
temperature: 0.8
append: yes
postprocessing:
script01: echo "Web chain complete"
name01: "Finalize Chain"Web Usage:
- Navigate to
/chainspage - Click "Load Chain" button
- Upload
test_web_yaml_config.yaml - Preview shows: 2 prompts, preprocessing/postprocessing scripts, append mode
- Enter content and start execution
- Monitor real-time progress in active chains section
- Complete Documentation - Full guides and API reference
- Configuration Guide - YAML configuration options
- Prompt Templates - Creating reusable prompts
- Troubleshooting - Common issues and solutions
# Help and information
openrouter-runner --help
openrouter-chain --help
openrouter-web --help
split-chapters --help
# Chapter splitting (supports markdown, plain text, Roman numerals)
split-chapters book.md
split-chapters book.txt -o chapters/
split-chapters book.md --dry-run
split-chapters book.md --overwrite-existing
# Debug and validation
openrouter-chain -c config.yaml --debug
python3 -c "import yaml; print(yaml.safe_load(open('config.yaml')))"
# Model and API testing
openrouter-runner -p prompts/test.json -i test.md -o test_output.mdopenrouter-interface/
├── src/openrouter_interface/ # Main Python package
│ ├── cli.py # CLI interface
│ ├── web.py # Web interface
│ ├── chain.py # Chain runner
│ ├── prompt_runner.py # Core processing engine
│ ├── prompt_chain_runner.py # Chain execution logic
│ └── split_chapters.py # Chapter splitter utility
├── config/ # Configuration files
├── prompts/ # Prompt templates
├── scripts/ # Utility scripts
├── tests/ # Test suite
└── docs/ # Documentation
# Run all tests
pytest
# Run with coverage
pytest --cov=openrouter_interface
# Run specific test files
pytest tests/test_cli.py
pytest tests/test_chain.py# Format code
black src tests
# Type checking
mypy src
# Linting
flake8 srcWe welcome contributions! Here's how to get started:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes with tests and documentation
- Run tests:
pytest && black src tests && flake8 src - Submit a pull request
- New Features: Prompt templates, chain configurations
- Model Support: Additional model integrations
- Documentation: Tutorials, examples, guides
- Testing: Test coverage improvements
- Performance: Optimization and profiling
- UI/UX: Web interface improvements
# Clone and install for development
git clone https://github.com/your-org/openrouter-interface.git
cd openrouter-interface
./install.sh dev
# Install pre-commit hooks
pip install pre-commit
pre-commit install
# Run development server
PYTHONPATH=src python3 -m openrouter_interface.web --debug- Bug Reports: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Complete Docs
- Feature Requests: GitHub Issues
Common Issues:
# API key not set
echo $OPENROUTER_API_KEY
# Permission issues
chmod +x install-global.sh
# Python path issues
which python3
python3 --versionDebug Mode:
# Enable detailed logging
openrouter-chain -c config.yaml --debug 2>&1 | tee debug.log
# Check configuration validity
python3 -c "import yaml; yaml.safe_load(open('config.yaml'))"This project is licensed under the MIT License - see the LICENSE file for details.
- OpenRouter for providing access to multiple AI models
- The Python community for excellent tools and libraries
- Contributors who help improve this project
- Stable: Core functionality tested and reliable
- Active Development: Regular updates and new features
- Well Tested: Comprehensive test suite
- Documented: Complete documentation and examples
- Per-Phase Scripts: Individual prescript/postscript for each step
- Variable Substitution: Dynamic {input_file}/{output_file} replacement
- Enhanced Monitoring: Improved web interface with real-time progress
- Model Compatibility: Automatic parameter filtering per model
- File Management: Advanced validation and size checking
- Load Buttons: Direct upload and execution of JSON prompts and YAML chains
- No-Registry Execution: Run files instantly without permanent storage
- Expanded Load Chain: Full-page configuration interface with rich preview and validation
- YAML Editor: Professional code editor with templates, syntax highlighting, and real-time validation
- Enhanced Error Handling: Comprehensive YAML parsing error detection and reporting
- Chapter Splitter: Standalone utility to split documents by chapters, prologues, and epilogues (supports markdown, plain text, Roman numerals)
Ready to get started? Follow the Quick Start guide above!