当前位置:   article > 正文

ChatGLM3在Docker环境部署_如何docker容器化部署chatglm3

如何docker容器化部署chatglm3

ChatGLM3 docker部署

1. 下载项目到本地

git clone https://github.com/THUDM/ChatGLM3
cd ChatGLM3
  • 1
  • 2

这目录ChatGLM3下应该还需要包含chatglm3-6b-32k HuggingFace 或者 ModelScope ,需要手动下载。

2.制作Docker镜像

1)配置基础的Dockerfile:
#基于的基础镜像
FROM python:3.11.6
# 设置工作目录
WORKDIR /LLM
# 拷贝应用程序文件到容器中
COPY ./chatglm3-6b-32k/ /models/
COPY ./ChatGLM3/ /LLM/
# 安装支持
RUN pip install  -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
RUN pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
2)打包生成镜像
docker build -t dmx:easy .
  • 1

3.启动容器

 docker run --restart=always -itd --name dmxApp3 -p 8000:8000 -w /LLM --gpus device=2 dmx:easy python LLM_Server.py
  • 1

4.查看运行日志

docker logs -t dmxApp3
  • 1

在这里插入图片描述

5. LLM_Server.py

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from transformers import AutoTokenizer, AutoModel
from utils import load_model_on_gpus
import uvicorn, json, datetime
import os

import torch


DEVICE = "cuda"
DEVICE_ID = "0"
CUDA_DEVICE = f"{DEVICE}:{DEVICE_ID}" if DEVICE_ID else DEVICE


def torch_gc():
    if torch.cuda.is_available():
        with torch.cuda.device(CUDA_DEVICE):
            torch.cuda.empty_cache()
            torch.cuda.ipc_collect()


app = FastAPI()  # 创建API实例
app.add_middleware(
    CORSMiddleware,
    # 允许跨域的源列表,例如 ["http://www.example.org"] 等等,["*"] 表示允许任何源
    allow_origins=["*"],
    # 跨域请求是否支持 cookie,默认是 False,如果为 True,allow_origins 必须为具体的源,不可以是 ["*"]
    allow_credentials=False,
    # 允许跨域请求的 HTTP 方法列表,默认是 ["GET"]
    allow_methods=["*"],
    # 允许跨域请求的 HTTP 请求头列表,默认是 [],可以使用 ["*"] 表示允许所有的请求头
    # 当然 Accept、Accept-Language、Content-Language 以及 Content-Type 总之被允许的
    allow_headers=["*"],
    # 可以被浏览器访问的响应头, 默认是 [],一般很少指定
    # expose_headers=["*"]
    # 设定浏览器缓存 CORS 响应的最长时间,单位是秒。默认为 600,一般也很少指定
    # max_age=1000
)


@app.post("/")
async def create_item(request: Request):
    global model, tokenizer
    json_post_raw = await request.json()
    json_post = json.dumps(json_post_raw)
    json_post_list = json.loads(json_post)
    prompt = json_post_list.get('prompt')
    history = json_post_list.get('history')
    max_length = json_post_list.get('max_length')
    top_p = json_post_list.get('top_p')
    temperature = json_post_list.get('temperature')
    response, history = model.chat(tokenizer,
                                   prompt,
                                   history=history,
                                   max_length=max_length if max_length else 32760,
                                   top_p=top_p if top_p else 0.7,
                                   #top_p=top_p if top_p else 0.1,
                                   temperature=temperature if temperature else 0.95)
                                   #temperature=temperature if temperature else 0.1)

    now = datetime.datetime.now()
    time = now.strftime("%Y-%m-%d %H:%M:%S")
    answer = {
        "response": response,
        "history": history,
        "status": 200,
        "time": time
    }
    log = "[" + time + "] " + '", prompt:"' + prompt + '", response:"' + repr(response) + '"'
    print(log)
    torch_gc()
    return answer

if __name__ == '__main__':
    modelPath="models/"
    tokenizer = AutoTokenizer.from_pretrained(modelPath, trust_remote_code=True)
    model = AutoModel.from_pretrained(modelPath, trust_remote_code=True).cuda()
    model.eval()
    uvicorn.run(app, host='0.0.0.0', port=8000, workers=1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/474426
推荐阅读
相关标签
  

闽ICP备14008679号