ollama 初體驗
參考
https://github.com/ollama/ollama
https://zhuanlan.zhihu.com/p/689555159
https://zhuanlan.zhihu.com/p/687099148
https://zhuanlan.zhihu.com/p/685166253
https://babyno.top/posts/2024/03/run-a-large-language-model-locally-2/ 提供RAG示例
https://sspai.com/post/85193#!
https://github.com/sugarforever/chat-ollama 一個支持RAG的webUI
Windows 設置環境變量
- OLLAMA_HOST, 取值為 0.0.0.0
- OLLAMA_MODELS, 取值為 D:\my_workspace\OLLAMA_MODELS\
下載并安裝 ollama windows版本
ollama 安裝后會提供一個命令行工具 ollama.exe, 可以用來下載模型, 也可以啟動本地rest服務.
ollama 會自動檢測GPU, 優先檢查CUDA, 然后是AMD ROMc, 如果沒有合適的GPU則使用CPU進行推理.
ollama pull qwen:0.5b #文件: 395MB, 千問小模型
ollama serve # 啟動 ollama 本地訪問, 端口為 11434
下載模型
即使是同一個family的模型, 也有不同參數量和不同的量化策略, 需要按照電腦的硬件來選擇合適的. 16 位浮點數精度(FP16)的模型, 推理所需顯存約為模型參數量的2倍, 4比特量化模型: 顯存(單位GB)為模型參數量的1/2(單位B).
ollama pull phi3 #微軟的phi3模型, 模型很小超過了很多規模更大的模型, 訓練的數據集是教科書級別的語料.
ollama pull qwen:0.5b #文件: 395MB, 千問小模型
ollama pull tinyllama #文件: 637MB, 一個比較有名的mini版llama模型
ollama pull qwen:1.8b #文件: 637MB
ollama pull nomic-embed-text #文件: 275MB
ollama pull qwen:7b #文件: 1.1GB
ollama pull mistral # mistral 模型
ollama pull llama2 #llama2模型
ollama pull llama2-chinese #中文微調的llama2模型
ollama pull unichat-llama3-chinese-8b # 中文llama3, https://ollama.com/ollam/unichat-llama3-chinese-8b
ollama API 示例
下面是 VS code 的 RestClient寫法, 不知為何 RestClient 無法使用 localhost 和 127.0.0.1 訪問, 如果是postman可能需要通過localhost來訪問.
可以訪問:
GET http://0.0.0.0:11434/ HTTP/1.1
不可以訪問:
GET http://localhost:11434/ HTTP/1.1
GET http://127.0.0.1:11434/ HTTP/1.1
POST http://0.0.0.0:11434/api/embeddings HTTP/1.1
content-type: application/json
{
"model": "qwen:0.5b",
"prompt": "Here is an article about llamas..."
}
POST http://0.0.0.0:11434/api/embeddings HTTP/1.1
content-type: application/json
{
"model": "nomic-embed-text",
"prompt": "Here is an article about llamas..."
}
POST http://0.0.0.0:11434/api/show HTTP/1.1
content-type: application/json
{
"name": "qwen:0.5b"
}
POST http://0.0.0.0:11434/api/generate HTTP/1.1
content-type: application/json
{
"model": "qwen:0.5b",
"prompt": "Here is an article about llamas...",
"context": [
],
"stream": false,
"format":"json",
"options": {
"seed": 123,
"temperature": 0
}
}
POST http://0.0.0.0:11434/api/chat HTTP/1.1
content-type: application/json
{
"model": "qwen:0.5b",
"stream": false,
"format":"json",
"messages": [
{
"role": "user",
"content": "why is the sky blue?"
}
]
}
## Send a chat message with a conversation history, 同時增加system role設定系統提示詞.
POST http://0.0.0.0:11434/api/chat HTTP/1.1
content-type: application/json
{
"model": "qwen:1.8b",
"stream": false,
"format":"json",
"messages": [
{
"role": "system",
"content": "以海盜的口吻簡單作答, 以中文回復"
},
{
"role": "user",
"content": "why is the sky blue?"
},
{
"role": "assistant",
"content": "due to rayleigh scattering."
},
{
"role": "user",
"content": "請解釋一下光的折射?"
}
]
}

浙公網安備 33010602011771號