"vscode:/vscode.git/clone" did not exist on "83c3a932d302d4fd67cfbd3e10db4ac8556fef1a"
protocol.py 6.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
Copyright 2023-2024 SGLang Team
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

Lianmin Zheng's avatar
Lianmin Zheng committed
16
"""Pydantic models for OpenAI API protocol"""
Liangsheng Yin's avatar
Liangsheng Yin committed
17

18
19
import time
from typing import Dict, List, Optional, Union
Lianmin Zheng's avatar
Lianmin Zheng committed
20

21
from pydantic import BaseModel, Field
Lianmin Zheng's avatar
Lianmin Zheng committed
22
from typing_extensions import Literal
Lianmin Zheng's avatar
Lianmin Zheng committed
23

24

zhyncs's avatar
zhyncs committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class ModelCard(BaseModel):
    """Model cards."""

    id: str
    object: str = "model"
    created: int = Field(default_factory=lambda: int(time.time()))
    owned_by: str = "sglang"
    root: Optional[str] = None


class ModelList(BaseModel):
    """Model list consists of model cards."""

    object: str = "list"
    data: List[ModelCard] = []


42
43
44
45
46
47
48
49
class ErrorResponse(BaseModel):
    object: str = "error"
    message: str
    type: str
    param: Optional[str] = None
    code: int


50
51
52
53
54
55
56
57
58
59
60
61
62
63
class LogProbs(BaseModel):
    text_offset: List[int] = Field(default_factory=list)
    token_logprobs: List[Optional[float]] = Field(default_factory=list)
    tokens: List[str] = Field(default_factory=list)
    top_logprobs: List[Optional[Dict[str, float]]] = Field(default_factory=list)


class UsageInfo(BaseModel):
    prompt_tokens: int = 0
    total_tokens: int = 0
    completion_tokens: Optional[int] = 0


class CompletionRequest(BaseModel):
64
65
    # Ordered by official OpenAI API documentation
    # https://platform.openai.com/docs/api-reference/completions/create
66
    model: str
67
68
    prompt: Union[List[int], List[List[int]], str, List[str]]
    best_of: Optional[int] = None
69
70
71
    echo: Optional[bool] = False
    frequency_penalty: Optional[float] = 0.0
    logit_bias: Optional[Dict[str, float]] = None
72
73
74
75
76
77
78
79
80
81
    logprobs: Optional[int] = None
    max_tokens: Optional[int] = 16
    n: int = 1
    presence_penalty: Optional[float] = 0.0
    seed: Optional[int] = None
    stop: Optional[Union[str, List[str]]] = Field(default_factory=list)
    stream: Optional[bool] = False
    suffix: Optional[str] = None
    temperature: Optional[float] = 1.0
    top_p: Optional[float] = 1.0
82
83
    user: Optional[str] = None

84
85
    # Extra parameters for SRT backend only and will be ignored by OpenAI models.
    regex: Optional[str] = None
Mingyi's avatar
Mingyi committed
86
    ignore_eos: Optional[bool] = False
87

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

class CompletionResponseChoice(BaseModel):
    index: int
    text: str
    logprobs: Optional[LogProbs] = None
    finish_reason: Optional[str] = None


class CompletionResponse(BaseModel):
    id: str
    object: str = "text_completion"
    created: int = Field(default_factory=lambda: int(time.time()))
    model: str
    choices: List[CompletionResponseChoice]
    usage: UsageInfo


class CompletionResponseStreamChoice(BaseModel):
    index: int
    text: str
    logprobs: Optional[LogProbs] = None
    finish_reason: Optional[str] = None


class CompletionStreamResponse(BaseModel):
    id: str
    object: str = "text_completion"
    created: int = Field(default_factory=lambda: int(time.time()))
    model: str
    choices: List[CompletionResponseStreamChoice]
Cody Yu's avatar
Cody Yu committed
118
119
120
    usage: UsageInfo


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
class ChatCompletionMessageGenericParam(BaseModel):
    role: Literal["system", "assistant"]
    content: str


class ChatCompletionMessageContentTextPart(BaseModel):
    type: Literal["text"]
    text: str


class ChatCompletionMessageContentImageURL(BaseModel):
    url: str
    detail: Optional[Literal["auto", "low", "high"]] = "auto"


class ChatCompletionMessageContentImagePart(BaseModel):
    type: Literal["image_url"]
    image_url: ChatCompletionMessageContentImageURL


ChatCompletionMessageContentPart = Union[
    ChatCompletionMessageContentTextPart, ChatCompletionMessageContentImagePart
]


class ChatCompletionMessageUserParam(BaseModel):
    role: Literal["user"]
    content: Union[str, List[ChatCompletionMessageContentPart]]


ChatCompletionMessageParam = Union[
    ChatCompletionMessageGenericParam, ChatCompletionMessageUserParam
]


156
157
158
159
160
class ResponseFormat(BaseModel):
    # type must be "json_object" or "text"
    type: Literal["text", "json_object"]


Cody Yu's avatar
Cody Yu committed
161
class ChatCompletionRequest(BaseModel):
162
163
164
    # Ordered by official OpenAI API documentation
    # https://platform.openai.com/docs/api-reference/chat/create
    messages: List[ChatCompletionMessageParam]
Cody Yu's avatar
Cody Yu committed
165
    model: str
166
167
168
169
    frequency_penalty: Optional[float] = 0.0
    logit_bias: Optional[Dict[str, float]] = None
    logprobs: Optional[bool] = False
    top_logprobs: Optional[int] = None
170
    max_tokens: Optional[int] = None
Cody Yu's avatar
Cody Yu committed
171
    n: Optional[int] = 1
172
173
174
    presence_penalty: Optional[float] = 0.0
    response_format: Optional[ResponseFormat] = None
    seed: Optional[int] = None
Cody Yu's avatar
Cody Yu committed
175
176
    stop: Optional[Union[str, List[str]]] = Field(default_factory=list)
    stream: Optional[bool] = False
177
178
    temperature: Optional[float] = 0.7
    top_p: Optional[float] = 1.0
Cody Yu's avatar
Cody Yu committed
179
180
    user: Optional[str] = None

181
182
183
    # Extra parameters for SRT backend only and will be ignored by OpenAI models.
    regex: Optional[str] = None

Cody Yu's avatar
Cody Yu committed
184
185
186
187
188
189
190
191
192

class ChatMessage(BaseModel):
    role: Optional[str] = None
    content: Optional[str] = None


class ChatCompletionResponseChoice(BaseModel):
    index: int
    message: ChatMessage
193
    logprobs: Optional[LogProbs] = None
Cody Yu's avatar
Cody Yu committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    finish_reason: Optional[str] = None


class ChatCompletionResponse(BaseModel):
    id: str
    object: str = "chat.completion"
    created: int = Field(default_factory=lambda: int(time.time()))
    model: str
    choices: List[ChatCompletionResponseChoice]
    usage: UsageInfo


class DeltaMessage(BaseModel):
    role: Optional[str] = None
    content: Optional[str] = None


class ChatCompletionResponseStreamChoice(BaseModel):
    index: int
    delta: DeltaMessage
214
    logprobs: Optional[LogProbs] = None
Cody Yu's avatar
Cody Yu committed
215
216
217
218
219
220
221
222
    finish_reason: Optional[str] = None


class ChatCompletionStreamResponse(BaseModel):
    id: str
    object: str = "chat.completion.chunk"
    created: int = Field(default_factory=lambda: int(time.time()))
    model: str
Liangsheng Yin's avatar
Liangsheng Yin committed
223
    choices: List[ChatCompletionResponseStreamChoice]