Adds pull method and example

This commit is contained in:
2025-12-28 12:18:35 +00:00
parent 65ea1dcfec
commit ac480881e4
6 changed files with 111 additions and 5 deletions

View File

@@ -13,6 +13,7 @@ use crate::{
chat::{ChatRequest, ChatResponse},
generate::{GenerateRequest, GenerateResponse},
ps::RunningModel,
pull::{PullRequest, PullResponse},
tags::Model,
},
};
@@ -154,4 +155,41 @@ impl OllamaClient {
}
})
}
pub async fn pull(
&self,
request: PullRequest,
) -> impl Stream<Item = OllamaResult<PullResponse>> {
let request_address = format!("{}/api/pull", self.server_address);
let client = reqwest::Client::new();
Box::pin(stream! {
let response = client
.post(request_address)
.json(&request)
.send()
.await
.map_err(|e| OllamaError::from(e))?; // Adjust based on your error type
let bytes_stream = response.bytes_stream();
let body_reader = StreamReader::new(
bytes_stream.map(|res| res.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))),
);
let mut lines_stream = FramedRead::new(body_reader, LinesCodec::new());
while let Some(line_result) = lines_stream.next().await {
match line_result {
Ok(line_content) => {
println!("{line_content}");
if let Ok(parsed) = serde_json::from_str::<PullResponse>(&line_content) {
yield Ok(parsed);
}
}
Err(e) => yield Err(OllamaError::from(e)),
}
}
})
}
}