0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LM Studio の Python SDK「lmstudio-python」でローカルLLM の軽いお試し(M4 MacBook Air を利用)

Last updated at Posted at 2025-08-16

はじめに

以下の記事などを書いていた、LM Studio と LM Studio の SDK を使ったローカルLLMに関する話です。

●LM Studio の TypeScript SDK「lmstudio-js」でローカルLLM の軽いお試し(M4 MacBook Air を利用)【Node.js】 - Qiita
 https://qiita.com/youtoy/items/a74f41499e0db7b1e19b

●ベータ版機能「LM Studio REST API」を使ってみる(curl でのお試し): M4 MacBook Air でのローカルLLM - Qiita
 https://qiita.com/youtoy/items/1b9b1ca09b1af5edf0bb

今回の内容

上記の記事では、LM Studio の SDK で「lmstudio-js」(Typescript SDK)を使っていました。

今回は、以下の「lmstudio-python」(Python SDK)を使ってみます。

●lmstudio-python (Python SDK) | LM Studio Docs
 https://lmstudio.ai/docs/python

image.png

PC は、上記と同様に自分が持っている M4 の MacBook Air を使います。

さっそく試してみる

さっそく試していきます。

ローカルLLM用のローカルサーバー

ローカルLLM用のローカルサーバーを用意します。それについては、前の記事と同様に、以下の記事に書いたやり方を使います。

●ベータ版機能「LM Studio REST API」を使ってみる(curl でのお試し): M4 MacBook Air でのローカルLLM - Qiita
 https://qiita.com/youtoy/items/1b9b1ca09b1af5edf0bb

そして、利用するモデルも以前と同じ「Gemma 3 270M」です。

仮想環境の準備と SDK のインストール

次に仮想環境の準備と SDK のインストールです。

仮想環境の準備

Python の機能を使って、仮想環境を準備します。

以下のコマンドで、myenv という名前の仮想環境を作り、それをアクティベートしました。

python -m venv myenv
source myenv/bin/activate

仮想環境をアクティベートした状態で、次へ進みます。

SDK のインストール

SDK のインストールで用いたコマンドは、具体的には以下です。

pip install lmstudio

一連の流れを試した結果は、以下になりました。

image.png

image.png

コードと実行結果

公式のコードサンプル

公式のコードサンプルを見てみます。Python SDK用の Python のサンプルコードは、2種類の内容(convenience API と scoped resource API)が示されていました。

それぞれ、以下に掲載してみます。

Python (convenience API)
import lmstudio as lms

model = lms.llm("llama-3.2-1b-instruct")
result = model.respond("What is the meaning of life?")

print(result)
Python (scoped resource API)
import lmstudio as lms

with lms.Client() as client:
    model = client.llm.model("llama-3.2-1b-instruct")
    result = model.respond("What is the meaning of life?")

    print(result)

今回は、1つ目のほうを元にした内容でやってみます。

今回使うコードと実行結果

今回のお試しに使ったコードは以下です。

import lmstudio as lms

model = lms.llm("gemma-3-270m-it")
result = model.respond("p5.jsについて簡単に説明して")

print(result)

そして、上記を実行してみた結果は以下のとおりです。

image.png

Typescript SDK を試した時と同様、無事、結果を得ることができました。

lmstudio-python を利用するメリット

最後に、lmstudio-python を利用するメリットについて記載します。

TypeScript SDK(lmstudio-js)を使うメリットとして、以下の公式情報を元にしたものを書いていました。
Python SDK「lmstudio-python」の公式ドキュメントと、TypeScript SDK(lmstudio-js)の公式ドキュメントで、それぞれの SDK の機能を見た感じだと、lmstudio-python を使うメリットは lmstudio-js を使うメリットと同じことがいえそうでした。

image.png

それと、その中に出てくる「スペキュレイティブ・デコーディング(Speculative Decoding)」については、以下のページに記載があります。

●Speculative Decoding | LM Studio Docs
 https://lmstudio.ai/docs/python/llm-prediction/speculative-decoding

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?