0
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?

Mac mini で ローカルLLM: Ollama で「REST API」とライブラリ「ollama-js」を試す(モデルは Gemma 3)

Last updated at Posted at 2025-05-14

はじめに

過去にローカルLLM を試していた際には、MLX LMMLX-VLM ばかり使っていましたが、最近、以下の記事に書いた Ollama + Mac mini の組み合わせを試しました。

●M4 の Mac mini で ローカルLLM: Ollama で Gemma 3 を軽く試す(テキスト入力、画像も入力するマルチモーダル) - Qiita
 https://qiita.com/youtoy/items/d921bfa19794e328c7e1

今回の内容はその続きで、「REST API」と「ollama-js」を試した話です。

REST API

https://github.com/ollama/ollama?tab=readme-ov-file#rest-api
image.png

ollama-js

https://github.com/ollama/ollama?tab=readme-ov-file#libraries
image.png

●ollama/ollama-js: Ollama JavaScript library
 https://github.com/ollama/ollama-js

お試し1: REST API を使う

まずは REST API を使った話です。

試したコマンドと実行結果

Ollama が起動している状態だと、HTTPリクエストでレスポンスを得られる状態に既になっているようです。

以下に、公式の情報を参考にして試したコマンドと、その実行結果を掲載します(※ 2通り試しています)。

curl http://localhost:11434/api/generate -d '{
  "model": "gemma3:12b-it-q8_0",
  "prompt":"なぜ空は青い?"
}'

image.png

curl http://localhost:11434/api/chat -d '{
  "model": "gemma3:12b-it-q8_0",
  "messages": [
    { "role": "user", "content": "なぜ空は青い?" }
  ]
}'

image.png

どちらもストリーミングで結果を得ることができました(付加情報ごと出てきているので、かなり見づらいですが...)。

お試し2: ollama-js を使う

次に ollama-js を使った話です。

●ollama/ollama-js: Ollama JavaScript library
 https://github.com/ollama/ollama-js

image.png

試した手順などと実行結果

以下、試した手順などと実行結果です。

まずはライブラリのインストールです。

npm i ollama

そして、以下の内容を実装して、nodeコマンドで実行しました。

import ollama from "ollama";

const message = { role: "user", content: "なぜ空は青い?" };
const response = await ollama.chat({
  model: "gemma3:12b-it-q8_0",
  messages: [message],
  stream: true,
});
for await (const part of response) {
  process.stdout.write(part.message.content);
}

以下が実行結果です(※ 途中を省略してます)。

前回の記事と同様に、ストリーミングの応答を得ることができました。

おわりに

引き続き Ollama を使ったローカルLLM のお試しをやっていこうと思います。

0
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
0
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?