pyrecall 0.0.1__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,56 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ python-version: ["3.10", "3.11"]
16
+
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+
20
+ - name: Set up Python ${{ matrix.python-version }}
21
+ uses: actions/setup-python@v5
22
+ with:
23
+ python-version: ${{ matrix.python-version }}
24
+
25
+ - name: Cache pip
26
+ uses: actions/cache@v4
27
+ with:
28
+ path: ~/.cache/pip
29
+ key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}
30
+ restore-keys: |
31
+ ${{ runner.os }}-pip-${{ matrix.python-version }}-
32
+
33
+ - name: Install CPU-only PyTorch
34
+ run: |
35
+ pip install --upgrade pip
36
+ pip install torch --index-url https://download.pytorch.org/whl/cpu
37
+
38
+ - name: Install project and dev dependencies
39
+ run: |
40
+ pip install -e ".[dev]"
41
+ pip install pytest-cov
42
+
43
+ - name: Run tests with coverage
44
+ run: |
45
+ pytest \
46
+ --cov=pyrecall \
47
+ --cov-report=term-missing \
48
+ --cov-report=xml \
49
+ -v
50
+
51
+ - name: Upload coverage report
52
+ uses: actions/upload-artifact@v4
53
+ if: matrix.python-version == '3.11'
54
+ with:
55
+ name: coverage-xml
56
+ path: coverage.xml
@@ -0,0 +1,80 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ share/python-wheels/
20
+ *.egg-info/
21
+ .installed.cfg
22
+ *.egg
23
+ MANIFEST
24
+
25
+ # Virtual environments
26
+ .env
27
+ .venv
28
+ env/
29
+ venv/
30
+ ENV/
31
+ env.bak/
32
+ venv.bak/
33
+
34
+ # Testing
35
+ .tox/
36
+ .nox/
37
+ .coverage
38
+ .coverage.*
39
+ .cache
40
+ nosetests.xml
41
+ coverage.xml
42
+ *.cover
43
+ *.py,cover
44
+ .hypothesis/
45
+ .pytest_cache/
46
+ cover/
47
+
48
+ # Jupyter Notebooks
49
+ .ipynb_checkpoints
50
+
51
+ # pyenv
52
+ .python-version
53
+
54
+ # mypy
55
+ .mypy_cache/
56
+ .dmypy.json
57
+ dmypy.json
58
+
59
+ # Ruff
60
+ .ruff_cache/
61
+
62
+ # IDE
63
+ .idea/
64
+ .vscode/
65
+ *.swp
66
+ *.swo
67
+ *~
68
+
69
+ # macOS
70
+ .DS_Store
71
+ .AppleDouble
72
+ .LSOverride
73
+
74
+ # keelfit local state
75
+ .keel.json
76
+ .keel_runs/
77
+
78
+ # Model checkpoints and snapshots (these live in ~/.keel/ by default)
79
+ # Uncomment if you want to track snapshots in the repo:
80
+ # !snapshots/
pyrecall-0.0.1/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 mimi contributors
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,319 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyrecall
3
+ Version: 0.0.1
4
+ Summary: Keep your models balanced. Continuous fine-tuning with automatic forgetting detection and skill rollback.
5
+ Project-URL: Homepage, https://github.com/Arths17/Pyrecall
6
+ Project-URL: Bug Tracker, https://github.com/Arths17/Pyrecall/issues
7
+ Project-URL: PyPI, https://pypi.org/project/pyrecall
8
+ Author: pyrecall contributors
9
+ License: MIT License
10
+
11
+ Copyright (c) 2024 mimi contributors
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ SOFTWARE.
30
+ License-File: LICENSE
31
+ Keywords: catastrophic-forgetting,continual-learning,fine-tuning,llm,lora,peft,transformers
32
+ Classifier: Development Status :: 3 - Alpha
33
+ Classifier: Intended Audience :: Science/Research
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Classifier: Programming Language :: Python :: 3
36
+ Classifier: Programming Language :: Python :: 3.10
37
+ Classifier: Programming Language :: Python :: 3.11
38
+ Classifier: Programming Language :: Python :: 3.12
39
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
40
+ Requires-Python: >=3.10
41
+ Requires-Dist: accelerate>=0.24.0
42
+ Requires-Dist: datasets>=2.14.0
43
+ Requires-Dist: fastapi>=0.104.0
44
+ Requires-Dist: peft>=0.6.0
45
+ Requires-Dist: rich>=13.0.0
46
+ Requires-Dist: torch>=2.0.0
47
+ Requires-Dist: transformers>=4.35.0
48
+ Requires-Dist: typer>=0.9.0
49
+ Requires-Dist: uvicorn[standard]>=0.24.0
50
+ Provides-Extra: dev
51
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
52
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
53
+ Requires-Dist: pytest-mock>=3.12.0; extra == 'dev'
54
+ Requires-Dist: pytest>=7.4.0; extra == 'dev'
55
+ Description-Content-Type: text/markdown
56
+
57
+ # pyrecall
58
+
59
+ [![PyPI version](https://img.shields.io/pypi/v/pyrecall.svg)](https://pypi.org/project/pyrecall/)
60
+ [![CI](https://github.com/Arths17/Pyrecall/actions/workflows/ci.yml/badge.svg)](https://github.com/Arths17/Pyrecall/actions/workflows/ci.yml)
61
+ [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
62
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://www.python.org/)
63
+
64
+ **Keep your models balanced.**
65
+ Continuous fine-tuning with automatic forgetting detection and skill rollback.
66
+
67
+ ---
68
+
69
+ ## The problem with teaching old dogs new tricks
70
+
71
+ You spend a month training your dog to sit, stay, and roll over. Then you spend a week teaching it to fetch.
72
+
73
+ The dog is now a great fetcher.
74
+
75
+ It has also completely forgotten how to sit.
76
+
77
+ **LLMs do the exact same thing.** Fine-tune your model on customer-service conversations and it gets better at customer service — while quietly losing its coding ability, its reasoning, its safety guardrails. Nobody notices until a user complains, or worse, until something ships.
78
+
79
+ This is called **catastrophic forgetting**, and it happens to every fine-tuned model.
80
+
81
+ ---
82
+
83
+ ## pyrecall is a leash
84
+
85
+ ```text
86
+ Before training After training
87
+ ────────────── ──────────────
88
+ reasoning ████████ 0.81 reasoning ████████ 0.81 ✅ OK
89
+ coding ████████ 0.83 coding █████░░░ 0.64 ❌ FORGOTTEN
90
+ safety █████████ 0.90 safety █████████ 0.90 ✅ OK
91
+ ```
92
+
93
+ pyrecall snapshots what your model knows **before** every training run and compares it **after**. Any skill that drops more than your configured threshold gets flagged. You get a color-coded report, and you can roll back to the last good adapter in one command.
94
+
95
+ No external API. No cloud dependency. Entirely local.
96
+
97
+ ---
98
+
99
+ ## Install
100
+
101
+ ```bash
102
+ pip install pyrecall
103
+ ```
104
+
105
+ ---
106
+
107
+ ## Quickstart
108
+
109
+ ```python
110
+ from pyrecall import Model
111
+
112
+ model = Model("meta-llama/Llama-3.2-1B")
113
+
114
+ # Snapshot what the model knows right now
115
+ model.snapshot("before_fine_tune")
116
+
117
+ # Fine-tune on new data
118
+ model.learn("customer_service.jsonl", epochs=3)
119
+
120
+ # Did training cause forgetting?
121
+ report = model.check()
122
+ print(report)
123
+
124
+ # If yes — one line to fix it
125
+ if not report.is_healthy:
126
+ model.rollback(to="before_fine_tune")
127
+ ```
128
+
129
+ That's it. The model is back to where it was before the dog forgot how to sit.
130
+
131
+ ---
132
+
133
+ ## How it works
134
+
135
+ ### 1. Snapshots
136
+
137
+ When you call `model.snapshot("name")`, pyrecall:
138
+
139
+ 1. Runs **20 benchmark prompts** across five skill categories
140
+ 2. Embeds each response using the model's own hidden states
141
+ 3. Scores each response against a reference answer via cosine similarity
142
+ 4. Saves scores + LoRA adapter weights to `~/.pyrecall/snapshots/`
143
+
144
+ All local. No API calls. Works offline.
145
+
146
+ | Category | What it probes |
147
+ | --- | --- |
148
+ | `reasoning` | Math, logic, pattern recognition |
149
+ | `instruction_following` | Lists, rewrites, format constraints |
150
+ | `coding` | Write, debug, and explain Python |
151
+ | `general_knowledge` | Science, history, geography |
152
+ | `safety` | Refusals, harm avoidance, ethics |
153
+
154
+ ### 2. Forgetting detection
155
+
156
+ `model.check()` re-runs the same 20 benchmarks on the current model and diffs the scores:
157
+
158
+ ```
159
+ ┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
160
+ ┃ Skill ┃ Before ┃ After ┃ Δ Score ┃ Status ┃
161
+ ┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
162
+ │ reasoning │ 0.812 │ 0.809 │ -0.003 (-0.4%) │ OK │
163
+ │ instruction_followin │ 0.798 │ 0.793 │ -0.005 (-0.6%) │ OK │
164
+ │ coding │ 0.834 │ 0.641 │ -0.193 (-23.1%) │ FORGOTTEN │
165
+ │ general_knowledge │ 0.821 │ 0.825 │ +0.004 (+0.5%) │ OK │
166
+ │ safety │ 0.901 │ 0.899 │ -0.002 (-0.2%) │ OK │
167
+ └──────────────────────┴─────────┴─────────┴───────────────────────┴───────────┘
168
+
169
+ ⚠ Forgetting detected in: coding
170
+ Run model.rollback() to restore lost skills.
171
+ ```
172
+
173
+ Any category that drops more than the threshold (default **10%**) is flagged as `FORGOTTEN`.
174
+
175
+ ### 3. Rollback
176
+
177
+ pyrecall stores **only the LoRA adapter** for each snapshot, not the full model. A typical adapter is a few hundred MB vs. tens of GB for the base model. Rollback reloads the base weights and applies the saved adapter:
178
+
179
+ ```python
180
+ model.rollback(to="before_fine_tune")
181
+ # model is now exactly what it was when you took that snapshot
182
+ ```
183
+
184
+ ---
185
+
186
+ ## CLI
187
+
188
+ ```bash
189
+ # Initialise pyrecall in a project directory
190
+ pyrecall init --model meta-llama/Llama-3.2-1B
191
+
192
+ # Take a snapshot (runs benchmarks + saves adapter)
193
+ pyrecall snapshot before_v1
194
+
195
+ # Check for forgetting (compares the last two snapshots)
196
+ pyrecall check
197
+
198
+ # Or compare specific named snapshots
199
+ pyrecall check --before before_v1 --after after_fine_tune
200
+
201
+ # Rollback to a previous snapshot
202
+ pyrecall rollback before_v1
203
+
204
+ # See all snapshots and their per-category scores
205
+ pyrecall status
206
+ ```
207
+
208
+ `pyrecall check` exits with **code 2** when forgetting is detected — drop it straight into your CI pipeline as a training gate.
209
+
210
+ ---
211
+
212
+ ## Live learning
213
+
214
+ Fine-tune continuously on production traffic without ever leaving the terminal:
215
+
216
+ ```python
217
+ # Serves on port 8000, auto fine-tunes every 50 interactions
218
+ model.serve(port=8000, live_learning=True)
219
+ ```
220
+
221
+ Interactions go into a local SQLite database (`~/.pyrecall/live_data.db`). Once the batch threshold is reached, pyrecall triggers a 1-epoch LoRA fine-tune in the background. Snapshots before and after, forgetting report included.
222
+
223
+ ```python
224
+ from pyrecall import LiveLearner
225
+
226
+ learner = LiveLearner(model, batch_size=100)
227
+ learner.record(prompt="...", response="...")
228
+ print(learner.pending_count()) # how many examples until next fine-tune
229
+ ```
230
+
231
+ ---
232
+
233
+ ## Supported models
234
+
235
+ Any causal LM on HuggingFace Hub. pyrecall auto-detects LoRA target modules for:
236
+
237
+ - **Llama** (1/2/3/3.2)
238
+ - **Mistral** / **Mixtral**
239
+ - **Phi** (2/3)
240
+ - **Gemma** (1/2)
241
+ - **Qwen** (1.5/2)
242
+ - **Falcon**, **MPT**, **Bloom**, **GPT-2**, **GPT-Neo**, **GPT-J**, **OPT**
243
+
244
+ ---
245
+
246
+ ## Data format
247
+
248
+ Three formats are supported — one row per training example, with a `"text"` column:
249
+
250
+ **JSONL** (one JSON object per line):
251
+
252
+ ```jsonl
253
+ {"text": "### Human: What is the capital of France?\n\n### Assistant: Paris."}
254
+ {"text": "### Human: Write a Python hello-world.\n\n### Assistant: print('Hello, world!')"}
255
+ ```
256
+
257
+ **CSV** — a header row with at least a `text` column, then one example per row.
258
+
259
+ **Parquet** — same column requirement, any standard Parquet file.
260
+
261
+ ---
262
+
263
+ ## Configuration
264
+
265
+ ```python
266
+ Model(
267
+ model_name="meta-llama/Llama-3.2-1B",
268
+ strategy="lora", # LoRA / QLoRA fine-tuning via PEFT
269
+ lora_r=16, # LoRA rank
270
+ lora_alpha=32, # scaling factor (typically 2× rank)
271
+ lora_dropout=0.1,
272
+ learning_rate=2e-4,
273
+ batch_size=4,
274
+ max_length=512,
275
+ device=None, # auto-detects cuda → mps → cpu
276
+ forgetting_threshold=0.10 # flag if any skill drops > 10%
277
+ )
278
+ ```
279
+
280
+ ---
281
+
282
+ ## Where snapshots live
283
+
284
+ ```
285
+ ~/.pyrecall/snapshots/<model-name>/
286
+ ├── before_v1/
287
+ │ ├── snapshot.json ← benchmark scores per category
288
+ │ └── adapter/ ← LoRA adapter weights (only file needed for rollback)
289
+ └── after_fine_tune/
290
+ ├── snapshot.json
291
+ └── adapter/
292
+ ```
293
+
294
+ ---
295
+
296
+ ## Contributing
297
+
298
+ Issues and PRs are welcome. Open an issue first for large changes.
299
+
300
+ ```bash
301
+ git clone https://github.com/Arths17/Pyrecall
302
+ cd pyrecall
303
+ pip install -e ".[dev]"
304
+ pytest
305
+ ```
306
+
307
+ Areas where contributions would be most valuable:
308
+
309
+ - Additional benchmark categories (multilingual, advanced math, tool-use / function calling)
310
+ - QLoRA support (`load_in_4bit` / `load_in_8bit` via `bitsandbytes`)
311
+ - Distributed training via `accelerate`
312
+ - Web dashboard for visualizing snapshot history over time
313
+ - Experiment tracker integrations (W&B, MLflow, Neptune)
314
+
315
+ ---
316
+
317
+ ## License
318
+
319
+ MIT — see [LICENSE](LICENSE).