部署本地的大語言模型,只需幾分鐘!
2023 年是 AI 高速發(fā)展的一年,除了功能強(qiáng)大的商用大語言模型之外,也出現(xiàn)了很多不錯(cuò)的開源大語言模型。比如,Llama2、Codellama、Mistral 和 Vicuna 等。雖然商用的大語言模型 ChatGPT、Bard 和 Claude 功能很強(qiáng)大,但需要支付一定的費(fèi)用,同時(shí)也存在一定的安全問題。對(duì)于某些場(chǎng)景,如果你要確保數(shù)據(jù)安全,那么你可以考慮部署本地大語言模型。
本文我將介紹如何利用 ollama[1] 這個(gè)開源項(xiàng)目,運(yùn)行 Llama2 和其它的大語言模型。
安裝 ollama
目前 ollama 只支持 macOS 和 Linux 系統(tǒng),Windows 平臺(tái)正在開發(fā)中。我們可以訪問 Download Ollama[2] 這個(gè)鏈接下載指定平臺(tái)的 ollama。
我下載的是 macOS 版本,成功下載后解壓 「Ollama-darwin.zip」 文件,雙擊 Ollama 可執(zhí)行文件,即可以開始安裝。
安裝時(shí)會(huì)提示是否把 Ollama 應(yīng)用移動(dòng)到系統(tǒng)的 Applications 目錄,這里我選擇 「Move to Applications」:
接著,按照軟件安裝指南一步步操作即可。
如果你想運(yùn)行 llama2,只需在終端中運(yùn)行 ollama run llama2 命令。運(yùn)行該命令后,會(huì)自動(dòng)下載 llama2 [3] 模型:
除了 llama2 模型之外,Ollama 還支持很多模型,完整的模型可以訪問 模型列表[4] 查看。
?
注意:你應(yīng)該至少有 8 GB 的 RAM 來運(yùn)行 3B 模型,16 GB 的 RAM 來運(yùn)行 7B 模型,32 GB 的 RAM 來運(yùn)行 13B 模型。
?
成功下載完模型之后,你就可以跟 llama2 模型交互了:
ollama CLI
利用 ollama CLI,我們可以方便地對(duì)模型執(zhí)行各種操作。比如,創(chuàng)建模型、拉取模型、移除模型或復(fù)制模型等。
創(chuàng)建模型
ollama create example -f Modelfile
拉取模型
ollama pull llama2
?
此命令還可用于更新本地模型。只會(huì)拉取差異的部分。
?
移除模型
ollama rm llama2
復(fù)制模型
ollama cp llama2 my-llama2
除了上述的命令之外,ollama CLI 還提供了其它的命令,通過 ollama --help
就可以查看完整的命令:
(base) ? ~ ollama --help
Large language model runner
Usage:
ollama [command]
Available Commands:
serve Start ollama
create Create a model from a Modelfile
show Show information for a model
run Run a model
pull Pull a model from a registry
push Push a model to a registry
list List models
cp Copy a model
rm Remove a model
help Help about any command
Flags:
-h, --help help for ollama
-v, --version version for ollama
啟動(dòng)本地服務(wù)器
如果你不想在終端中與大語言模型交互,那么你可以通過 ollama serve 命令啟動(dòng)一個(gè)本地的服務(wù)器。成功運(yùn)行該命令之后,你就可以通過 REST API 的形式跟本地的大語言模型交互:
curl http://localhost:11434/api/generate -d '{
"model": "llama2",
"prompt":"Why is the sky blue?"
}'
在實(shí)際項(xiàng)目中,我們可以利用 langchainjs[5] 封裝的 ChatOllama[6] 對(duì)象來高效地與 Ollama 做交互。
ChatOllama
Ollama 還支持 JSON 模式,可以強(qiáng)制讓大語言模型輸出合法的 JSON。下面我們來介紹一下如何利用 langchainjs) 封裝的 「ChatOllama」 對(duì)象實(shí)現(xiàn)文本翻譯的功能。
初始化 ChatOllama 項(xiàng)目。
mkdir ChatOllama
npm init -y
安裝 langchainjs。
npm install -S langchain # or
yarn add langchain # or
pnpm add langchainjs
創(chuàng)建 index.mjs 文件。
import { ChatOllama } from "langchain/chat_models/ollama";
import { ChatPromptTemplate } from "langchain/prompts";
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
`You are an expert translator. Format all responses as JSON objects with two keys: "original" and "translated".`,
],
["human", `Translate "{input}" into {language}.`],
]);
const model = new ChatOllama({
baseUrl: "http://localhost:11434", // Default value
model: "llama2", // Default value
format: "json",
});
const chain = prompt.pipe(model);
const result = await chain.invoke({
input: "I love programming",
language: "Chinese",
});
console.log(result);
之后,在項(xiàng)目的根目錄下,打開終端并執(zhí)行 node index.mjs 命令。當(dāng)成功運(yùn)行上述命令后,終端會(huì)輸出以下結(jié)果:
除了實(shí)現(xiàn)文本翻譯的功能之外,你還可以實(shí)現(xiàn)很多不同功能。比如,開發(fā) RAG(Retrieval Augmented Generation)應(yīng)用來實(shí)現(xiàn)高效地信息檢索。感興趣的小伙伴,可以自行了解 RAG 相關(guān)內(nèi)容。
總結(jié)
本文介紹了如何利用 Ollama 在本地快速部署開源的大語言模型,并介紹了基于 langchainjs 封裝的 ChatOllama 對(duì)象,實(shí)現(xiàn)文本翻譯的功能。其實(shí),Ollama 還支持我們自定義模型,它允許我們導(dǎo)入 GGUF 格式的模型。如果你對(duì)自定義模型感興趣,可以閱讀 Customize your own model[7] 這一部分的內(nèi)容。
Reference
[1]ollama:https://github.com/jmorganca/ollama。
[2]Download Ollama:https://ollama.ai/download。
[3]llama2 :https://ollama.ai/library/llama2。
[4]模型列表:https://ollama.ai/library。
[5]langchainjs:https://github.com/langchain-ai/langchainjs。
[6]ChatOllama:https://js.langchain.com/docs/integrations/chat/ollama。
[7]Customize your own model:https://github.com/jmorganca/ollama?tab=readme-ov-file#customize-your-own-model。