hf_text-generation-inference/router/src/lib.rs

111 lines
2.5 KiB
Rust
Raw Normal View History

/// Text Generation Inference Webserver
mod infer;
mod queue;
2022-10-17 10:27:33 -06:00
pub mod server;
2022-10-18 07:19:03 -06:00
mod validation;
2022-10-17 06:59:00 -06:00
use infer::Infer;
use queue::{Entry, Queue};
2022-10-18 07:19:03 -06:00
use serde::{Deserialize, Serialize};
2022-10-17 06:59:00 -06:00
use validation::Validation;
2022-10-18 07:19:03 -06:00
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct GenerateParameters {
#[serde(default = "default_temperature")]
pub temperature: f32,
#[serde(default = "default_repetition_penalty")]
pub repetition_penalty: f32,
2022-10-18 07:19:03 -06:00
#[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,
2022-12-15 09:03:56 -07:00
#[serde(default)]
2022-12-12 10:25:22 -07:00
pub stop: Vec<String>,
2022-12-15 09:03:56 -07:00
#[serde(default)]
pub details: bool,
#[serde(default)]
pub seed: Option<u64>,
2022-10-18 07:19:03 -06:00
}
fn default_temperature() -> f32 {
1.0
}
fn default_repetition_penalty() -> f32 {
1.0
}
2022-10-18 07:19:03 -06:00
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(),
repetition_penalty: default_repetition_penalty(),
2022-10-18 07:19:03 -06:00
top_k: default_top_k(),
top_p: default_top_p(),
do_sample: default_do_sample(),
max_new_tokens: default_max_new_tokens(),
2022-12-12 10:25:22 -07:00
stop: vec![],
2022-12-15 09:03:56 -07:00
details: false,
seed: None,
2022-10-18 07:19:03 -06:00
}
}
#[derive(Clone, Debug, Deserialize)]
pub(crate) struct GenerateRequest {
pub inputs: String,
#[serde(default = "default_parameters")]
pub parameters: GenerateParameters,
}
#[derive(Debug, Serialize)]
pub struct Token(u32, String, f32);
2022-12-15 09:03:56 -07:00
#[derive(Serialize)]
pub(crate) struct Details {
pub finish_reason: String,
pub generated_tokens: u32,
pub seed: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prefill: Option<Vec<Token>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tokens: Option<Vec<Token>>,
2022-12-15 09:03:56 -07:00
}
2022-10-18 07:19:03 -06:00
#[derive(Serialize)]
pub(crate) struct GenerateResponse {
2022-10-18 07:19:03 -06:00
pub generated_text: String,
2022-12-15 09:03:56 -07:00
#[serde(skip_serializing_if = "Option::is_none")]
pub details: Option<Details>,
2022-10-18 07:19:03 -06:00
}
2022-10-27 06:25:29 -06:00
#[derive(Serialize)]
pub(crate) struct StreamResponse {
pub token: Token,
pub generated_text: Option<String>,
pub details: Option<Details>,
}
2022-10-27 06:25:29 -06:00
#[derive(Serialize)]
pub(crate) struct ErrorResponse {
pub error: String,
}