すぐできる!Llama 2をWindowsのローカル環境で使う方法

Llama 2 LLM
Llama 2

Llama 2とは、Metaが開発したオープンソースの大規模言語モデル(LLM)です。

ブラウザーをアップデートしてください | Facebook

Llama 2は無料で商用利用可能のモデルでありながら、OpenAIのGPT-3.5に匹敵する性能を持つといわれています。

この記事では、Windowsのローカル環境へのLlama 2のインストール方法と使い方を説明します。

実行環境

この記事では以下の環境で動作を確認しました。

  • Python 3.10.9
  • Windows 11 Home
  • llama-cpp-python 0.1.74

前提

Llama 2のモデルをMetaの公式サイトからダウンロードするには利用申請が必要です。

しかし、利用申請からモデルをダウンロードできるようになるまでには数日かかる場合があり面倒です。

そのため、この記事では非公式のモデルの使用方法を紹介します。

非公式とはいっても、Llama 2のモデルは条件を満たせば再頒布することが認められているため、性能は変わらず、ライセンスについても安心して利用できます。

また、Llama 2のモデルを利用するためにLlama.cppを使います。

Llama.cppはC言語で実装されたLlamaの実行環境で、MacOS、Linux、WindowsのいずれのOSでもLlamaを動かすことができる優れものです。

C言語で実装されているため高速で動作し、GPUがないマシンでも動作します。

Llama 2モデルのダウンロード

Hugging FaceのページからLlama 2の13Bモデルをダウンロードします。

llama-2-13b-chat.ggmlv3.q4_K_M.bin · TheBloke/Llama-2-13B-chat-GGML at main
We’re on a journey to advance and democratize artificial intelligence through open source and open science.

今回はLlama.cppを使うためGGML形式のモデルを選びます。

ダウンロードしたらわかりやすいフォルダに置いておきましょう。

ここではCドライブ直下に「Llama 2」というフォルダを作ってその中に入れました。

必要なライブラリをインストールする

次にLlama.cppのPython版のライブラリをインストールします。

pipでインストールできます。

pip install llama-cpp-python

Llama 2を使う

あとはPythonでコードを書いて実行するだけです。

今回は以下のコードを用意しました。

プロンプト部分(prompt)は自由に書き換えてください。

from llama_cpp import Llama

# プロンプト
prompt = "What are your favorite places to visit in Japan for a summer trip?"

# モデルの準備
llm = Llama(model_path="./llama-2-13b-chat.ggmlv3.q4_K_M.bin")

# 出力
output = llm(
    prompt,
    max_tokens=300,
    echo=True,
)
print(output['choices'][0]['text'])

これをexample.pyという名前で先ほどモデルを配置したフォルダと同じ場所に保存します。

コマンドプロンプトでこれを実行すると、Llama 2からの回答が得られます。

python example.py

以下が質問と回答です。

What are your favorite places to visit in Japan for a summer trip?

Summer is one of the best seasons to visit Japan, with warm weather and long days that allow you to make the most of your time exploring the country. From the beautiful beaches of Okinawa to the bustling streets of Tokyo, there's something for everyone in Japan during the summer months. Here are some of my favorite places to visit in Japan during the summer:

1. Okinawa - With its crystal-clear waters and pristine beaches, Okinawa is a tropical paradise that's perfect for soaking up the sun and enjoying water activities like snorkeling, diving, and surfing. Some must-visit spots in Okinawa include Shuri Castle, Churaumi Aquarium, and Cape Zanpa.

2. Tokyo - While Tokyo can be hot and humid during the summer months, it's still an amazing place to visit. From traditional festivals like the Star Festival to modern attractions like the VR Zone Shinjuku, there's always something new to explore in this vibrant city. Don't miss out on trying some delicious Japanese street food like takoyaki and okonomiyaki!

3. Kyoto - Japan's former capital is a must-visit destination for any traveler, especially during the summer months when the cherry blossoms are in full bloom. Take a

一応、DeepLで翻訳したものも以下に載せておきます。

夏の旅行で訪れたい日本の名所は?

夏は日本を訪れるのに最適な季節のひとつです。暖かい気候で日が長いので、日本を探索する時間を最大限に楽しむことができます。沖縄の美しいビーチから東京の賑やかな通りまで、夏の日本には誰もが楽しめるものがある。ここでは、私が夏の間に日本で訪れたいお気に入りの場所をいくつか紹介しよう:

1. 沖縄 - 透明度の高い海と手つかずのビーチが広がる沖縄は、日光浴やシュノーケリング、ダイビング、サーフィンなどのウォーターアクティビティを楽しむのに最適な南国の楽園だ。首里城、美ら海水族館、残波岬など、沖縄でぜひ訪れたいスポットもある。

2. 東京 - 東京は夏の間は蒸し暑いが、それでも訪れるべき素晴らしい場所だ。七夕のような伝統的なお祭りから、VRゾーン新宿のような現代的なアトラクションまで、この活気あふれる街には常に新しい発見がある。たこ焼きやお好み焼きなど、おいしい日本のストリートフードもお見逃しなく!

3. 京都 - 日本のかつての首都である京都は、特に桜が満開になる夏の間は、旅行者なら必ず訪れるべき場所だ。桜の季節には

回答の精度は悪くありませんね。

オープンソースで商用利用可能で、ローカル環境で実行できるものとしてはなかなかのレベルだと思います。

上記では英語で質問をしていますが、日本語で質問した場合はどうでしょうか?

実行した結果は以下でした。

夏の旅行で訪れたい日本の名所は?

There are many beautiful places to visit in Japan during the summer, but here are some of the most popular and must-visit destinations:

1. Kyoto: Known for its temples and gardens, Kyoto is a great place to experience traditional Japanese culture and beauty. Must-visit spots include Kinkakuji Temple (Golden Pavilion), Fushimi Inari Shrine (Thousand Torii Gates), and Arashiyama Bamboo Grove.
2. Hakone: This picturesque town is located near Mount Fuji and offers stunning views of the mountain, as well as hot springs, scenic cruises on Lake Ashi, and a beautiful botanical garden.
3. Yakushima Island: This island in southern Japan is known for its pristine beaches, ancient forests, and unique flora and fauna. It's a great place to escape the crowds and enjoy nature.
4. Naoshima Island: This small island in the Seto Inland Sea is famous for its modern art museums and sculptures, as well as its beautiful beaches and coastal scenery.
5. Mount Fuji: Japan's iconic mountain is a must-visit destination during the

日本語の質問にも答えられますが、回答は英語になってしまいます。

日本語で使いたい場合は、プロンプトを工夫したりDeepLなどの翻訳サービスのAPIを使うなどの対策が必要になりそうです。

まとめ

この記事ではLlama 2をWindowsのローカル環境で使う方法を紹介しました。

Llama.cppを使うことでローカル環境でも早く簡単に使えます。

回答の精度もよいため、上手く使えばChatGPTのAPIを使うよりも低コストでLLMを活用できそうです。

記事の内容にわからないことがある方は以下のアカウントに気軽にDMしてください!

@sti320a

また、最新の技術の活用方法など役に立つツイートを心がけているので、ぜひフォローしてもらえると嬉しいです!

タイトルとURLをコピーしました