lib.rs 6.73 KB
Newer Older
1
/// Text Generation Inference Webserver
2
mod infer;
3
mod queue;
Olivier Dehaene's avatar
Olivier Dehaene committed
4
pub mod server;
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
5
mod validation;
Olivier Dehaene's avatar
Olivier Dehaene committed
6

7
use infer::Infer;
8
use queue::{Entry, Queue};
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
9
use serde::{Deserialize, Serialize};
10
use utoipa::ToSchema;
Olivier Dehaene's avatar
Olivier Dehaene committed
11
use validation::Validation;
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
12

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// Hub type
#[derive(Clone, Debug, Deserialize)]
pub struct ModelInfo {
    #[serde(rename(deserialize = "id"))]
    pub model_id: String,
    pub sha: Option<String>,
    pub pipeline_tag: Option<String>,
}

#[derive(Clone, Debug, Serialize, ToSchema)]
pub struct Info {
    #[schema(example = "bigscience/blomm-560m")]
    pub model_id: String,
    #[schema(nullable = true, example = "e985a63cdc139290c5f700ff1929f0b5942cced2")]
    pub model_sha: Option<String>,
    #[schema(nullable = true, example = "text-generation")]
    pub model_pipeline_tag: Option<String>,
    #[schema(example = "0.5.0")]
    pub version: &'static str,
    #[schema(nullable = true, example = "null")]
    pub sha: Option<&'static str>,
}

36
#[derive(Clone, Debug, Deserialize, ToSchema)]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
37
pub(crate) struct GenerateParameters {
38
39
40
    #[serde(default)]
    #[schema(exclusive_minimum = 0, nullable = true, default = "null", example = 1)]
    pub best_of: Option<usize>,
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    #[serde(default)]
    #[schema(
        exclusive_minimum = 0.0,
        nullable = true,
        default = "null",
        example = 0.5
    )]
    pub temperature: Option<f32>,
    #[serde(default)]
    #[schema(
        exclusive_minimum = 0.0,
        nullable = true,
        default = "null",
        example = 1.03
    )]
    pub repetition_penalty: Option<f32>,
    #[serde(default)]
    #[schema(exclusive_minimum = 0, nullable = true, default = "null", example = 10)]
    pub top_k: Option<i32>,
    #[serde(default)]
    #[schema(
        exclusive_minimum = 0.0,
        maximum = 1.0,
        nullable = true,
        default = "null",
        example = 0.95
    )]
    pub top_p: Option<f32>,
69
    #[serde(default)]
70
71
72
73
74
75
76
77
78
    #[schema(
        exclusive_minimum = 0.0,
        maximum = 1.0,
        nullable = true,
        default = "null",
        example = 0.95
    )]
    pub typical_p: Option<f32>,
    #[serde(default)]
79
    #[schema(default = "false", example = true)]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
80
81
    pub do_sample: bool,
    #[serde(default = "default_max_new_tokens")]
82
    #[schema(exclusive_minimum = 0, exclusive_maximum = 512, default = "20")]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
83
    pub max_new_tokens: u32,
OlivierDehaene's avatar
OlivierDehaene committed
84
    #[serde(default)]
85
    #[schema(nullable = true, default = "null", example = false)]
86
87
    pub return_full_text: Option<bool>,
    #[serde(default)]
88
    #[schema(inline, max_items = 4, example = json ! (["photographer"]))]
89
    pub stop: Vec<String>,
OlivierDehaene's avatar
OlivierDehaene committed
90
    #[serde(default)]
91
    #[schema(nullable = true, default = "null", example = "null")]
92
93
    pub truncate: Option<usize>,
    #[serde(default)]
94
95
96
    #[schema(default = "false", example = true)]
    pub watermark: bool,
    #[serde(default)]
97
    #[schema(default = "true")]
OlivierDehaene's avatar
OlivierDehaene committed
98
    pub details: bool,
99
    #[serde(default)]
100
101
102
103
104
105
    #[schema(
        exclusive_minimum = 0,
        nullable = true,
        default = "null",
        example = "null"
    )]
106
    pub seed: Option<u64>,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
107
108
109
110
111
112
113
114
}

fn default_max_new_tokens() -> u32 {
    20
}

fn default_parameters() -> GenerateParameters {
    GenerateParameters {
115
        best_of: None,
116
117
118
119
        temperature: None,
        repetition_penalty: None,
        top_k: None,
        top_p: None,
120
        typical_p: None,
121
        do_sample: false,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
122
        max_new_tokens: default_max_new_tokens(),
123
        return_full_text: None,
124
        stop: Vec::new(),
125
        truncate: None,
126
        watermark: false,
OlivierDehaene's avatar
OlivierDehaene committed
127
        details: false,
128
        seed: None,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
129
130
131
    }
}

132
#[derive(Clone, Debug, Deserialize, ToSchema)]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
133
pub(crate) struct GenerateRequest {
134
    #[schema(example = "My name is Olivier and I")]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
135
136
137
138
139
    pub inputs: String,
    #[serde(default = "default_parameters")]
    pub parameters: GenerateParameters,
}

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#[derive(Clone, Debug, Deserialize, ToSchema)]
pub(crate) struct CompatGenerateRequest {
    #[schema(example = "My name is Olivier and I")]
    pub inputs: String,
    #[serde(default = "default_parameters")]
    pub parameters: GenerateParameters,
    #[serde(default)]
    #[allow(dead_code)]
    pub stream: bool,
}

impl From<CompatGenerateRequest> for GenerateRequest {
    fn from(req: CompatGenerateRequest) -> Self {
        Self {
            inputs: req.inputs,
            parameters: req.parameters,
        }
    }
}

160
161
162
163
164
165
#[derive(Debug, Serialize, ToSchema)]
pub struct PrefillToken {
    #[schema(example = 0)]
    id: u32,
    #[schema(example = "test")]
    text: String,
166
    #[schema(nullable = true, example = - 0.34)]
167
168
169
    logprob: f32,
}

170
171
172
173
174
175
#[derive(Debug, Serialize, ToSchema)]
pub struct Token {
    #[schema(example = 0)]
    id: u32,
    #[schema(example = "test")]
    text: String,
176
    #[schema(nullable = true, example = - 0.34)]
177
    logprob: f32,
178
179
    #[schema(example = "false")]
    special: bool,
180
181
182
183
184
185
186
187
188
189
190
191
192
}

#[derive(Serialize, ToSchema)]
#[serde(rename_all(serialize = "snake_case"))]
pub(crate) enum FinishReason {
    #[schema(rename = "length")]
    Length,
    #[serde(rename = "eos_token")]
    #[schema(rename = "eos_token")]
    EndOfSequenceToken,
    #[schema(rename = "stop_sequence")]
    StopSequence,
}
193

194
195
196
197
198
199
200
201
202
203
204
205
206
207
#[derive(Serialize, ToSchema)]
pub(crate) struct BestOfSequence {
    #[schema(example = "test")]
    pub generated_text: String,
    #[schema(example = "length")]
    pub finish_reason: FinishReason,
    #[schema(example = 1)]
    pub generated_tokens: u32,
    #[schema(nullable = true, example = 42)]
    pub seed: Option<u64>,
    pub prefill: Vec<PrefillToken>,
    pub tokens: Vec<Token>,
}

208
#[derive(Serialize, ToSchema)]
OlivierDehaene's avatar
OlivierDehaene committed
209
pub(crate) struct Details {
210
211
212
    #[schema(example = "length")]
    pub finish_reason: FinishReason,
    #[schema(example = 1)]
OlivierDehaene's avatar
OlivierDehaene committed
213
    pub generated_tokens: u32,
214
    #[schema(nullable = true, example = 42)]
215
    pub seed: Option<u64>,
216
217
    pub prefill: Vec<PrefillToken>,
    pub tokens: Vec<Token>,
218
219
    #[serde(skip_serializing_if = "Option::is_none")]
    pub best_of_sequences: Option<Vec<BestOfSequence>>,
OlivierDehaene's avatar
OlivierDehaene committed
220
221
}

222
#[derive(Serialize, ToSchema)]
223
pub(crate) struct GenerateResponse {
224
    #[schema(example = "test")]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
225
    pub generated_text: String,
OlivierDehaene's avatar
OlivierDehaene committed
226
227
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<Details>,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
228
}
229

230
231
232
233
234
235
#[derive(Serialize, ToSchema)]
pub(crate) struct StreamDetails {
    #[schema(example = "length")]
    pub finish_reason: FinishReason,
    #[schema(example = 1)]
    pub generated_tokens: u32,
236
    #[schema(nullable = true, example = 42)]
237
238
239
240
    pub seed: Option<u64>,
}

#[derive(Serialize, ToSchema)]
241
242
pub(crate) struct StreamResponse {
    pub token: Token,
243
    #[schema(nullable = true, default = "null", example = "test")]
244
    pub generated_text: Option<String>,
245
246
    #[schema(nullable = true, default = "null")]
    pub details: Option<StreamDetails>,
247
248
}

249
#[derive(Serialize, ToSchema)]
250
251
pub(crate) struct ErrorResponse {
    pub error: String,
252
    pub error_type: String,
253
}