DEV Community

Cover image for 使用 Ollama + Ngrok 搭建本地 LLM,遠端存取 AI 模型教學
Let's Write
Let's Write

Posted on • Originally published at letswrite.tw

2 1 1 1 1

使用 Ollama + Ngrok 搭建本地 LLM,遠端存取 AI 模型教學

使用 Ollama + Ngrok 搭建本地 LLM,遠端存取 AI 模型教學

本篇要解決的問題

ChatGPT 等 AI 工具很方便,但有時工作上的數據可能會被 AI 當作學習資料,尤其這陣子新聞被洗版的 DeepSeek,幾個國家會禁用的原因都是安全疑慮。

前陣子新買了一台 Mac mini,就想來在本地安裝 LLM,然後在工作時使用。

本地安裝好 LLM 後,要能遠端使用,試了幾次 NAT 通訊埠轉發都失敗,就改用最簡單的 Ngrok。


安裝、使用 Ollama

Ollama 官網:https://ollama.com/

點擊 Download 就可以下載、安裝。

安裝完後,可以藉由 Ollama 下載 Models,像 Llama、Phi、Gemma、Mistral,有開源的這邊幾乎都有:

https://ollama.com/search

August 實測,買的 Mac mini 是:

  • 10 核心 CPU
  • 10 核心 GPU
  • 16GB 統一記憶體

運行 Phi-4 14B 還算順暢,但更大的模型(如 Mistral Small3 22B)就會明顯卡頓 QQ。

Ollama 要下載 Models 很簡單,從上面的官網網址找到想下載的,點進去後就會看到可以複製的命令,貼上終端機後執行就行:

Ollama 官網 Model 下載介面

Ollama 的指令如下:

以下是 Ollama 的指令對應的中文翻譯:

  • ollama serve - 啟動 Ollama
  • ollama run - 執行模型
  • ollama list - 列出所有模型
  • ollama rm - 移除模型
  • ollama create - 從 Modelfile 建立模型
  • ollama show - 顯示模型的資訊
  • ollama stop - 停止運行中的模型
  • ollama pull - 從 Ollama Models 拉取模型
  • ollama push - 將模型推送至 Ollama Models
  • ollama ps - 列出正在運行的模型
  • ollama cp - 複製模型
  • ollama help - 顯示指令的幫助資訊

前四個是最常用的。

安裝好 model,會看到一個基本的對話模式正在使用:

對話模式

輸入「/bye」就可以退出。

因為想讓遠端公司的電腦可以使用,需要的是 API 模式。

Ollama 啟動後,預設就可以用 API 的方式調用。

API

文件:https://github.com/ollama/ollama/blob/main/docs/api.md

POST 端點:

http://localhost:11434/api/generate

出去的參數,常用部份:

  • model:(必填)模型名稱
  • prompt:用來生成回應的提示詞
  • suffix:模型回應後的附加文字
  • images:(選填)Base64 編碼的圖片列表(適用於如 Llava 這類的多模態模型)
  • format:回應的格式。格式可以是 json 或 JSON schema
  • options:額外的模型參數,可參考 Modelfile 文件,例如 temperature
  • system:系統訊息(會覆蓋 Modelfile 中定義的內容)
  • template:要使用的提示詞模板(會覆蓋 Modelfile 中定義的內容)
  • stream:如果設定為 false,回應將以單一物件返回,而不是以流(stream)方式傳輸
  • raw:如果設定為 true,則不會對提示詞進行格式化。當請求 API 時,如果已提供完整的模板化提示詞,可以使用此選項
  • keep_alive:控制模型在記憶體中保持加載的時間(預設:5 分鐘)

這邊用 Postman 簡單測試一下:

用 Postman 測試

有收到 response 就代表成功了。


安裝、使用 Ngrok

Ngrok 是做什麼的呢?

想像我們在家裡建了一個「秘密基地」,可是我們的朋友都在公司當社畜,沒辦法直接走過來看秘密基地的樣子。

Ngrok 就是一個神奇的隧道,可以幫把家裡的「秘密基地」(我們的電腦或伺服器)連接到網路上,讓社畜們(咦)可以從公司(遠端位置)透過這個隧道找到我們。

再簡單來,就是我們原本只對內的 localhost,讓外部的外星人也連結得到。

Ngrok 官網:https://ngrok.com/

註冊以後,就可以看到有多種的安裝方式:

Ngrok 提供多種安裝方法

本篇示範 MacOS 的方式,上圖中選擇 MacOS 後,頁面下方就會出現安裝的命令:

MacOS 安裝 Ngrok

第一個命令是安裝 Ngrok。

第二個命令是設定 token,有 token 才可以使用 Ngrok 的服務。

接著以下命令,是把我們本地 Ollama API 的 11434 綁到 Ngrok:

ngrok http 11434 --host-header="localhost:11434"
Enter fullscreen mode Exit fullscreen mode

實測後,必需要是這樣下命令才可以。

執行後,會看到終端機出現以下訊息:

Ngrok 執行成功

最重要的是 Forwarding 那行:

https://fb55-118-233-2-60.ngrok-free.app -> http://localhost:11434
Enter fullscreen mode Exit fullscreen mode

代表我們本地的 localhost:11434,已經被 Ngrok 建立成公開網址 https://fb55-118-233-2-60.ngrok-free.app。

Postman 的測試可以修改後來測試:

換成 Ngrok 的網域

成功啦~ 之後可以在公司用 API 的方式使用了。

要注意的是,如果是用免費方案的 Ngrok,每次出來的公開網址都會不一樣,要一樣的話要升成付費方案。

本人的財力就跟小八差不多,免費的就覺得好用了 QQ。

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (1)

Collapse
 
xiamaocheng profile image
maocheng.xia

不错,谢谢分享

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay