fleet-rlm 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ # Authors and Contributors
2
+
3
+ This project is developed and maintained by the Qredence team.
4
+
5
+ ## Core Contributors
6
+
7
+ - Qredence - Project lead and architecture
8
+
9
+ ## Acknowledgments
10
+
11
+ This project is based on academic research by:
12
+
13
+ - **Alex L. Zhang** (MIT CSAIL)
14
+ - **Omar Khattab** (Stanford)
15
+ - **Tim Kraska** (MIT)
16
+
17
+ > Reference: [Recursive Language Models](https://arxiv.org/abs/2501.123) (Zhang, Kraska, Khattab, 2025)
18
+
19
+ ## Contributing
20
+
21
+ We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on how to contribute to this project.
22
+
23
+ ## License
24
+
25
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Qredence
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,397 @@
1
+ Metadata-Version: 2.4
2
+ Name: fleet-rlm
3
+ Version: 0.1.0
4
+ Summary: Python package + Typer CLI implementation of the RLM with Modal notebook
5
+ Author: Qredence
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/qredence/fleet-rlm
8
+ Project-URL: Repository, https://github.com/qredence/fleet-rlm
9
+ Project-URL: Issues, https://github.com/qredence/fleet-rlm/issues
10
+ Keywords: dspy,llm,modal,recursive-language-model,rlm
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Requires-Python: >=3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ License-File: AUTHORS.md
22
+ Requires-Dist: aiohttp
23
+ Requires-Dist: dspy==3.1.3
24
+ Requires-Dist: modal>=1.3.2
25
+ Requires-Dist: pyyaml
26
+ Requires-Dist: typer>=0.12
27
+ Provides-Extra: dev
28
+ Requires-Dist: pre-commit>=3.7; extra == "dev"
29
+ Requires-Dist: pytest>=8.2; extra == "dev"
30
+ Requires-Dist: ruff>=0.8; extra == "dev"
31
+ Dynamic: license-file
32
+
33
+ # fleet-rlm
34
+
35
+ A Python package implementing **Recursive Language Models (RLM)** with DSPy and Modal for secure, cloud-based code execution. This project demonstrates how LLMs can treat long contexts as external environments, using programmatic code exploration in sandboxed environments.
36
+
37
+ **Reference**: [Recursive Language Models](https://arxiv.org/abs/2501.123) (Zhang, Kraska, Khattab, 2025)
38
+
39
+ ---
40
+
41
+ ## Overview
42
+
43
+ Recursive Language Models (RLM) represent an inference strategy where:
44
+
45
+ - LLMs treat long contexts as an **external environment** rather than direct input
46
+ - The model writes Python code to programmatically explore data
47
+ - Code executes in a sandboxed environment (Modal cloud)
48
+ - Only relevant snippets are sent to sub-LLMs for semantic analysis
49
+
50
+ This package provides both a comprehensive Jupyter notebook and a Typer CLI for running RLM workflows.
51
+
52
+ ---
53
+
54
+ ## Features
55
+
56
+ - **Secure Cloud Execution**: Code runs in Modal's isolated sandbox environment
57
+ - **DSPy Integration**: Built on DSPy 3.1.3 with custom signatures for RLM tasks
58
+ - **CLI Interface**: Typer-based CLI with multiple demo commands
59
+ - **Extensible Tools**: Support for custom tools that bridge sandbox and host
60
+ - **Secret Management**: Secure handling of API keys via Modal secrets
61
+
62
+ ---
63
+
64
+ ## Technology Stack
65
+
66
+ | Component | Technology |
67
+ | ------------------ | ------------------------------------ |
68
+ | Language | Python >= 3.10 |
69
+ | Package Manager | `uv` (modern Python package manager) |
70
+ | Core Framework | DSPy 3.1.3 |
71
+ | Cloud Sandbox | Modal |
72
+ | CLI Framework | Typer >= 0.12 |
73
+ | Testing | pytest >= 8.2 |
74
+ | Linting/Formatting | ruff >= 0.8 |
75
+
76
+ ---
77
+
78
+ ## Installation
79
+
80
+ ```bash
81
+ # Clone the repository
82
+ git clone https://github.com/qredence/fleet-rlm.git
83
+ cd fleet-rlm
84
+
85
+ # Install dependencies with uv
86
+ uv sync
87
+
88
+ # For development (includes test tools)
89
+ uv sync --extra dev
90
+ ```
91
+
92
+ ---
93
+
94
+ ## Quick Start
95
+
96
+ ### 1. Configure Environment
97
+
98
+ Create a `.env` file in the repository root:
99
+
100
+ ```bash
101
+ # Required
102
+ DSPY_LM_MODEL=openai/gemini-3-flash-preview
103
+ DSPY_LLM_API_KEY=sk-...
104
+
105
+ # Optional
106
+ DSPY_LM_API_BASE=https://your-litellm-proxy.com
107
+ DSPY_LM_MAX_TOKENS=65536
108
+ ```
109
+
110
+ ### 2. Setup Modal
111
+
112
+ ```bash
113
+ # Authenticate with Modal
114
+ uv run modal setup
115
+
116
+ # Create a Modal volume for data
117
+ uv run modal volume create rlm-volume-dspy
118
+
119
+ # Create Modal secret for API keys
120
+ uv run modal secret create LITELLM \
121
+ DSPY_LM_MODEL=... \
122
+ DSPY_LM_API_BASE=... \
123
+ DSPY_LLM_API_KEY=... \
124
+ DSPY_LM_MAX_TOKENS=...
125
+ ```
126
+
127
+ ### 3. Run CLI Commands
128
+
129
+ ```bash
130
+ # Show all available commands
131
+ uv run fleet-rlm --help
132
+
133
+ # Run a basic demo
134
+ uv run fleet-rlm run-basic --question "What are the first 12 Fibonacci numbers?"
135
+
136
+ # Doc-analysis commands require --docs-path
137
+ # Extract architecture from documentation
138
+ uv run fleet-rlm run-architecture \
139
+ --docs-path rlm_content/dspy-knowledge/dspy-doc.txt \
140
+ --query "Extract all modules and optimizers"
141
+
142
+ # Extract API endpoints
143
+ uv run fleet-rlm run-api-endpoints --docs-path rlm_content/dspy-knowledge/dspy-doc.txt
144
+
145
+ # Find error patterns
146
+ uv run fleet-rlm run-error-patterns --docs-path rlm_content/dspy-knowledge/dspy-doc.txt
147
+
148
+ # Inspect trajectory on a document sample
149
+ uv run fleet-rlm run-trajectory \
150
+ --docs-path rlm_content/dspy-knowledge/dspy-doc.txt \
151
+ --chars 5000
152
+
153
+ # Use custom regex tool
154
+ uv run fleet-rlm run-custom-tool \
155
+ --docs-path rlm_content/dspy-knowledge/dspy-doc.txt \
156
+ --chars 5000
157
+
158
+ # Check Modal secrets are configured
159
+ uv run fleet-rlm check-secret
160
+ ```
161
+
162
+ ---
163
+
164
+ ## CLI Commands
165
+
166
+ | Command | Description |
167
+ | -------------------- | -------------------------------------------- |
168
+ | `run-basic` | Basic code generation (Fibonacci example) |
169
+ | `run-architecture` | Extract DSPy architecture from documentation |
170
+ | `run-api-endpoints` | Extract API endpoints using batched queries |
171
+ | `run-error-patterns` | Find and categorize error patterns in docs |
172
+ | `run-trajectory` | Examine RLM execution trajectory |
173
+ | `run-custom-tool` | Demo with custom regex tool |
174
+ | `check-secret` | Verify Modal secret presence |
175
+ | `check-secret-key` | Inspect specific secret key |
176
+
177
+ ---
178
+
179
+ ## Architecture
180
+
181
+ ```
182
+ ┌─────────────────────────────────────────────────────────────┐
183
+ │ LOCAL (Jupyter/CLI) │
184
+ │ ┌─────────────┐ ┌──────────────┐ ┌──────────────────┐ │
185
+ │ │ Planner LM │ │ RLM Module │ │ ModalInterpreter │ │
186
+ │ │ (decides │→ │ (builds │→ │ (manages sandbox │ │
187
+ │ │ what code │ │ signatures) │ │ lifecycle) │ │
188
+ │ │ to write) │ │ │ │ │ │
189
+ │ └─────────────┘ └──────────────┘ └──────────────────┘ │
190
+ │ │ │ │ │
191
+ │ │ │ JSON stdin │ gRPC │
192
+ │ │ ↓ ↓ │
193
+ └───────────┼────────────────┼──────────────────┼─────────────┘
194
+ │ │ │
195
+ │ │ ▼
196
+ │ │ ┌──────────────────────┐
197
+ │ │ │ MODAL CLOUD │
198
+ │ │ │ ┌────────────────┐ │
199
+ │ └────→│ │ Sandbox │ │
200
+ │ │ │ - Python 3.12 │ │
201
+ │ │ │ - Volume /data │ │
202
+ │ │ │ - Secrets │ │
203
+ │ │ └────────────────┘ │
204
+ │ │ │ │
205
+ │ │ ▼ │
206
+ │ │ ┌────────────────┐ │
207
+ │ │ │ Driver Process │ │
208
+ │ │ │ - exec() code │ │
209
+ │ │ │ - tool bridging│ │
210
+ │ │ └────────────────┘ │
211
+ │ └──────────────────────┘
212
+ │ │
213
+ └────────────────────────────────┘
214
+ tool_call requests
215
+ (llm_query, etc.)
216
+ ```
217
+
218
+ ---
219
+
220
+ ## Package Structure
221
+
222
+ ```
223
+ src/fleet_rlm/
224
+ ├── __init__.py # Package exports
225
+ ├── cli.py # Typer CLI interface
226
+ ├── config.py # Environment configuration
227
+ ├── driver.py # Sandbox protocol driver
228
+ ├── interpreter.py # ModalInterpreter implementation
229
+ ├── runners.py # High-level RLM demo runners
230
+ ├── signatures.py # DSPy signatures for RLM tasks
231
+ └── tools.py # Custom RLM tools
232
+ ```
233
+
234
+ ### Module Descriptions
235
+
236
+ - **`config.py`**: Loads environment variables, configures DSPy's planner LM, guards against Modal package shadowing
237
+ - **`cli.py`**: Typer CLI with commands for running demos and checking secrets
238
+ - **`driver.py`**: Runs inside Modal's sandbox as a long-lived JSON protocol driver
239
+ - **`interpreter.py`**: DSPy-compatible `CodeInterpreter` managing Modal sandbox lifecycle
240
+ - **`runners.py`**: High-level functions orchestrating complete RLM workflows
241
+ - **`signatures.py`**: RLM task signatures (ExtractArchitecture, ExtractAPIEndpoints, etc.)
242
+ - **`tools.py`**: Custom tools like `regex_extract()` for RLM use
243
+
244
+ ---
245
+
246
+ ## RLM Patterns
247
+
248
+ ### Pattern 1: Navigate → Query → Synthesize
249
+
250
+ 1. Code searches for headers in documentation
251
+ 2. `llm_query()` extracts info from relevant sections
252
+ 3. `SUBMIT(modules=list, optimizers=list, principles=str)` returns structured output
253
+
254
+ ### Pattern 2: Parallel Chunk Processing
255
+
256
+ 1. Split documents into chunks by headers
257
+ 2. `llm_query_batched([chunk1, chunk2, ...])` executes in parallel
258
+ 3. Aggregate results into final output
259
+
260
+ ### Pattern 3: Stateful Multi-Step
261
+
262
+ 1. Search for keywords in documentation
263
+ 2. Save matches to variable (persists across iterations)
264
+ 3. Query LLM to categorize findings
265
+ 4. Iterate with refined queries
266
+
267
+ ---
268
+
269
+ ## Testing
270
+
271
+ ```bash
272
+ # Run all tests
273
+ uv run pytest
274
+
275
+ # Or via Make
276
+ make test
277
+ ```
278
+
279
+ | Test File | Purpose |
280
+ | ------------------------- | ----------------------------------------------------- |
281
+ | `test_cli_smoke.py` | CLI help display, command discovery, error handling |
282
+ | `test_config.py` | Environment loading, quoted values, fallback API keys |
283
+ | `test_driver_protocol.py` | SUBMIT output mapping, tool call round-trips |
284
+ | `test_tools.py` | Regex extraction, groups, flags |
285
+
286
+ ---
287
+
288
+ ## Development
289
+
290
+ ```bash
291
+ # Install dev dependencies
292
+ make sync-dev
293
+
294
+ # Run linting
295
+ make lint
296
+
297
+ # Format code
298
+ make format
299
+
300
+ # Run all checks
301
+ make check
302
+
303
+ # Run release validation (lint, tests, build, twine check)
304
+ make release-check
305
+
306
+ # Install pre-commit hooks
307
+ make precommit-install
308
+ make precommit-run
309
+ ```
310
+
311
+ Release process documentation is in [`RELEASING.md`](RELEASING.md), including the TestPyPI-first workflow.
312
+
313
+ ---
314
+
315
+ ## Jupyter Notebook
316
+
317
+ The original implementation is available as a Jupyter notebook:
318
+
319
+ ```bash
320
+ # Launch Jupyter Lab
321
+ uv run jupyter lab notebooks/rlm-dspy-modal.ipynb
322
+
323
+ # Execute headlessly (for CI/validation)
324
+ uv run jupyter nbconvert \
325
+ --to notebook \
326
+ --execute \
327
+ --inplace \
328
+ --ExecutePreprocessor.timeout=3600 \
329
+ notebooks/rlm-dspy-modal.ipynb
330
+ ```
331
+
332
+ ---
333
+
334
+ ## Security
335
+
336
+ - **Secrets Management**: All credentials stored in Modal secrets, never in code
337
+ - **Sandbox Isolation**: Code executes in Modal's isolated sandbox environment
338
+ - **Local .env**: Contains API keys - is gitignored and should never be committed
339
+ - **Shadow Protection**: `config.py` guards against `modal.py` naming conflicts
340
+
341
+ ---
342
+
343
+ ## Troubleshooting
344
+
345
+ ### "Planner LM not configured"
346
+
347
+ Set `DSPY_LM_MODEL` and `DSPY_LLM_API_KEY` in `.env`, then restart your shell or kernel.
348
+
349
+ ### "Modal sandbox process exited unexpectedly"
350
+
351
+ ```bash
352
+ uv run modal token set
353
+ uv run modal volume list
354
+ ```
355
+
356
+ ### "No module named 'modal'"
357
+
358
+ ```bash
359
+ uv sync
360
+ ```
361
+
362
+ ### Modal package shadowing
363
+
364
+ Remove any `modal.py` file or `__pycache__/modal.*.pyc` in the working directory.
365
+
366
+ ---
367
+
368
+ ## References
369
+
370
+ - **RLM Paper**: [Recursive Language Models](https://arxiv.org/abs/2501.123)
371
+ - **DSPy Docs**: https://dspy-docs.vercel.app/
372
+ - **Modal Docs**: https://modal.com/docs
373
+ - **UV Docs**: https://docs.astral.sh/uv/
374
+
375
+ ---
376
+
377
+ ## License
378
+
379
+ This project is licensed under the [MIT License](LICENSE).
380
+
381
+ ---
382
+
383
+ ## Contributing
384
+
385
+ Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
386
+
387
+ ---
388
+
389
+ ## Acknowledgments
390
+
391
+ This project is based on research from the [Recursive Language Models paper](https://arxiv.org/abs/2501.123) by Zhang, Kraska, and Khattab (2025).
392
+
393
+ Built with:
394
+
395
+ - [DSPy](https://dspy-docs.vercel.app/) - Framework for programming with language models
396
+ - [Modal](https://modal.com/) - Serverless computing for AI
397
+ - [Typer](https://typer.tiangolo.com/) - CLI framework