llmservice_flows/
embeddings.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
use serde::Serialize;
use urlencoding::encode;

use crate::LLMApi;
use crate::Retry;

/// The input type for the embeddings.
///
/// For more detail about parameters, please refer to
/// [OpenAI docs](https://platform.openai.com/docs/api-reference/embeddings/create)
///
#[derive(Debug, Serialize)]
pub enum EmbeddingsInput {
    String(String),
    Vec(Vec<String>),
}

impl LLMApi for (Option<&str>, EmbeddingsInput) {
    type Output = Vec<Vec<f64>>;
    async fn api(&self, endpoint: &str, api_key: &str) -> Retry<Self::Output> {
        create_embeddings_inner(endpoint, api_key, self.0, &self.1).await
    }
}

impl<'a> crate::LLMServiceFlows<'a> {
    /// Create embeddings from the provided input.
    ///
    /// `params` is an [EmbeddingsInput] object.
    ///
    ///```rust
    ///   // This code snippet computes embeddings for `text`, the question created in previous step.
    ///   // Wrap the `text` in EmbeddingsInput struct.
    ///   let input = EmbeddingsInput::String(text.to_string());
    ///   // Call the create_embeddings function.
    ///   let question_vector = match llm.create_embeddings(Some("text-embedding-ada-002"), input).await {
    ///       Ok(r) => r[0],
    ///       Err(e) => {your error handling},
    ///   };
    /// ```

    pub async fn create_embeddings(
        &self,
        model: Option<&str>,
        input: EmbeddingsInput,
    ) -> Result<Vec<Vec<f64>>, String> {
        self.keep_trying((model, input)).await
    }
}

async fn create_embeddings_inner(
    endpoint: &str,
    api_key: &str,
    model: Option<&str>,
    input: &EmbeddingsInput,
) -> Retry<Vec<Vec<f64>>> {
    let flows_user = unsafe { crate::_get_flows_user() };

    let uri = format!(
        "{}/{}/create_embeddings?endpoint={}&api_key={}&model={}",
        crate::LLM_API_PREFIX.as_str(),
        flows_user,
        encode(endpoint),
        encode(api_key),
        encode(model.unwrap_or_default())
    );
    let body = match input {
        EmbeddingsInput::String(ref s) => serde_json::to_vec(&s).unwrap_or_default(),
        EmbeddingsInput::Vec(ref v) => serde_json::to_vec(&v).unwrap_or_default(),
    };
    match reqwest::Client::new()
        .post(uri)
        .header("Content-Type", "application/json")
        .header("Content-Length", body.len())
        .body(body)
        .send()
        .await
    {
        Ok(res) => {
            let status = res.status();
            let body = res.bytes().await.unwrap();
            match status.is_success() {
                true => Retry::No(
                    serde_json::from_slice::<Vec<Vec<f64>>>(&body.as_ref())
                        .or(Err(String::from("Unexpected error"))),
                ),
                false => {
                    match status.into() {
                        409 | 429 | 503 => {
                            // 409 TryAgain 429 RateLimitError
                            // 503 ServiceUnavailable
                            Retry::Yes(String::from_utf8_lossy(&body.as_ref()).into_owned())
                        }
                        _ => Retry::No(Err(String::from_utf8_lossy(&body.as_ref()).into_owned())),
                    }
                }
            }
        }
        Err(e) => Retry::No(Err(e.to_string())),
    }
}