🔬

Raspberry Pi 4 で LLM (ollama x phi)

2024/04/13に公開

背景

色々あって、LLM の環境が欲しかったので、部屋に落ちていた Raspberry Pi 4 (Mem:4G) で LLMをしてみたいと思います。

OSインストール


今回は、Ubuntu Server 22.04.4 LTS (64-BIT) を選択しました。

設定


設定はこんな感じです。

SSHでログイン

% ssh -2 llm01@llm01.local

とりあえず最新に

$ sudo apt update
$ sudo apt full-upgrade

Docker のインストール

Dockerの公式リポジトリを追加する前に、必要なパッケージをインストールします。
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common

Dockerの公式 GPG キーを追加します。
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

適切なアーキテクチャのリポジトリを追加
$ dpkg --print-architecture
$ sudo add-apt-repository "deb [arch=arm64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

$ sudo apt update
$ sudo apt install docker-ce docker-ce-cli containerd.io
$ sudo systemctl status docker
$ sudo usermod -aG docker $USER
$ exit
再度ログインして確認
$ groups
llm01 adm dialout cdrom sudo audio video plugdev games users input render netdev docker gpio spi i2c

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
478afc919002: Pull complete
Digest: sha256:03b30c6a3c320ff172b52bd68eddffde6ded08ce47e650fe52de861c5e9df46d
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

ollama 起動 & 会話してみる

ollama とは

簡単にローカルにLLMを使用できる環境を構築するツール。様々なモデルに対応しています。

https://ollama.com/

$ docker pull ollama/ollama
$ docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
$ docker ps
CONTAINER ID   IMAGE           COMMAND               CREATED         STATUS         PORTS                                           NAMES
a4cbf1de589b   ollama/ollama   "/bin/ollama serve"   8 seconds ago   Up 6 seconds   0.0.0.0:11434->11434/tcp, :::11434->11434/tcp   ollama
$ docker exec -it ollama ollama run phi

普段は、RTX4090で、Mixtralを使用していますが、今回は、raspberry pi なので、モデルは、phi を使用します。
実行すると、最初に、モデル(1.6G)がダウンロードされます。

>>> hello
 Hello, how can I assist you today?

ちゃんと、会話できました。すごい時代です。

おまけ

>>> can you speak japanese ?
 Yes, of course! Here are some examples of Japanese phrases:
- 你好,世界! (Nǐ hǎo, wǒ rén!) - Hello, world!
- こんにちは(こんにちは) - Konnichiwa (hello)
- ありがとう(ありがとう) - Arigatou (thank you)
- 劇法(婦人や子供たちの話ごとに) - Kōgen (story or talk about something interesting)

>>> 日本で一番高い山は?
 こんにちは! そこに悲しみの辺には一番高い山があります。 (Konnichiwa! Sochi ni tsukuru shimasho ni ikimasu. Tsumetachi no shita-no-ri wa one-gai heya ga arimasemasu.)

Translation: Hello! The snowy peak of Mount Fuji is the most famous mountain in Japan.

phi さんは日本語も少し話せるようです。

Discussion