LangChain开发环境准备-AI大模型私有部署的技术指南


今天开始小智将开启系列AI应用开发课程,主要基于LangChain框架基于实战项目手把手教大家如何将AI这一新时代的基础设施应用到自己开发应用中来。欢迎大家持续关注

当下在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模型库中,我们可以通过以下步骤来下载它:

从huggingface上下载模型及配置文件

  1. 模型下载链接如下
https://huggingface.co/baichuan-inc/Baichuan2-13B-Chat-4bits
  1. 在模型页面中,点击右上角的Download按钮,选择Download files选项。

3. 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

这样就完成了基础依赖库的安装。

模型测试

安装好基础依赖库后,我们可以先测试一下模型是否能够正常运行。我们可以通过以下步骤来测试:

基于官方示例,创建模型对象,并调用对话方法

  1. 在当前目录即/aidev中创建一个名为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)
  1. 启用测试
python app.py

运行效果如下图所示

基于FastAPI创建模型访问接口示例

测试好模型后,我们已经掌握了与大模型对话的入口,可以进一步基于FastAPI创建一个模型访问接口,这样就可以让外部的应用或者用户通过网络来调用我们部署在LangChain开发环境中的AI大模型。我们可以通过以下步骤来创建:

  1. 安装uvicorn
pip install uvicorn
  1. 在当前目录即/aidev中创建一个名为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))
  1. 启动接口
uvicorn app:app --host 0.0.0.0 --port 8000 > server.log 2>&1 &
  1. 使用测试如下图所示

总结

本文介绍了如何在LangChain开发环境中准备AI大模型私有部署的技术指南,以Baichuan2-13B-Chat-4bits为例,分别介绍了模型下载、基础依赖安装、模型测试和基于FastAPI创建模型访问接口的步骤和代码。当然只是简单的提供对话接口的话,还无法将大模型接入langchain的开发流程中。

点击链接获取一份GPT至强笔记:「链接」

下一节课我将讲解如何基于Baichuan2-13B-Chat-4bits,提供符合langchain接入标准的openai-api接口,请大家持续关注。

展开阅读全文

页面更新:2024-02-23

标签:模型   环境   示例   步骤   接口   本文   文件   测试   基础   目录   指南   技术

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top