aleph_alpha_client/
detokenization.rs

1use crate::Task;
2use serde::{Deserialize, Serialize};
3
4/// Input for a [crate::Client::detokenize] request.
5pub struct TaskDetokenization<'a> {
6    /// List of token ids which should be detokenized into text.
7    pub token_ids: &'a [u32],
8}
9
10/// Body send to the Aleph Alpha API on the POST `/detokenize` route
11#[derive(Serialize, Debug)]
12struct BodyDetokenization<'a> {
13    /// Name of the model tasked with completing the prompt. E.g. `luminous-base"`.
14    pub model: &'a str,
15    /// List of ids to detokenize.
16    pub token_ids: &'a [u32],
17}
18
19#[derive(Deserialize, Debug, PartialEq, Eq)]
20pub struct ResponseDetokenization {
21    pub result: String,
22}
23
24#[derive(Debug, PartialEq, Eq)]
25pub struct DetokenizationOutput {
26    pub result: String,
27}
28
29impl From<ResponseDetokenization> for DetokenizationOutput {
30    fn from(response: ResponseDetokenization) -> Self {
31        Self {
32            result: response.result,
33        }
34    }
35}
36
37impl Task for TaskDetokenization<'_> {
38    type Output = DetokenizationOutput;
39    type ResponseBody = ResponseDetokenization;
40
41    fn build_request(
42        &self,
43        client: &reqwest::Client,
44        base: &str,
45        model: &str,
46    ) -> reqwest::RequestBuilder {
47        let body = BodyDetokenization {
48            model,
49            token_ids: self.token_ids,
50        };
51        client.post(format!("{base}/detokenize")).json(&body)
52    }
53
54    fn body_to_output(&self, response: Self::ResponseBody) -> Self::Output {
55        DetokenizationOutput::from(response)
56    }
57}