Python

llama2の使い方

2023年10月3日



目的

Meta社と提携しているHugging Faceが提供する自然言語系モデルであるllama2を使用して、チャット環境を構築、利用する。

参考

https://pc.watch.impress.co.jp/docs/column/nishikawa/1519390.html

llama2モデルの主なバリエーション

パラメータ数に応じて、7B、13B、70Bのモデルが存在する
本紙では下記のモデルについて検証する

検証結果

モデル パラメータ数 検証方法 フィードバック
llama-2-7b-chat.ipynb 7B (70億) google colab (https://colab.research.google.com/github/camenduru/text-generation-webui-colab/blob/main/llama-2-7b-chat.ipynb) 回答具合は良好。
llama-2-13b-chat-GPTQ-4bit.ipynb 13B (130億) google colab (https://colab.research.google.com/github/camenduru/text-generation-webui-colab/blob/main/llama-2-13b-chat-GPTQ-4bit.ipynb) 使い出すとGPU RAMは9.4GB/15GBまで上がる。回答具合は良好。日本語での質問は英語で返す。技術質問の回答も的確。
japanese-elyza-llama2-7b-instruct.ipynb 7B (70億) google colab (https://colab.research.google.com/github/camenduru/japanese-text-generation-webui-colab/blob/main/japanese-elyza-llama2-7b-instruct.ipynb) 使い出すとGPU RAMは10.6GB/15GBまで上がる。回答は良好。回答の入力が遅い。日本語で返答される。
llama-2-70b 70B (700億) ブラウザ実装サイト(https://llama.replicate.dev/ 回答具合は良好。
llama-2-70b 70B (700億) ブラウザ実装サイト(https://www.llama2.ai/ 回答具合は良好。回答が遅い
Llama-2-7b-chat-hf 7B (70億) ローカル端末 モデルを指定してテキスト生成するスクリプトが失敗
ELYZA-japanese-Llama-2-7b-fast-instruct 7B (70億) ローカル端末 モデルを指定してテキスト生成するスクリプトが失敗

google colabを利用したモデルの起動方法

google colab

複数モデルの実行は不可。アクティブなセッションを切ってから次のモデルを構築する。

ローカル環境でモデルを利用する方法

MetaおよびHugging Faceへモデル利用申請する

https://ai.meta.com/llama/

アカウント登録が必要

ダウンロードスクリプト実行時に必要になるURLがメールで通知される

Hugging FaceでSignUpする

https://huggingface.co/

Read/Write権限を選択してトークンを発行すると、メールでトークンが通知される

git cloneでllamaリポジトリを取得


$ git clone https://github.com/facebookresearch/llama

$ cd llama

ダウンロードスクリプトを実行

$ bash download.sh

Enter the URL from email

・・・申請時に返信されたメールに記載のURLをコピペしてEnter

メール参考

Model weights available:

Llama-2-7b

Llama-2-7b-chat

Llama-2-13b

Llama-2-13b-chat

Llama-2-70b

Llama-2-70b-chat

With each model download, you’ll receive a copy of the License and Acceptable Use Policy, and can find all other information on the model and code on GitHub.

How to download the models:

Visit the Llama repository in GitHub and follow the instructions in the README to run the download.sh script.

When asked for your unique custom URL, please insert the following:

https://download.llamameta.net/xxxxxxxxxx★該当URL

Select which model weights to download

The unique custom URL provided will remain valid for model downloads for 24 hours, and requests can be submitted multiple times.

上記URLを入力後に、ダウンロードするモデルを選択する

Enter the list of models to download without spaces (7B, 13B, 70B, 7B-chat, 13B-chat, 70B-chat), or press Enter for all :

7B

仮想環境でpipライブラリをインストール

仮想環境を構築する


$ cd llama

$ python -m venv myenv

$ source myenv/bin/activate

pipライブラリインストール

$ pip install transformers sentencepiece accelerate

Hugging Faceにログイン

(myenv) $ python -c "from huggingface_hub.hf_api import HfFolder; HfFolder.save_token('MY_HUGGINGFACE_TOKEN_HERE')"

(myenv) $ huggingface-cli login

Token:

・・・Hugging Face

CATEGORIES & TAGS

Python,

Author:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

関連記事

カテゴリー

むるし

フリーランスのインフラ系エンジニア。
備忘録で色々書いていきます。
お問い合わせは↓
mo-gyu@murci.net
LPIC303 Security
%d人のブロガーが「いいね」をつけました。