lib.rs 2.25 KB
Newer Older
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
1
/// Text Generation Inference Webserver
Olivier Dehaene's avatar
Olivier Dehaene committed
2
mod db;
3
mod infer;
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

Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
7
use db::{Db, Entry};
8
use infer::Infer;
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
9
use serde::{Deserialize, Serialize};
Olivier Dehaene's avatar
Olivier Dehaene committed
10
use validation::Validation;
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
11
12
13
14
15
16
17
18
19
20
21
22
23

#[derive(Clone, Debug, Deserialize)]
pub(crate) struct GenerateParameters {
    #[serde(default = "default_temperature")]
    pub temperature: f32,
    #[serde(default = "default_top_k")]
    pub top_k: i32,
    #[serde(default = "default_top_p")]
    pub top_p: f32,
    #[serde(default = "default_do_sample")]
    pub do_sample: bool,
    #[serde(default = "default_max_new_tokens")]
    pub max_new_tokens: u32,
OlivierDehaene's avatar
OlivierDehaene committed
24
    #[serde(default)]
25
    pub stop: Vec<String>,
OlivierDehaene's avatar
OlivierDehaene committed
26
27
    #[serde(default)]
    pub details: bool,
28
29
    #[serde(default)]
    pub seed: Option<u64>,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
}

fn default_temperature() -> f32 {
    1.0
}

fn default_top_k() -> i32 {
    0
}

fn default_top_p() -> f32 {
    1.0
}

fn default_do_sample() -> bool {
    false
}

fn default_max_new_tokens() -> u32 {
    20
}

fn default_parameters() -> GenerateParameters {
    GenerateParameters {
        temperature: default_temperature(),
        top_k: default_top_k(),
        top_p: default_top_p(),
        do_sample: default_do_sample(),
        max_new_tokens: default_max_new_tokens(),
59
        stop: vec![],
OlivierDehaene's avatar
OlivierDehaene committed
60
        details: false,
61
        seed: None,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
62
63
64
65
66
67
68
69
70
71
    }
}

#[derive(Clone, Debug, Deserialize)]
pub(crate) struct GenerateRequest {
    pub inputs: String,
    #[serde(default = "default_parameters")]
    pub parameters: GenerateParameters,
}

72
73
74
#[derive(Debug, Serialize)]
pub struct Token(u32, String, f32);

OlivierDehaene's avatar
OlivierDehaene committed
75
76
77
78
#[derive(Serialize)]
pub(crate) struct Details {
    pub finish_reason: String,
    pub generated_tokens: u32,
79
    pub seed: Option<u64>,
80
81
82
83
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prefill: Option<Vec<Token>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tokens: Option<Vec<Token>>,
OlivierDehaene's avatar
OlivierDehaene committed
84
85
}

Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
86
#[derive(Serialize)]
87
pub(crate) struct GenerateResponse {
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
88
    pub generated_text: String,
OlivierDehaene's avatar
OlivierDehaene committed
89
90
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<Details>,
Olivier Dehaene's avatar
v0.1.0  
Olivier Dehaene committed
91
}
92

93
94
95
96
97
98
99
#[derive(Serialize)]
pub(crate) struct StreamResponse {
    pub token: Token,
    pub generated_text: Option<String>,
    pub details: Option<Details>,
}

100
101
102
103
#[derive(Serialize)]
pub(crate) struct ErrorResponse {
    pub error: String,
}