fix: embed_batch length+index pairing; reject non-numeric components
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+16
-2
@@ -72,12 +72,26 @@ pub fn embed_batch(
|
||||
let resp: serde_json::Value = client.post_json("/embeddings", &body)?;
|
||||
let data = resp["data"].as_array()
|
||||
.ok_or_else(|| AppError::Ionos("embeddings: no data[]".into()))?;
|
||||
if data.len() != texts.len() {
|
||||
return Err(AppError::Ionos(format!(
|
||||
"embeddings: expected {} items, got {}", texts.len(), data.len()
|
||||
)));
|
||||
}
|
||||
let mut n = 0;
|
||||
for (i, item) in data.iter().enumerate() {
|
||||
let idx = item["index"].as_u64().map(|u| u as usize).unwrap_or(i);
|
||||
if idx >= texts.len() {
|
||||
return Err(AppError::Ionos(format!(
|
||||
"embeddings: index {idx} out of range (n={})", texts.len()
|
||||
)));
|
||||
}
|
||||
let v: Vec<f32> = item["embedding"].as_array()
|
||||
.ok_or_else(|| AppError::Ionos("embeddings: no embedding[]".into()))?
|
||||
.iter().map(|x| x.as_f64().unwrap_or(0.0) as f32).collect();
|
||||
store.put(&texts[i], &v)?;
|
||||
.iter()
|
||||
.map(|x| x.as_f64().ok_or_else(|| AppError::Ionos("embeddings: non-numeric component".into())))
|
||||
.collect::<Result<Vec<f64>, _>>()?
|
||||
.into_iter().map(|f| f as f32).collect();
|
||||
store.put(&texts[idx], &v)?;
|
||||
n += 1;
|
||||
}
|
||||
Ok(n)
|
||||
|
||||
Reference in New Issue
Block a user