私有化部署 Llama3 大模型, 支持 API 訪問(wèn)

視頻
https://www.bilibili.com/video/BV1wD421n75p/
前言
通過(guò) ollama 本地運(yùn)行 Llama3 大模型其實(shí)對(duì)我們開(kāi)發(fā)來(lái)說(shuō)很有意義,你可以私有化放服務(wù)上了。
然后通過(guò) api 訪問(wèn),來(lái)處理我們的業(yè)務(wù),比如翻譯多語(yǔ)言、總結(jié)文章、提取關(guān)鍵字等等。
你也可以安裝 enchanted 客戶端去直接訪問(wèn)這個(gè)服務(wù) api 使用。
參考
https://llama.meta.com/llama3/
https://github.com/ollama/ollama
https://github.com/ollama/ollama/blob/main/docs/api.md
https://github.com/sugarforever/chat-ollama
https://github.com/AugustDev/enchanted
Llama3
https://llama.meta.com/llama3/

https://github.com/meta-llama/llama3/blob/main/MODEL_CARD.md

安全性
https://llama.meta.com/trust-and-safety/


步驟
安裝 ollama

安裝 Llama3 8b 模型
https://ollama.com/library/llama3
模型選擇

安裝命令
$ ollama run llama3
訪問(wèn) api 服務(wù)
https://github.com/ollama/ollama/blob/main/docs/api.md
curl http://localhost:11434/api/generate -d '{
"model":"llama3",
"prompt": "請(qǐng)分別翻譯成中文、韓文、日文 -> Meta Llama 3: The most capable openly available LLM to date",
"stream": false
}'
參數(shù)解釋如下:
model(必需):模型名稱(chēng)。
prompt:用于生成響應(yīng)的提示文本。
images(可選):包含多媒體模型(如llava)的圖像的base64編碼列表。
高級(jí)參數(shù)(可選):
- format:返回響應(yīng)的格式。目前僅支持json格式。
- options:模型文件文檔中列出的其他模型參數(shù),如溫度(temperature)。
- system:系統(tǒng)消息,用于覆蓋模型文件中定義的系統(tǒng)消息。
- template:要使用的提示模板,覆蓋模型文件中定義的模板。
- context:從先前的/generate請(qǐng)求返回的上下文參數(shù),可以用于保持簡(jiǎn)短的對(duì)話記憶。
- stream:如果為false,則響應(yīng)將作為單個(gè)響應(yīng)對(duì)象返回,而不是一系列對(duì)象流。
- raw:如果為true,則不會(huì)對(duì)提示文本應(yīng)用任何格式。如果在請(qǐng)求API時(shí)指定了完整的模板化提示文本,則可以使用raw參數(shù)。
- keep_alive:控制模型在請(qǐng)求后保持加載到內(nèi)存中的時(shí)間(默認(rèn)為5分鐘)。
返回 json 數(shù)據(jù)
{
"model": "llama3",
"created_at": "2024-04-23T08:05:11.020314Z",
"response": "Here are the translations:\n\n**Chinese:** 《Meta Llama 3》:迄今最強(qiáng)大的公開(kāi)可用的LLM\n\n**Korean:** 《Meta Llama 3》:?? ?? ??? ?? ?? ??? LLM\n\n**Japanese:**\n\n《Meta Llama 3》:現(xiàn)在最強(qiáng)の公開(kāi)使用可能なLLM\n\n\n\nNote: (Meta Llama 3) is a literal translation, as there is no direct equivalent for \"Meta\" in Japanese. In Japan, it's common to use the English term \"\" or \"\" when referring to Meta.",
"done": true,
"context": [
...
],
"total_duration": 30786629492,
"load_duration": 3000782,
"prompt_eval_count": 32,
"prompt_eval_duration": 6142245000,
"eval_count": 122,
"eval_duration": 24639975000
}
返回值的解釋如下:
- total_duration:生成響應(yīng)所花費(fèi)的總時(shí)間。
- load_duration:以納秒為單位加載模型所花費(fèi)的時(shí)間。
- prompt_eval_count:提示文本中的標(biāo)記(tokens)數(shù)量。
- prompt_eval_duration:以納秒為單位評(píng)估提示文本所花費(fèi)的時(shí)間。
- eval_count:生成響應(yīng)中的標(biāo)記數(shù)量。
- eval_duration:以納秒為單位生成響應(yīng)所花費(fèi)的時(shí)間。
- context:用于此響應(yīng)中的對(duì)話編碼,可以在下一個(gè)請(qǐng)求中發(fā)送,以保持對(duì)話記憶。
- response:如果響應(yīng)是以流的形式返回的,則為空;如果不是以流的形式返回,則包含完整的響應(yīng)。
要計(jì)算生成響應(yīng)的速度,以標(biāo)記數(shù)每秒(tokens per second,token/s)為單位,可以將 eval_count / eval_duration 進(jìn)行計(jì)算。
ollama 生態(tài)
https://github.com/ollama/ollama
- 客戶端 桌面、Web
- 命令行工具
- 數(shù)據(jù)庫(kù)工具
- 包管理工具
- 類(lèi)庫(kù)
桌面 enchanted 客戶端
https://github.com/AugustDev/enchanted

設(shè)置服務(wù)器地址

提問(wèn)使用

代碼
https://github.com/ollama/ollama
小結(jié)
感謝閱讀本文
如果有什么建議,請(qǐng)?jiān)谠u(píng)論中讓我知道。我很樂(lè)意改進(jìn)。
flutter 學(xué)習(xí)路徑
- Flutter 優(yōu)秀插件推薦 https://flutter.ducafecat.com
- Flutter 基礎(chǔ)篇1 - Dart 語(yǔ)言學(xué)習(xí) https://ducafecat.com/course/dart-learn
- Flutter 基礎(chǔ)篇2 - 快速上手 https://ducafecat.com/course/flutter-quickstart-learn
- Flutter 實(shí)戰(zhàn)1 - Getx Woo 電商APP https://ducafecat.com/course/flutter-woo
- Flutter 實(shí)戰(zhàn)2 - 上架指南 Apple Store、Google Play https://ducafecat.com/course/flutter-upload-apple-google
- Flutter 基礎(chǔ)篇3 - 仿微信朋友圈 https://ducafecat.com/course/flutter-wechat
- Flutter 實(shí)戰(zhàn)3 - 騰訊 tim 即時(shí)通訊開(kāi)發(fā) https://ducafecat.com/course/flutter-tim
? 貓哥
ducafecat.com
end