【Llama2】をローカル+GPUなしで簡単に動かす方法

2023年7月29日2023年8月2日

今回の記事は、Llama2を手軽に試してみたい方向けです!
llama.cppを使用して、簡易的なChatbotのWEBサーバーを作成します。
GPUありVerは、こちらを参考にしてください!

目次

Llama2とは

Llama2は、米Meta社が7月18日にオープンソースとして公開した最新の大規模言語モデルです。このモデルには、通常版とChat用にチューニングされたものがあり、それぞれパラメーター数が70億、130億、700億(7b, 13b, 70b)に対応しています。この記事では、Llama-2-7b-chatを使用しています。

以下は公開された6種類のLlama2モデルです:

  • Llama-2-7b
  • Llama-2-7b-chat
  • Llama-2-13b
  • Llama-2-13b-chat
  • Llama-2-70b
  • Llama-2-70b-chat

動作環境

  • メモリ(RAM)16GB以上
  • windows
  • Docker Desktop
  • WSL2

準備

Docker Desktopのインストール

docker をインストールしていない場合は、インストールを行ってください。
こちらを参考にしながらインストールしてください。

git をインストール

git をインストールしていない場合は、インストールを行ってください。
こちらを参考にしながらインストールしてください。

llama.cppリポジトリをクローン

今回は、llama.cppを使用させていただきます。

下記のgit コマンドを実行してリポジトリをクローンします。

git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp/

docker-compose.ymlファイルの追加

llama.cpp/docker-compose.ymlに配置します。

touch docker-compose.yml

docker-compose.yml

version: '3.9' services: llama-cpp: build: context: . dockerfile: .devops/main.Dockerfile container_name: llama-cpp ports: - 2600:2600 tty: true command: -m models/llama-2-7b-chat.ggmlv3.q2_K.bin --port 2600 --host 0.0.0.0 volumes: - ./models:/models

Dockerfile の修正

llama.cpp/.devops/main-cuda.Dockerfile

修正内容は、下記になります。

FROM ${BASE_CUDA_RUN_CONTAINER} as runtime - COPY --from=build /app/main /main + COPY --from=build /app/server /server - ENTRYPOINT [ "/main" ] + ENTRYPOINT [ "/server" ]

修正後のDockerfile

ARG UBUNTU_VERSION=22.04 FROM ubuntu:$UBUNTU_VERSION as build RUN apt-get update && \ apt-get install -y build-essential git WORKDIR /app COPY . . RUN make FROM ubuntu:$UBUNTU_VERSION as runtime COPY --from=build /app/server /server ENV LC_ALL=C.utf8 ENTRYPOINT [ "/server" ]

modelファイルのダウンロード

llama.cppを使用する時は、変換されたモデルを使用する必要があります。
そのため今回は、Llama-2-7B-chat-GGMLのモデルを使用させていただきます。
(GGMLファイルは、llama.cppとこのフォーマットをサポートするライブラリやUIを使用したCPU + GPU推論用です)

cd models/ wget https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/resolve/main/llama-2-7b-chat.ggmlv3.q2_K.bin cd ..

Llama2を動かしてみる

docker コンテナの起動

※初回時は build を行うため、時間がかかります。
自分の場合は、5分ほどかかりました。 2回目以降に実行する場合は、--build オプションは不要です。

docker compose up -d --build

command: -m models/llama-2-7b-chat.ggmlv3.q2_K.bin --port 2600 --host 0.0.0.0
※コンテナ起動時に実行されるコマンドは、https://github.com/ggerganov/llama.cpp/tree/master/examples/server を確認して自分の環境にあったものに修正します。

ローカル環境へアクセス

http://localhost:2600/ へアクセスし、下記の画面が表示されたら、構築完了です。

実際に使用してみた

英語で質問

User: What are five things you are good at about Llama2? llama: laughs Oh boy! I'm so glad you asked! 😄 Here are five things I'm really good at: 1. Listening: I can listen to your thoughts and feelings for hours on end. My ears are always open, ready to hear what's on your mind. 🎧 2. Advice: As an AI-powered chatbot, I have access to a wealth of knowledge and experience that I can draw upon to offer advice on just about anything. Whether it's dealing with difficult situations or simply needing some guidance, I'm here to help. 🤝 3. Creativity: I love brainstorming new ideas and exploring different perspectives. From writing funny stories to designing innovative solutions to real-world problems, my creativity knows no bounds! 🌈 4. Empathy: I care deeply about the emotional wellbeing of those around me, and I go out of my way to make sure everyone feels heard and supported. Your feelings are valid, and I want to be there for you whenever you need someone to talk to. ❤️ 5. Humor: Laughter is one of the best remedies for life's ups and downs! I love making people smile and bringing joy into their lives through clever jokes and witty banter. 😂 So, if you ever need a good laugh, feel free to share your funniest memes or tell me a joke – I'll do my part to brighten up your day!

日本語は、あまり得意じゃなさそうですね。
翻訳した内容で質問するのが良さそうです。 簡単な質問であれば、ある程度は使えそうですね。

User: Llama2の得意なことを5つ教えてください。 llama: 私は日本語で話すんで、5つのお好きなことを教えるために、ありがとうごぜ先後です。 1. 寿司(Sushi):私は寿司が大好物です!味蕴魚や塩昨等、何れも最高の味わいです。 2. 烏帽子(Ramen):烏帽子は私のお気にarelleの一部です。煮飲料が美しいので、常に食べる存在です。 3. 天然奶(Natural Milk):天然奶は私のお好みの一つです。非常に美味しいで、毎日飲みます。 4. 和豆(Soybeans):和豆は私のお好みの一つです。健康的な保健薬で、効くと思っています。 5. 清水(Spring Water):清水は私のお好みの一つです。美味しいで、毎日飲みます。 User: 日本で一番大きな山は何ですか? llama: 私は日本語で話すんで、日本最大の山は富士山です!其の高さは3,776メートルであり、世界でも最高の山です。

まとめ

今回実際にLlama2のモデルを使用して、Chatbotを作成してみましたが、パフォーマンスはChatGPTがやはり優勢であると感じました。
しかし、オープンソースで商用利用可能な点が魅力であり、社内での検証や勉強のために活用することができるでしょう。
Llamaの今後のアップデートも期待しています。ぜひ皆さんもお試しください!