赞
踩
构建conda环境
【Conda】超详细的linux-conda环境安装教程_linux安装conda-CSDN博客
FastChat是一个用于训练、部署和评估基于大型语言模型的聊天机器人的开放平台
支持以下大模型:FastChat/docs/model_support.md at main · lm-sys/FastChat · GitHub
- # 查看镜像
- docker image ls
img1
的容器,分配所有 GPU 资源,映射三个后面用到的端口,挂载一个主机目录,并启动一个交互式 Bash shell 以便于在容器内进行操作。- # 启动容器并进行端口映射
- sudo docker run --gpus all -p 21002:21001 -p 21003:21002 -p 21004:21003 -v /home/before_path:/home/after_path --name container_name -it img1:latest /bin/bash
docker run
:
-p 21002:21001 -p 21003:21002 -p 21004:21003
: 端口映射,因为主机21001端口已占用,所以将容器的21001~21003分别映射至主机21002~21004
-v /home/before_path:/home/after_path
: 挂载主机目录到容器内部:将主机的 /home/before_path
挂载到容器的 /home/after_path
--name container
:
-it
:
-i
:交互模式,保持标准输入打开。-t
:分配一个伪终端。这两者结合使用,允许你进入容器并进行交互操作。img1:latest
:
img1
是镜像名称,latest
是镜像的标签,表示使用该镜像的最新版本。/bin/bash
:
docker restart container_name
docker ps
docker ps -a
docker exec -it container_name /bin/bash
FastChat官方文档:https://github.com/lm-sys/FastChat/blob/main/docs/openai_api.md
安装FastChat:
pip install "fschat[model_worker,webui]"
创建三个终端,分别执行以下命令:
1.启动controller
python3 -m fastchat.serve.controller --host 0.0.0.0 --port 21001
2. 启动model worker
CUDA_VISIBLE_DEVICES="0" python3 -m fastchat.serve.model_worker --model-path weights/model_weights --host 0.0.0.0 --port 21002 --worker-address "http://0.0.0.0:21002"
3.启动RESTful API server
python3 -m fastchat.serve.openai_api_server --host 0.0.0.0 --port 21003 --controller-address http://0.0.0.0:21001
注意:1. 此处本机只能用0.0.0.0,使用127.0.0.1会报错
2. control必须使用默认端口21001,否则启动model_worker会报错
3. 必须docker开放端口21003,即必须将21003与主机端口映射,否则无法访问
查看模型信息
curl http://xxx.xxx.xx.xxx:21004/v1/models
模型对话
curl http://xx.xxx.xx.xxx:21004/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "model_weights","messages": [{"role": "user", "content": "Hello! What is your name?"}]}'
- import json
- import time
- import urllib.request
- import sys
- import requests
-
- def test_api_server(input_text):
- header = {'Content-Type': 'application/json'}
-
- data = {
- "messages": [{"role": "system", "content": ""}, {"role": "user", "content": input_text}],
- "temperature": 0.3,
- "top_p" : 0.95,
- "max_tokens": 512,
- "model": "model_weights",
- "stream" : False,
- "n" : 1,
- "best_of": 1,
- "presence_penalty": 1.2,
- "frequency_penalty": 0.2,
- "top_k": 50,
- "use_beam_search": False,
- "stop": [],
- "ignore_eos" :False,
- "logprobs": None
- }
- response = requests.post(
- url='http://xx.xxx.xx.xxx:21004/v1/chat/completions', # 外机运行
- # url='http://0.0.0.0:21003/v1/chat/completions', # 本机运行
- headers=header,
- data=json.dumps(data).encode('utf-8')
- )
-
- result = None
- try:
- result = json.loads(response.content)
- print(json.dumps(data, ensure_ascii=False, indent=2))
- print(json.dumps(result, ensure_ascii=False, indent=2))
-
- except Exception as e:
- print(e)
-
- return result
-
- if __name__ == "__main__":
- test_api_server("Hello,what is your name?")

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。