An LLM-based workflow for iteratively improving academic papers using two AI agents (Author and Reviewer) in an adversarial/collaborative loop.
📚 Documentation Hub | Quick Start | API Setup | Verify API | Implementation Details
The Paper Polish System implements an "Actor-Critic" workflow that leverages two LLM agents to polish academic papers:
- Reviewer Agent: Acts as a strict academic peer reviewer, providing critical feedback and determining when the paper is "acceptable"
- Author Agent: Takes feedback and rewrites the paper to address reviewer concerns
- Workflow Loop: Iterates between agents until the paper is accepted or max rounds reached
✅ Double-Blind Design: Author doesn't know the iteration number (maintains fairness)
✅ LaTeX Preservation: Uses pylatexenc to ensure LaTeX syntax integrity
✅ latexdiff Generation: Automatically generates visual diffs between iterations
✅ Robust Error Handling: Handles API rate limits, parsing errors, and LaTeX corruption
✅ Comprehensive Testing: 80+ unit tests with full mocking (no API calls during testing)
✅ Flexible Configuration: JSON config files for API credentials and parameters
✅ Logging Control: INFO/DEBUG logging shows all LLM calls for debugging
✅ API Verification: Built-in command to validate Gemini API setup before running
paper_polish_system/
├── config.py # Configuration management
├── models.py # Gemini API abstraction
├── latex_utils.py # LaTeX validation & safety
├── io_handler.py # Input/output & latexdiff
├── workflow.py # Main workflow controller
├── main.py # CLI entry point
├── agents/
│ ├── __init__.py
│ └── base_agent.py # Author & Reviewer agents
├── tests/ # 80+ comprehensive unit tests
│ ├── test_config.py
│ ├── test_models.py
│ ├── test_latex_utils.py
│ ├── test_agents.py
│ ├── test_io_handler.py
│ └── test_workflow.py
└── requirements.txt
- Python 3.10+
- Conda environment:
ml - Gemini API key (from https://aistudio.google.com/app/apikey)
-
Activate conda environment:
conda activate ml
-
Install dependencies:
pip install -r requirements.txt
-
Initialize configuration file:
python main.py init
This creates
config.jsonwith the following structure:{ "api": { "api_key": "<YOUR_GEMINI_API_KEY_HERE>", "model": "gemini-1.5-pro", "temperature": 0.7, "max_tokens": 4096 }, "workflow": { "max_rounds": 5 }, "reviewer": { "strict_mode": true }, "logging": { "level": "WARNING" } } -
Fill in your Gemini API key in
config.json:"api_key": "AIzaSyD... (your actual key)"
This project includes comprehensive documentation. Here's where to find what you need:
- QUICKSTART.md - Get up and running in 5 minutes (recommended for first-time users)
- API_QUICK_REFERENCE.md - Quick lookup for API configuration and model selection
- API_VERIFICATION.md - Complete guide to the
verifycommand, troubleshooting tips, and validation checks - API_VERIFICATION_IMPLEMENTATION.md - Technical details on the verification feature implementation
- IMPLEMENTATION.md - Detailed architecture and implementation guide
- PROJECT_COMPLETION.md - Project status and feature overview
- STATUS.md - Current development status and capabilities
- DOCUMENTATION_ENHANCEMENT.md - Documentation improvements made
- CLI_UPGRADE.md - Modern CLI with Typer and Rich
- CLI_UPGRADE_COMPLETE.md - Complete CLI upgrade report
- README_CN.md - Complete Chinese documentation (中文文档)
- New to the system? Start with QUICKSTART.md
- Need API help? See API_QUICK_REFERENCE.md or API_VERIFICATION.md
- Want technical details? Read IMPLEMENTATION.md
python main.py --helpOutput:
Usage: main.py [OPTIONS] COMMAND [ARGS]...
Paper Polish System - Iterative LLM-based Academic Paper Improvement
╭─ Commands ──────────────────────────────────────────────────────────────────╮
│ polish Polish an academic paper using LLM agents. │
│ init Initialize configuration file with sample values. │
│ verify Verify Gemini API configuration and connectivity. │
╰─────────────────────────────────────────────────────────────────────────────╯
Before running the main workflow, validate your API configuration:
python main.py verifyThis command:
- ✓ Checks if the config file exists
- ✓ Validates that an API key is configured
- ✓ Tests the connection to Gemini API
- ✓ Confirms the specified model is available
- ✓ Provides helpful error messages if anything is wrong
Success output:
✓ API key found (length: 39 chars)
✓ API connection successful
✨ All checks passed! Your API is ready to use.
Troubleshooting? See API_VERIFICATION.md for detailed help.
python main.py polish paper.texThis command:
- Loads your LaTeX paper
- Creates an output directory with timestamp
- Runs up to 5 iterations (configurable)
- Displays a beautiful progress bar with status
- Shows final results in a formatted table
- Saves all iterations, feedback, and diffs
For detailed command options, see QUICKSTART.md#command-line-options or run:
python main.py polish --helpAfter running, you'll find:
output/
└── paper_<timestamp>/
├── iteration_01.tex # Paper after iteration 1
├── iteration_02.tex # Paper after iteration 2
├── feedback_01.txt # Reviewer feedback for iteration 1
├── diff_01.tex # latexdiff output (if available)
├── final_paper.tex # Final polished paper
├── SUMMARY.md # Human-readable summary
├── metadata.json # Structured workflow data
└── paper_polish.log # Detailed logs
The system currently supports Google Gemini API. For comprehensive API configuration guide, see API_QUICK_REFERENCE.md.
-
Edit config.json and update the
apisection:{ "api": { "api_key": "your-new-api-key-here", "provider": "gemini", "model": "gemini-1.5-pro", "temperature": 0.7, "max_tokens": 4096, "timeout_seconds": 120 } } -
Get a Gemini API Key from https://aistudio.google.com/app/apikey
-
Verify your configuration:
python main.py verify
Available Gemini Models:
gemini-1.5-pro- Most powerful (recommended for academic papers)gemini-1.5-flash- Faster and lower costgemini-2.0-pro- Latest model (if available)
📖 For detailed information on:
- Configuration parameters: See API_QUICK_REFERENCE.md
- API verification: See API_VERIFICATION.md
- Troubleshooting: See API_QUICK_REFERENCE.md#troubleshooting-api-issues
For detailed model comparison and selection, see API_QUICK_REFERENCE.md#model-selection-guide.
gemini-1.5-pro (Recommended)
- ✅ Most powerful model
- ✅ Best paper understanding
- ✅ Supports long context (100K tokens)
- ❌ Slightly higher cost
- Best for: High-quality feedback on academic papers
gemini-1.5-flash
- ✅ Faster response time
- ✅ Lower cost
- ✅ Still high quality
- ❌ May miss some details
- Best for: Quick iterations, initial drafts
Test your setup with:
python main.py verifyFor detailed troubleshooting, see API_VERIFICATION.md.
Input Paper (LaTeX)
↓
[ITERATION LOOP]
├─→ Reviewer: Analyze & Feedback
│ └─→ Return (feedback_text, is_acceptable)
│
├─→ If NOT acceptable:
│ └─→ Author: Rewrite with feedback
│ └─→ Validate LaTeX preservation
│ └─→ Save iteration & diff
│
└─→ Loop until: accepted OR max_rounds reached
↓
Output: Final paper + metadata + diffs
For comprehensive architectural details, see IMPLEMENTATION.md.
Key modules:
- config.py - Configuration management and validation
- models.py - LLM abstraction (Gemini API with rate limiting)
- agents/base_agent.py - Author and Reviewer agents
- latex_utils.py - LaTeX validation and structure analysis
- io_handler.py - File I/O and diff generation
- workflow.py - Main workflow orchestration
- main.py - CLI entry point
All modules have comprehensive unit tests with mocking (no real API calls):
# Run all tests
python -m pytest tests/ -v
# Run specific test file
python -m pytest tests/test_agents.py -v
# Run with coverage
python -m pytest tests/ --covTotal Test Coverage: 80+ unit tests across all modules
For detailed test information, see IMPLEMENTATION.md#testing.
- Workflow initialization
- Paper acceptance scenarios
- Max rounds exceeded
- Error handling
- Metadata tracking
Complete configuration documentation is available in API_QUICK_REFERENCE.md.
{
"api": {
"api_key": "your-gemini-key",
"provider": "gemini",
"model": "gemini-1.5-pro",
"temperature": 0.7,
"max_tokens": 4096,
"timeout_seconds": 120
},
"workflow": {
"max_rounds": 5,
"timeout_per_call": 60
},
"reviewer": {
"strict_mode": true,
"check_items": [
"grammatical_correctness",
"logical_clarity",
"academic_rigor",
"citation_accuracy",
"structure_coherence"
]
},
"author": {
"preserve_formatting": true,
"maintain_references": true
},
"latex": {
"validation_enabled": true,
"use_pylatexenc": true
},
"logging": {
"level": "WARNING",
"log_file": "paper_polish.log",
"log_llm_calls": false
},
"output": {
"save_iterations": true,
"generate_latexdiff": true,
"output_dir": "output"
}
}The system ensures LaTeX integrity through multiple checks:
- Brace Balancing: Validates
{...}nesting - Environment Balancing: Checks
\begin{...}\end{...}pairing - Citation Preservation: Ensures citations aren't lost/added
- Section Preservation: Maintains document structure
- Math Block Detection: Identifies and protects equations
If LaTeX corruption is detected, the iteration is saved but warnings are logged.
See IMPLEMENTATION.md#latex-safety for details.
Complete dependency list available in requirements.txt. Key packages:
google-generativeai: Gemini API clientpylatexenc: LaTeX parsing & validationpydantic: Configuration validationpython-dotenv: Environment variable supporttyper: Modern CLI frameworkrich: Beautiful terminal formatting
"API key not found" error:
"latexdiff not found" warning:
- This is optional; system works without it
- Install:
sudo apt-get install texlive-extra-utils(Linux)
"ModuleNotFoundError" for google.generativeai:
- Run:
pip install -r requirements.txt
Test failures:
- Run:
python -m pytest tests/ -v - See IMPLEMENTATION.md#testing
For comprehensive troubleshooting, see:
- Per Iteration: ~30-60 seconds (API latency dependent)
- API Calls: 1 reviewer + 1 author per iteration
- Rate Limiting: 1 second minimum between calls
- Token Budget: ~4000 tokens per response
- Support for OpenAI API (GPT-4)
- Support for Anthropic Claude
- Section-by-section processing
- Multi-reviewer consensus
- Web UI
- Integration with Overleaf
- Batch processing
See PROJECT_COMPLETION.md for completed features.
MIT License - See LICENSE file for details
Contributions welcome! Please:
- Add tests for new features
- Ensure all tests pass:
pytest tests/ -q - Follow existing code style
- Update documentation
- Quick Start: QUICKSTART.md
- API Setup: API_QUICK_REFERENCE.md
- Verification: API_VERIFICATION.md
- Technical Details: IMPLEMENTATION.md
- Status Report: STATUS.md
- Chinese Docs: README_CN.md
Built with: Python 3.10+ | Google Gemini API | pylatexenc | Typer | Rich
Latest Status: ✅ Complete and production-ready
Test Coverage: 80+ unit tests with full mocking