aleph_alpha_client/completion.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::{http::Task, Distribution, Logprob, Logprobs, Prompt, StreamTask, Usage};
/// Completes a prompt. E.g. continues a text.
pub struct TaskCompletion<'a> {
/// The prompt (usually text) to be completed. Unconditional completion can be started with an
/// empty string. The prompt may contain a zero shot or few shot task.
pub prompt: Prompt<'a>,
/// Controls in which circumstances the model will stop generating new tokens.
pub stopping: Stopping<'a>,
/// Sampling controls how the tokens ("words") are selected for the completion.
pub sampling: Sampling,
/// Whether to include special tokens (e.g. <|endoftext|>, <|python_tag|>) in the completion.
pub special_tokens: bool,
/// Wether you are interessted in the probabilities of the sampled tokens, or most likely
/// tokens.
pub logprobs: Logprobs,
}
impl<'a> TaskCompletion<'a> {
/// Convenience constructor leaving most setting to default, just completing a given text
pub fn from_text(text: &'a str) -> Self {
TaskCompletion {
prompt: Prompt::from_text(text),
stopping: Stopping::NO_TOKEN_LIMIT,
sampling: Sampling::MOST_LIKELY,
special_tokens: false,
logprobs: Logprobs::No,
}
}
pub fn with_maximum_tokens(mut self, maximum_tokens: u32) -> Self {
self.stopping.maximum_tokens = Some(maximum_tokens);
self
}
pub fn with_stop_sequences(mut self, stop_sequences: &'a [&str]) -> Self {
self.stopping.stop_sequences = stop_sequences;
self
}
/// Include special tokens (e.g. <|endoftext|>, <|python_tag|>) in the completion.
pub fn with_special_tokens(mut self) -> Self {
self.special_tokens = true;
self
}
pub fn with_logprobs(mut self, logprobs: Logprobs) -> Self {
self.logprobs = logprobs;
self
}
}
/// Sampling controls how the tokens ("words") are selected for the completion.
pub struct Sampling {
/// A temperature encourages the model to produce less probable outputs ("be more creative").
/// Values are expected to be between 0 and 1. Try high values for a more random ("creative")
/// response.
pub temperature: Option<f64>,
/// Introduces random sampling for generated tokens by randomly selecting the next token from
/// the k most likely options. A value larger than 1 encourages the model to be more creative.
/// Set to 0 to get the same behaviour as `None`.
pub top_k: Option<u32>,
/// Introduces random sampling for generated tokens by randomly selecting the next token from
/// the smallest possible set of tokens whose cumulative probability exceeds the probability
/// top_p. Set to 0 to get the same behaviour as `None`.
pub top_p: Option<f64>,
/// When specified, this number will decrease (or increase) the likelihood of repeating tokens
/// that were mentioned prior in the completion. The penalty is cumulative. The more a token
/// is mentioned in the completion, the more its probability will decrease.
/// A negative value will increase the likelihood of repeating tokens.
pub frequency_penalty: Option<f64>,
/// The presence penalty reduces the likelihood of generating tokens that are already present
/// in the generated text (repetition_penalties_include_completion=true) respectively the
/// prompt (repetition_penalties_include_prompt=true). Presence penalty is independent of the
/// number of occurrences. Increase the value to reduce the likelihood of repeating text.
/// An operation like the following is applied:
///
/// logits[t] -> logits[t] - 1 * penalty
///
/// where logits[t] is the logits for any given token. Note that the formula is independent
/// of the number of times that a token appears.
pub presence_penalty: Option<f64>,
}
impl Sampling {
/// Always chooses the token most likely to come next. Choose this if you do want close to
/// deterministic behaviour and do not want to apply any penalties to avoid repetitions.
pub const MOST_LIKELY: Self = Sampling {
temperature: None,
top_k: None,
top_p: None,
frequency_penalty: None,
presence_penalty: None,
};
}
impl Default for Sampling {
fn default() -> Self {
Self::MOST_LIKELY
}
}
/// Controls the conditions under which the language models stops generating text.
pub struct Stopping<'a> {
/// The maximum number of tokens to be generated. Completion will terminate after the maximum
/// number of tokens is reached. Increase this value to allow for longer outputs. A text is split
/// into tokens. Usually there are more tokens than words. The total number of tokens of prompt
/// and maximum_tokens depends on the model.
/// If maximum tokens is set to None, no outside limit is opposed on the number of maximum tokens.
/// The model will generate tokens until it generates one of the specified stop_sequences or it
/// reaches its technical limit, which usually is its context window.
pub maximum_tokens: Option<u32>,
/// List of strings which will stop generation if they are generated. Stop sequences are
/// helpful in structured texts. E.g.: In a question answering scenario a text may consist of
/// lines starting with either "Question: " or "Answer: " (alternating). After producing an
/// answer, the model will be likely to generate "Question: ". "Question: " may therefore be used
/// as stop sequence in order not to have the model generate more questions but rather restrict
/// text generation to the answers.
pub stop_sequences: &'a [&'a str],
}
impl<'a> Stopping<'a> {
/// Only stop once the model reaches its technical limit, usually the context window.
pub const NO_TOKEN_LIMIT: Self = Stopping {
maximum_tokens: None,
stop_sequences: &[],
};
/// Stop once the model has reached maximum_tokens.
pub fn from_maximum_tokens(maximum_tokens: u32) -> Self {
Self {
maximum_tokens: Some(maximum_tokens),
stop_sequences: &[],
}
}
pub fn from_stop_sequences(stop_sequences: &'a [&'a str]) -> Self {
Self {
maximum_tokens: None,
stop_sequences,
}
}
}
impl Default for Stopping<'_> {
fn default() -> Self {
Self::NO_TOKEN_LIMIT
}
}
/// Body send to the Aleph Alpha API on the POST `/completion` Route
#[derive(Serialize, Debug)]
struct BodyCompletion<'a> {
/// Name of the model tasked with completing the prompt. E.g. `luminous-base"`.
pub model: &'a str,
/// Prompt to complete. The modalities supported depend on `model`.
pub prompt: Prompt<'a>,
/// Limits the number of tokens, which are generated for the completion.
#[serde(skip_serializing_if = "Option::is_none")]
pub maximum_tokens: Option<u32>,
/// List of strings which will stop generation if they are generated. Stop sequences are
/// helpful in structured texts. E.g.: In a question answering scenario a text may consist of
/// lines starting with either "Question: " or "Answer: " (alternating). After producing an
/// answer, the model will be likely to generate "Question: ". "Question: " may therefore be used
/// as stop sequence in order not to have the model generate more questions but rather restrict
/// text generation to the answers.
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub stop_sequences: &'a [&'a str],
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_k: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f64>,
/// If true, the response will be streamed.
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub stream: bool,
/// Forces the raw completion of the model to be returned.
/// For some models, the completion that was generated by the model may be optimized and
/// returned in the completion field of the CompletionResponse.
/// The raw completion, if returned, will contain the un-optimized completion.
/// Setting tokens to true or log_probs to any value will also trigger the raw completion to be returned.
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub raw_completion: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub frequency_penalty: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub presence_penalty: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub log_probs: Option<u8>,
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub tokens: bool,
}
impl<'a> BodyCompletion<'a> {
pub fn new(model: &'a str, task: &'a TaskCompletion<'a>) -> Self {
let TaskCompletion {
prompt,
stopping,
sampling,
special_tokens,
logprobs,
} = task;
Self {
model,
prompt: prompt.borrow(),
maximum_tokens: stopping.maximum_tokens,
stop_sequences: stopping.stop_sequences,
temperature: sampling.temperature,
top_k: sampling.top_k,
top_p: sampling.top_p,
stream: false,
raw_completion: *special_tokens,
frequency_penalty: sampling.frequency_penalty,
presence_penalty: sampling.presence_penalty,
log_probs: logprobs.to_logprobs_num(),
tokens: logprobs.to_tokens(),
}
}
pub fn with_streaming(mut self) -> Self {
self.stream = true;
self
}
}
#[derive(Deserialize, Debug, PartialEq)]
pub struct ResponseCompletion {
model_version: String,
completions: Vec<DeserializedCompletion>,
num_tokens_prompt_total: u32,
num_tokens_generated: u32,
}
#[derive(Deserialize, Debug, PartialEq)]
struct DeserializedCompletion {
completion: String,
finish_reason: String,
raw_completion: Option<String>,
#[serde(default)]
log_probs: Vec<HashMap<String, f64>>,
#[serde(default)]
completion_tokens: Vec<String>,
}
/// Completion and metainformation returned by a completion task
#[derive(Deserialize, Debug, PartialEq)]
pub struct CompletionOutput {
pub completion: String,
pub finish_reason: String,
pub logprobs: Vec<Distribution>,
pub usage: Usage,
}
impl Task for TaskCompletion<'_> {
type Output = CompletionOutput;
type ResponseBody = ResponseCompletion;
fn build_request(
&self,
client: &reqwest::Client,
base: &str,
model: &str,
) -> reqwest::RequestBuilder {
let body = BodyCompletion::new(model, self);
client.post(format!("{base}/complete")).json(&body)
}
fn body_to_output(&self, mut response: Self::ResponseBody) -> Self::Output {
// We expect the API to return exactly one completion, despite them being modled as an array
let DeserializedCompletion {
completion,
finish_reason,
raw_completion,
log_probs,
completion_tokens,
} = response.completions.pop().unwrap();
let completion = if self.special_tokens {
raw_completion.unwrap()
} else {
completion
};
CompletionOutput {
completion,
finish_reason,
logprobs: completion_logprobs_to_canonical(
log_probs,
completion_tokens,
self.logprobs.top_logprobs().unwrap_or_default(),
),
usage: Usage {
prompt_tokens: response.num_tokens_prompt_total,
completion_tokens: response.num_tokens_generated,
},
}
}
}
fn completion_logprobs_to_canonical(
log_probs: Vec<HashMap<String, f64>>,
completion_tokens: Vec<String>,
num_expected_top_logprobs: u8,
) -> Vec<Distribution> {
let mut logprobs = Vec::new();
for (token, map) in completion_tokens.into_iter().zip(log_probs) {
let logprob = *map.get(&token).unwrap_or(&f64::NAN);
let mut top_logprobs = map
.into_iter()
.map(|(token, logprob)| Logprob {
token: token.into_bytes(),
logprob,
})
.collect::<Vec<_>>();
// We want to make sure the most likely tokens are first in the array
top_logprobs.sort_by(|a, b| b.logprob.total_cmp(&a.logprob));
// The aa api always makes the sampled token part of the array, even if not in the top n
// elements. Since we translate into a representation with the sampled token separate, we
// can keep the top n elements constant. In case the sampled token has not been in the top
// n, the below line will shorten the array by one.
top_logprobs.resize_with(num_expected_top_logprobs as usize, || {
unreachable!("Vec should only shorten")
});
logprobs.push(Distribution {
sampled: Logprob {
token: token.into_bytes(),
logprob,
},
top: top_logprobs,
});
}
logprobs
}
/// Describes a chunk of a completion stream
#[derive(Deserialize, Debug)]
pub struct StreamChunk {
/// The index of the stream that this chunk belongs to.
/// This is relevant if multiple completion streams are requested (see parameter n).
pub index: u32,
/// The completion of the stream.
pub completion: String,
}
/// Denotes the end of a completion stream.
///
/// The index of the stream that is being terminated is not deserialized.
/// It is only relevant if multiple completion streams are requested, (see parameter n),
/// which is not supported by this crate yet.
#[derive(Deserialize)]
pub struct StreamSummary {
/// Model name and version (if any) of the used model for inference.
pub model_version: String,
/// The reason why the model stopped generating new tokens.
pub finish_reason: String,
}
/// Denotes the end of all completion streams.
#[derive(Deserialize)]
pub struct CompletionSummary {
/// Number of tokens combined across all completion tasks.
/// In particular, if you set best_of or n to a number larger than 1 then we report the
/// combined prompt token count for all best_of or n tasks.
pub num_tokens_prompt_total: u32,
/// Number of tokens combined across all completion tasks.
/// If multiple completions are returned or best_of is set to a value greater than 1 then
/// this value contains the combined generated token count.
pub num_tokens_generated: u32,
}
#[derive(Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum CompletionEvent {
StreamChunk(StreamChunk),
StreamSummary(StreamSummary),
CompletionSummary(CompletionSummary),
}
impl StreamTask for TaskCompletion<'_> {
type Output = CompletionEvent;
type ResponseBody = CompletionEvent;
fn build_request(
&self,
client: &reqwest::Client,
base: &str,
model: &str,
) -> reqwest::RequestBuilder {
let body = BodyCompletion::new(model, self).with_streaming();
client.post(format!("{base}/complete")).json(&body)
}
fn body_to_output(response: Self::ResponseBody) -> Self::Output {
response
}
}
impl Logprobs {
/// Convert into a number for completion endpoint
fn to_logprobs_num(self) -> Option<u8> {
match self {
Logprobs::No => None,
Logprobs::Sampled => Some(0),
Logprobs::Top(n) => Some(n),
}
}
/// Wether or not we want to return the completion tokens
fn to_tokens(self) -> bool {
match self {
Logprobs::No => false,
Logprobs::Sampled | Logprobs::Top(_) => true,
}
}
}