feat: add load endpoint

This commit is contained in:
2026-04-10 12:29:34 +02:00
parent db14d816cb
commit 1a06577fa3
4 changed files with 114 additions and 6 deletions
+56
View File
@@ -43,6 +43,29 @@ impl OllamaProvider {
Ok(())
}
fn parse_keep_alive(s: &str) -> Result<(), OllamaError> {
let s = s.trim();
// Ollama also accepts plain integers (seconds) or "-1" (load forever)
if s == "-1" || s.parse::<u64>().is_ok() {
return Ok(());
}
// Otherwise expect: <number><unit> e.g. "10m", "2h", "30s"
let (num, unit) = s
.find(|c: char| c.is_alphabetic())
.map(|i| s.split_at(i))
.ok_or_else(|| OllamaError::InvalidKeepAlive(s.to_string()))?;
num.parse::<u64>()
.map_err(|_| OllamaError::InvalidKeepAlive(s.to_string()))?;
match unit {
"s" | "m" | "h" => Ok(()),
_ => Err(OllamaError::InvalidKeepAlive(s.to_string())),
}
}
// ── public endpoints ─────────────────────────────────────────────────────
pub async fn list_models(&self) -> Result<Value, OllamaError> {
@@ -51,6 +74,39 @@ impl OllamaProvider {
Ok(res)
}
pub async fn load_model(
&self,
model: &str,
keep_alive: Option<&str>,
) -> Result<Value, OllamaError> {
self.validate_model(model).await?;
let keep_alive = keep_alive.unwrap_or("5m");
Self::parse_keep_alive(keep_alive)?; // ← validated before any network call
let payload = json!({
"model": model,
"prompt": "",
"keep_alive": keep_alive,
"stream": false,
});
let res = self
.client
.post(format!("{}/api/generate", self.base_url))
.json(&payload)
.send()
.await?
.json::<Value>()
.await?;
Ok(json!({
"model": res.get("model"),
"status": "loaded",
"keep_alive": keep_alive,
}))
}
pub async fn completions(&self, body: Value) -> Result<Value, OllamaError> {
let prompt = body
.get("prompt")