"examples/vscode:/vscode.git/clone" did not exist on "0f091a1d84ab97a869e40870e9608380e7ca49c1"
restful_api.md 5.2 KB
Newer Older
AllentDan's avatar
AllentDan committed
1
2
3
4
# Restful API

### 启动服务

5
6
7
8
9
用户将下面命令输出的 http url 复制到浏览器打开,详细查看所有的 API 及其使用方法。
请一定查看`http://{server_ip}:{server_port}`!!!
请一定查看`http://{server_ip}:{server_port}`!!!
请一定查看`http://{server_ip}:{server_port}`!!!
重要的事情说三遍。
AllentDan's avatar
AllentDan committed
10
11

```shell
12
lmdeploy serve api_server ./workspace 0.0.0.0 --server_port ${server_port} --instance_num 64 --tp 1
AllentDan's avatar
AllentDan committed
13
14
```

AllentDan's avatar
AllentDan committed
15
我们提供的 restful api,其中三个仿照 OpenAI 的形式。
16
17
18
19
20
21

- /v1/chat/completions
- /v1/models
- /v1/completions

不过,我们建议用户用我们提供的另一个 API: `/v1/chat/interactive`
AllentDan's avatar
AllentDan committed
22
23
24
25
它有更好的性能,提供更多的参数让用户自定义修改。

### python

26
27
28
29
30
31
32
33
34
35
36
37
38
我们将这些服务的客户端功能集成在 `APIClient` 类中。下面是一些例子,展示如何在客户端调用 `api_server` 服务。
如果你想用 `/v1/chat/completions` 接口,你可以尝试下面代码:

```python
from lmdeploy.serve.openai.api_client import APIClient
api_client = APIClient('http://{server_ip}:{server_port}')
model_name = api_client.available_models[0]
messages = [{"role": "user", "content": "Say this is a test!"}]
for item in api_client.chat_completions_v1(model=model_name, messages=messages):
    print(item)
```

如果你想用 `/v1/completions` 接口,你可以尝试:
AllentDan's avatar
AllentDan committed
39
40

```python
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from lmdeploy.serve.openai.api_client import APIClient
api_client = APIClient('http://{server_ip}:{server_port}')
model_name = api_client.available_models[0]
for item in api_client.completions_v1(model=model_name, prompt='hi'):
    print(item)
```

LMDeploy 的 `/v1/chat/interactive` api 支持将对话内容管理在服务端,但是我们默认关闭。如果想尝试,请阅读以下介绍:

- 交互模式下,对话历史保存在 server。在一次完整的多轮对话中,所有请求设置`interactive_mode = True`, `session_id`保持相同 (不为 -1,这是缺省值)。
- 非交互模式下,server 不保存历史记录。

交互模式可以通过 `interactive_mode` 布尔量参数控制。下面是一个普通模式的例子,
如果要体验交互模式,将 `interactive_mode=True` 传入即可。

```python
from lmdeploy.serve.openai.api_client import APIClient
api_client = APIClient('http://{server_ip}:{server_port}')
for item in api_client.generate(prompt='hi'):
    print(item)
AllentDan's avatar
AllentDan committed
61
62
```

63
### Java/Golang/Rust
AllentDan's avatar
AllentDan committed
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
可以使用代码生成工具 [openapi-generator-cli](https://github.com/OpenAPITools/openapi-generator-cli)`http://{server_ip}:{server_port}/openapi.json` 转成 java/rust/golang 客户端。
下面是一个使用示例:

```shell
$ 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
```
AllentDan's avatar
AllentDan committed
81
82
83
84
85
86
87
88

### cURL

cURL 也可以用于查看 API 的输出结果

查看模型列表:

```bash
89
curl http://{server_ip}:{server_port}/v1/models
AllentDan's avatar
AllentDan committed
90
91
```

92
Interactive Chat:
AllentDan's avatar
AllentDan committed
93
94

```bash
95
curl http://{server_ip}:{server_port}/v1/chat/interactive \
AllentDan's avatar
AllentDan committed
96
97
  -H "Content-Type: application/json" \
  -d '{
98
    "prompt": "Hello! How are you?",
99
    "session_id": 1,
100
    "interactive_mode": true
AllentDan's avatar
AllentDan committed
101
102
103
104
105
106
  }'
```

Chat Completions:

```bash
107
curl http://{server_ip}:{server_port}/v1/chat/completions \
AllentDan's avatar
AllentDan committed
108
109
110
  -H "Content-Type: application/json" \
  -d '{
    "model": "internlm-chat-7b",
111
    "messages": [{"role": "user", "content": "Hello! How are you?"}]
AllentDan's avatar
AllentDan committed
112
113
114
  }'
```

115
Text Completions:
AllentDan's avatar
AllentDan committed
116

117
118
119
```shell
curl http://{server_ip}:{server_port}/v1/completions \
  -H 'Content-Type: application/json' \
AllentDan's avatar
AllentDan committed
120
  -d '{
121
122
123
  "model": "llama",
  "prompt": "two steps to build a house:"
}'
AllentDan's avatar
AllentDan committed
124
125
```

126
127
128
129
130
### CLI client

restful api 服务可以通过客户端测试,例如

```shell
131
132
# restful_api_url is what printed in api_server.py, e.g. http://localhost:23333
lmdeploy serve api_client api_server_url
133
134
135
136
137
138
139
```

### webui

也可以直接用 webui 测试使用 restful-api。

```shell
140
141
142
143
# api_server_url 就是 api_server 产生的,比如 http://localhost:23333
# server_name 和 server_port 是用来提供 gradio ui 访问服务的
# 例子: lmdeploy serve gradio http://localhost:23333 --server_name localhost --server_port 6006
lmdeploy serve gradio api_server_url --server_name ${gradio_ui_ip} --server_port ${gradio_ui_port}
144
145
```

AllentDan's avatar
AllentDan committed
146
147
### FAQ

148
1. 当返回结果结束原因为 `"finish_reason":"length"`,这表示回话长度超过最大值。如需调整会话支持的最大长度,可以通过启动`api_server`时,设置`--session_len`参数大小。
AllentDan's avatar
AllentDan committed
149
150
151

2. 当服务端显存 OOM 时,可以适当减小启动服务时的 `instance_num` 个数

152
3. 当同一个 `session_id` 的请求给 `/v1/chat/interactive` 函数后,出现返回空字符串和负值的 `tokens`,应该是 `session_id` 混乱了,可以先将交互模式关闭,再重新开启。
AllentDan's avatar
AllentDan committed
153

154
4. `/v1/chat/interactive` api 支持多轮对话, 但是默认关闭。`messages` 或者 `prompt` 参数既可以是一个简单字符串表示用户的单词提问,也可以是一段对话历史。