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
use serde_json::json;

pub use crate::api::{API, Strings, StringsBatch};

/// Summary definition
pub struct Summary {
    api: API
}

/// Summary implementation
impl Summary {
    /// Creates a Summary instance.
    ///
    /// # Arguments
    /// * `url` - base url of txtai API
    pub fn new(url: &str) -> Summary {
        Summary {
            api: API::new(url)
        }
    }

    /// Runs a summarization model against a block of text.
    /// 
    /// # Arguments
    /// * `text` text to summarize
    /// * `minlength` minimum length for summary
    /// * `maxlength` maximum length for summary
    pub async fn summary(&self, text: &str, minlength: Option<i32>, maxlength: Option<i32>) -> Strings {
        // Query parameters
        let mut params = vec![("text", text)];

        let minl = minlength.unwrap_or(-1).to_string();
        let maxl = maxlength.unwrap_or(-1).to_string();

        if minl != "-1" {
            params.push(("minlength", &minl));
        }
        if maxl != "-1" {
            params.push(("maxlength", &maxl));
        }

        // Execute API call
        Ok(self.api.get("summary", &params).await?.json().await?)
    }

    /// Runs a summarization model against a block of text.
    /// 
    /// # Arguments
    /// * `texts` list of text to summarize
    /// * `minlength` minimum length for summary
    /// * `maxlength` maximum length for summary
    pub async fn batchsummary(&self, texts: &Vec<&str>, minlength: Option<i32>, maxlength: Option<i32>) -> StringsBatch {
        // Post parameters
        let params = json!({
            "text": texts,
            "minlength": minlength,
            "maxlength": maxlength
        });

        // Execute API call
        Ok(self.api.post("batchsummary", &params).await?.json().await?)
    }
}