部署本地的大語言模型,只需幾分鐘!
2023 年是 AI 高速發展的一年,除了功能強大的商用大語言模型之外,也出現了很多不錯的開源大語言模型。比如,Llama2、Codellama、Mistral 和 Vicuna 等。雖然商用的大語言模型 ChatGPT、Bard 和 Claude 功能很強大,但需要支付一定的費用,同時也存在一定的安全問題。對于某些場景,如果你要確保數據安全,那么你可以考慮部署本地大語言模型。
本文我將介紹如何利用 ollama[1] 這個開源項目,運行 Llama2 和其它的大語言模型。
安裝 ollama
目前 ollama 只支持 macOS 和 Linux 系統,Windows 平臺正在開發中。我們可以訪問 Download Ollama[2] 這個鏈接下載指定平臺的 ollama。
我下載的是 macOS 版本,成功下載后解壓 「Ollama-darwin.zip」 文件,雙擊 Ollama 可執行文件,即可以開始安裝。
安裝時會提示是否把 Ollama 應用移動到系統的 Applications 目錄,這里我選擇 「Move to Applications」:
接著,按照軟件安裝指南一步步操作即可。
如果你想運行 llama2,只需在終端中運行 ollama run llama2 命令。運行該命令后,會自動下載 llama2 [3] 模型:
除了 llama2 模型之外,Ollama 還支持很多模型,完整的模型可以訪問 模型列表[4] 查看。
?
注意:你應該至少有 8 GB 的 RAM 來運行 3B 模型,16 GB 的 RAM 來運行 7B 模型,32 GB 的 RAM 來運行 13B 模型。
?
成功下載完模型之后,你就可以跟 llama2 模型交互了:
ollama CLI
利用 ollama CLI,我們可以方便地對模型執行各種操作。比如,創建模型、拉取模型、移除模型或復制模型等。
創建模型
ollama create example -f Modelfile
拉取模型
ollama pull llama2
?
此命令還可用于更新本地模型。只會拉取差異的部分。
?
移除模型
ollama rm llama2
復制模型
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
啟動本地服務器
如果你不想在終端中與大語言模型交互,那么你可以通過 ollama serve 命令啟動一個本地的服務器。成功運行該命令之后,你就可以通過 REST API 的形式跟本地的大語言模型交互:
curl http://localhost:11434/api/generate -d '{
"model": "llama2",
"prompt":"Why is the sky blue?"
}'
在實際項目中,我們可以利用 langchainjs[5] 封裝的 ChatOllama[6] 對象來高效地與 Ollama 做交互。
ChatOllama
Ollama 還支持 JSON 模式,可以強制讓大語言模型輸出合法的 JSON。下面我們來介紹一下如何利用 langchainjs) 封裝的 「ChatOllama」 對象實現文本翻譯的功能。
初始化 ChatOllama 項目。
mkdir ChatOllama
npm init -y
安裝 langchainjs。
npm install -S langchain # or
yarn add langchain # or
pnpm add langchainjs
創建 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);
之后,在項目的根目錄下,打開終端并執行 node index.mjs 命令。當成功運行上述命令后,終端會輸出以下結果:
除了實現文本翻譯的功能之外,你還可以實現很多不同功能。比如,開發 RAG(Retrieval Augmented Generation)應用來實現高效地信息檢索。感興趣的小伙伴,可以自行了解 RAG 相關內容。
總結
本文介紹了如何利用 Ollama 在本地快速部署開源的大語言模型,并介紹了基于 langchainjs 封裝的 ChatOllama 對象,實現文本翻譯的功能。其實,Ollama 還支持我們自定義模型,它允許我們導入 GGUF 格式的模型。如果你對自定義模型感興趣,可以閱讀 Customize your own model[7] 這一部分的內容。
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。