Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
README.md 7.70 KiB

LM Studio Python SDK

Using the SDK

Installation

The SDK can be installed from PyPI as follows:

$ pip install lmstudio

Installation from the repository URL or a local clone is also supported for development and pre-release testing purposes.

Examples

The base component of the LM Studio SDK is the (synchronous) Client. This should be created once and used to manage the underlying websocket connections to the LM Studio instance.

However, a top level convenience API is provided for convenience in interactive use (this API implicitly creates a default Client instance which will remain active until the Python interpreter is terminated).

Using this convenience API, requesting text completion from an already loaded LLM is as straightforward as:

import lmstudio as lms

model = lms.llm()
model.complete("Once upon a time,")

Requesting a chat response instead only requires the extra step of setting up a Chat helper to manage the chat history and include it in response prediction requests:

import lmstudio as lms

EXAMPLE_MESSAGES = (
    "My hovercraft is full of eels!",
    "I will not buy this record, it is scratched."
)

model = lms.llm()
chat = lms.Chat("You are a helpful shopkeeper assisting a foreign traveller")
for message in EXAMPLE_MESSAGES:
    chat.add_user_message(message)
    print(f"Customer: {message}")
    response = model.respond(chat)
    chat.add_assistant_response(response)
    print(f"Shopkeeper: {response}")

Additional SDK examples and usage recommendations may be found in the main LM Studio Python SDK documentation.

SDK versioning

The LM Studio Python SDK uses a 3-part X.Y.Z numeric version identifier:

  • X: incremented when the minimum version of significant dependencies is updated (for example, dropping support for older versions of Python or LM Studio). Previously deprecated features may be dropped when this part of the version number increases.
  • Y: incremented when new features are added, or some other notable change is introduced (such as support for additional versions of Python). New deprecation warnings may be introduced when this part of the version number increases.
  • Z: incremented for bug fix releases which don't contain any other changes. Adding exceptions and warnings for previously undetected situations is considered a bug fix.

This versioning policy is intentionally similar to semantic versioning, but differs in the specifics of when the different parts of the version number will be updated.

Release candidates may be published prior to full releases, but this will typically only occur when seeking broader feedback on particular features prior to finalizing the release.

Outside the preparation of a new release, the SDK repository will include a .devN suffix on the nominal Python package version.