しめ鯖日記

swift, iPhoneアプリ開発, ruby on rails等のTipsや入門記事書いてます

MacでPython+Anacondaを使ってDolly2.0を動かす

商用利用もできるLLMのDolly2.0を試してみました。

まずはAnacondaをインストールします。
Anacondaはデータサイエンスや機械学習などで良く使われるツールで、便利なライブラリなどを含んだプラットフォームです。

Anacondaはインストーラーを使ってインストールしました。
最初はpyenvを使ったのですがうまくできなかったので公式サイトからインストールしました。

www.anaconda.com

次はDolly2.0を使ってみます。
今回は下URLを参考にしました。

qiita.com

まずはAnacondaでDolly用の仮想環境を作ってPythonをインストールします。

conda create --yes -n env-dolly-v2
conda activate env-dolly-v2
conda install python=3.10.10 --yes

次は必要なライブラリをインストールします。

pip install torch torchvision torchaudio accelerate transformers

最後にmain.pyを作って下のように記述します。
今回はKotlinでButtonの背景色を変える方法を聞いてみました。

import torch
from transformers import pipeline

model_name = "databricks/dolly-v2-12b"
generate_text = pipeline(model=model_name, torch_dtype=torch.bfloat16, trust_remote_code=True, device_map="auto")
text = generate_text("How to change the background color of Button in kotlin?")
print(text[0]['generated_text'])

あとはpythonでmain.pyを動かせば完了です。

python main.py

Dollyをダウンロードするので初回起動は非常に時間がかかります。

2回目以降も4分ほど時間がかかりました。
Activityモニタで見た所、メモリも20GB以上使っていました。

回答は下の通りです。
Buttonの初期化やbackgroundTintListのセット方法など違っているのですがそれっぽい答えは返ってきました。

To change the background color of Button in kotlin, you can use the property backgroudTintList.
For example:
Button().backgroundTintList(Color.rgb(255, 255, 255))

プログラミング以外の質問もしてみました。
今回は旅行先を尋ねました。

text = generate_text("Do you have any recommendations for destinations?")

旅行先についてはかなりそれらしい答えを返してくれました。

Recommended destinations vary based on the type of traveller and the purpose of visit. For solo travellers, Barcelona is a great destinations to kickstart your travel experience. The vibrant nightlife and artistic scene make it a great place to have some fun. For those who like a great culture experience, Italy can be a great option. From food, wine and art, it offers everything. For those who like to get away from the city and see the natural beauty, Arizona, United States is an option. With its diverse nature scenes, it offers something for everyone.

日本語でも聞いてみました。

text = generate_text("おすすめの旅行先があれば教えてください。")

回答は下の通りです。
旅行っぽい言葉を返してくれるのですが内容は支離滅裂でした。

ベトナムに言語根性のない人以外は不向きなのであまりおすすめできないかもしれませんが、日本を一時帰国させてからデッキンasusの港に出発する airports に Japan と printed でスペリングしていただきたい場所があります。私が行ったように検問で確認されただけでも便利ですね