A toolkit for managing and testing LM Studio models with automatic context limit discovery
Project description
LMStrix: The Unofficial Toolkit for Mastering LM Studio
LMStrix is a professional, installable Python toolkit designed to supercharge your interaction with LM Studio. It provides a powerful command-line interface (CLI) and a clean Python API for managing, testing, and running local language models, with a standout feature: the Adaptive Context Optimizer.
Why LMStrix? The Problem it Solves
Working with local LLMs via LM Studio is powerful, but it comes with challenges:
- The Context Window Mystery: What's the true maximum context a model can handle on your machine? Advertised context lengths are often theoretical. The practical limit depends on your hardware, the model's architecture, and LM Studio's own overhead. Finding this limit manually is a tedious, frustrating process of trial and error.
- Repetitive Workflows: Managing models, crafting prompts, and running inference often involves repetitive boilerplate code or manual steps in the LM Studio UI.
- Lack of Programmatic Control: The UI is great for exploration, but developers building applications on top of local LLMs need a robust, scriptable interface for automation and integration.
LMStrix solves these problems by providing a seamless, developer-friendly toolkit that automates the tedious parts and lets you focus on building.
How It Works: The Adaptive Context Optimizer
The core innovation in LMStrix is its ability to automatically discover the maximum operational context length for any model loaded in LM Studio.
It uses a sophisticated binary search algorithm:
- It starts with a wide range for the possible context size.
- It sends a specially crafted prompt to the model, progressively increasing the amount of "filler" text.
- It analyzes the model's response (or lack thereof) to see if it successfully processed the context.
- By repeatedly narrowing the search range, it quickly pinpoints the precise token count where the model's performance degrades or fails.
This gives you a reliable, empirical measurement of the model's true capabilities on your specific hardware, eliminating guesswork and ensuring your applications run with optimal performance.
Key Features
- Automatic Context Optimization: Discover the true context limit of any model with the
optimizecommand. - Full Model Management: Programmatically
listavailable models andscanfor newly downloaded ones. - Flexible Inference Engine: Run inference with a powerful two-phase prompt templating system that separates prompt structure from its content.
- Rich CLI: A beautiful and intuitive command-line interface built with
richandfire, providing formatted tables, progress indicators, and clear feedback. - Modern Python API: An
async-first API designed for high-performance, concurrent applications. - Robust and Resilient: Features automatic retries with exponential backoff for network requests and a comprehensive exception hierarchy.
- Lightweight and Focused: Built with a minimal set of modern, high-quality dependencies.
Installation
uv pip install --system lmstrix
Quick Start
Command-Line Interface (CLI)
# Discover the optimal context window for a model
lmstrix optimize "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf"
# List all available models with their detected context limits
lmstrix models list
# Scan for any new models you've added to LM Studio
lmstrix models scan
# Run inference using a prompt template
lmstrix infer --model "Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf" --prompt "greeting"
Python API
import asyncio
from lmstrix import LMStrix
async def main():
# Initialize the client
client = LMStrix()
# Optimize the context window for a specific model
print("Optimizing context, this may take a moment...")
optimization = await client.optimize_context("Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf")
print(f"Optimal context found: {optimization.optimal_size} tokens")
print("-" * 20)
# List all models and their properties
print("Available Models:")
models = await client.list_models()
for model in models:
limit = model.context_limit or "Not yet optimized"
print(f"- {model.id}: {limit} tokens")
print("-" * 20)
# Run inference with a template
print("Running inference...")
result = await client.infer(
model_id="Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf",
prompt_template="Summarize this text: {text}",
context={"text": "Your long document goes here..."}
)
print("Inference Result:")
print(result.content)
if __name__ == "__main__":
asyncio.run(main())
Architecture
LMStrix is designed with a clean, modular architecture:
api/: A dedicated client for communicating with the LM Studio local server API.core/: The heart of the application, containing the core business logic for models, inference, and the context optimization algorithm.loaders/: Handles loading and managing data for models, prompts, and context files.cli/: Implements the command-line interface.utils/: Shared utilities and helper functions.
Development
# Install in editable mode for development
pip install -e .
# Run the test suite
pytest
# Format and lint the codebase
ruff format .
ruff check .
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are highly welcome! Please feel free to submit pull requests or file issues on our GitHub repository.
Support
For bugs, feature requests, or general questions, please file an issue on our GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lmstrix-1.0.22.tar.gz.
File metadata
- Download URL: lmstrix-1.0.22.tar.gz
- Upload date:
- Size: 43.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2520a2f0ddab462a5365c50ced2b6524e9433379cf9e390f3a08a5098430657
|
|
| MD5 |
b684206718dd23d05230542838c31346
|
|
| BLAKE2b-256 |
cdfdc199614d2ae0e23a8f209725557917beb862b531138ae02ed03c15d97128
|
File details
Details for the file lmstrix-1.0.22-py3-none-any.whl.
File metadata
- Download URL: lmstrix-1.0.22-py3-none-any.whl
- Upload date:
- Size: 31.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd0b71d9d7b9d498fd746e0fe2129c951ec55028002bc94540cdaebcf59fa53a
|
|
| MD5 |
c1b94318ff30dc6c184527e0e7f89be9
|
|
| BLAKE2b-256 |
1410cf7d4506bf796b62bf1c7dcf464b9945ee3638e0a2b3872420ba15be72b3
|