OpenAI 兼容服务器#
本文主要讨论了在单个节点上跨多个GPU部署单个大型视觉语言模型,提供与OpenAI接口兼容的服务,以及服务API的使用。
为了方便起见,我们将此服务称为api_server
。关于多模型的并行服务,请参考关于请求分发服务器的指南。
在接下来的部分中,我们将首先介绍两种启动服务的方法,根据您的应用场景选择合适的方案。
接下来,我们重点介绍服务的RESTful API定义,探索与接口交互的各种方式,并演示如何通过Swagger UI或LMDeploy CLI工具尝试该服务。
最后,我们展示了如何将服务集成到WebUI中,为您提供一个参考,以便轻松设置演示示例。
启动服务#
以托管在huggingface hub上的llava-v1.6-vicuna-7b模型为例,您可以选择以下方法之一来启动服务。
选项1:使用lmdeploy CLI启动#
lmdeploy serve api_server liuhaotian/llava-v1.6-vicuna-7b --server-port 23333
api_server
的参数可以通过命令 lmdeploy serve api_server -h
查看,例如,--tp
用于设置张量并行,--session-len
用于指定上下文窗口的最大长度,--cache-max-entry-count
用于调整 k/v 缓存的 GPU 内存比例等。
选项2:使用docker部署#
使用LMDeploy 官方docker镜像,您可以按如下方式运行OpenAI兼容服务器:
docker run --runtime nvidia --gpus all \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HUGGING_FACE_HUB_TOKEN=<secret>" \
-p 23333:23333 \
--ipc=host \
openmmlab/lmdeploy:latest \
lmdeploy serve api_server liuhaotian/llava-v1.6-vicuna-7b
api_server
的参数与“选项 1”部分中提到的参数相同
每个模型可能需要Docker镜像中未包含的特定依赖项。如果遇到问题,您可能需要根据具体情况自行安装这些依赖项。如果有疑问,请参考特定模型项目的文档。
例如,对于Llava:
FROM openmmlab/lmdeploy:latest
RUN apt-get update && apt-get install -y python3 python3-pip git
WORKDIR /app
RUN pip3 install --upgrade pip
RUN pip3 install timm
RUN pip3 install git+https://github.com/haotian-liu/LLaVA.git --no-deps
COPY . .
CMD ["lmdeploy", "serve", "api_server", "liuhaotian/llava-v1.6-34b"]
RESTful API#
LMDeploy的RESTful API兼容以下三个OpenAI接口:
/v1/chat/completions
/v1/models
/v1/completions
图像交互的接口是/v1/chat/completions
,与OpenAI一致。
您可以通过网站http://0.0.0.0:23333
来概览并尝试提供的RESTful API,如下所示,在成功启动服务后。
如果您需要将服务集成到您自己的项目或产品中,我们推荐以下方法:
与OpenAI
集成#
这里是一个通过openai包与v1/chat/completions
服务交互的示例。
在运行之前,请通过pip install openai
安装openai包。
from openai import OpenAI
client = OpenAI(api_key='YOUR_API_KEY', base_url='http://0.0.0.0:23333/v1')
model_name = client.models.list().data[0].id
response = client.chat.completions.create(
model=model_name,
messages=[{
'role':
'user',
'content': [{
'type': 'text',
'text': 'Describe the image please',
}, {
'type': 'image_url',
'image_url': {
'url':
'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg',
},
}],
}],
temperature=0.8,
top_p=0.8)
print(response)
与lmdeploy APIClient
集成#
以下是一些示例,展示了如何通过APIClient
访问服务。
如果你想使用/v1/chat/completions
端点,你可以尝试以下代码:
from lmdeploy.serve.openai.api_client import APIClient
api_client = APIClient(f'http://0.0.0.0:23333')
model_name = api_client.available_models[0]
messages = [{
'role':
'user',
'content': [{
'type': 'text',
'text': 'Describe the image please',
}, {
'type': 'image_url',
'image_url': {
'url':
'https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg',
},
}]
}]
for item in api_client.chat_completions_v1(model=model_name,
messages=messages):
print(item)
与Java/Golang/Rust集成#
可以使用 openapi-generator-cli 将 http://{server_ip}:{server_port}/openapi.json
转换为 java/rust/golang 客户端。
以下是一个示例:
$ docker run -it --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i /local/openapi.json -g rust -o /local/rust
$ ls rust/*
rust/Cargo.toml rust/git_push.sh rust/README.md
rust/docs:
ChatCompletionRequest.md EmbeddingsRequest.md HttpValidationError.md LocationInner.md Prompt.md
DefaultApi.md GenerateRequest.md Input.md Messages.md ValidationError.md
rust/src:
apis lib.rs models