CLI utility that summarizes single files into teaching briefs using DSPy
Project description
dspyteach – DSPy File Teaching Analyzer
DSPy-powered CLI that analyzes source files (one or many) and produces teaching briefs
Each run captures:
- an overview of the file and its major sections
- key teaching points, workflows, and pitfalls highlighted in the material
- a polished markdown brief suitable for sharing with learners
The implementation mirrors the multi-file tutorial (tutorials/multi-llmtxt_generator) but focuses on per-file inference. The program is split into:
dspy_file/signatures.py– DSPy signatures that define inputs/outputs for each stepdspy_file/file_analyzer.py– the main DSPy module that orchestrates overview, teaching extraction, and report composition. It now wraps the final report stage withdspy.Refine, pushing for 450–650+ word briefs.dspy_file/file_helpers.py– utilities for loading files and rendering the markdown briefdspy_file/analyze_file_cli.py– command line entry point that configures the local model and prints results. It can walk directories, apply glob filters, and batch-generate briefs.
Quick start
-
Confirm Python 3.10–3.12 is available and pull at least one OpenAI-compatible model (Ollama, LM Studio, or a hosted provider).
-
From the repository root, create an isolated environment and install dependencies:
uv venv -p 3.12 source .venv/bin/activate uv sync
-
Run a smoke test to confirm the CLI is wired up:
dspyteach --helpExpected result: the help output lists available flags and displays the active version string.
-
Analyze a sample file to confirm end-to-end output:
dspyteach path/to/example.pyExpected result: the command prints a teaching brief to stdout and writes a
.teaching.mdfile underdspy_file/data/.
Requirements
- Python 3.10-3.12+
- DSPy installed in the environment
- A language-model backend. You can choose between:
- Ollama (default): run it locally with the model
hf.co/unsloth/Qwen3-4B-Instruct-2507-GGUF:Q6_K_XLpulled. - LM Studio (OpenAI-compatible): start the LM Studio server (
lms server start) and download a model such asqwen3-4b-instruct-2507@q6_k_xl. - Any other OpenAI-compatible endpoint: point the CLI at a hosted provider by supplying an API base URL and key (defaults to
gpt-5).
- Ollama (default): run it locally with the model
- (Optional)
.envfile for DSPy configuration.dotenvloads variables such asDSPYTEACH_PROVIDER,DSPYTEACH_MODEL,DSPYTEACH_API_BASE,DSPYTEACH_API_KEY, andOPENAI_API_KEY.
Example output
[example-data after running a few passes]
Installation
Install with uv (recommended for local development)
uv venv -p 3.12
source .venv/bin/activate
uv sync
Expected result: the virtual environment contains the project dependencies and dspyteach --version reports the local build.
Install from PyPI
pip install dspyteach
Expected result: running dspyteach --help prints the CLI usage banner from the installed package.
Configure the language model
The CLI now supports configurable OpenAI-compatible providers in addition to the default Ollama runtime. You can override the backend via CLI options or environment variables:
# Use LM Studio's OpenAI-compatible server with its default port
dspyteach path/to/project \
--provider lmstudio \
--model qwen3-4b-instruct-2507@q6_k_xl \
--api-base http://localhost:1234/v1
# Environment variable alternative (e.g. inside .env)
export DSPYTEACH_PROVIDER=lmstudio
export DSPYTEACH_MODEL=qwen3-4b-instruct-2507@q6_k_xl
export DSPYTEACH_API_BASE=http://localhost:1234/v1
dspyteach path/to/project
LM-Studio Usage Notes
LM Studio must expose its local server before you run the CLI. Start it from the Developer tab inside the LM Studio app or via lms server start (details in the LM Studio configuration guide); otherwise the CLI will exit early with a connection warning.
OpenAI-compatible others usage
For hosted OpenAI-compatible services, set --provider openai, supply --api-base if needed, and pass an API key either through --api-key, DSPYTEACH_API_KEY, or the standard OPENAI_API_KEY. To keep a local Ollama model running after the CLI finishes, add --keep-provider-alive.
Usage
Run the CLI to extract a teaching brief from a single file:
dspyteach path/to/your_file
Expected result: the CLI prints a markdown teaching brief to stdout and saves a copy under dspy_file/data/.
You can also point the CLI at a directory. The tool will recurse by default:
dspyteach path/to/project --glob "**/*.py" --glob "**/*.md"
Expected result: each matched file produces its own .teaching.md report in the output directory.
Use --non-recursive to stay in the top-level directory, add --glob repeatedly to narrow the target set, and pass --raw to print the raw DSPy prediction object instead of the formatted report.
Command examples
-
Analyze a single markdown file
dspyteach docs/example.mdExpected result: the CLI prints a teaching brief and stores
docs__example.teaching.mdin the output directory. -
Process a repository while skipping generated assets
dspyteach ./repo \ --glob "**/*.py" \ --glob "**/*.md" \ --exclude-dirs "build/,dist/,data/"
Expected result: only
.pyand.mdfiles outside the excluded directories are analyzed. -
Generate refactor templates instead of teaching briefs
dspyteach ./repo --mode refactor --prompt refactor_prompt_template
Expected result:
.refactor.mdfiles appear alongside the teaching outputs with guidance tailored to the selected prompt.
Need to double-check files before the model runs? Add --confirm-each (alias --interactive) to prompt before every file, accepting with Enter or skipping with n.
To omit specific subdirectories entirely, pass one or more --exclude-dirs options. Each value can list comma-separated relative paths (for example --exclude-dirs "build/,venv/" --exclude-dirs data/raw). The analyzer ignores any files whose path begins with the provided prefixes.
Prefer short flags? The common options include -r (--raw), -m (--mode), -nr (--non-recursive), -g (--glob), -i (--confirm-each), -ed (--exclude-dirs), and -o (--output-dir). Mix and match them as needed.
Refactor files/dirs
Want to scaffold refactor prompt templates instead of teaching briefs? Switch the mode:
dspyteach path/to/project --mode refactor --glob "**/*.md"
Additional Information
The CLI reuses the same file resolution pipeline but feeds each document through the bundled dspy-file_refactor-prompt_template.md instructions (packaged under dspy_file/prompts/), saving .refactor.md files alongside the teaching reports. Teaching briefs remain the default (--mode teach), so existing workflows continue to work unchanged.
When multiple templates live in dspy_file/prompts/, the refactor mode surfaces a picker so you can choose which one to use. You can also point at a specific template explicitly with -p/--prompt, passing either a bundled name (-p refactor_prompt_template) or an absolute path to your own Markdown prompt.
Each run only executes the analyzer for the chosen mode. When you pass --mode refactor the teaching inference pipeline stays idle, and you can alias the command (for example alias dspyrefactor='dspyteach --mode refactor') if you prefer refactor templates to be the default in your shell.
To change where reports land, supply --output-dir /path/to/reports. When omitted the CLI writes to dspy_file/data/ next to the module. Every run prints the active model name and the resolved output directory before analysis begins so you can confirm the environment at a glance. For backwards compatibility the installer also registers dspy-file-teaching as an alias.
Each analyzed file is saved under the chosen directory with a slugged name (e.g. src__main.teaching.md or src__main.refactor.md). If a file already exists, the CLI appends a numeric suffix to avoid overwriting previous runs.
The generated brief is markdown that mirrors the source material:
- Overview paragraphs for quick orientation
- Section-by-section bullets capturing the narrative
- Key concepts, workflows, pitfalls, and references learners should review
- A
dspy.Refinewrapper keeps retrying until the report clears a length reward (defaults scale to ~50% of the source word count, with min/max clamps), so the content tends to be substantially longer than a single LM call. - If a model cannot honour DSPy's structured-output schema, the CLI prints a
Structured output fallbacknotice and heuristically parses the textual response so you still get usable bullets.
Behind the scenes the CLI:
- Loads environment variables via
python-dotenv. - Configures DSPy with the provider selected via CLI or environment variables (Ollama by default).
- Resolves all requested files, reads contents, runs the DSPy
FileTeachingAnalyzermodule, and prints a human-friendly report for each. - Persists each report to the configured output directory so results are easy to revisit.
- Stops the Ollama model when appropriate so local resources are returned to the pool.
Extending
- Adjust the
TeachingReportsignature or add new chains indspy_file/file_analyzer.pyto capture additional teaching metadata. - Customize the render logic in
dspy_file.file_helpers.render_predictionif you want richer CLI output or structured JSON. - Tune
TeachingConfiginsidefile_analyzer.pyto raisemax_tokens, adjust theRefineword-count reward, or add extra LM kwargs. - Add more signatures and module stages to capture additional metadata (e.g., security checks) and wire them into
FileAnalyzer.
Releasing
Maintainer release steps live in docs/RELEASING.md.
Troubleshooting
- If the program cannot connect to Ollama, verify that the server is running on
http://localhost:11434and the requested model has been pulled. - When you see
ollama command not found, ensure theollamabinary is on yourPATH. - For encoding errors, the helper already falls back to
latin-1, but you can add more fallbacks infile_helpers.read_file_contentif needed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dspyteach-0.1.6b1.tar.gz.
File metadata
- Download URL: dspyteach-0.1.6b1.tar.gz
- Upload date:
- Size: 28.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efca9050d6d079b4d8c9616ef0a6695b1a3c4c50383d79f50454321eb4ad53f4
|
|
| MD5 |
3d78e2a249212fb41ec4ff50b53c75bc
|
|
| BLAKE2b-256 |
953362174ab8580c037f0116dea638dc239a6790470d6fefb279a63a7c292ce1
|
File details
Details for the file dspyteach-0.1.6b1-py3-none-any.whl.
File metadata
- Download URL: dspyteach-0.1.6b1-py3-none-any.whl
- Upload date:
- Size: 26.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a5041d4a9e80d77fd72738d95d17ec3c992e4f4bbc601b1a4c400c731d2c68a
|
|
| MD5 |
7c9982301ca0f70148972203c5951d5a
|
|
| BLAKE2b-256 |
9c00810ede353ad81997032ab238060fe5151a458a0aca0ae671563dd61bf229
|