|
#!/usr/bin/env python3 |
|
""" |
|
Denario Paper Generator with Zhipu GLM Support |
|
|
|
Generates NeurIPS-format research papers using GLM-4.7 and Perplexity for citations. |
|
|
|
Usage: |
|
1. Create input_files/data_description.md with your research description |
|
2. Set environment variables (or edit this script) |
|
3. Run: python generate_paper.py |
|
|
|
Requirements: |
|
- Python 3.12+ |
|
- denario (pip install denario) |
|
- TinyTeX for PDF compilation |
|
- Patched reader.py files (run patch_reader.py first) |
|
""" |
|
|
|
import os |
|
import sys |
|
import time |
|
|
|
# ============================================================================ |
|
# CONFIGURATION - Edit these values or set as environment variables |
|
# ============================================================================ |
|
|
|
# Zhipu API (https://open.bigmodel.cn) |
|
ZHIPU_API_KEY = os.environ.get("ZHIPU_API_KEY", "YOUR_ZHIPU_API_KEY_HERE") |
|
ZHIPU_BASE_URL = "https://open.bigmodel.cn/api/coding/paas/v4" |
|
|
|
# Perplexity API for citations (https://perplexity.ai) |
|
PERPLEXITY_API_KEY = os.environ.get("PERPLEXITY_API_KEY", "YOUR_PERPLEXITY_API_KEY_HERE") |
|
|
|
# Model settings |
|
MODEL_NAME = "glm-4.7" # Options: glm-4.5-air, glm-4.5, glm-4.6, glm-4.7 |
|
MAX_OUTPUT_TOKENS = 16384 |
|
TEMPERATURE = 0.7 |
|
|
|
# Rate limiting (Zhipu has per-minute limits) |
|
RATE_LIMIT_DELAY = 2 # seconds between API calls |
|
|
|
# Journal format |
|
JOURNAL = "NeurIPS" # Options: NeurIPS, ICML, Nature, etc. |
|
|
|
# ============================================================================ |
|
# SETUP |
|
# ============================================================================ |
|
|
|
# Add TinyTeX to PATH (adjust for your system) |
|
tinytex_paths = [ |
|
f"{os.environ['HOME']}/Library/TinyTeX/bin/universal-darwin", # macOS |
|
f"{os.environ['HOME']}/.TinyTeX/bin/x86_64-linux", # Linux |
|
f"{os.environ['HOME']}/bin", # Generic |
|
] |
|
for path in tinytex_paths: |
|
if os.path.exists(path): |
|
os.environ["PATH"] = f"{path}:" + os.environ["PATH"] |
|
break |
|
|
|
# Set API credentials |
|
os.environ["OPENAI_API_KEY"] = ZHIPU_API_KEY |
|
os.environ["OPENAI_BASE_URL"] = ZHIPU_BASE_URL |
|
os.environ["PERPLEXITY_API_KEY"] = PERPLEXITY_API_KEY |
|
|
|
# ============================================================================ |
|
# RATE LIMITING MONKEY-PATCH |
|
# ============================================================================ |
|
|
|
from langchain_openai import ChatOpenAI |
|
|
|
_original_invoke = ChatOpenAI.invoke |
|
_original_stream = ChatOpenAI.stream |
|
|
|
def _patched_invoke(self, *args, **kwargs): |
|
time.sleep(RATE_LIMIT_DELAY) |
|
return _original_invoke(self, *args, **kwargs) |
|
|
|
def _patched_stream(self, *args, **kwargs): |
|
time.sleep(RATE_LIMIT_DELAY) |
|
return _original_stream(self, *args, **kwargs) |
|
|
|
ChatOpenAI.invoke = _patched_invoke |
|
ChatOpenAI.stream = _patched_stream |
|
print(f"Applied {RATE_LIMIT_DELAY}s rate limit delay") |
|
|
|
# ============================================================================ |
|
# MAIN GENERATION |
|
# ============================================================================ |
|
|
|
from denario import Denario |
|
from denario.llm import LLM |
|
|
|
def main(): |
|
print(f"=== Denario Paper Generator ===") |
|
print(f"Model: {MODEL_NAME}") |
|
print(f"Journal: {JOURNAL}") |
|
print() |
|
|
|
# Check for data description |
|
if not os.path.exists("input_files/data_description.md"): |
|
print("ERROR: input_files/data_description.md not found!") |
|
print("Create this file with your research description first.") |
|
sys.exit(1) |
|
|
|
# Initialize Denario |
|
d = Denario( |
|
project_dir=".", |
|
clear_project_dir=False |
|
) |
|
|
|
# Create LLM instance |
|
llm = LLM( |
|
name=MODEL_NAME, |
|
max_output_tokens=MAX_OUTPUT_TOKENS, |
|
temperature=TEMPERATURE |
|
) |
|
|
|
# Step 1: Generate Research Idea |
|
print("=== Step 1: Generating Research Idea ===") |
|
d.get_idea(llm=llm) |
|
print("Idea generation complete!") |
|
|
|
# Step 2: Generate Methodology |
|
print("\n=== Step 2: Generating Methodology ===") |
|
d.get_method(llm=llm) |
|
print("Methodology generation complete!") |
|
|
|
# Step 3: Generate Paper with Citations |
|
print(f"\n=== Step 3: Generating {JOURNAL} Paper with Citations ===") |
|
print("(This will take 15-30 minutes...)") |
|
d.get_paper(llm=llm, journal=JOURNAL, add_citations=True) |
|
|
|
print("\n=== Paper generation complete! ===") |
|
print("Output files:") |
|
print(" - ./paper/paper_v1_preliminary.pdf") |
|
print(" - ./paper/paper_v2_no_citations.pdf") |
|
print(" - ./paper/paper_v3_citations.pdf <-- Final paper") |
|
|
|
if __name__ == "__main__": |
|
main() |