use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::error::Error;
#[derive(Serialize)]
struct GenerateRequest<'a> {
apikey: &'a str,
prompt: &'a str,
model: &'a str,
stream: bool,
}
#[derive(Deserialize)]
struct GenerateResponse {
}
#[derive(Deserialize)]
struct HealthResponse {
status: String,
timestamp: String,
}
pub struct ApiMyLlama {
client: Client,
ip: String,
port: u16,
}
impl ApiMyLlama {
pub fn new(ip: String, port: u16) -> Self {
Self {
client: Client::new(),
ip,
port,
}
}
pub async fn generate(
&self,
apikey: &str,
prompt: &str,
model: &str,
stream: bool,
) -> Result<GenerateResponse, Box<dyn Error>> {
let url = format!("http://{}:{}/generate", self.ip, self.port);
let request = GenerateRequest {
apikey,
prompt,
model,
stream,
};
let response = self.client.post(&url).json(&request).send().await?;
let response_json = response.json::<GenerateResponse>().await?;
Ok(response_json)
}
pub async fn get_health(&self, apikey: &str) -> Result<HealthResponse, Box<dyn Error>> {
let url = format!("http://{}:{}/health?apikey={}", self.ip, self.port, apikey);
let response = self.client.get(&url).send().await?;
let response_json = response.json::<HealthResponse>().await?;
Ok(response_json)
}
}