欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > 利用Llama2 7b自己实现一套离线AI

利用Llama2 7b自己实现一套离线AI

2024/10/24 3:26:00 来源:https://blog.csdn.net/FENGQIYUNRAN/article/details/141107808  浏览:    关键词:利用Llama2 7b自己实现一套离线AI

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家, 可以当故事来看,轻松学习。

离了 ChatGPT 本人简直寸步难行,今天 ChatGPT 大面积宕机,服务直到文章写作(下午5点)时尚未恢复,抓耳挠腮了一下午。已经习惯了用 AI 的我们该怎么办呢?

答案就是——自己在本地运行一个大模型,只要电脑不关机,服务就一直在!

那么怎么在自己电脑上轻松方便地运行大模型呢?一般来说,运行这些模型的 Python / PyTorch 往往包含有 3GB 以上的相互依赖的包。即使设法安装了这些包,也总会与你的 GPU 或其他硬件加速器不兼容,导致非常糟糕的性能。

其实需要这么复杂的设置!我们可以使用 Rust 和 WasmEdge[1],在本地创建和部署非常快速和轻量级的 LLM 推理应用。安装文件也非常小,是一个只有几 MB 的简单二进制可执行文件。更赞的是,这个负责推理的应用程序从 Rust 编译成Wasm 后,完全可以跨广泛的 CPU、GPU 和操作系统移植。最赞的是,它完全没有 Python 依赖。让我们开始吧!

这个教程,我们将涵盖以下内容:

在日常使用的 macOS 或几乎任何平台上运行 LLM,包括 Llama2 7b 和各种其他 LLM;
在几分钟内为你的大模型构建一个轻量级的 AI Agent ;
在 LLM 之外的 AI 上使用 WasmEdge。


第 1 部分:运行 LLM 和 AI 模型的超简单方法


1. 安装具有 LLM 支持的 WasmEdge

可以从一个命令行开始,安装完整的 LLM 支持的 WasmEdge runtime。

curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- --plugin wasi_nn-ggml

或者可以按照这里[2]的安装指南手动下载和复制 WasmEdge 安装文件。

2. 下载 Wasm 格式的 LLM 推理应用

接下来,获取超小的 2MB 跨平台二进制文件 —— llama-chat.wasm。这是效率的证明,无需其他依赖,并能在不同环境中无缝操作。这个小的 Wasm 文件是从 Rust 编译的。想要构建自己的 AI 推理 应用,请查看 llama-utils[3] Repo。

curl -LO https://github.com/second-state/llama-utils/raw/main/chat/llama-chat.wasm

3. 下载 Llama2 7b chat 模型

curl -LO https://huggingface.co/wasmedge/llama2/blob/main/llama-2-7b-chat-q5_k_m.gguf

WasmEdge 还支持 Llama2、CodeLlama、Llemma、Codeshell、Mistrial、MistrialLite、TinyLlama、Baichuan、BELLE、Alpaca、Vicuna、OpenChat、Starcoder、OpenBuddy 等更多!

4. 在 CLI 上与 Llama2 7b chat

现在你已经准备好了一切,可以开始使用命令行与 Llama2 7b chat 模型聊天。

wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-wasm-q5_k_m.gguf 

llama-chat.wasm

现在,可以和 Llama2 7b chat 对话啦。让 Llama2 7b chat 模型帮我写代码,解决一些看起来比较枯燥的工作!

[USER]: I want to combine all the txt files in the local directory. write a python program for me.[ASSISTANT]:Of course! Here is a simple Python program that can combine all the text files in the current directory:import osfor filename in os.listdir(): if filename.endswith('.txt'): with open(filename, 'a') as f: f.write('')This program uses the `os` module to list all the files in the current directory, and then checks if each file ends with the `.txt` extension using the `endswith()` method. If a `.txt` file is found, it is opened in append mode (`'a'`) using the `open()` function, and the contents are written to the file using the `write()` method.Please let me know if you have any questions or if you need further assistance![USER]: How can I run this program? I'm not familar with python.[ASSISTANT]:Of course! Here are the steps to run the Python program:1. Open a terminal or command prompt on your computer.2. Navigate to the directory where the text files are located using the `cd` command. For example, if the text files are located in the `Documents` directory, you would type `cd Documents`.3. Type `python combine_txt_files.py` and press Enter. This will run the `combine_txt_files.py` program.4. The program will combine all the text files in the current directory and write the combined contents to a new file called `combined_txt_files.txt`.If you are using a Python IDE (Integrated Development Environment) such as PyCharm or Visual Studio Code, you can open the `combine_txt_files.py` file and run it by clicking the "Run" button or pressing `F5`.I hope this helps! Let me know if you have any questions.

就这样!你还可以使用同样的 llama-chat.wasm 文件来运行其他 LLM,如 OpenChat、CodeLlama、Mistral 等。

<iframe src="//player.bilibili.com/player.html?bvid=BV1zQ4y1n7mZ&page=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>

资源:

通过以下额外资源,探索可以在 WasmEdge 上运行的各种 LLM:

运行其他 LLM:其他可用 LLM 的综合视频指南[4]。
在 containerd 和 Docker 中运行 LLM:了解如何在 Docker + Wasm 中运行和管理 LLM 应用[5]。


第 2 部分:构建超轻量级 AI Agent


1. 创建一个与 OpenAI 兼容的 API 服务

当用你的领域知识微调了一个模型或你想要运行一个私有模型时,仅使用 CLI 运行模型是不够的。我们通常需要依靠这个大模型构建 Agent,让 LLM 更好地帮我工作。接下来,让我们为大模型设置兼容 OpenAI 的 API 服务,然后我们可以将这个私有模型集成到其他 SaaS 工作流程中。这样即便 ChatGPT 宕机,你的 Agent 依然可以正常为你工作!

这里假设你已经安装了带有 ggml 插件的 WasmEdge 并下载了你需要的模型。


首先,通过终端下载构建 API 服务器的 Wasm 文件。

curl -LO https://github.com/second-state/llama-utils/raw/main/api-server/llama-api-server.wasm

其次,用基于 WasmEdge 的API 服务运行模型。

wasmedge --dir .:. --nn-preload default:GGML:AUTO:llama-2-7b-chat-wasm-q5_k_m.gguf llama-api-server.wasm -p llama-2-chat -s 0.0.0.0:8080

在看到连接建立后,可以使用以下命令行来尝试你的模型。

curl -X POST http://localhost:8080/v1/chat/completions \-H 'accept: application/json' \-H 'Content-Type: application/json' \-d '{"messages":[{"role":"system", "content": "You are a helpful assistant. Answer each question in one sentence."}, {"role":"user", "content": "**What is Wasm?**"}], "model":"llama-2-chat"}'

2. 部署连接 API 服务的聊天机器人 UI

接下来,让我们将你的 API 服务链接到由 Second State 提供的 Chatbot-UI[6],为用户提供丝滑的对话体验。

需要做的就是将你的 Chat API 复制并粘贴到左下角侧边栏的 Chat API URL 中。请注意,你的 API URL 必须是一个 HTTPs 代理。nike可以使用 ngrok 来实现 HTTPs 代理,或者绑定你的域名。

接下来,就可以通过 Chatbot-UI 与模型聊天啦。 

3. 围绕模型构建 RAG 机器人和 Agent

如果我们将模型的能力应用到现实世界的工作流程中,LLM Agent 比 Web 聊天机器人更有帮助。Flows.network[7] 是一个 Severless 平台,用 Rust 和 Wasm 构建 LLM Agent 和机器人。flows.network 平台允许开发者将像 ChatGPT 和 Claude 的大模型连接到不同的 SaaS 提供商。由于模型已经具备与 OpenAI 兼容的 API,我们可以像在 OpenAI 上做的那样,轻松地将模型与 Telegram、Slack 和 Discord 等其他 SaaS 集成。查看如何构建基于 RAG 的 LLM Agent[8]!

或者,可以使用 LangChain[9] 和 LlamaIndex[10] 来构建 RAG(检索增强生成)应用程序。

来试一试使用这种方式构建的 RAG 机器人,帮助你学习 Rust 语言 👉https://flows.network/learn-rust

第 3 部分:不止于大语言模型


WasmEdge 的 AI 不仅限于 LLM 任务;它还扩展到视觉和音频领域。除了 ggml 后端,WasmEdge 运行时还支持 PyTorch、TensorFlow 和 OpenVINO AI 框架。为了使 AI 推理更加流畅,WasmEdge 还支持使用流行的 OpenCV 和 FFmpeg 库进行图像处理。

了解如何借助 mediapipe-rs 开发图像和音频 AI 应用[11],并上手尝试其他 WASI-NN 示例。

Mediapipe-rs:一个基于 Rust 的 Mediapipe 库,
源代码:Mediapipe-rs GitHub[12]
教程:使用 Rust 进行 Mediapipe 推理[13]
更多 WASI NN 示例:探索更多使用 WasmEdge 和 Rust 的 AI 推理示例[14]。
结论
在有了强大的 ChatGPT 后,我们是不是还需要一个私有的模型?答案是肯定的!WasmEdge 为运行最先进的 LLM,包括你自己微调的 Llama 2 模型,提供了一个轻量级、安全、可靠和高效的环境。无论你是希望部署 AI 应用的开发者,还是寻求将智能 Agent 集成到你的服务中,使用 Rust + WasmEdge 都将为你提供价值。

加入 WasmEdge discord[15] 的讨论,来 GitHub 为 WasmEdge 做贡献吧。

参考资料

[1] WasmEdge: https://github.com/WasmEdge/WasmEdge

[2] https://wasmedge.org/docs/start/install

[3] llama-utils: https://github.com/second-state/llama-utils/tree/main/chat

[4] 其他可用 LLM 的综合视频指南: https://youtu.be/lB2A7S1vexM

[5] 了解如何在 Docker + Wasm 中运行和管理 LLM 应用: https://youtu.be/ZGM6irhi1jA

[6] 由 Second State 提供的 Chatbot-UI: https://second-state.github.io/chatbot-ui/

[7] Flows.network: https://flows.network/

[8] 构建基于 RAG 的 LLM Agent: https://docs.flows.network/docs/category/rag-based-llm-agent

[9] LangChain: https://www.langchain.com/

[10] LlamaIndex: https://www.llamaindex.ai/

[11] mediapipe-rs 开发图像和音频 AI 应用: https://wasmedge.org/docs/develop/rust/wasinn/mediapipe

[12] Mediapipe-rs GitHub: https://github.com/WasmEdge/mediapipe-rs

[13] 使用 Rust 进行 Mediapipe 推理: https://wasmedge.org/docs/develop/rust/wasinn/mediapipe

[14] 探索更多使用 WasmEdge 和 Rust 的 AI 推理示例: https://github.com/second-state/WasmEdge-WASINN-examples

[15] WasmEdge discord: https://discord.com/invite/U4B5sFTkFc 作者:Second_State https://www.bilibili.com/read/cv27600812/?jump_opus=1 出处:bilibili

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com