Summarize Research Papers from the Command Line
Level: Intermediate · Time: 35–45 min · Category: Command line
Tags: Python · OpenAI SDK · PDF · Summarization
Build a CLI that reads a PDF and asks the Research Computing OpenAI-compatible API for a structured summary: the problem, the approach, the key findings, and the limitations. It is a practical tool for reading groups and literature reviews.
Prerequisites
- LLM API Guide — create an API key, choose a model ID, and test the endpoint.
- Use Python with the LLM gateway — review the OpenAI SDK and base URL pattern.
Confirm Python 3 and pip are available before you start:
- macOS / Linux
- Windows (PowerShell)
python3 --version # expect 3.9 or newer
python3 -m pip --version
python --version # expect 3.9 or newer
python -m pip --version
If Python is not found, install it from python.org. On the supercomputer, load a Python module instead (for example module load python), then run the checks again.
What you will do
- Create a Python CLI workspace with the OpenAI SDK and a PDF reader.
- Extract text from a PDF file.
- Ask the API for a structured, section-by-section summary.
- Add batch mode and a markdown export.
Build it step by step
1. Create the CLI workspace
Install the OpenAI SDK, an environment loader, a PDF reader, and a terminal formatter.
- macOS / Linux
- Windows (PowerShell)
mkdir paper-summarizer
cd paper-summarizer
python3 -m venv .venv
source .venv/bin/activate
pip install openai python-dotenv pypdf rich
mkdir paper-summarizer
cd paper-summarizer
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install openai python-dotenv pypdf rich
If you track this project with Git, do not commit your virtual environment or any secrets. Create a .gitignore file in the project folder with at least these lines:
.venv/
.env
__pycache__/
.venv/ is large and specific to your machine, and .env holds your API key — neither belongs in a shared repository. New to Git? See Track a New Project with Git.
2. Add API connection values
Your program needs three values to reach the API: your API key, the gateway URL, and a model ID. You keep them in a small text file named .env so they stay out of your code — and out of Git.
What is a .env file, and where does it go?
A .env ("dot-env") file is a plain text file that stores settings as NAME=value lines, one per line. Programs read it at startup so you never have to paste secrets like API keys directly into your code.
- The filename is literally
.env— a leading dot with nothing before it (notconfig.envor.env.txt). - It goes in your project folder: the same directory you just created, where the
.venvlives and where you run your commands. - The
python-dotenvpackage loads it withload_dotenv(), which looks for.envin the folder you run the program from. - Because it holds a secret, never commit it to Git (see the note below).
Create a new file named .env in your project folder — in VS Code use File → New File, or run code .env in the terminal — then paste these three lines and replace the placeholders:
OPENAI_API_KEY="paste-your-key-here"
OPENAI_BASE_URL="https://openai.rc.asu.edu/v1"
RC_LLM_MODEL="paste-model-id-here"
Get your API key and a valid model ID from the LLM API guide. The OPENAI_BASE_URL above is already correct for the Research Computing gateway.
- Use the
/v1base URL, not the full/chat/completionsURL, for the Python SDK. - Use a model ID that appears in the portal model list for your account.
- Keep
.envout of Git: add a line containing.envto your.gitignore(see Track a New Project with Git).
3. Extract text from a PDF
Test extraction before involving the model. Put a sample paper named paper.pdf in the folder, then confirm the reader works.
python -c "from pypdf import PdfReader; r = PdfReader('paper.pdf'); print(len(r.pages), 'pages')"
If you see a page count, the extraction step works and you can move on.
4. Ask for the first summarizer
Name the SDK, the input, and the output you want. Keep the first version to a single readable summary.
You are helping me build a beginner Python CLI that summarizes a research paper using an OpenAI-compatible API.
Project goal:
- Command: python summarize.py paper.pdf
- Read text from the PDF with pypdf.
- Send the text to the chat completions API through the OpenAI Python SDK.
- Print a readable summary to the terminal with rich.
- Read OPENAI_API_KEY, OPENAI_BASE_URL, and RC_LLM_MODEL from a .env file.
Constraints:
- Keep the first version in one file named summarize.py.
- If the extracted text is very long, truncate it to a safe character limit for now.
- Add friendly errors for a missing file, missing API key, or API failure.
Please:
1. Create summarize.py.
2. Explain how the API request is assembled.
3. Show the command to run it on paper.pdf.
5. Ask for structured output
Consistent sections make summaries easy to compare across papers.
Please improve the summarizer to return consistent sections:
Sections:
- Problem
- Methods
- Key findings
- Limitations
- Why it matters
Constraints:
- Ask the model to use exactly these headings.
- If the paper is long, summarize it in parts and then combine the parts into one final summary.
- Explain how to test it on a long paper.
6. Add one feature of your own
Choose a single improvement that makes the tool more useful.
Please add one focused feature to the summarizer:
Feature:
- Add a --save option that writes the summary to a markdown file next to the PDF.
Constraints:
- Keep the default terminal output unchanged when --save is not used.
- Keep the code beginner-readable.
- Explain how to test the new option.
More prompts to try
- Add a --batch folder option that summarizes every PDF in a folder.
- Add a --questions option that answers a few specific questions about the paper.
- Add a short "one-sentence takeaway" line at the top of each summary.
Troubleshooting
- If extraction returns almost no text, the PDF may be scanned images. Those need OCR, which is beyond this tutorial.
- If a PDF is password protected,
pypdfwill fail. Use an unlocked copy. - If the request is rejected for length, lower the truncation limit or use the part-by-part approach from step 5.
- Always read the summary against the paper. Models can miss or overstate findings.
Next steps
- Build a Q&A Assistant over Your Notes — search many documents, not just one.
- Build a Streamlit AI Chatbot — put a friendly UI in front of a summarizer.