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

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

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接口,请大家持续关注。

相关实践学习
AnalyticDB PostgreSQL 企业智能数据中台:一站式管理数据服务资产
企业在数据仓库之上可构建丰富的数据服务用以支持数据应用及业务场景;ADB PG推出全新企业智能数据平台,用以帮助用户一站式的管理企业数据服务资产,包括创建, 管理,探索, 监控等; 助力企业在现有平台之上快速构建起数据服务资产体系
目录
相关文章
|
4月前
|
云安全 人工智能 自然语言处理
阿里云x硅基流动:AI安全护栏助力构建可信模型生态
阿里云AI安全护栏:大模型的“智能过滤系统”。
2087 120
|
4月前
|
负载均衡 测试技术 调度
大模型分布式推理:张量并行与流水线并行技术
本文深入探讨大语言模型分布式推理的核心技术——张量并行与流水线并行。通过分析单GPU内存限制下的模型部署挑战,详细解析张量并行的矩阵分片策略、流水线并行的阶段划分机制,以及二者的混合并行架构。文章包含完整的分布式推理框架实现、通信优化策略和性能调优指南,为千亿参数大模型的分布式部署提供全面解决方案。
1099 4
|
4月前
|
存储 人工智能 前端开发
Qoder + ADB Supabase :5分钟GET超火AI手办生图APP
本文介绍如何利用Qoder、阿里云ADB Supabase和通义千问图像编辑模型,快速搭建AI手办生图Flutter应用。无需传统后端,实现从前端生成到数据存储、AI服务集成的全链路敏捷开发,展现Vibe Coding的高效实践。
Qoder + ADB Supabase :5分钟GET超火AI手办生图APP
|
4月前
|
人工智能 搜索推荐 程序员
当AI学会“跨界思考”:多模态模型如何重塑人工智能
当AI学会“跨界思考”:多模态模型如何重塑人工智能
499 120
|
4月前
|
人工智能 机器人 人机交互
当AI学会“看、听、懂”:多模态技术的现在与未来
当AI学会“看、听、懂”:多模态技术的现在与未来
362 117
|
4月前
|
人工智能 文字识别 自然语言处理
从“看见”到“预见”:合合信息“多模态文本智能技术”如何引爆AI下一场革命。
近期,在第八届中国模式识别与计算机视觉学术会议(PRCV 2025)上,合合信息作为承办方举办了“多模态文本智能大模型前沿技术与应用”论坛,汇聚了学术界的顶尖智慧,更抛出了一颗重磅“炸弹”——“多模态文本智能技术”概念。
246 1
|
4月前
|
监控 算法 测试技术
大模型推理服务优化:动态批处理与连续批处理技术
本文系统阐述大语言模型推理服务中的关键技术——动态批处理与连续批处理。通过分析传统静态批处理的局限性,深入解析动态批处理的请求调度算法、内存管理策略,以及连续批处理的中断恢复机制。文章包含完整的服务架构设计、核心算法实现和性能基准测试,为构建高性能大模型推理服务提供全面解决方案。
597 3
|
4月前
|
存储 缓存 算法
淘宝买家秀 API 深度开发:多模态内容解析与合规推荐技术拆解
本文详解淘宝买家秀接口(taobao.reviews.get)的合规调用、数据标准化与智能推荐全链路方案。涵盖权限申请、多模态数据清洗、情感分析、混合推荐模型及缓存优化,助力开发者提升审核效率60%、商品转化率增长28%,实现UGC数据高效变现。
|
4月前
|
存储 人工智能 搜索推荐
拔俗AI助教系统:基于大模型与智能体架构的新一代教育技术引擎
AI助教融合大语言模型、教育知识图谱、多模态感知与智能体技术,重构“教、学、评、辅”全链路。通过微调LLM、精准诊断错因、多模态交互与自主任务规划,实现个性化教学。轻量化部署与隐私保护设计保障落地安全,未来将向情感感知与教育深度协同演进。(238字)
483 0

热门文章

最新文章