请求分发服务器#
请求分发服务可以并行化多个api_server服务。用户只需访问代理URL,就可以间接访问不同的api_server服务。代理服务将在内部自动分发请求,实现负载均衡。
启动#
启动代理服务:
lmdeploy serve proxy --server-name {server_name} --server-port {server_port} --strategy "min_expected_latency"
启动成功后,代理服务的URL也将由脚本打印出来。在浏览器中访问此URL以打开Swagger UI。
随后,用户可以在启动api_server
服务时,通过使用--proxy-url
命令直接将其添加到代理服务中。例如:
lmdeploy serve api_server InternLM/internlm2-chat-1_8b --proxy-url http://0.0.0.0:8000
。
这样,用户可以通过代理节点访问api_server
的服务,代理节点的使用方式与api_server
完全相同,两者都兼容OpenAI格式。
/v1/models
/v1/chat/completions
/v1/completions
节点管理#
通过Swagger UI,我们可以看到多个API。与api_server节点管理相关的包括:
/nodes/status
/nodes/add
/nodes/remove
它们分别表示查看所有api_server服务节点、添加某个节点和删除某个节点。
通过curl进行节点管理#
curl -X 'GET' \
'http://localhost:8000/nodes/status' \
-H 'accept: application/json'
curl -X 'POST' \
'http://localhost:8000/nodes/add' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"url": "http://0.0.0.0:23333"
}'
curl -X 'POST' \
'http://localhost:8000/nodes/remove?node_url=http://0.0.0.0:23333' \
-H 'accept: application/json' \
-d ''
通过Python进行节点管理#
# query all nodes
import requests
url = 'http://localhost:8000/nodes/status'
headers = {'accept': 'application/json'}
response = requests.get(url, headers=headers)
print(response.text)
# add a new node
import requests
url = 'http://localhost:8000/nodes/add'
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
data = {"url": "http://0.0.0.0:23333"}
response = requests.post(url, headers=headers, json=data)
print(response.text)
# delete a node
import requests
url = 'http://localhost:8000/nodes/remove'
headers = {'accept': 'application/json',}
params = {'node_url': 'http://0.0.0.0:23333',}
response = requests.post(url, headers=headers, data='', params=params)
print(response.text)
调度策略#
代理服务的当前分发策略如下:
random:根据用户提供的每个api_server节点处理请求的能力进行调度。请求吞吐量越大,越有可能被分配。未提供吞吐量的节点将根据其他节点的平均吞吐量进行处理。
min_expected_latency:根据当前每个节点上等待处理的请求数量以及每个节点的吞吐能力进行分配,计算完成响应所需的预期时间。选择最短的进行分配。不提供吞吐能力的节点也会被类似处理。
min_observed_latency:根据每个节点处理一定数量过去请求所需的平均时间进行分配。时间最短的节点将被分配。