![見出し画像](https://assets.st-note.com/production/uploads/images/124803797/rectangle_large_type_2_7ed7926a4ac17264e11fbf9e2e7d5ac6.png?width=1200)
M3 MacbookProでフレームワーク「MLX」をつかってローカルLLM Mistral編
前日の記事の続きです。
今度は、examplesにあるMistralを試してみます。
conda activate mlxでconda環境を立ち上げて、私の設定した場所に従って、
cd ai/mlx/mlx-examples/mistral でdirectoryに移動。
pip install -r requirements.txt
上で、必要なパッケージをインストール。
model と tokenize を以下でダウンロード。
1行ずつ実行してみます。
curl -O https://files.mistral-7b-v0-1.mistral.ai/mistral-7B-v0.1.tar
tar -xf mistral-7B-v0.1.tar
このコマンド群は、UNIX系オペレーティングシステムのターミナルで使用するためのものです。それぞれのコマンドは以下のような操作を行います。
1. `curl -O https://files.mistral-7b-v0-1.mistral.ai/mistral-7B-v0.1.tar`
``curl`はコマンドラインツールで、URLからデータを転送するために使われます。`-O`(大文字のオー)オプションは、指定されたURLからファイルをダウンロードして、そのファイルをローカルマシンに保存する際に、URLで指定されたファイル名をそのまま使用することを指示します。この場合、`mistral-7B-v0.1.tar`という名前のアーカイブファイルをダウンロードしています。
2. `tar -xf mistral-7B-v0.1.tar`
``tar`はアーカイブユーティリティで、ファイルやディレクトリをアーカイブ(一つのファイルにまとめる)したり、アーカイブを展開したりするために使用されます。`-x`オプションはアーカイブを展開することを意味し、`-f`オプションは「ファイル名を指定する」という意味です。したがって、`tar -xf mistral-7B-v0.1.tar` コマンドは、`mistral-7B-v0.1.tar`というアーカイブファイルを展開します。
これらのコマンドを順に実行すると、最初に指定されたURLから`mistral-7B-v0.1.tar`という名前のファイルをダウンロードし、次にこのアーカイブファイルを展開して中に含まれるファイルやディレクトリにアクセスできるようにします。
ダウンロードにそこそこ時間がかかります。
解凍後のファイル構成みてみると、以下の通りでした。
(mlx) mistral % ls
README.md mistral-7B-v0.1.tar test.py
convert.py mistral.py
mistral-7B-v0.1 requirements.txt
documentに従って、下記を実行。
python convert.py
何の変化があったのかわかりません…
documentをGPT-4に説明してもらいました。
訳文:
```
一度重みをMLX形式に変換したら、Mistralモデルを使ってテキストを生成することができます:
```
意味の説明:
この文章は、あるプロセスや手順を完了した後に、何ができるようになるかを説明しています。具体的には、「重みをMLX形式に変換した後」という部分がその手順を指しており、MLX形式に関連する何らかのデータ変換が必要であることを示しています。変換が完了すると、それを利用して「Mistralモデルを使ってテキストを生成」することが可能になります。
「重み」とは、ここでは機械学習モデルの文脈で使われており、モデルが学習過程で獲得したパラメータのことを指しています。MLX形式は、Mistralモデルが理解しやすい、あるいは処理しやすいように最適化された特定のファイル形式を指している可能性があります。
総じて、この指示は、ユーザーが機械学習モデルの重みを互換性のある形式に変換することに成功したら、Mistralという特定の機械学習モデルを使用して新たなテキストを生成できるようになる、というプロセスを説明しています。テキスト生成は、例えば、文章の自動生成、チャットボットの応答生成、あるいはその他の自然言語処理タスクで用いられることがあります。
とりあえず、documentどおりに、下記を実行。
python mistral.py --prompt "It is a truth universally acknowledged," --temp 0
こんな出力でした。
[INFO] Loading model from disk.
[INFO] Starting generation...
It is a truth universally acknowledged, that a single man in possession of a good salary, must be in want of a wife.
Or at least, that’s what the 19th century novelist Jane Austin thought.
But what about the single woman?
In the 19th century, the single woman was often seen as a burden on society. She was often seen as a burden on her family, and was often seen as a burden on the state.
The single woman was
------
ちょっと、日本語をいれてみます。
(mlx) mistral % python mistral.py --prompt "昔々あるところに," --temp 0
[INFO] Loading model from disk.
[INFO] Starting generation...
昔々あるところに, あるところに, あるところに, あるところに, あるところに, あるところに, あるところに, あるところに, あるところに, あるところに, あるところに, あるところに, あるところに, あると
------
う〜ん という感じですね。ヘルプを見てます。
python mistral.py --help
(mlx) mistral % python mistral.py --help
usage: mistral.py [-h] [--model_path MODEL_PATH] [--prompt PROMPT]
[--max_tokens MAX_TOKENS] [--temp TEMP]
[--tokens_per_eval TOKENS_PER_EVAL] [--seed SEED]
Mistral inference script
options:
-h, --help show this help message and exit
--model_path MODEL_PATH
The path to the model weights and tokenizer
--prompt PROMPT The message to be processed by the model
--max_tokens MAX_TOKENS, -m MAX_TOKENS
Maximum number of tokens to generate
--temp TEMP The sampling temperature.
--tokens_per_eval TOKENS_PER_EVAL
The batch size of tokens to generate.
--seed SEED The PRNG seed
あと、試してみたのは、こんな感じ
(mlx) mistral % python mistral.py --prompt "a long long time ago," --temp 0
[INFO] Loading model from disk.
[INFO] Starting generation...
a long long time ago, in a galaxy far far away, i was a little girl who loved to play with her barbies. i had a whole collection of them, and i loved to dress them up and play with them. i would spend countless afternoons playing with my barbies, and i would always have a great time.
i was a little girl who loved to play with her barbies. i had a whole collection of them, and i loved to dress them up and play with
------
私ができたのは、ここまでです。
chat形式でするには、きっと mistral.pyの内容を理解して手を加えないといけないのでしょうか?
いいなと思ったら応援しよう!
![Lucas](https://assets.st-note.com/production/uploads/images/107879020/profile_1a18442658444d51682a0e9f99ec2cbd.jpeg?width=600&crop=1:1,smart)