LangChain 开发环境准备与 AI 大模型私有部署指南
AI 大模型私有部署环境搭建涉及基础环境配置、模型文件下载、依赖库安装及接口测试。以 Baichuan2-13B-Chat-4bits 为例,演示在 Linux 环境下通过 pip 安装依赖,利用 aria2 从 HuggingFace 获取模型权重,并通过 Python 脚本验证模型对话功能。此外,还展示了如何使用 FastAPI 封装模型服务,提供 HTTP 接口供外部调用,为后续接入 LangChain 框架奠定基础。

AI 大模型私有部署环境搭建涉及基础环境配置、模型文件下载、依赖库安装及接口测试。以 Baichuan2-13B-Chat-4bits 为例,演示在 Linux 环境下通过 pip 安装依赖,利用 aria2 从 HuggingFace 获取模型权重,并通过 Python 脚本验证模型对话功能。此外,还展示了如何使用 FastAPI 封装模型服务,提供 HTTP 接口供外部调用,为后续接入 LangChain 框架奠定基础。

当下在 AI 应用开发领域,LangChain 框架可以说是唯一选择。然而,上手学习的小伙伴们大多被拦在了第一步,没有可供使用的 AI 大模型底座,没有可供实验的环境进行实操。而各大厂商比如 OpenAI 提供的 API 需要付费不说,从注册、开通到使用都有诸多的不便和限制。
因此,如何在本地或者私有网络中部署和使用 AI 大模型,成为了一个亟待解决的挑战。本文将以 Baichuan2-13B-Chat-4bits 为例,介绍如何进行 AI 大模型私有部署,用于 LangChain 开发实验实操。
环境配置与安装
1. python 3.8 及以上版本,官网安装即可
地址:https://www.python.org/
2. pytorch 1.12 及以上版本,推荐 2.0 及以上版本,官网安装即可
地址:https://pytorch.org/get-started/locally/
3. 建议使用 CUDA 11.4 及以上,根据显卡匹配英伟达推荐驱动进行安装
地址:https://developer.nvidia.com/cuda-11-4-0-download-archive
4. 建议使用 linux 环境安装,本文示例基于 linux 环境演示
5. 假定以下动作执行的当前目录为/aidev
要在 LangChain 开发环境中部署 AI 大模型,首先需要下载模型文件和配置文件。Baichuan2-13B-Chat-4bits 模型已经上传到了 huggingface 这个知名的 AI 模型库中,我们可以通过以下步骤来下载它。
https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits
在模型页面中,点击右上角的 Download 按钮,选择 Download files 选项。
Linux 系统可以直接通过以下脚本下载
apt-get -y install -qq aria2
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/config.json -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o config.json
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/configuration_baichuan.py -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o configuration_baichuan.py
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/generation_config.json -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o generation_config.json
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/generation_utils.py -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o generation_utils.py
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/handler.py -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o handler.py
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/modeling_baichuan.py -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o modeling_baichuan.py
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/resolve/main/pytorch_model.bin -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o pytorch_model.bin
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/quantizer.py -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o quantizer.py
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/requirements.txt -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o requirements.txt
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/special_tokens_map.json -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o special_tokens_map.json
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/tokenization_baichuan.py -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o tokenization_baichuan.py
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/resolve/main/tokenizer.model -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o tokenizer.model
aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits/raw/main/tokenizer_config.json -d /content/baichuan-inc/Baichuan2-13B-Chat-4bits -o tokenizer_config.json
下载好模型文件和配置文件后,主目录为/aidev,文件目录为baichuan-inc/Baichuan2-13B-Chat-4bits(备注后续启动模型会默认加载此目录,请按此命名),我们还需要安装一些基础的依赖库,以便于在 LangChain 开发环境中运行模型。
# pip 安装模型运行的依赖,requirment.txt 文件
pip install -r baichuan-inc/Baichuan2-13B-Chat-4bits/requirements.txt
这样就完成了基础依赖库的安装。
安装好基础依赖库后,我们可以先测试一下模型是否能够正常运行。
app.py 的文件,并输入以下内容:import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig
tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Chat-4bits", use_fast=False, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Chat-4bits", device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan2-13B-Chat-4bits")
messages = []
messages.append({"role": "user", "content": "解释一下'温故而知新'"})
response = model.chat(tokenizer, messages)
print(response)
python app.py
测试好模型后,我们已经掌握了与大模型对话的入口,可以进一步基于 FastAPI 创建一个模型访问接口,这样就可以让外部的应用或者用户通过网络来调用我们部署在 LangChain 开发环境中的 AI 大模型。
pip install uvicorn
api.py 的文件,并输入以下内容:from fastapi import FastAPI
from fastapi import HTTPException
from pydantic import BaseModel
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig
tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Chat-4bits", use_fast=False, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Chat-4bits", device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan2-13B-Chat-4bits")
app = FastAPI()
# This defines the data json format expected for the endpoint, change as needed
class RequestItem(BaseModel):
message: str
@app.post("/generate/")
async def generate_text(request_item: RequestItem):
try:
# 在这里处理接收到的 JSON 请求
reqStr = request_item.message
messages = []
messages.append({"role": "user", "content": reqStr})
response = model.chat(tokenizer, messages)
return {"generated_text": response}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
uvicorn app:app --host 0.0.0.0 --port 8000 > server.log 2>&1 &

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online