1
1

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 の OpenAI互換のAPI でローカルLLM でのストリーミングの軽いお試し(M4 MacBook Air を利用)【Node.js】

Posted at

はじめに

以下の記事を書いていたりする、LM Studio の API利用の話です。以下では、ベータ版機能の「LM Studio REST API」を試しました。

今回の記事では、OpenAI互換の API を試してみます。またストリーミング出力を使ってみます。

先に出力結果から紹介してみる

実装内容などに入っていく前に、先に出力結果を紹介してみます。

以下は OpenAI公式の Node.js用パッケージを使い、ローカルの LM Studio の APIサーバーにアクセスするクライアントの処理結果です。実装では、OpenAI の Responses API を使うのと同じ形の処理を使い、なおかつストリーミング出力になるようにしています。

応答・生成のスピードは、どちらも良い感じになりました。

OpenAI互換の API

今回の実装内容などの話に入っていく前に、LM Studio の公式ドキュメントに書かれた内容を見ていきます。

公式ドキュメントの左メニューを見ると、「REST Endpoints」という項目の下に 2つの項目があります。「LM Studio REST API (beta)」という項目が、冒頭で掲載していた記事で書いていたものです。

そして、上側にある「OpenAI Compatibility API」が今回試すものです。

image.png

API のエンドポイント

さらに、公式ドキュメントの記載を見ていきます。

「OpenAI-like API endpoints」という部分を見ると、API のエンドポイントに関する情報が掲載されています。

image.png

これらを見た感じだと、今回は「/v1/chat/completions」を使う感じになりそうです。

また、Re-using an existing OpenAI client という部分を見てみると、今回の LM Studio での OpenAI互換のAPI を使う場合の、アクセス先URL の情報が書いてあります。

image.png

URL は http://localhost:1234/v1 を指定する形になるようです。それについて、上で書いていた /v1/chat/completions を使った場合の使用が Endpoints overview の部分に書かれていました。

image.png

今回、Node.js の実装で試しますが、公式ドキュメント内で掲載されている Python のコードも、参考として掲載します。

# Example: reuse your existing OpenAI setup
from openai import OpenAI

# Point to the local server
client = OpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio")

completion = client.chat.completions.create(
  model="model-identifier",
  messages=[
    {"role": "system", "content": "Always answer in rhymes."},
    {"role": "user", "content": "Introduce yourself."}
  ],
  temperature=0.7,
)

print(completion.choices[0].message)

Node.js用のパッケージを使ったお試しなど

ここからは、Node.js用のパッケージを使ったお試しの流れなどの話です。

利用するライブラリについて

冒頭で掲載していた記事(ベータ版機能「LM Studio REST API」を使った話の記事)では、API を使うのに curl、または LM Studio公式の 2種類の SDK(TypeScript SDK「lmstudio-js」と Python SDK「lmstudio-python」)を使っていました。

今回は OpenAI互換の API を使うので、その API を使う処理の実装には、以下の OpenAI公式のパッケージを使うことにします。

●openai - npm
 https://www.npmjs.com/package/openai

image.png

パッケージのインストール用コマンドは、以下になります。

npm i openai

ローカルサーバーの準備

今回の API を使うために、LM Studio でローカルサーバーを準備します。

そのために、LM Studio のアプリを開き、左メニューの上から 2番目の部分を選びます。さらに、「Status Stopped」と書かれた横の部分をクリックし、サーバー機能を ON にします。

image.png

さらに、画面上の部分のメニュー部分から、今回使うモデルの読み込みを行います。

image.png

今回、これまでのお試しでも使った Gemma 3 270M(lmstudio-community/gemma-3-270m-it-GGUF)を使います。

image.png

読み込みが完了すると、以下のように準備完了の状態になり、API の使い方の情報も見ることができます。

image.png

API を使った処理と出力

ここから実装などの話です。

ストリーミングを使わない出力

とりあえず、過去に OpenAI の Responses API のお試しをやった時のコードを元に、今回用の実装を行います。具体的には以下のとおりです。

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: `http://localhost:1234/v1`,
  apiKey: "lm-studio",
});

const response = await client.responses.create({
  model: "model-identifier",
  input: "あなたは誰?",
});

console.log(response.output_text);

LM Studio公式ドキュメントと、OpenAI の公式情報を見つつ、上記のコード内で以下の設定をしています。

...
  baseURL: `http://localhost:1234/v1`,
  apiKey: "lm-studio",
...

...
  model: "model-identifier",
...

ちなみに上記の apiKeymodel は、以下のコードのように空の文字列にしても動作するようでした。

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: `http://localhost:1234/v1`,
  // apiKey: "lm-studio",
  apiKey: "",
});

const response = await client.responses.create({
  // model: "model-identifier",
  model: "",
  input: "あなたは誰?",
});

console.log(response.output_text);

上記を実行して、API のレスポンスを得られることが確認できました。

ストリーミングを使った出力

最後にストリーミングを使った出力を試します。

以前にもストリーミング出力は実装したことがあったので、それを上で使っていたお試し用コードに適用しました。実装内容は、以下のとおりです。

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: `http://localhost:1234/v1`,
  // apiKey: "lm-studio",
  apiKey: "",
  stream: true,
});

const input = "p5.jsとは?";

const stream = await client.responses.create({
  model: "",
  input,
  stream: true,
});

console.log(`\nプロンプト: ${input}\n`);

for await (const event of stream) {
  if (event.type === "response.output_text.delta") {
    const chunk = event.delta;
    if (chunk && chunk.trim() !== "") {
      process.stdout.write(chunk);
    }
  }
}
process.stdout.write("\n");

この内容で、冒頭にも掲載していた以下の出力を得られました。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?