FastChat启动与部署通义千问大模型

FastChat简介

FastChat is an open platform for training, serving, and evaluating large language model based chatbots.

  • FastChat powers Chatbot Arena, serving over 10 million chat requests for 70+ LLMs.
  • Chatbot Arena has collected over 500K human votes from side-by-side LLM battles to compile an online LLM Elo leaderboard.

FastChat’s core features include

  • The training and evaluation code for state-of-the-art models (e.g., Vicuna, MT-Bench).
  • A distributed multi-model serving system with web UI and OpenAI-compatible RESTful APIs.

FastChat Github地址: https://github.com/lm-sys/FastChat
FastChat架构:https://github.com/lm-sys/FastChat/blob/main/docs/server_arch.md

在这里插入图片描述

安装FastChat

pip3 install "fschat[model_worker,webui]"

如果网速较慢或无网就使用国内镜像如:

#阿里源
pip3 install "fschat[model_worker,webui]" -i https://mirrors.aliyun.com/pypi/simple/ 

#清华源
pip3 install "fschat[model_worker,webui]" -i https://pypi.tuna.tsinghua.edu.cn/simple/

# 下载模型到本地

这里以通义千问1.8b为例,其他模型类似,就是文件大小大了些,可以通过huggingface或modelscope两个网站进行下载
https://www.modelscope.cn/qwen/Qwen-1_8B-Chat.git
https://huggingface.co/Qwen/Qwen1.5-1.8B-Chat

比如把模型下载到/home/liu/目录

#cd到目录
cd /home/liu/

#大文件下载需要执行以下:
git lfs install

#先下小文件,先用命令把小文件下了
GIT_LFS_SKIP_SMUDGE=1 git https://www.modelscope.cn/qwen/Qwen-1_8B-Chat.git

#然后cd进去文件夹,下大文件,每个大文件之间可以续传,大文件内部不能续传,以下命令下载所有的大文件
git lfs pull

#上面的git lfs pull是下载所有的大文件,可能你只需要下载模型下的部分大文件,可以通过git lfs pull指定匹配模式,下载部分文件,比如
#下载bin结尾文件
git lfs pull --include="*.bin"
    
#如果你只想要单个文件,写文件名就可以,比如
git lfs pull --include "model-00001-of-00004.safetensors"

注: 如果上面的git lfs pull不成功或报错如git: ‘lfs’ is not a git command. See ‘git --help’.,可能是因为没有安装git lfs,执行安装即可,并执行git lfs install

sudo apt-get install git-lfs

启动服务(OpenAI-Compatible RESTful APIs)

官网参考:https://github.com/lm-sys/FastChat/blob/main/docs/openai_api.md

# 1.启动controller,默认端口为21001,可通过 --port 指定。
python3 -m fastchat.serve.controller > controller.log 2>&1 &

# 2.启动model_worker,默认端口为21002,可通过 --port 指定,model_worker会向controller注册。
python3 -m fastchat.serve.model_worker --model-path /home/liu/Qwen-1_8B-Chat --model-name=Qwen-1_8B-Chat --num-gpus 1  > model_worker.log 2>&1 &

# 3.启动openai_api_server,默认端口为 8000,可通过 --port 指定。
python3 -m fastchat.serve.openai_api_server --host 0.0.0.0 --port 9000  > openai_api_server.log 2>&1 &

# 4.(可选),如果还需要web界面,启动gradio_web_server,默认端口为 7860,可通过 --port 指定。
python3 -m fastchat.serve.gradio_web_server > gradio_web_server.log 2>&1 &

注:

--num-gpus 指定运行模型的gpu个数
--model-name 默认以部署的model-path作为模型名称,可通过--model-name修改,比如--model-name Qwen

Api访问测试

python脚本测试

pip install openai

import openai

openai.api_key = "EMPTY"
openai.base_url = "http://localhost:9000/v1/"

model = "Qwen-1_8B-Chat"
prompt = "Once upon a time"

# create a completion
completion = openai.completions.create(model=model, prompt=prompt, max_tokens=64)

# print the completion
print(prompt + completion.choices[0].text)

# create a chat completion
completion = openai.chat.completions.create(
  model=model,
  messages=[{"role": "user", "content": "Hello! What is your name?"}]
)

# print the completion
print(completion.choices[0].message.content)

python流式输出测试

from openai import OpenAI

client = OpenAI(base_url="http://localhost:9000/v1", api_key="")
model = "Qwen-1_8B-Chat"

completion = client.chat.completions.create(
    model = model,
    messages=[
        {
            "role": "user",
            "content": "Hello",
        }
    ],
    stream=True
)

for chunk in completion:
    if chunk.choices[0].finish_reason == "stop":
        break
    else:
        print(chunk.choices[0].delta.content, end="", flush=True)

curl调用接口测试

curl -X POST http://0.0.0.0:9000/v1/chat/completions -H "Content-Type: application/json" -d "{\"model\": \"Qwen-1_8B-Chat\", \"messages\": [{\"role\": \"user\", \"content\": \"hello?\"}]}"
  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FastChat是一个文本嵌入模型,它可以用于文本相似度计算和文本匹配任务。FastChat通过学习将文本映射到低维向量空间中的嵌入表示,从而捕捉文本之间的语义信息。 FastChat模型主要由两个组成部分构成:句子嵌入的编码器和相似度计算的度量方法。 首先,FastChat使用编码器将句子嵌入到低维向量空间中。编码器通常是基于深度学习的模型,如卷积神经网络(CNN)或循环神经网络(RNN)。编码器网络将输入文本进行特征提取,并将其转换为固定长度的向量表示。这个向量表示能够保留文本的语义信息,并能够被用来计算文本之间的相似度。 其次,FastChat采用一种度量方法来计算文本之间的相似度。一种常见的度量方法是余弦相似度。余弦相似度度量了两个向量之间的夹角,夹角越小,表示两个向量越相似。因此,通过计算两个文本向量的余弦相似度,就可以得到它们之间的相似度分数。 FastChat的优势在于它能够快速且准确地计算文本之间的相似度。由于FastChat使用了低维的向量表示,计算相似度的复杂度相对较低。这使得FastChat在实际应用中具有高效性和实用性。 总而言之,FastChat是一个重要的文本嵌入模型,它通过将文本转换为低维向量表示,并通过度量方法计算文本相似度。这个模型在很多自然语言处理任务中都有广泛的应用,如文本匹配、问答系统、推荐系统等。它为我们提供了一种有效的方式来捕捉和比较文本之间的语义信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值