use serde::{Serialize, Deserialize};
use reqwest::Client;
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug)]
pub struct ResponseValue {
pub format: String,
pub value: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct FloomResponse {
#[serde(rename = "messageId")]
pub message_id: String,
#[serde(rename = "chatId")]
pub chat_id: String,
pub values: Vec<ResponseValue>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct FloomRequest {
#[serde(rename = "pipelineId")]
pub pipeline_id: String,
#[serde(rename = "chatId")]
pub chat_id: String,
pub input: String,
pub variables: Option<HashMap<String, String>>,
}
pub struct FloomClient {
url: String,
api_key: String,
client: Client,
}
impl FloomClient {
pub fn new(endpoint: String, api_key: String) -> FloomClient {
FloomClient {
url: endpoint,
api_key,
client: Client::new(),
}
}
pub async fn run(&self, pipeline_id: String, chat_id: String, input: String, variables: Option<HashMap<String, String>>) -> Result<FloomResponse, reqwest::Error> {
let floom_request = FloomRequest {
pipeline_id,
chat_id,
input,
variables
};
let response = self.client.post(format!("{}/v1/Pipelines/Run", self.url))
.json(&floom_request)
.header("Api-Key", &self.api_key)
.send()
.await?;
if response.status().is_success() {
let floom_response = response.json::<FloomResponse>().await?;
Ok(floom_response)
} else {
Err(response.error_for_status().unwrap_err())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::test as tokio_test;
#[tokio_test]
async fn test_floom_client() {
let floom_client = FloomClient::new("http://127.0.0.1:80".to_string(), "COqRR8qLz4RrXygsDoYMXRvDJheXj3MO".to_string());
let pipeline_id = "docs-pipeline-v1".to_string();
let chat_id = "abcdefghijklmnop".to_string();
let input = "Who was the first US president?".to_string();
match floom_client.run(pipeline_id, chat_id, input, None).await {
Ok(response) => {
println!("Pipeline Response Valid");
println!("Message ID: {}", response.message_id);
println!("Chat ID: {}", response.chat_id);
for value in response.values {
println!("Value - Format: {}, Value: {}", value.format, value.value);
}
},
Err(e) => println!("Error: {:?}", e),
}
}
}