aleph_alpha_client/
logprobs.rs

1use std::str::{self, Utf8Error};
2
3use serde::Deserialize;
4
5#[derive(Clone, Copy)]
6pub enum Logprobs {
7    /// Do not return any logprobs
8    No,
9    /// Return only the logprob of the tokens which have actually been sampled into the completion.
10    Sampled,
11    /// Request between 0 and 20 tokens
12    Top(u8),
13}
14
15#[derive(Deserialize, Debug, PartialEq)]
16pub struct Logprob {
17    // The API returns both a UTF-8 String token and bytes as an array of numbers. We only
18    // deserialize bytes as it is the better source of truth.
19    /// Binary represtantation of the token, usually these bytes are UTF-8.
20    #[serde(rename = "bytes")]
21    pub token: Vec<u8>,
22    pub logprob: f64,
23}
24
25impl Logprob {
26    pub fn token_as_str(&self) -> Result<&str, Utf8Error> {
27        str::from_utf8(&self.token)
28    }
29}