llmservice_flows/
embeddings.rsuse serde::Serialize;
use urlencoding::encode;
use crate::LLMApi;
use crate::Retry;
#[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> {
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 => {
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())),
}
}