Dockerで簡単構築!Ollamaを使ってLLMを試そう

最近話題の大規模言語モデル(LLM)を自分の環境で手軽に試したいと思いませんか?
この記事では、Dockerを使って ollama を動かす方法を解説します。
特に、CPU環境でサクッとセットアップできるので、LLM初心者やハードウェアリソースが限られている方にもおすすめです。
1. Ollamaを動かすための準備
まずは必要なファイルを作成します。
Docker Composeファイルの作成
以下の内容で docker-compose.yml を作成します。これにより、ollamaをDockerコンテナとして動かす設定が整います。
services: ollama: image: ollama/ollama container_name: ollama ports: - "11434:11434" volumes: - ollama:/root/.ollama volumes: ollama:
コンテナの起動
次に、以下のコマンドを実行してコンテナを起動します。
docker compose up -d
これで、ollamaが立ち上がります。特にエラーが出なければ、次に進みましょう。
2. tinyllamaモデルを試す
起動したコンテナ内でtinyllamaモデルを利用できるように設定します。
モデルをPullする
以下のコマンドで、tinyllamaモデルを取得します。
docker exec -it ollama ollama run tinyllama
取得したモデルはコンテナ内の /root/.ollama に保存されます。
3. モデルに質問してみる
tinyllamaを使って質問してみましょう。以下のコマンドを実行します。
質問コマンド例
curl http://localhost:11434/api/generate -d '{ "model": "tinyllama", "prompt": "Why is the sky blue?", "stream": false }'
サンプルレスポンス
以下のようなレスポンスが返ってきます。
{ "model": "tinyllama", "response": "The sky blue color that we see in the sky is due to the presence of sunlight reflected by the Earth's surface. ...", "done": true }
シンプルな設定でLLMを利用できましたね!
4. Pythonからもっと便利に使う
CLIだけでなく、Pythonスクリプトからもollamaを使うことができます。
Pythonスクリプトの作成
以下のコードを ollama.py というファイル名で保存してください。
import requests # Ollamaのエンドポイント url = "http://localhost:11434/api/generate" # モデル名を指定 model_name = "tinyllama" # プロンプト prompt = "tell me about popular place in san diego." # リクエストのペイロード payload = { "model": model_name, "prompt": prompt, "stream": False } # リクエストの送信 response = requests.post(url, json=payload) # レスポンスの確認 if response.status_code == 200: print("Response:", response.json()["response"]) else: print("Error:", response.status_code, response.text)
スクリプトを実行
以下のコマンドでスクリプトを実行します。
python3 ollama.py
実行結果
例えば、以下のような回答が返ってきます。
Response: San Diego is a popular tourist destination known for its beautiful beaches, rich history, world-class restaurants and attractions such as the San Diego Zoo, SeaWorld, Balboa Park, La Jolla Cove, Mission Valley Farmers Market, and more. Some of the most popular places to visit in San Diego include:
1) Balboa Park - home to numerous museums, gardens, and cultural attractions, such as the San Diego Museum of Art, California Museum of Photography, and the Mexican Heritage Plaza.
2) San Diego Zoo - one of the largest zoo complexes in the world, featuring over 3,000 animals from more than 800 species.
3) La Jolla Cove - a scenic spot located at the foot of Mount Soledad where visitors can observe sea lions, seals, and otters playfully swimming in the crystal-clear waters.
4) Mission Valley Farmers Market - an outdoor market featuring fresh produce, locally grown foods, and unique artisanal goods.
5) Coronado Island - a picturesque seaside village known for its charming streets lined with historic mansions, beautiful beaches, and a range of dining options.
6) USS Midway Museum - a restored World War II aircraft carrier that offers interactive exhibits, live-aboard tours, and memorabilia displays.
7) La Jolla Botanical Cactus Garden - an extensive collection of cacti and succulents, featuring native species from all over the world.
8) Torrey Pines State Natural Reserve - a scenic coastal reserve with hiking trails, picnic areas, and stunning views of the Pacific Ocean.
9) Balboa Park Golf Course - a 13-hole golf course located in San Diego's Balboa Park that offers beautiful views of the city skyline.
10) Pacific Beach - a popular surfing spot known for its long beaches and stunning views of La Jolla Cove.
These are just a few examples of the many attractions and activities available in San Diego, but there's always something new to discover at any time!
旅行のアドバイスから、技術的な質問まで、あらゆるプロンプトに答えてくれます!
注意事項
LLMが生成する回答は便利な一方で、必ずしも正確であるとは限りません。回答内容に誤りが含まれている場合や、曖昧な表現が使われていることがあります。そのため、重要な決定や判断を行う際には、他の信頼できる情報源と照らし合わせて確認してください。
おわりに
Dockerを使えば、ollamaのセットアップはとても簡単です。tinyllamaモデルを使った質問やPythonスクリプトによる応用など、ローカル環境でLLMを自由に試せるのは魅力的ですよね。次は、さらに高度なモデルを試してみるのも良いかもしれません。
ぜひこの記事を参考に、LLMの世界を体験してみてください!
この記事をシェアする
合同会社raisexでは一緒に働く仲間を募集中です。
ご興味のある方は以下の採用情報をご確認ください。