openai_protocol/
completion.rs1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use serde_json::{Map, Value};
5
6use super::common::*;
7
8#[serde_with::skip_serializing_none]
13#[derive(Debug, Clone, Deserialize, Serialize)]
14pub struct CompletionRequest {
15 pub model: String,
17
18 pub prompt: StringOrArray,
20
21 pub suffix: Option<String>,
23
24 pub max_tokens: Option<u32>,
26
27 pub temperature: Option<f32>,
29
30 pub top_p: Option<f32>,
32
33 pub n: Option<u32>,
35
36 #[serde(default)]
38 pub stream: bool,
39
40 pub stream_options: Option<StreamOptions>,
42
43 pub logprobs: Option<u32>,
45
46 #[serde(default)]
48 pub echo: bool,
49
50 pub stop: Option<StringOrArray>,
52
53 pub presence_penalty: Option<f32>,
55
56 pub frequency_penalty: Option<f32>,
58
59 pub best_of: Option<u32>,
61
62 pub logit_bias: Option<HashMap<String, f32>>,
64
65 pub user: Option<String>,
67
68 pub seed: Option<i64>,
70
71 pub top_k: Option<i32>,
74
75 pub min_p: Option<f32>,
77
78 pub min_tokens: Option<u32>,
80
81 pub repetition_penalty: Option<f32>,
83
84 pub regex: Option<String>,
86
87 pub ebnf: Option<String>,
89
90 pub json_schema: Option<String>,
92
93 pub stop_token_ids: Option<Vec<u32>>,
95
96 #[serde(default)]
98 pub no_stop_trim: bool,
99
100 #[serde(default)]
102 pub ignore_eos: bool,
103
104 #[serde(default = "default_true")]
106 pub skip_special_tokens: bool,
107
108 pub lora_path: Option<String>,
110
111 pub session_params: Option<HashMap<String, Value>>,
113
114 #[serde(default)]
116 pub return_hidden_states: bool,
117
118 pub sampling_seed: Option<u64>,
120
121 #[serde(flatten)]
123 pub other: Map<String, Value>,
124}
125
126impl GenerationRequest for CompletionRequest {
127 fn is_stream(&self) -> bool {
128 self.stream
129 }
130
131 fn get_model(&self) -> Option<&str> {
132 Some(&self.model)
133 }
134
135 fn extract_text_for_routing(&self) -> String {
136 match &self.prompt {
137 StringOrArray::String(s) => s.clone(),
138 StringOrArray::Array(v) => v.join(" "),
139 }
140 }
141}
142
143#[serde_with::skip_serializing_none]
148#[derive(Debug, Clone, Deserialize, Serialize)]
149pub struct CompletionResponse {
150 pub id: String,
151 pub object: String, pub created: u64,
153 pub model: String,
154 pub choices: Vec<CompletionChoice>,
155 pub usage: Option<Usage>,
156 pub system_fingerprint: Option<String>,
157}
158
159#[derive(Debug, Clone, Deserialize, Serialize)]
160pub struct CompletionChoice {
161 pub text: String,
162 pub index: u32,
163 #[serde(skip_serializing_if = "Option::is_none")]
164 pub logprobs: Option<LogProbs>,
165 pub finish_reason: Option<String>, #[serde(skip_serializing_if = "Option::is_none")]
168 pub matched_stop: Option<Value>, }
170
171#[derive(Debug, Clone, Deserialize, Serialize)]
172pub struct CompletionStreamResponse {
173 pub id: String,
174 pub object: String, pub created: u64,
176 pub choices: Vec<CompletionStreamChoice>,
177 pub model: String,
178 #[serde(skip_serializing_if = "Option::is_none")]
179 pub system_fingerprint: Option<String>,
180}
181
182#[derive(Debug, Clone, Deserialize, Serialize)]
183pub struct CompletionStreamChoice {
184 pub text: String,
185 pub index: u32,
186 #[serde(skip_serializing_if = "Option::is_none")]
187 pub logprobs: Option<LogProbs>,
188 pub finish_reason: Option<String>,
189}