Metadata-Version: 2.4
Name: gleefultransformer
Version: 0.0.1
Summary: Run, stream, and fine-tune chat models in Python, with optional document retrieval.
License-Expression: Apache-2.0
Project-URL: Documentation, https://ericfillion.github.io/gleefultransformer/
Project-URL: Repository, https://github.com/EricFillion/gleefultransformer
Keywords: ai,transformer,gleeful,nlp,training,fine-tuning,rag
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.6
Requires-Dist: tqdm>=4.66.3
Requires-Dist: transformers[chat-template]<6,>=5.23.0
Requires-Dist: accelerate<2,>=1.16
Requires-Dist: datasets<5.0.0,>=4.0.0
Requires-Dist: huggingface-hub<3,>=1.31
Requires-Dist: sentencepiece
Requires-Dist: protobuf
Requires-Dist: tokenizers<0.24,>=0.23.1
Requires-Dist: safetensors>=0.8
Requires-Dist: matplotlib<4.0,>=3.9
Requires-Dist: numpy<3.0,>=2.0
Requires-Dist: mcp<3,>=2.3
Requires-Dist: pydantic<3,>=2
Requires-Dist: filelock>=3
Provides-Extra: search
Requires-Dist: sentence-transformers<6,>=5.7; extra == "search"
Provides-Extra: test
Requires-Dist: pytest>=7.4; extra == "test"
Provides-Extra: qlora
Requires-Dist: peft<1,>=0.22; extra == "qlora"
Requires-Dist: bitsandbytes>=0.48; (sys_platform == "linux" or sys_platform == "win32") and extra == "qlora"
Dynamic: license-file

# Gleeful Transformer

A Python library for training and running open-weight language models.

- Use MCP to run training and evaluation jobs from your AI assistant.
- Fine-tune chat models with LoRA, QLoRA, or full-model training.
- Pretrain from scratch on your own text, with FSDP2 for multi-GPU runs.
- Add RAG to your chats with Gleeful Search, which pulls in relevant passages from your documents.

## MCP

Use the built-in Model Context Protocol (MCP) server to chat with a local model, evaluate it on a dataset, or fine-tune it.

Paste this prompt into your assistant for setup help.

> Help me set up Gleeful Transformer MCP from <https://github.com/EricFillion/gleefultransformer>. Ask which MCP client I use and whether I want to chat with a local model, evaluate it on a dataset, or fine-tune it. Then guide me through setup and verify the connection by listing my registered models.

[Manual setup](https://ericfillion.github.io/gleefultransformer/mcp/) · [Connect a client](https://ericfillion.github.io/gleefultransformer/mcp/#connect)

## Install

Use Python 3.11 or later. On Linux or macOS, clone the repository and install it in a virtual environment.

```sh
git clone https://github.com/EricFillion/gleefultransformer.git
cd gleefultransformer
python3 -m venv .venv
. .venv/bin/activate
python -m pip install .
```

## Chat

```python
from gleefultransformer import CHATCallArgs, GleefulChat

chat = GleefulChat(model_name="HuggingFaceTB/SmolLM2-360M-Instruct")
messages = [{"role": "user", "content": "Hi"}]
args = CHATCallArgs(max_len=96, temp=0)

print(chat(messages, args=args).text)
```

Pass the same messages and settings to `stream()` to display the reply as it arrives.

```python
for event in chat.stream(messages, args=args):
    if event.marker == "text":
        print(event.text, end="", flush=True)
print()
```

[Chat guide](https://ericfillion.github.io/gleefultransformer/chat/) · [Inference arguments](https://ericfillion.github.io/gleefultransformer/call-args/)

## Fine-tune

From the checkout root, create separate JSONL files for training, validation, and the final comparison. [Create the quickstart datasets](https://ericfillion.github.io/gleefultransformer/chat/train/#create-the-quickstart-datasets)

```python
from gleefultransformer import CHATTokArgs, GleefulChat, GleefulTrainArgs

chat = GleefulChat(model_name="HuggingFaceTB/SmolLM2-135M-Instruct")
tok_args = CHATTokArgs(max_len=128)
test_path = "outputs/fine-tune-data/test.jsonl"

baseline = chat.eval(test_path, tok_args=tok_args).loss
chat.train(
    "outputs/fine-tune-data/train.jsonl",
    args=GleefulTrainArgs(eval_steps=1),
    eval_path="outputs/fine-tune-data/validation.jsonl",
    tok_args=tok_args,
)
chat.save("outputs/fine-tuned-chat")

tuned = GleefulChat(model_name="outputs/fine-tuned-chat")
after_reload = tuned.eval(test_path, tok_args=tok_args).loss
print(f"Before training {baseline:.3f}, after reload {after_reload:.3f}")
```

The training guide covers evaluation, LoRA, QLoRA, and checkpoint resumption. [Training guide](https://ericfillion.github.io/gleefultransformer/chat/train/) · [Training arguments](https://ericfillion.github.io/gleefultransformer/train-args/) · [Databricks Dolly example](https://github.com/EricFillion/gleefultransformer/tree/main/examples/fine_tuning/chat/databricks)

## Pretrain with FSDP2

After installing, run this from the checkout root on a Linux machine with two NVIDIA GPUs that support BF16.

```sh
cd examples/pretraining/chat/common_corpus
python data.py
python tok.py
torchrun --standalone --nproc-per-node=2 train.py --fsdp_config fsdp2.json --precision bf16 --bs 2 --gas 4 \
  --token_budget 261120 --eval_steps 16 --checkpoint_steps 16 --save_model_path outputs/fsdp2
```

The consolidated model is saved to `outputs/fsdp2` in the example directory. If NCCL stalls at startup on Runpod, retry with `NCCL_P2P_DISABLE=1` before `torchrun`. [Full example and troubleshooting](https://github.com/EricFillion/gleefultransformer/tree/main/examples/pretraining/chat/common_corpus)

## Search

From the checkout root, install the search extra and create a sample passage.

```sh
python -m pip install '.[search]'
mkdir -p outputs
cat > outputs/passages.jsonl <<'JSONL'
{"text":"Titan is the largest moon orbiting Saturn.","metadata":{"source":"moon-guide"}}
JSONL
```

```python
from gleefultransformer import (
    CHATCallArgs,
    GleefulChat,
    GleefulSearch,
    SearchCallArgs,
    SearchTrainArgs,
)

search = GleefulSearch()
search.train("outputs/passages.jsonl", args=SearchTrainArgs(out_dir="outputs/my-index"))

chat = GleefulChat(
    model_name="HuggingFaceTB/SmolLM2-360M-Instruct",
    gleeful_search=GleefulSearch(data_name="outputs/my-index"),
)
args = CHATCallArgs(max_len=96, search_args=SearchCallArgs(limit=1))
print(chat("What is Saturn's largest moon?", args=args).text)
```

`search_result` stream events include the retrieved text and its metadata. [Search guide](https://ericfillion.github.io/gleefultransformer/search/) · [Search example](https://github.com/EricFillion/gleefultransformer/tree/main/examples/search/hello_world)
