use reqwest::ClientBuilder; use serde::{Deserialize, Serialize}; use std::time::Duration;
#[derive(Serialize, Debug)]
struct Request {
model: String,
prompt: String,
stream: bool,
options: Options,
}
#[derive(Serialize, Debug)]
struct Options {
temperature: f64,
}
#[derive(Deserialize, Debug)]
#[allow(dead_code)] struct Response {
model: String,
created_at: String,
response: String,
done: bool,
}
pub async fn send_request(
model: &String,
prompt: &String,
timeout_in_sec: u64,
) -> Result<String, Box<dyn std::error::Error>> {
let request = Request {
model: model.clone(),
prompt: prompt.clone(),
stream: false,
options: Options { temperature: 0.75 },
};
let client = ClientBuilder::new()
.timeout(Duration::from_secs(timeout_in_sec))
.build()?;
let reply = client
.post("http://localhost:11434/api/generate")
.json(&request)
.send()
.await?
.json::<Response>()
.await?;
Ok(reply.response)
}