狠狠躁夜夜躁人人爽超碰97香蕉|色婷婷日日躁夜夜躁|亚洲一区欧美一区在线播|久久久久久性高|伊人久久大香线蕉亚洲

歡迎來到同城快修-附近家電維修、家電清洗、家電安裝服務平臺

24小時家電維修熱線:

400—1558638

當前位置:主頁 > 空氣能 > 維修資訊 >

LangChain開發環境準備-AI大模型私有部署的技術指南

發布日期:2023-09-21 21:14:37 瀏覽:
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-archive4.建議使用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 aria2aria2c --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.jsonaria2c --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.pyaria2c --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.jsonaria2c --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.pyaria2c --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.pyaria2c --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.pyaria2c --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.binaria2c --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.pyaria2c --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.txtaria2c --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.jsonaria2c --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.pyaria2c --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.modelaria2c --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 torchfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom transformers.generation.utils import GenerationConfigtokenizer = 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 FastAPIfrom fastapi import HTTPExceptionfrom pydantic import BaseModelimport torchfrom transformers import AutoModelForCausalLM, AutoTokenizerfrom transformers.generation.utils import GenerationConfigtokenizer = 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 neededclass 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接口,請大家持續關注。

主站蜘蛛池模板: 香港三级韩国三级日本三级| 久久精品国产网红主播| 成+人+网+站+免费观看| 久久精品国产99精品国产2021| 国产色婷婷亚洲99精品| 少妇的丰满人妻hd高清| 欧美精品videosex极品| 亚洲精品无码久久久久去q| 2020国产精品午夜福利在线观看 | 久久国产精品无码网站| 国产精品成人va在线观看 | 欧美,日韩在线高清一区,二区| 色橹橹欧美在线观看视频高清| 久久不见久久见中文字幕免费| 国产aⅴ精品一区二区四区| 亚洲va久久久噜噜噜久久狠狠| 色情一区二区三区免费看| aa区一区二区三无码精片| 无码专区人妻系列日韩精品| 精品国产一区二区久久久久网站| 人人妻人人澡人人爽精品日本| 国产成人午夜高潮毛片| 日本精品一区二区三区在线观看视频 | 国产亚洲欧美日韩在线三区 | 亚洲 欧美 激情 小说 另类| 久久亚洲精品国产亚洲老地址| 日本xxxx色视频在线观看免费 | 国产精品福利无圣光一区二区| 艹b视频在线观看一区三区| 日本中文字幕第一区| av亚洲产国偷v产偷v自拍| 国产成a人亚洲精v品无码| 国产与黑人在线播放| 久久99视频一区| 国产电影视频一区二区| 国产一区二区三区导航| 欧洲偷拍一区二区三区| 亚洲精品乱码久久久久久自慰 | 激情影院内射美女| 国产成人无码a区视频在线观看| 欧美一区二区三区日本一区二区三区 |