A Rust library for ModelSocket, a protocol for efficiently integrating with LLMs
Project description
modelsocket.rs
Modelsocket.rs provides a Rust client and protocol types for talking to Modelsocket model hosting endpoints. It exposes a WebSocket based ModelSocket client for opening sequences, streaming generations, and coordinating multiple forks of a conversation. The crate is designed so that consumers can either embed the client directly in Rust projects or build higher level integrations, such as Python bindings.
Crate layout
protocolcontains serde-powered request/response structures that mirror the Modelsocket wire format.client(enabled with theclientfeature flag) implements a Tokio/WebSocket powered sequence client with helpers for opening sequences, appending messages, streaming tokens, and forking conversations.python(enabled with thepythonfeature flag) re-exports PyO3 bindings that expose the blocking client API to Python.
Feature flags
The crate ships with two opt-in feature flags so it can be kept lightweight when only the protocol types are needed:
| Feature | Description |
|---|---|
client |
Pulls in the asynchronous WebSocket client and its Tokio/futures dependencies. |
python |
Builds the PyO3 powered blocking bindings. This automatically enables the client feature. |
Enable the feature(s) you need when running Cargo commands, for example:
cargo check --features client
cargo test --features client
Python bindings
Enabling the python feature exposes a modelsocket extension module that wraps the Rust client in a "blocking" API. The bindings reuse a shared single-threaded Tokio runtime under the hood so each call feels natural to synchronous Python code while still delegating the work to the async Rust implementation.
Installing the bindings locally
The easiest way to experiment with the bindings is to install them into a virtual environment with maturin:
python -m venv .venv
source .venv/bin/activate
pip install maturin
maturin develop --release -F python
The last command builds the extension module (modelsocket) in-place using the python feature flag and makes it importable inside the virtual environment.
Example
Once the module is installed you can drive the blocking client just like the Rust version. The snippet below opens a sequence, sends a user message, and prints the generated response:
from modelsocket import BlockingModelSocketClient
client = BlockingModelSocketClient.connect(
"wss://models.mixlayer.ai/ws",
api_key="sk_example_123"
)
seq = client.open("meta/llama-3.1-8b-instruct-free")
seq.append("Hello there!", role="user")
# blocking generate
reply = seq.gen_text()
print(reply)
# ...or streaming generate
stream = seq.gen_text_stream(role="assistant",max_tokens=200)
for chunk in stream:
print(chunk, end="", flush=True)
seq.close()
License
The crate is distributed under the terms of the Apache 2.0 license.
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 Distributions
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 modelsocket-0.1.0-py3-none-macosx_11_0_arm64.whl.
File metadata
- Download URL: modelsocket-0.1.0-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ad5b733faf79917607608bc97ed7fe4c60bb8933d66321b496877540d243155
|
|
| MD5 |
9a94b0552b68ce9d4ead9ba2496bf21a
|
|
| BLAKE2b-256 |
ce47bbae39c48899bccd2a2f3183a14e7f3620a83c325bee86f22a496171e249
|