關於 Ollama 的簡單筆記
前言
Ollama 是款大型語言模型 (LLM) 的軟體框架,其為研究人員和使用者提供了簡單且易用的環境,讓使用者於本機端執行各家的大型語言模型。
基本概念
模型所要的系統需求各不相同,例如:
- 3B 參數至少需要 8 GB RAM
- 7B 參數至少需要 16 GB RAM
- 13B 模型至少需要 32 GB RAM
快速安裝
curl -fsSL https://ollama.com/install.sh | sh
- Ollama 預設安裝在 /usr 目錄下,要注意 root 空間
- 可用以下方式手動指定目錄:
mkdir /home/[USER]/Program/Ollama
sudo chown -R ollama:ollama /home/[USER]/Program/Ollama
sudo systemctl stop ollama
sudo systemctl edit ollama.service
### Anything between here and the comment below will become the new contents of the file
[Service]
Environment="OLLAMA_MODELS=/home/[USER]/Program/Ollama"
### Lines below this comment will be discarded
下載並執行 Ollama 的模型
ollama serve
ollama list
ollama run gemma2:2b
ollama run llama3
https://ollama.com/library/gemma2
https://ollama.com/library/llama3
將模型檔 GGUF 轉換給 Ollama 使用
建立一個名為 Modelfile 的文字檔,內容輸入:
FROM ./gemma.gguf
終端機執行:
$ ollama create gemma_gguf_2_ollama -f Modelfile
$ ollama gemma_gguf_2_ollama
設定 Ollama 的 Max Tokens
/set parameter num_ctx 32768
https://python.langchain.com/api_reference/ollama/llms/langchain_ollama.llms.OllamaLLM.html#langchain_ollama.llms.OllamaLLM.num_ctx
關閉 Ollama 的休眠功能
Ollama 預設在沒有使用的狀態下五分鐘後會進入休眠,因此經常要重新載入模型。若要停用休眠功能,可以在載入模型時加入以下指令:習翠維尼
ollama run llama3.1:70b --keepalive=-1m
匯出 Ollama 模型
由於 Ollama 是採用自己的方式儲存模型,而不是單一的 gguf 給使用者。因此如何備份或匯入他人的模型,就成了麻煩的痛點。對於這個問題,Mitja Martini 便寫了一支 Python 程式碼給使用者備份系統中的模型。
匯出範例
phi3:mini
python export_ollama_model.py phi3 mini --output phi3_mini.zip
注意
將 phi3_mini.zip 解壓縮
用 tar.gz 重新壓縮
匯入模型
將 phi3_mini.tar.gz 複製到 .ollama 同階層資料夾。
/home/[USER]/phi3_mini.tar.gz
/home/[USER]/.ollama
執行指令:
tar -xf phi3_mini.tar.gz
備份 Ollama 模型的 Python 程式碼
https://mitjamartini.com/posts/export-models-from-ollama
https://web.archive.org/save/https://mitjamartini.com/posts/export-models-from-ollama/
https://archive.is/wip/DiGMr
import os
import json
import zipfile
import argparse
from pathlib import Path
def get_model_manifest_path(registry, repository, model_name, model_tag):
return Path(f".ollama/models/manifests/{registry}/{repository}/{model_name}/{model_tag}")
def get_blob_file_path(digest):
return Path(f".ollama/models/blobs/sha256-{digest.split(':')[1]}")
def read_manifest(ollamamodels, manifest_path):
with open(Path.joinpath(ollamamodels, manifest_path), 'r') as file:
return json.load(file)
def create_zip(ollamamodels, registry, repository, model_name, model_tag, output_zip):
manifest_path = get_model_manifest_path(registry, repository, model_name, model_tag)
manifest = read_manifest(ollamamodels, manifest_path)
with zipfile.ZipFile(output_zip, 'w') as zipf:
# Add manifest file
zipf.write(Path.joinpath(ollamamodels, manifest_path), arcname=manifest_path.relative_to('.'))
# Add blobs
for layer in manifest['layers']:
blob_path = get_blob_file_path(layer['digest'])
zipf.write(Path.joinpath(ollamamodels, blob_path), arcname=blob_path.relative_to('.'))
# Add config blob
config_blob_path = get_blob_file_path(manifest['config']['digest'])
zipf.write(Path.joinpath(ollamamodels, config_blob_path), arcname=config_blob_path.relative_to('.'))
print(f"Model '{repository}{model_name}:{model_tag}' exported successfully to '{output_zip}'")
print(f"You can import it to another Ollama instance with 'tar -xf <modelname>_<tag>_export.zip'")
def main():
homedir = Path.home()
parser = argparse.ArgumentParser(description='Export Ollama model to a zip file.')
parser.add_argument('model_name', type=str, help='Name of the model (e.g., gemma)')
parser.add_argument('model_tag', type=str, help='Tag of the model (e.g., 2b)')
parser.add_argument('--ollamamodels', type=str, default=homedir, help='The folder for OLLAMA_MODELS (default: homedir)')
parser.add_argument('--registry', type=str, default="registry.ollama.ai", help="The Ollama model registry.")
parser.add_argument('--repository', type=str, default="library", help="name of the repository, (eg. jina)")
parser.add_argument('--output', type=str, default='model_export.zip', help='Output zip file name')
args = parser.parse_args()
create_zip(args.ollamamodels, args.registry, args.repository, args.model_name, args.model_tag, args.output)
if __name__ == "__main__":
main()
其他
- Ollama + AnythingLLM 與 RAG (Retrieval-Augmented Generation) 成果沒那麼好,別被氾濫成災的 AI 仔騙了,基本上都是為了賺流量。
- llava 可用來執行影像分析,但不適合進行 OCR。
- 建議針對不同需求選用合適的模型。
- llama3-taide-lx-8b-chat-alpha1 台灣專用
- codellama 寫程式
- llama2-uncensored 嘿嘿嘿版的 Llama
- FLUX.1 (Stable Diffusion) 文生圖
- whisper 影音語言辨識
- Krita + Stable Diffusion Plugin-in
留言
張貼留言
本站留言採審核發佈, 請耐心等候
圖片消失? 請關閉瀏覽器的擋廣告工具