learn: wire up /train endpoint for approved candidates
When 's' is pressed on the learn screen, approved candidates are now sent to the inference server's /train endpoint. Samples are marked as sent immediately in the UI, and mark_trained() is called after successful API response to prevent re-scoring. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
50b7b3a33a
commit
5d9d3ffc5b
2 changed files with 94 additions and 3 deletions
|
|
@ -648,3 +648,69 @@ pub fn node_timestamp_ms(node: &AstNode) -> Option<i64> {
|
||||||
}?;
|
}?;
|
||||||
Some(ts.timestamp_millis())
|
Some(ts.timestamp_millis())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Training API ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Training sample for /train endpoint.
|
||||||
|
#[derive(serde::Serialize)]
|
||||||
|
struct TrainingSample {
|
||||||
|
context_ids: Vec<u32>,
|
||||||
|
continuation_ids: Vec<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Data needed to send a training sample.
|
||||||
|
pub struct TrainData {
|
||||||
|
pub context_ids: Vec<u32>,
|
||||||
|
pub continuation_ids: Vec<u32>,
|
||||||
|
pub timestamp_ms: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Send training samples to the server.
|
||||||
|
///
|
||||||
|
/// Returns job_id on success, marks each sample as trained.
|
||||||
|
pub async fn send_to_train(
|
||||||
|
samples: Vec<TrainData>,
|
||||||
|
client: &ApiClient,
|
||||||
|
) -> anyhow::Result<String> {
|
||||||
|
if samples.is_empty() {
|
||||||
|
anyhow::bail!("no samples to train");
|
||||||
|
}
|
||||||
|
|
||||||
|
let api_samples: Vec<TrainingSample> = samples.iter()
|
||||||
|
.map(|s| TrainingSample {
|
||||||
|
context_ids: s.context_ids.clone(),
|
||||||
|
continuation_ids: s.continuation_ids.clone(),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let body = serde_json::json!({
|
||||||
|
"training_data": {
|
||||||
|
"samples": api_samples,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let http = http_client();
|
||||||
|
let url = format!("{}/train", client.base_url());
|
||||||
|
let response = http.send_json("POST", &url, &[], &body).await?;
|
||||||
|
|
||||||
|
let status = response.status();
|
||||||
|
let result: serde_json::Value = response.json().await?;
|
||||||
|
|
||||||
|
if !status.is_success() {
|
||||||
|
let msg = result.get("error").and_then(|e| e.as_str()).unwrap_or("unknown error");
|
||||||
|
anyhow::bail!("train API HTTP {}: {}", status, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark all samples as trained
|
||||||
|
for s in &samples {
|
||||||
|
mark_trained(s.timestamp_ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
let job_id = result.get("job_id")
|
||||||
|
.and_then(|j| j.as_str())
|
||||||
|
.unwrap_or("unknown")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
dbglog!("[finetune] sent {} samples, job_id={}", samples.len(), job_id);
|
||||||
|
Ok(job_id)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -165,14 +165,39 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finetune_send_approved(&mut self) {
|
fn finetune_send_approved(&mut self) {
|
||||||
// TODO: Send approved candidates to /finetune endpoint
|
// Collect approved candidates
|
||||||
// For now, just mark them as sent and record as trained
|
let samples: Vec<crate::subconscious::learn::TrainData> = self.finetune_candidates.iter()
|
||||||
|
.filter(|c| c.status == learn::CandidateStatus::Approved)
|
||||||
|
.map(|c| crate::subconscious::learn::TrainData {
|
||||||
|
context_ids: c.context_ids.clone(),
|
||||||
|
continuation_ids: c.continuation_ids.clone(),
|
||||||
|
timestamp_ms: c.timestamp_ms,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if samples.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark as sent in UI immediately
|
||||||
for candidate in &mut self.finetune_candidates {
|
for candidate in &mut self.finetune_candidates {
|
||||||
if candidate.status == learn::CandidateStatus::Approved {
|
if candidate.status == learn::CandidateStatus::Approved {
|
||||||
crate::subconscious::learn::mark_trained(candidate.timestamp_ms);
|
|
||||||
candidate.status = learn::CandidateStatus::Sent;
|
candidate.status = learn::CandidateStatus::Sent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Spawn async task to send to training server
|
||||||
|
let client = self.agent.client.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
match crate::subconscious::learn::send_to_train(samples, &client).await {
|
||||||
|
Ok(job_id) => {
|
||||||
|
dbglog!("[finetune] training started: {}", job_id);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
dbglog!("[finetune] send failed: {:#}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue