Metadata-Version: 2.4
Name: llm-holdem-34j
Version: 2.1.0
Summary: Let LLMs play Texas hold 'em
Author: 34j
License-Expression: MIT
Project-URL: Bug Tracker, https://github.com/34j/llm-holdem-34j/issues
Project-URL: Changelog, https://github.com/34j/llm-holdem-34j/blob/main/CHANGELOG.md
Project-URL: documentation, https://llm-holdem-34j.readthedocs.io
Project-URL: repository, https://github.com/34j/llm-holdem-34j
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cyclopts
Requires-Dist: pokerkit>=0.6
Requires-Dist: tqdm
Provides-Extra: train
Requires-Dist: accelerate>=1; extra == "train"
Requires-Dist: datasets>=3; extra == "train"
Requires-Dist: liger-kernel>=0.5; sys_platform == "linux" and extra == "train"
Requires-Dist: peft>=0.14; extra == "train"
Requires-Dist: torch>=2.5; extra == "train"
Requires-Dist: transformers>=4.51; extra == "train"
Requires-Dist: trl>=0.16; extra == "train"
Dynamic: license-file

# LLM Hold 'em 34j

<p align="center">
  <a href="https://github.com/34j/llm-holdem-34j/actions/workflows/ci.yml?query=branch%3Amain">
    <img src="https://img.shields.io/github/actions/workflow/status/34j/llm-holdem-34j/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" >
  </a>
  <a href="https://llm-holdem-34j.readthedocs.io">
    <img src="https://img.shields.io/readthedocs/llm-holdem-34j.svg?logo=read-the-docs&logoColor=fff&style=flat-square" alt="Documentation Status">
  </a>
</p>
<p align="center">
  <a href="https://codecov.io/gh/34j/llm-holdem-34j">
    <img src="https://img.shields.io/codecov/c/github/34j/llm-holdem-34j.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage">
  </a>
  <a href="https://github.com/astral-sh/uv">
    <img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json" alt="uv">
  </a>
  <a href="https://github.com/astral-sh/ruff">
    <img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff">
  </a>
  <a href="https://github.com/j178/prek">
    <img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/j178/prek/master/docs/assets/badge-v0.json" alt="prek">
  </a>
</p>
<p align="center">
  <a href="https://pypi.org/project/llm-holdem-34j/">
    <img src="https://img.shields.io/pypi/v/llm-holdem-34j.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version">
  </a>
  <img src="https://img.shields.io/pypi/pyversions/llm-holdem-34j.svg?style=flat-square&logo=python&amp;logoColor=fff" alt="Supported Python versions">
  <img src="https://img.shields.io/pypi/l/llm-holdem-34j.svg?style=flat-square" alt="License">
</p>

---

**Documentation**: <a href="https://llm-holdem-34j.readthedocs.io" target="_blank">https://llm-holdem-34j.readthedocs.io </a>

**Source Code**: <a href="https://github.com/34j/llm-holdem-34j" target="_blank">https://github.com/34j/llm-holdem-34j </a>

---

Let LLMs play Texas hold 'em — a lightweight, SpinGPT-inspired toolkit for training small open-weight LLMs (default: `Qwen/Qwen3.5-0.8B`) on 2-player Heads-Up and 3-player Spin & Go No-Limit Texas Hold'em on a single consumer or Colab GPU.

## Highlights

- **Exact Poker Engine**: Built on [PokerKit](https://github.com/uoftcprg/pokerkit) (`NoLimitTexasHoldem`) with exact hand evaluation, side pots, 2-to-6 player tables, and SpinGPT-style compact text prompts (`pos:H=BTN stacks:... hand:TsQs | pre:... | flop:... H:` -> `f`/`x`/`c`/`a`/`b{amt}`/`r{amt}`).
- **Two-Stage Modern Training Stack**: Powered by Hugging Face [TRL](https://github.com/huggingface/trl) (`SFTTrainer` + `GRPOTrainer`), [PEFT](https://github.com/huggingface/peft) (LoRA), and [Liger-Kernel](https://github.com/linkedin/Liger-Kernel) fused Triton kernels.
  1. **Stage 1 (SFT)**: Completion-only LoRA fine-tuning on decisions generated by `EquityGTOBot` (position/stack-depth preflop ranges + PokerKit Monte-Carlo equity & EV estimation).
  2. **Stage 2 (GRPO)**: Online Group Relative Policy Optimization with verifiable poker rewards (strict legality, continuous EV regret, and bet-sizing calibration).
- **SpinGPT Metrics & Live Evaluation**: Exact accuracy, tolerant accuracy (±0.5 BB), macro F1, MAE, MAPE, illegal-move rate, EV regret, and live `BB/100 ± 95% CI` head-to-head / 3-handed matches.

## Installation

Core package (PokerKit engine, bots, dataset generator, metrics, CLI):

```shell
pip install llm-holdem-34j
```

With GPU training & LLM inference dependencies (`torch`, `transformers`, `trl`, `peft`, `liger-kernel`, `datasets`, `accelerate`):

```shell
pip install "llm-holdem-34j[train]"
```

## Quickstart

```shell
# 1. Generate SpinGPT-formatted datasets with per-action EV annotations
llm-holdem-34j generate --output data/train_sft.jsonl --num-decisions 10000 --seed 42 --mc-samples 64 --workers 4
llm-holdem-34j generate --output data/train_grpo.jsonl --num-decisions 1500 --seed 100 --mc-samples 64 --min-ev-spread 0.5 --workers 4
llm-holdem-34j generate --output data/test.jsonl --num-decisions 500 --seed 999 --mc-samples 64 --workers 4

# 2. Stage 1: LoRA SFT (default: Qwen/Qwen3.5-0.8B, all-linear LoRA)
llm-holdem-34j train-sft --train-path data/train_sft.jsonl --eval-path data/test.jsonl --output-dir runs/sft

# 3. Stage 2: Online GRPO with verifiable monotonic EV-advantage poker rewards
llm-holdem-34j train-grpo --train-path data/train_grpo.jsonl --sft-adapter-dir runs/sft --output-dir runs/grpo --max-steps 80

# 4. Evaluate offline metrics and live PokerKit gameplay
llm-holdem-34j eval-offline runs/sft --test-path data/test.jsonl
llm-holdem-34j eval-play --hero runs/sft --opponents calling_station --num-hands 200

# 5. Play interactively in the terminal
llm-holdem-34j play --opponent runs/sft --hands 5
```

## Benchmark Results (RTX 4070 SUPER 12 GB, < 10 min total)

Trained `Qwen/Qwen3-0.6B` (BF16, LoRA `r=16, alpha=32`, Liger-Kernel) on 10k SFT decisions (2 epochs, 6m 46s) + 80 GRPO steps (1m 15s):

### Offline Metrics (500 held-out decisions)

| Model | Exact Acc | Tolerant Acc (±0.5 BB) | Macro F1 | MAE (BB) | MAPE | Illegal Rate | EV Regret (BB) |
| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| Zero-shot `Qwen/Qwen3-0.6B` | 1.6% | 1.6% | 1.5% | 0.00 | 0.0% | 97.0% | 10.763 |
| Stage 1 SFT (`runs/sft`) | **80.2%** | **80.8%** | **61.6%** | **0.06** | **2.8%** | **0.0%** | **0.471** |
| Stage 2 GRPO (`runs/grpo`) | 77.6% | 78.4% | 57.1% | 0.09 | 3.9% | **0.0%** | 0.501 |

### Live PokerKit Gameplay (200 hands each, 0.00% illegal moves)

| Matchup | Format | `runs/sft` (BB/100) | `runs/grpo` (BB/100) |
| --- | --- | ---: | ---: |
| vs `random` | 2p HU | +122.6 ± 180.2 | -7.4 ± 200.7 |
| vs `calling_station` | 2p HU | **+125.2 ± 97.4** | **+82.2 ± 76.6** |
| vs `lag` | 2p HU | +56.6 ± 159.7 | -37.2 ± 177.5 |
| vs `random + lag` | 3p Spin & Go | +176.5 ± 239.3 | +168.8 ± 233.2 |

## Benchmark Results (Google Colab Tesla T4 15 GB, `Qwen/Qwen3.5-0.8B`)

Trained `Qwen/Qwen3.5-0.8B` (FP16, `all-linear` LoRA `r=16, alpha=32`) on 4k high-precision (`mc_samples=64`) SFT decisions (120 steps, 6m 21s) + 40 GRPO steps on high-EV-spread (`min_ev_spread=0.5`) spots (1m 21s):

| Model | Exact Acc | Illegal Rate | EV Regret (BB) | Live vs `calling_station` (100 hands, seed 7) |
| --- | ---: | ---: | ---: | ---: |
| Zero-shot `Qwen/Qwen3.5-0.8B` | 0.0% | 100.0% | 11.449 | — |
| Stage 1 SFT (`runs/sft`) | **74.3%** | **0.0%** | 0.614 | -18.0 ± 32.9 BB/100 (0.00% illegal) |
| Stage 2 GRPO (`runs/grpo`) | 73.7% | **0.0%** | **0.601** | **+27.0 ± 31.7 BB/100** (0.00% illegal) |
