Skip to content

DNET_Ollama

nishi_74322014 edited this page Sep 11, 2026 · 1 revision

Ollama

概要

  • LLMをローカルで実行するためのCLIツール兼ランタイム

  • その他にも、LM Studio、Llamafile、GPT4ALL、H2oGPTなどがあるらしい。

  • Hugging Faceと同様にLLM界隈のPFっぽいヤツだが、機能的には補完関係にある。

    • Hugging Face:OSSのLLMを学習させ、その結果をホスティングするPF&ツール
    • Ollama:OSSのLLMをローカル実行させるためのPF&ツール
  • また、GitHub、Docker Hubに似た、モデル管理・配布のプラットフォーム性がある。

    • Ollama pull/pushは、git pull/push、docker pull/pushっぽくもある。
    • Modelfileは、Dockerfile に似ているが、仕組みは全く異なり、
      Ollama 独自のミニ構文で、モデル構成やパラメタ定義に使われているだけ。

詳細

インストール

Windows(インストール)

WindowsにOllamaをインストールしてLlama3を実行する。

Linux(インストール)

LinuxにOllama - Llama3をインストールして実行する。

  • インストール

    curl https://ollama.ai/install.sh | sh
    

    ※ リダイレクトされる場合は、-Lオプションを追加する。

  • サーバ起動

    ollama serve
    

動作確認

LLMの起動

  • 以下のようなコマンドを使用してLLMを実行
    (必要に応じて、別ターミナルで実行する)

    • llama

      ollama run llama2
      ollama run llama3
      
    • gemma

      ollama run gemma2
      
    • phi

      ollama run phi3
      
  • 使用可能なLLM(コマンドライン引数)は以下のサイトから確認できる。

標準I/Oから

success
>>> こんにちは、あなたは誰ですか?
こんにちは!I'm LLaMA, a large language model trained by a team of researcher at Meta AI. My primary function is
to understand and respond to human input in a helpful and informative manner. I can converse on a wide range of
topics, from science and history to entertainment and culture. I'm constantly learning and improving my abilities,
so please bear with me if I make any mistakes. Nice to meet you!

WebAPIから

  • WSL2寄せ
    modelをphi3にして、ollama run phi3もWebAPIで動作した
    (故に、11434ポートのWebAPIはollama仕様と思われる)。

    • +curl
      生成形式

      curl http://localhost:11434/api/generate -d '{
        "model": "llama3",
        "prompt":"user", "content": "why is the sky blue?"
      }'
      

      チャット形式

      curl http://localhost:11434/api/chat -d '{
        "model": "llama3",
        "messages": [
          { "role": "user", "content": "why is the sky blue?" }
        ]
      }'
      
    • +Notebook
      HTTPClient

      import requests
      url = "http://localhost:11434/api/chat"
      payload = {
          "model": "llama3",
          "messages": [
              {"role": "user", "content": "why is the sky blue?"}
          ]
      }
      
      response = requests.post(url, json=payload)
      print(response.text) # print(response.json())
  • Linux+curl
    「WSL2寄せ」での説明内容と同じ。

  • 環境を跨ぐ

    • Windows+curl → WSL2(Ollama)

      curl -X POST -H "Content-Type:application/json" http://localhost:11434/api/chat -d "{\"model\": \"llama3\", \"messages\": [{ \"role\": \"user\", \"content\": \"why is the sky blue?\" }]}"
      
    • WSL2+curl → Windows(Ollama)

      • IPアドレスを取得

        >ipconfig
         イーサネット アダプター vEthernet (WSL):
           接続固有の DNS サフィックス . . . . .:
           リンクローカル IPv6 アドレス. . . . .: ...
           IPv4 アドレス . . . . . . . . . . . .: 172.xxx.xxx.1
           サブネット マスク . . . . . . . . . .: 255.255.240.0
           デフォルト ゲートウェイ . . . . . . .:
        
      • CORS設定
        ナニをドウ設定したら良いか解らんわ(笑)

        OLLAMA_HOST
        OLLAMA_ORIGINS
        

Error: digest mismatch

Error: digest mismatchになる場合(恐らくプロキシ環境で発生)

プロキシ設定

以下のようにOllamaプロキシ系の環境変数を設定してもエラーになる。
(キャッシュ・中継プロキシがコンテンツを改変するため、とのこと)

  • Ollama設定ファイルを開く。

    sudo systemctl edit ollama.service
    
  • エディタにプロキシ環境に合わせた内容を記述

  • 設定を反映

    sudo systemctl daemon-reload
    sudo systemctl restart ollama
    

手動ダウンロード

huggingface.co(dockerで言うとdocker.comみたいな仕組み)から、
モデルのファイル(GGUF file、Model file)を手動ダウンロード。

  • Phi-3-mini-4k

    • GGUF file、Model fileをダウンロード
      https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-gguf

      • Download the GGUF model:

        huggingface-cli download microsoft/Phi-3-mini-4k-instruct-gguf Phi-3-mini-4k-instruct-q4.gguf --local-dir .
        
      • Get the Modelfile:

        huggingface-cli download microsoft/Phi-3-mini-4k-instruct-gguf Modelfile_q4 --local-dir .
        
    • GGUF file、Model fileをファイルをカレント・ディレクトリに配置

    • モデルを

      • 作成する。
        ※ Model file中からGGUF fileをポイント

        ollama create phi3 -f Modelfile_q4
        
      • 実行する。

        ollama run phi3
        
      • 削除する。

        ollama rm phi3
        
  • Llama-3.2-1B

    • GGUF fileをダウンロード
      https://huggingface.co/bartowski/Llama-3.2-1B-Instruct-GGUF

      • Downloading using huggingface-cli

        huggingface-cli download bartowski/Llama-3.2-1B-Instruct-GGUF Llama-3.2-1B-Instruct-Q8_0.gguf --local-dir .
        
    • Model fileを作成する

      • プロキシなし環境で以下を試みる。
        (何気に、コレで動かせてしまうかも?)

        ollama run hf.co/bartowski/Llama-3.2-1B-Instruct-GGUF:Q8_0
        
      • 以下のコマンドの出力をModelfile.txtに保存する。

        ollama show --modelfile hf.co/bartowski/Llama-3.2-1B-Instruct-GGUF:Q8_0 > Modelfile.txt
        
    • GGUF file、Model fileをファイルをカレント・ディレクトリに配置

    • モデルを

      • 作成する。
        ※ Modelfile中からGGUF fileをポイント

        ollama create Llama3 -f Modelfile.txt
        
      • 実行する。

        ollama run Llama3
        
      • 削除する。

        ollama rm Llama3
        
  • 参考

参考

環境

Ubuntu

Windows(参考)

C/S

クライアント

サーバー(LLM)

移行メモ

  • 見出し・本文で「Window」となっていた箇所は「Windows」に統一した。
  • 同名の見出し(「Windows」「Linux」)がインストールと参考で重複し GitHub Wiki でアンカが衝突するため、括弧で文脈を補って一意にした。
  • PukiWiki のページ内アンカ(#xxxxxxxx)は GitHub Wiki では再現できないため、 他ページのアンカを指すリンクは「〜(ページ名 の該当節を参照)」の形に置き換えた。

Tags: 移行, Ollama, ローカルLLM, Llama3, Phi-3, GGUF, Modelfile, WSL2, Docker

NetDevInfraWiki

マイクロソフト系技術情報 Wiki
Open 棟梁 Wiki

(未着手)

開発基盤部会 Wiki

移行管理: DONETODO

Clone this wiki locally