Fastapi LLM支持

FastChat 服务架构

请添加图片描述

安装

方法一:pip

pip3 install "fschat[model_worker,webui]"

方法二:源文件

git clone https://github.com/lm-sys/FastChat.git
cd FastChat

Mac 上运行:

brew install rust cmake

从源文件安装

pip3 install --upgrade pip  # enable PEP 660 support
pip3 install -e ".[model_worker,webui]"

加载大模型推理

1.命令行

python3 -m fastchat.serve.cli --model-path lmsys/vicuna-7b-v1.3

请添加图片描述

关键参数汇总:

参数说明
–model-path模型路径,可以是本地文件夹或HuggingFace的repo ID
–revisionHuggingFace Hub模型修订版本标识
–device设备类型,可选择cpu/cuda/mps/xpu
–gpus指定单个或多个GPU,如1或0,2
–num-gpusGPU数量
–max-gpu-memory每个GPU用于存储模型权重的最大内存,使用字符串表示如’13Gib’
–load-8bit使用8位量化
–cpu-offloading仅用于8位量化:将超出GPU容量的权重卸载到CPU
–gptq-ckptGPTQ检查点路径,用于GPTQ量化
–gptq-wbitsGPTQ量化比特数,可选择2/3/4/8/16
–gptq-groupsizeGPTQ量化分组大小,默认为整行
–gptq-act-order是否应用GPTQ激活顺序启发式方法
–awq-ckptAWQ检查点路径,用于AWQ量化
–awq-wbitsAWQ量化比特数,可选择4/16
–awq-groupsizeAWQ量化分组大小,默认为整行
–conv-template对话提示模板
–conv-system-msg对话系统消息
–temperature温度参数
–repetition_penalty重复惩罚参数
–max-new-tokens最大新生成词元数量
–no-history不保留历史记录
–style显示样式,可选择simple/rich/programmatic
–multiline启用多行输入
–mouse启用鼠标支持光标定位(仅限rich样式)
–judge-sent-end是否启用识别句子结束的逻辑纠正
–debug打印调试信息(如prompt)

2.Web

要使用 Web UI 提供服务,您需要三个主要组件:

  1. 与用户交互的 Web 服务器

  2. 托管一个或多个模型的模型工作人员

  3. 协调网络服务器和模型工作人员的控制器。

启动控制器
python3 -m fastchat.serve.controller

请添加图片描述

启动模型工作
python3 -m fastchat.serve.model_worker --model-path lmsys/vicuna-7b-v1.3

等到进程完成加载模型,您会看到“Uvicorn running on …”。模型工作者将自己注册到控制器。

请添加图片描述

测试模型是否注册到控制器,成功会有一个简短的输出
请添加图片描述

启动 Gradio Web 服务器
python3 -m fastchat.serve.gradio_web_server

这是用户将与之交互的用户界面。

请添加图片描述

打开 http://0.0.0.0:7860 ,可以看到交互界面

请添加图片描述

注册多个模型
  • 您可以将多个模型工作人员注册到单个控制器,该控制器可用于为具有更高吞吐量的单个模型提供服务或同时为多个模型提供服务。执行此操作时,请为不同的模型工作人员分配不同的 GPU 和端口。
# worker 0
CUDA_VISIBLE_DEVICES=0 python3 -m fastchat.serve.model_worker --model-path lmsys/vicuna-7b-v1.3 --controller http://localhost:21001 --port 31000 --worker http://localhost:31000
# worker 1
CUDA_VISIBLE_DEVICES=1 python3 -m fastchat.serve.model_worker --model-path lmsys/fastchat-t5-3b-v1.0 --controller http://localhost:21001 --port 31001 --worker http://localhost:31001
  • 您还可以启动多选项卡 gradio 服务器,其中包括 Chatbot Arena 选项卡。
python3 -m fastchat.serve.gradio_web_server_multi

3.兼容 OpenAI 的 RESTful API

RESTful API 服务器

  1. 启动控制器
python3 -m fastchat.serve.controller
  1. 启动模型加载
python3 -m fastchat.serve.model_worker --model-path /mnt/code/LLM_Service/model/Baichuan-13b-Chat 
#多卡运行
python3 -m fastchat.serve.model_worker --model-path my_model_path --num-gpus 4 --max-gpu-memory "20GiB"
# CUDA_VISIBLE_DEVICES=0,1,2,3 --gpus "0,1,2,3"这几个参数多卡没有效果,必须加--max-gpu-memory
  1. 启动 RESTful API 服务器
python3 -m fastchat.serve.openai_api_server --host localhost --port 8000

  • 安装openai-python:
pip install --upgrade openai

通过ChatOpenAI接口调用模型:

from langchain.chat_models import ChatOpenAI
from langchain import LLMChain
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
)

api_base_url = "http://192.168.175.6:8000/v1" 
api_key= "EMPTY"
LLM_MODEL = "Baichuan-13b-Chat"
model = ChatOpenAI(
    streaming=True,
    verbose=True,
    # callbacks=[callback],
    openai_api_key=api_key,
    openai_api_base=api_base_url,
    model_name=LLM_MODEL
)


human_prompt = "{input}"
human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)

chat_prompt = ChatPromptTemplate.from_messages(
    [("human", "我们来玩成语接龙,我先来,生龙活虎"),
     ("ai", "虎头虎脑"),
     ("human", "{input}")])


chain = LLMChain(prompt=chat_prompt, llm=model, verbose=True)
print(chain({"input": "恼羞成怒"}))

请添加图片描述

如果您想在同一台机器上、同一进程中运行多个模型,可以将model_worker上述步骤替换为多模型变体:

python3 -m fastchat.serve.multi_model_worker \
    --model-path lmsys/vicuna-7b-v1.3 \
    --model-names vicuna-7b-v1.3 \
    --model-path lmsys/longchat-7b-16k \
    --model-names longchat-7b-16k

本地GPU微调

安装依赖

pip3 install -e ".[train]"

微调

您可以使用以下命令使用 4 x A100 (40GB) 训练 Vicuna-7B。--model_name_or_path使用 LLaMA 权重的实际路径和--data_path数据的实际路径进行更新。

torchrun --nproc_per_node=4 --master_port=20001 fastchat/train/train_mem.py \
    --model_name_or_path ~/model_weights/llama-7b  \
    --data_path data/dummy_conversation.json \
    --bf16 True \
    --output_dir output_vicuna \
    --num_train_epochs 3 \
    --per_device_train_batch_size 2 \
    --per_device_eval_batch_size 2 \
    --gradient_accumulation_steps 16 \
    --evaluation_strategy "no" \
    --save_strategy "steps" \
    --save_steps 1200 \
    --save_total_limit 10 \
    --learning_rate 2e-5 \
    --weight_decay 0. \
    --warmup_ratio 0.03 \
    --lr_scheduler_type "cosine" \
    --logging_steps 1 \
    --fsdp "full_shard auto_wrap" \
    --fsdp_transformer_layer_cls_to_wrap 'LlamaDecoderLayer' \
    --tf32 True \
    --model_max_length 2048 \
    --gradient_checkpointing True \
    --lazy_preprocess True
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐