A client library for interacting with the LM Studio API
Project description
LM Studio Wrapper
A Python client that provides a seamless interface to LM Studio's OpenAI-compatible API, enabling easy integration of local large language models into your applications.
🌟 Features
- 🔄 Streaming Support: Real-time token-by-token generation for responsive UIs
- 💬 Chat Completions: OpenAI-compatible chat interface with support for multiple message roles
- 📝 Text Generation: Traditional completion API for non-chat applications
- 📹 Video Analysis: Specialized methods for video content summarization and note generation
- 🔢 Embeddings: Vector embedding generation for semantic search and text analysis
- 🔍 Model Discovery: Easily list and select from available local models
- ⚡ Performance: Optimized for local inference with minimal latency
- 🛠️ Customization: Fine-grained control over generation parameters
📦 Installation
From PyPI
pip install lmstudio-wrapper
From source
git clone https://github.com/harshitkumar9030/lmstudio-client
cd lmstudio-client
pip install -e .
🚀 Quick Start
from lmstudio_wrapper import LMStudioClient
# Initialize client
client = LMStudioClient(base_url="http://localhost:1234/v1")
# Simple chat completion
response = client.chat_completion([
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What are the benefits of local LLMs?"}
])
print(response)
📚 Usage Examples
Chat Completions
from lmstudio_wrapper import LMStudioClient
client = LMStudioClient()
# Standard completion
messages = [
{"role": "system", "content": "You are a technical expert."},
{"role": "user", "content": "Explain how transformers work in NLP"}
]
response = client.chat_completion(messages, temperature=0.7)
print(response)
# With streaming
def print_token(token):
print(token, end="", flush=True)
# Stream tokens as they're generated
for _ in client.chat_completion(messages, stream=True, callback=print_token):
pass
Text Generation
# Direct text completion (non-chat)
prompt = "Write a poem about artificial intelligence:"
response = client.generate_text(prompt, max_tokens=500)
print(response)
Video Analysis
# Prepare video information
video_info = {
'filename': 'tech_lecture.mp4',
'total_duration': 1200.0,
'scenes_detected': 5,
'scenes': [
{'scene_index': 0, 'start_time': 0.0, 'end_time': 240.0, 'text_content': 'Introduction to machine learning concepts'},
{'scene_index': 1, 'start_time': 240.0, 'end_time': 480.0, 'text_content': 'Neural network architectures explained'},
{'scene_index': 2, 'start_time': 480.0, 'end_time': 720.0, 'text_content': 'Training methodologies and best practices'},
{'scene_index': 3, 'start_time': 720.0, 'end_time': 960.0, 'text_content': 'Real-world applications and case studies'},
{'scene_index': 4, 'start_time': 960.0, 'end_time': 1200.0, 'text_content': 'Future trends and conclusion'}
]
}
# Generate detailed summary
summary = client.generate_summary(video_info)
print(summary)
# Generate structured notes
notes = client.generate_notes(video_info)
print(notes)
Working with Embeddings
# Generate embeddings for semantic search
texts = [
"What is machine learning?",
"How do neural networks work?",
"What are transformers in NLP?"
]
embeddings = client.get_embeddings(texts)
print(f"Generated {len(embeddings)} embeddings with {len(embeddings[0])} dimensions each")
Exploring Available Models
# List all models available in LM Studio
models = client.list_models()
print("Available models:")
for model in models.get("data", []):
print(f"- {model.get('id')}")
📋 API Reference
LMStudioClient
LMStudioClient(api_key=None, base_url="http://localhost:1234/v1")
api_key: Optional API key (not required for local LM Studio servers)base_url: URL of the LM Studio API server
Methods
Chat Completions
chat_completion(messages, model=None, max_tokens=2048, temperature=0.7, stream=False, callback=None)
Text Generation
generate_text(prompt, model=None, max_tokens=2048, temperature=0.7, stream=False, callback=None)
Video Analysis
generate_summary(video_info, stream=False, callback=None)
generate_notes(processed_data, stream=False, callback=None)
Embeddings
get_embeddings(input_text)
Model Management
list_models()
🔧 Configuration
The client adapts to your LM Studio server configuration:
- Local Inference: Works seamlessly with the default LM Studio server
- Custom Endpoints: Supports custom server URLs and ports
- Model Selection: Can target specific models loaded in LM Studio
- Generation Parameters: Full control over temperature, max tokens, etc.
⚠️ Error Handling
from lmstudio_wrapper.exceptions import LMStudioAPIError, LMStudioInvalidResponseError, LMStudioRequestError
try:
response = client.chat_completion(messages)
except LMStudioRequestError as e:
print(f"Network error: {e}")
except LMStudioInvalidResponseError as e:
print(f"Invalid response: {e}")
except LMStudioAPIError as e:
print(f"API error: {e}")
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🤝 Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
📣 Acknowledgments
- This library is designed for use with LM Studio, a powerful tool for running local LLMs
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
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 lmstudio_wrapper-0.1.4.tar.gz.
File metadata
- Download URL: lmstudio_wrapper-0.1.4.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c866084de694e46c5a4009d9a2730fa664a44e0d575a03deaa11e2cb089ed23
|
|
| MD5 |
6670ae5853bb4d5114c2cf31123e21fb
|
|
| BLAKE2b-256 |
02a9d4becc99732e99c6dfcd3b1ef882ace9f033a7d511923a5eb9e5dba85049
|
File details
Details for the file lmstudio_wrapper-0.1.4-py3-none-any.whl.
File metadata
- Download URL: lmstudio_wrapper-0.1.4-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1040f4e4573d717510a479e3be2cb884070c3eaaa5b0093ea81444a99efa5342
|
|
| MD5 |
8f590f47af9a5fbe41fda08e5facb4a2
|
|
| BLAKE2b-256 |
e305f05fbb4efe2a74d634cd3d01532b4e197365f2e644e644ded43a5a8209c6
|