- Python 98.9%
- Shell 1.1%
- rename modules: addestramento->training, azioni->actions, casi->cases, estrazione->extraction, indice->index, utilita->utils, piano->plan - rename CLI commands: addestra->train, aggiorna->update, casi->cases, esegui->run; flags --percorso->--path, --modello->--model, --richiesta->--request, --si->--yes - English identifiers, config keys, JSON artifact keys and action names (create_folder, create_file, copy_file, ...) across code, tests and docs - rename files/dirs: esempi->examples, requisiti*.txt->requirements*.txt, installa_dipendenze.sh->install_dependencies.sh, modelli->models, tesi->thesis, promt.md->prompt.md, config/ultimo_addestramento.txt->last_training.txt - translate README, system prompt, CLI help, log/print messages and the demo dataset contents to English - update .gitignore for the renamed paths Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai> |
||
|---|---|---|
| examples/archive | ||
| migenta | ||
| tests | ||
| .gitignore | ||
| install_dependencies.sh | ||
| LICENSE | ||
| migenta.py | ||
| README.md | ||
| requirements-training.txt | ||
| requirements.txt | ||
Migenta - local document agent for legacy devices
Migenta turns a folder of company documents (jobs, quotes, orders, delivery notes...) into a "memory" a local chatbot can consult, so you can say: "create the new job C2025-007 starting from this customer request" and get as output the already-organized folder (subfolders, file names, quote draft) just like all the previous jobs.
Everything works 100% offline on a 2014 laptop (e.g. ASUS S550CB, i7-3537U, 8 GB RAM, HDD): no cloud, no GPU.
Key idea: low CPU/RAM, more storage
Legacy devices have little RAM and a slow CPU but plenty of disk. Migenta therefore moves the computational cost entirely into the training phase (one-off) and leaves only light file operations at runtime:
| Phase | What happens | Cost |
|---|---|---|
Training (train) |
reads PDF/DOCX/XLSX/images, extracts the text and saves it in index/texts/*.txt; builds a BM25 index in JSON (index/index.json) and the case list (index/cases.json) |
CPU/RAM once, then only storage |
Chat (chat / run) |
retrieves similar cases with pure-Python BM25 (no embeddings in RAM), sends the lightweight model (GGUF on CPU, 4k context) a few-shot prompt with the examples, receives ONLY a JSON array of actions, which a stdlib engine executes on the filesystem | very low CPU, very low RAM, reads only text from disk |
No ChromaDB, no sentence-transformers, no torch/onnx at runtime:
the only heavy dependency (llama-cpp-python) is needed for chat only and is
optional for training/retrieval/tests.
Architecture
migenta.py CLI launcher (python3 migenta.py <command>)
migenta/
config.py defaults + config/migenta.json (user override)
utils.py logging, JSON, hash, slug (stdlib)
extraction.py PDF/DOCX/XLSX/CSV/JSON/OCR -> text, with sidecar cache
index.py pure-Python BM25 index, saved as JSON on disk
cases.py "cases" = folders of the history (jobs/quotes)
training.py pipeline: scan -> sidecar -> index -> cases (incremental)
retrieval.py query -> chunks + cases + similar projects (BM25)
llm.py GGUF on CPU (llama-cpp), auto-download with resume
plan.py few-shot prompt + robust parsing of the JSON plan
actions.py stdlib action engine, writes confined to the work dir
chat.py chatbot REPL
cli.py commands: info, train, update, cases, chat, run, test
examples/archive/ demo dataset (2 jobs + 1 quote, fictional)
tests/ unittest tests, standard library only
index/ (generated) text sidecars + BM25 index + cases + manifest
config/migenta.json (generated/user) configuration
models/llm/ .gguf models
data/ work dir: this is where new jobs are born
Flow of a request:
user request ──> BM25 retrieval on index.json (disk, no extra RAM)
──> few-shot prompt with similar cases (structure + names + truncated texts)
──> lightweight GGUF LLM (CPU, n_ctx=4096) -> ONLY a JSON array of actions
──> robust parse -> dry-run / confirm -> stdlib engine executes
──> incremental re-training of only the new files (manifest hash/mtime)
Installation
# 1. (optional) virtualenv
python3 -m venv .venv && source .venv/bin/activate
# 2. core dependencies: only llama-cpp-python, compiled CPU-only for legacy devices
./install_dependencies.sh
# or by hand:
CMAKE_ARGS="-DLLAMA_BLAS=OFF -DLLAMA_NATIVE=OFF" pip install -r requirements.txt
# 3. ONLY if you need to train on non-textual documents (PDF/DOCX/XLSX/images):
pip install -r requirements-training.txt
sudo apt-get install tesseract-ocr tesseract-ocr-eng # only for image OCR
LLM model: if models/llm/ is empty, the first use downloads it
automatically (with resume) from the URL in config/migenta.json.
| Hardware | Recommended model | Typical RAM |
|---|---|---|
| 8 GB (ASUS S550CB) | mistral-7b-instruct-v0.2.Q4_K_M.gguf (~4.1 GB on disk) |
~4.5 GB |
| 4 GB | mistral-7b-instruct-v0.2.Q2_K.gguf or phi-2.Q4_K_M.gguf |
~2-3 GB |
To change model: python3 migenta.py chat --model phi-2.Q4_K_M.gguf
or edit default_model in config/migenta.json.
Usage
# 0. quick try with the bundled demo dataset
python3 migenta.py train --path examples/archive
python3 migenta.py info
python3 migenta.py cases
# 1. ONE-OFF training on your real folder (jobs/quotes)
python3 migenta.py train --path /path/to/your/jobs_folder
# 2. interactive chatbot
python3 migenta.py chat
# inside the chat:
# migenta> create the new job C2025-007 starting from the request in /tmp/customer_request.pdf
# (shows the JSON plan -> asks for confirmation -> executes -> updates the index)
# 3. one-shot from scripts (dry-run by default, --yes executes without asking)
python3 migenta.py run --request "create job C2025-008 from /tmp/other_request.txt"
python3 migenta.py run --request "..." --yes
# 4. incremental re-training (only new/modified files, thanks to the manifest)
python3 migenta.py update
python3 migenta.py train --path /another/folder # adds new cases to the same index
# 5. automatic tests (stdlib, no model required)
python3 migenta.py test
Expected structure of the source folder
No rigid structure is required: Migenta registers as a "case" every folder
that directly contains files, and classifies input/output with configurable
globs (input_folders / output_folders in config/migenta.json).
Example that works well:
archive/
jobs/
C2024-001/
00_customer_request/ <- input (glob 00_* / *request* / *customer*)
request.pdf
01_quote/ <- output (glob 01_* / *quote* / ...)
quote_C2024-001.pdf
technical_attachment.pdf
C2024-002/ ...
quotes/
O2024-010/ ...
When a new job is requested, the model receives the structure of these cases (folder names, file names) and the truncated texts of the most similar documents, and returns a JSON that imitates it:
{
"folder": "jobs/C2025-007",
"subfolders": [
{"name": "00_customer_request",
"files": [{"name": "customer_request.pdf", "source": "/tmp/customer_request.pdf"}]},
{"name": "01_quote",
"files": [{"name": "quote_C2025-007.md", "content": "# Quote C2025-007\n..."}]}
]
}
The parser also accepts a direct array of actions
([{"action": "create_folder", ...}, ...]) and, in any case, a deterministic
compiler (plan.compile_structure) translates the structure into executable
actions. A heuristic (plan.link_sources) automatically links the files cited
in the request (e.g. /tmp/request.pdf) to the "..." placeholders left by
the model in the input folders. So even a heavily quantized model (Q2_K)
produces a correct executable plan: the "brain" is in the examples on disk
and in the deterministic code, not in the LLM.
Safety
Writes (create/rename/delete/copy-destination) are confined to the
work_dir (default data/, also configurable as an absolute path
to the real jobs folder). Reads (copy from external sources,
read_file) can point to any path, to consume documents just received
from the customer. Every chat execution is preceded by a dry-run and a
confirmation, unless --yes.
Configuration (config/migenta.json)
Created/read automatically; the defaults are in migenta/config.py.
Main fields:
| Field | Default | Meaning |
|---|---|---|
work_dir |
data |
where new jobs are created (writes confined here) |
training_dir |
archive |
source folder of the history |
index_dir |
index |
text sidecars + BM25 index + cases + manifest |
default_model / model_url |
mistral-7b Q4_K_M | GGUF and URL for auto-download |
n_ctx / n_threads / n_gpu_layers |
4096 / auto / 0 | CPU-only, reduced context |
max_tokens / temperature |
768 / 0.2 | little output = little compute |
top_k / max_doc_chars |
3 / 1200 | how many similar cases and how much text to give the model |
input_folders / output_folders |
00_*, *request*, ... |
globs to classify subfolders |
Tests
python3 migenta.py test # or: python3 -m unittest discover -s tests
37 tests on the BM25 index, plan parsing/compilation (both formats), source linking, the action engine (including path confinement) and the incremental multi-source training + retrieval pipeline. Stdlib only: no model needed.
License
MIT - see LICENSE.