Skip to main content

A toolkit for managing and testing LM Studio models with automatic context limit discovery

Project description

LMStrix

LMStrix is a professional Python toolkit designed to supercharge your interaction with LM Studio. It provides a powerful command-line interface (CLI) and Python API for managing, testing, and running local language models, with a standout feature: Adaptive Context Optimization.

Key Features

  • ๐Ÿ” Automatic Context Discovery: Binary search algorithm to find the true operational context limit of any model
  • ๐Ÿ“Š Beautiful Verbose Logging: Enhanced stats display with emojis showing inference metrics, timing, and token usage
  • ๐Ÿš€ Smart Model Management: Models persist between calls to reduce loading overhead
  • ๐ŸŽฏ Flexible Inference Engine: Run inference with powerful prompt templating and percentage-based output control
  • ๐Ÿ“‹ Comprehensive Model Registry: Track models, their context limits, and test results with JSON persistence
  • ๐Ÿ›ก๏ธ Safety Controls: Configurable thresholds and fail-safes to prevent system crashes
  • ๐Ÿ’ป Rich CLI Interface: Beautiful terminal output with progress indicators and formatted tables
  • ๐Ÿ“ˆ Compact Test Output: Live-updating tables show test progress without verbose logging clutter

Installation

# Using pip
pip install lmstrix

# Using uv (recommended)
uv pip install lmstrix

Quick Start

Command-Line Interface

# Scan for available models in LM Studio
lmstrix scan

# List all models with their context limits and test status
lmstrix list

# Test context limit for a specific model
lmstrix test llama-3.2-3b-instruct

# Test all untested models with safety threshold
lmstrix test --all --threshold 102400

# Run inference with enhanced verbose logging
lmstrix infer "What is the capital of Poland?" -m llama-3.2-3b-instruct --verbose

# Run inference with percentage-based output tokens
lmstrix infer "Explain quantum computing" -m llama-3.2-3b-instruct --out_ctx "25%"

# Use file-based prompts with templates
lmstrix infer summary -m llama-3.2-3b-instruct --file_prompt adam.toml --text_file document.txt

# Direct text input for prompts
lmstrix infer "Summarize: {{text}}" -m llama-3.2-3b-instruct --text "Your content here"

Enhanced Verbose Output

When using --verbose, LMStrix provides comprehensive statistics:

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
๐Ÿค– MODEL: llama-3.2-3b-instruct
๐Ÿ”ง CONFIG: maxTokens=26214, temperature=0.7
๐Ÿ“ PROMPT (1 lines, 18 chars): Capital of Poland?
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ ธ Running inference...
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
๐Ÿ“Š INFERENCE STATS
โšก Time to first token: 0.82s
โฑ๏ธ  Total inference time: 11.66s
๐Ÿ”ข Predicted tokens: 338
๐Ÿ“ Prompt tokens: 5
๐ŸŽฏ Total tokens: 343
๐Ÿš€ Tokens/second: 32.04
๐Ÿ›‘ Stop reason: eosFound
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

Python API

from lmstrix.loaders.model_loader import load_model_registry
from lmstrix.core.inference_manager import InferenceManager

# Load model registry
registry = load_model_registry()

# List available models
models = registry.list_models()
print(f"Available models: {len(models)}")

# Run inference
manager = InferenceManager(verbose=True)
result = manager.infer(
    model_id="llama-3.2-3b-instruct",
    prompt="What is the meaning of life?",
    out_ctx=100,
    temperature=0.7
)

if result["succeeded"]:
    print(f"Response: {result['response']}")
    print(f"Tokens used: {result['tokens_used']}")
    print(f"Time: {result['inference_time']:.2f}s")

Context Testing & Optimization

LMStrix uses a sophisticated binary search algorithm to discover true model context limits:

Safety Features

  • Threshold Protection: Configurable maximum context size to prevent system crashes
  • Progressive Testing: Starts with small contexts and increases safely
  • Persistent Results: Saves test results to avoid re-testing

Testing Commands

# Test specific model
lmstrix test llama-3.2-3b-instruct

# Test all models with custom threshold
lmstrix test --all --threshold 65536

# Test at specific context size
lmstrix test --all --ctx 32768

# Reset and re-test a model
lmstrix test llama-3.2-3b-instruct --reset

# Test with custom prompt
lmstrix test llama-3.2-3b-instruct --prompt "What is 2+2?"

# Test with file-based prompt
lmstrix test llama-3.2-3b-instruct --file_prompt test_prompt.txt

Compact Output (Default)

When running without --verbose, tests display a clean, live-updating table:

Model                                   Context      Status
llama-3.2-3b-instruct                   32,768      Testing...
โ†’
Model                                   Context      Status  
llama-3.2-3b-instruct                   32,768      โœ“ Success

For batch testing with --all, a progress column is added to track multiple models.

Model Management

Registry Commands

# Scan for new models
lmstrix scan --verbose

# List models with different sorting
lmstrix list --sort size        # Sort by size
lmstrix list --sort ctx         # Sort by tested context
lmstrix list --show json        # Export as JSON

# Check system health
lmstrix health --verbose

Model Persistence

Models stay loaded between inference calls for improved performance:

  • When no explicit context is specified, models remain loaded
  • Last-used model is remembered for subsequent calls
  • Explicit context changes trigger model reloading

Prompt Templating

LMStrix supports flexible prompt templating with TOML files:

# adam.toml
[aps]
prompt = """
You are an AI assistant skilled in Abstractive Proposition Segmentation.
Convert the following text: {{text}}
"""

[summary] 
prompt = "Create a comprehensive summary: {{text}}"

Use with CLI:

lmstrix infer aps --file_prompt adam.toml --text "Your text here"
lmstrix infer summary --file_prompt adam.toml --text_file document.txt

Development

# Clone repository
git clone https://github.com/twardoch/lmstrix.git
cd lmstrix

# Install for development
pip install -e ".[dev]"

# Run tests
pytest

# Run linting
hatch run lint:all

Project Structure

src/lmstrix/
โ”œโ”€โ”€ cli/main.py              # CLI interface
โ”œโ”€โ”€ core/
โ”‚   โ”œโ”€โ”€ inference_manager.py # Unified inference engine
โ”‚   โ”œโ”€โ”€ models.py            # Model registry
โ”‚   โ””โ”€โ”€ context_tester.py    # Context limit testing
โ”œโ”€โ”€ api/client.py            # LM Studio API client
โ”œโ”€โ”€ loaders/                 # Data loading utilities
โ””โ”€โ”€ utils/                   # Helper utilities

Features in Detail

Adaptive Context Optimizer

  • Binary search algorithm for efficient context limit discovery
  • Safety thresholds to prevent system crashes
  • Automatic persistence of test results
  • Resume capability for interrupted tests

Enhanced Logging

  • Beautiful emoji-rich output in verbose mode
  • Comprehensive inference statistics
  • Progress indicators for long operations
  • Clear error messages with context

Smart Model Management

  • Automatic model discovery from LM Studio
  • Persistent registry with JSON storage
  • Model state tracking (loaded/unloaded)
  • Batch operations for multiple models

Requirements

  • Python 3.11+
  • LM Studio installed and configured
  • Models downloaded in LM Studio

License

MIT License - see LICENSE file for details.

Contributing

Contributions welcome! Please read our contributing guidelines and submit pull requests for any improvements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lmstrix-1.0.68.tar.gz (72.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lmstrix-1.0.68-py3-none-any.whl (65.4 kB view details)

Uploaded Python 3

File details

Details for the file lmstrix-1.0.68.tar.gz.

File metadata

  • Download URL: lmstrix-1.0.68.tar.gz
  • Upload date:
  • Size: 72.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for lmstrix-1.0.68.tar.gz
Algorithm Hash digest
SHA256 cfe321d2962eefb68c26a0f7b9db9c7934cabeb00c6ccc09f735a7a72a4c8bd9
MD5 f582dff13b5677ac1d6d856035c2be21
BLAKE2b-256 cfdbb12e47dda4450b59ee0f95af1df34f4b4199273e553a0afd9685ae0ed25c

See more details on using hashes here.

File details

Details for the file lmstrix-1.0.68-py3-none-any.whl.

File metadata

  • Download URL: lmstrix-1.0.68-py3-none-any.whl
  • Upload date:
  • Size: 65.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for lmstrix-1.0.68-py3-none-any.whl
Algorithm Hash digest
SHA256 d0412bbd8e279d9bfb85ac55307d3d18f9a29c2fe0b036bca2d41943eee0e853
MD5 b4828fd4543279ed6d01eab5419f8577
BLAKE2b-256 55ff1334792f876f13f492bf4889a0a267a5d64162b1aa1066438c6611aea943

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page