lib.rs 4.66 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
#[derive(Clone, Debug, Deserialize, ToSchema)]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
14
pub(crate) struct GenerateParameters {
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    #[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>,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
43
    #[serde(default = "default_do_sample")]
44
    #[schema(default = "false", example = true)]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
45
46
    pub do_sample: bool,
    #[serde(default = "default_max_new_tokens")]
47
    #[schema(exclusive_minimum = 0, exclusive_maximum = 512, default = "20")]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
48
    pub max_new_tokens: u32,
OlivierDehaene's avatar
OlivierDehaene committed
49
    #[serde(default)]
50
    #[schema(inline, max_items = 4, example = json ! (["photographer"]))]
51
    pub stop: Vec<String>,
OlivierDehaene's avatar
OlivierDehaene committed
52
    #[serde(default)]
53
    #[schema(default = "true")]
OlivierDehaene's avatar
OlivierDehaene committed
54
    pub details: bool,
55
56
    #[serde(default)]
    pub seed: Option<u64>,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
57
58
59
60
61
62
63
64
65
66
67
68
}

fn default_do_sample() -> bool {
    false
}

fn default_max_new_tokens() -> u32 {
    20
}

fn default_parameters() -> GenerateParameters {
    GenerateParameters {
69
70
71
72
        temperature: None,
        repetition_penalty: None,
        top_k: None,
        top_p: None,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
73
74
        do_sample: default_do_sample(),
        max_new_tokens: default_max_new_tokens(),
75
        stop: vec![],
OlivierDehaene's avatar
OlivierDehaene committed
76
        details: false,
77
        seed: None,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
78
79
80
    }
}

81
#[derive(Clone, Debug, Deserialize, ToSchema)]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
82
pub(crate) struct GenerateRequest {
83
    #[schema(example = "My name is Olivier and I")]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
84
85
86
87
88
    pub inputs: String,
    #[serde(default = "default_parameters")]
    pub parameters: GenerateParameters,
}

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#[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,
        }
    }
}

109
110
111
112
113
114
#[derive(Debug, Serialize, ToSchema)]
pub struct PrefillToken {
    #[schema(example = 0)]
    id: u32,
    #[schema(example = "test")]
    text: String,
115
    #[schema(nullable = true, example = - 0.34)]
116
117
118
    logprob: f32,
}

119
120
121
122
123
124
#[derive(Debug, Serialize, ToSchema)]
pub struct Token {
    #[schema(example = 0)]
    id: u32,
    #[schema(example = "test")]
    text: String,
125
    #[schema(nullable = true, example = - 0.34)]
126
    logprob: f32,
127
128
    #[schema(example = "false")]
    special: bool,
129
130
131
132
133
134
135
136
137
138
139
140
141
}

#[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,
}
142

143
#[derive(Serialize, ToSchema)]
OlivierDehaene's avatar
OlivierDehaene committed
144
pub(crate) struct Details {
145
146
147
    #[schema(example = "length")]
    pub finish_reason: FinishReason,
    #[schema(example = 1)]
OlivierDehaene's avatar
OlivierDehaene committed
148
    pub generated_tokens: u32,
149
    #[schema(example = 42)]
150
    pub seed: Option<u64>,
151
    pub prefill: Option<Vec<PrefillToken>>,
152
    pub tokens: Option<Vec<Token>>,
OlivierDehaene's avatar
OlivierDehaene committed
153
154
}

155
#[derive(Serialize, ToSchema)]
156
pub(crate) struct GenerateResponse {
157
    #[schema(example = "test")]
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
158
    pub generated_text: String,
OlivierDehaene's avatar
OlivierDehaene committed
159
160
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<Details>,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
161
}
162

163
164
165
166
167
168
169
170
171
172
173
#[derive(Serialize, ToSchema)]
pub(crate) struct StreamDetails {
    #[schema(example = "length")]
    pub finish_reason: FinishReason,
    #[schema(example = 1)]
    pub generated_tokens: u32,
    #[schema(example = 42)]
    pub seed: Option<u64>,
}

#[derive(Serialize, ToSchema)]
174
175
pub(crate) struct StreamResponse {
    pub token: Token,
176
    #[schema(nullable = true, default = "null", example = "test")]
177
    pub generated_text: Option<String>,
178
179
    #[schema(nullable = true, default = "null")]
    pub details: Option<StreamDetails>,
180
181
}

182
#[derive(Serialize, ToSchema)]
183
pub(crate) struct ErrorResponse {
184
    #[schema(inline)]
185
186
    pub error: String,
}