筆記本也能部署本地AI模型進行聊天啦!
博主是AI新手,如有不對還請評論區(qū)指教~
這里介紹mac的部署方式,win也可以實現。
本案例使用到:ollama + nextjs + langchain.js + milvus 來實現知識庫問答和聊天。
ollama:本地運行模型服務
nextjs:前端框架項目
langchain.js:調用模型服務并對話
milvus:向量數據庫
開源代碼:GitHub - huangj17/gemma-nextjs: 使用nextjs本地化部署AI大模型gemma
1、下載 ollama?在本地運行
- 安裝教程:22K star的超強工具:Ollama,一條命令在本地跑 Llama2 - 知乎
- 官方文檔:gemma?安裝后使用?ollama run gemma:2b 命令把gemma:2b模型拉取到本地運行
2、創(chuàng)建一個nextjs項目
- 安裝教程:Getting Started: Installation | Next.js?(前端小伙伴就很清楚啦~)
- 創(chuàng)建后安裝langchainjs依賴包,yarn add?langchain?@langchain/community??Installation | ????? Langchain
- 安裝完成yarn dev跑起來
- 創(chuàng)建一個ollama實例:
import { ChatOllama } from "@langchain/community/chat_models/ollama"; const chatModel = new ChatOllama({ baseUrl: "http://localhost:11434", // Default value model: "gemma:2b", });
- 發(fā)起對話:
await chatModel.invoke("what is LangSmith?");
-
你就會得到一個結果:
文章來源:http://www.zghlxwxcb.cn/news/detail-850263.html
3、安裝Milvus向量數據庫
- 安裝教程:Install Milvus Standalone with Docker Milvus documentation?從官網下載安裝。
- 安裝完成后可以使用官方桌面化工具 Attu 來進行連接查看向量數據。
- 輸入默認Milvus服務地址進行連接
- 連接后可以查看集合也可以進行向量查詢
4、使用langchainjs連接向量庫
- cd到next項目安裝milvus依賴包(注意milvus包僅限node環(huán)境運行)
yarn add @zilliz/milvus2-sdk-node
- 在next項目中 src/pages/api 新建milvus.ts文件
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama"; import { Milvus } from "@langchain/community/vectorstores/milvus"; import type { NextApiRequest, NextApiResponse } from "next"; type ResponseData = { result: any; }; export default async function handler( req: NextApiRequest, res: NextApiResponse<ResponseData> ) { const embeddings = new OllamaEmbeddings({ model: "gemma:2b", baseUrl: "http://localhost:11434", }); const vectorStore = await Milvus.fromTexts( [ "Tortoise: Labyrinth? Labyrinth? Could it Are we in the notorious Little\ Harmonic Labyrinth of the dreaded Majotaur?", "Achilles: Yiikes! What is that?", "Tortoise: They say-although I person never believed it myself-that an I\ Majotaur has created a tiny labyrinth sits in a pit in the middle of\ it, waiting innocent victims to get lost in its fears complexity.\ Then, when they wander and dazed into the center, he laughs and\ laughs at them-so hard, that he laughs them to death!", "Achilles: Oh, no!", "Tortoise: But it's only a myth. Courage, Achilles.", ], [{ id: 2 }, { id: 1 }, { id: 3 }, { id: 4 }, { id: 5 }], embeddings, { collectionName: "goldel_escher_bach", clientConfig: { address: "localhost:19530", token: "root:Milvus", ssl: false, }, } ); const response = await vectorStore.similaritySearch("scared", 2); res.status(200).json({ result: response }); }
注意:OllamaEmbeddings第一個參數填寫本地運行的ollama環(huán)境配置。clientConfig為milvus配置文章來源地址http://www.zghlxwxcb.cn/news/detail-850263.html
- 返回前端頁面index.tsx中使用axios或者fetch調用上面的接口 /api/milvus GET 就能看到返回的查詢向量結果了
5、用向量結果配合ollama運行的gemma模型進行提問
- 回到前端頁面,創(chuàng)建ollama聊天模型,創(chuàng)建提示詞結合上下文來進行對話
const chatModel = new ChatOllama({ baseUrl: "http://localhost:11434", model: "gemma:2b", }); const prompt = ChatPromptTemplate.fromMessages([ ["user", "{input}"], [ "system", `您是一位經驗豐富的文檔編寫專家。使用所提供的上下文:\n\n{context},盡最大努力回答用戶的問題。生成給定問題的簡明答案。不要發(fā)重復文字!`, ], ]); const documentChain = await createStuffDocumentsChain({ llm: chatModel, prompt: prompt, }); const invoke = await documentChain.invoke({ input: 'Who is Tortoise talking to?', context: result.map((item: any) => new Document({ ...item })), // result接口返回的向量結果 });
- 搞定!打印invoke就可以看到最終結果啦!
到了這里,關于使用nextjs本地化部署AI大模型gemma的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!