Skip to main content

TextPolicy

Reinforcement learning toolkit for text generation on MLX (Apple Silicon). TextPolicy provides algorithms (GRPO/GSPO), text-generation environments, a rollout runner, reward functions with a decorator registry, and LoRA/QLoRA utilities.

Install (uv)

uv add textpolicy

Optional model integration:

uv add mlx mlx-lm

Quickstart

Working example using a real model and tokenizer (mlx-lm required):

import mlx.core as mx
import textpolicy as tp
from textpolicy import load_model, create_policy
from textpolicy.environment.text_generation import TextGenerationEnv
from textpolicy.rollout import RolloutRunner, create_strategy

# 1) Load model and tokenizer (mlx-lm)
model, tokenizer = load_model("Qwen/Qwen3-0.6B")

# 2) Create a policy (controls generation)
generation_params = {"max_tokens": 25, "temperature": 0.7}
policy_fn = create_policy(model, tokenizer, generation_params)

# 3) Define a reward function (env uses this to score responses)
@tp.reward
def length_reward(prompt: str, completion: str, example: dict, **kwargs) -> float:
    return float(len(completion.split()))

# 4) Create an environment (requires a tokenizer)
env = TextGenerationEnv(["What is AI?"], length_reward, tokenizer=tokenizer)

# 5) Collect one rollout step
strategy = create_strategy('grpo')
runner = RolloutRunner(env, policy=policy_fn, strategy=strategy, max_steps=1)
buffer = runner.collect()
print(len(buffer.episodes))

Docs:

  • Quickstart: docs/QUICKSTART_UV.md
  • LoRA/QLoRA: docs/10_lora_qlora.md
  • Full index: docs/index.md

FAQ:

  • Do I need a model?
    • Yes for generation with create_policy. Use load_model() (mlx‑lm) to get (model, tokenizer). For reward‑only code (no generation), a model is not required.
  • Do I need a tokenizer?
    • Yes. Both TextGenerationEnv and TextGenerationEnvironment require a tokenizer. load_model() returns one for mlx‑lm models.
  • How do I control generation?
    • Pass generation_params to create_policy (for example, max_tokens, temperature, top_p, repetition_penalty).
  • What does step() return?
    • A dict with observation, reward, terminated, truncated, info. The runner enforces this.

Examples:

  • 01–06: reward functions, batch processing, minimal training
  • 08: GRPO training with rollout + buffer
  • 09–10: length reduction (GRPO/GSPO)
  • 11: LoRA/QLoRA configuration
  • 12: TinyLoRA + GTPO + HICRA reasoning run (experiments/countdown_reasoning_lora.py)

Metadata

Release files for textpolicy 0.1.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for textpolicy 0.1.11
File Size Uploaded Provenance
textpolicy-0.1.11.tar.gz 316.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for textpolicy 0.1.11
File Interpreter ABI Platform
textpolicy-0.1.11-py3-none-any.whl Python 3 none any Details

Total release size: 538.1 kB

Release files / textpolicy-0.1.11.tar.gz

Download URL textpolicy-0.1.11.tar.gz
Size 316.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d3f336c905a9944c53678ed62d81f84ae7277ec9fa3791b1e6236121701e14f6
BLAKE2b-256 checksum
How to use checksums
754d07872c69d502af0a21e367a42f56c2e1eb66381ee0f8308cc012b69d3754
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.16

Release files / textpolicy-0.1.11-py3-none-any.whl

Download URL textpolicy-0.1.11-py3-none-any.whl
Size 221.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1a0030c37a718baf7cc9572f13b2a3f89e234924c53881d6e19b94953c2fc55
BLAKE2b-256 checksum
How to use checksums
2c126783f621a3e63598979d5b95b6abed6dfbda39e178df9b696ec1e413fe4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.16

Security for textpolicy 0.1.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page