Skip to content

World's first open-source enterprise world model.

Keep indexed embedding responses paired with their input texts (#784)

WaylandYangb9fed549bd89
diff --git a/crates/utopia-llm/src/lib.rs b/crates/utopia-llm/src/lib.rs
index b0aeb23a..67dcd47f 100644
--- a/crates/utopia-llm/src/lib.rs
+++ b/crates/utopia-llm/src/lib.rs
@@ -769,8 +769,40 @@ impl LlmClient {
let data = body["data"]
.as_array()
.ok_or_else(|| anyhow::anyhow!("Unexpected embedding response shape"))?;
- let mut out = Vec::with_capacity(data.len());
- for item in data {
+ // 调用方把这些向量和输入的文本按位置配对。响应里写了 index,那它才是配对的
+ // 依据——网关把条目打乱了顺序也认得回来。
+ //
+ // **看值,不看键**:`get("index")` 对 `"index": null` 也返回 Some,而兼容端点
+ // 写个空值、写成字符串的都有。按键判断会把它们送进索引分支,再在 `as_u64` 上
+ // 报错,于是今天能用的响应明天整批失败。取不出数就当它没有索引,照旧按位置配。
+ let items: Vec<&serde_json::Value> = if data.iter().any(|item| {
+ item.get("index")
+ .and_then(serde_json::Value::as_u64)
+ .is_some()
+ }) {
+ let mut ordered = vec![None; texts.len()];
+ for item in data {
+ let index = item["index"]
+ .as_u64()
+ .and_then(|i| usize::try_from(i).ok())
+ .ok_or_else(|| {
+ anyhow::anyhow!("Embedding response has a missing or invalid index")
+ })?;
+ let slot = ordered
+ .get_mut(index)
+ .ok_or_else(|| anyhow::anyhow!("Embedding response index is out of range"))?;
+ anyhow::ensure!(slot.is_none(), "Embedding response has a duplicate index");
+ *slot = Some(item);
+ }
+ ordered
+ .into_iter()
+ .collect::<Option<Vec<_>>>()
+ .ok_or_else(|| anyhow::anyhow!("Embedding response is missing an input index"))?
+ } else {
+ data.iter().collect()
+ };
+ let mut out = Vec::with_capacity(items.len());
+ for item in items {
let v = item["embedding"]
.as_array()
.ok_or_else(|| anyhow::anyhow!("Embedding 响应缺少向量"))?
@@ -1556,4 +1588,66 @@ data: {\"choices\":[{\"delta\":{\"content\":\"tail\"},\"finish_reason\":\"stop\"
let e = anyhow::anyhow!("LLM request failed (401 Unauthorized): bad key");
assert!(!is_unreachable(&e));
}
+ async fn embeddings_from(data: serde_json::Value) -> anyhow::Result<Vec<Vec<f32>>> {
+ let body = json!({ "data": data }).to_string();
+ let (addr, server) = an_http_response("200 OK", "application/json", &body).await;
+ let result = client_at(addr)
+ .embed(&["first".into(), "second".into()])
+ .await;
+ server.await.unwrap();
+ result
+ }
+
+ /// 写了 index 却取不出数的(`null`、字符串、浮点)按没有索引算。这些形状今天能用,
+ /// 按键判断会把它们送进索引分支再报错,于是整批失败
+ #[tokio::test]
+ async fn an_index_that_is_not_a_number_falls_back_to_position() {
+ for data in [
+ json!([{"index": null, "embedding": [1.0]}, {"index": null, "embedding": [2.0]}]),
+ json!([{"index": "0", "embedding": [1.0]}, {"index": "1", "embedding": [2.0]}]),
+ json!([{"index": 0.5, "embedding": [1.0]}, {"index": 1.5, "embedding": [2.0]}]),
+ ] {
+ let out = embeddings_from(data.clone())
+ .await
+ .unwrap_or_else(|e| panic!("{data} 该按位置配对,却报错:{e}"));
+ assert_eq!(out, vec![vec![1.0], vec![2.0]], "{data}");
+ }
+ }
+
+ #[tokio::test]
+ async fn indexed_embeddings_follow_the_input_order() {
+ let out = embeddings_from(json!([
+ {"index": 1, "embedding": [2.0, 20.0]},
+ {"index": 0, "embedding": [1.0, 10.0]}
+ ]))
+ .await
+ .unwrap();
+ assert_eq!(out, vec![vec![1.0, 10.0], vec![2.0, 20.0]]);
+ }
+
+ #[tokio::test]
+ async fn ambiguous_embedding_indices_are_rejected() {
+ for data in [
+ json!([{"index": 0, "embedding": [1.0]}, {"index": 0, "embedding": [2.0]}]),
+ json!([{"index": 0, "embedding": [1.0]}, {"index": 2, "embedding": [2.0]}]),
+ json!([{"index": 0, "embedding": [1.0]}, {"embedding": [2.0]}]),
+ json!([{"index": 0, "embedding": [1.0]}, {"index": -1, "embedding": [2.0]}]),
+ json!([{"index": 0, "embedding": [1.0]}]),
+ ] {
+ assert!(
+ embeddings_from(data.clone()).await.is_err(),
+ "accepted {data}"
+ );
+ }
+ }
+
+ #[tokio::test]
+ async fn embeddings_without_indices_keep_positional_compatibility() {
+ let out = embeddings_from(json!([
+ {"embedding": [1.0, 10.0]}, {"embedding": [2.0, 20.0]}
+ ]))
+ .await
+ .unwrap();
+ assert_eq!(out, vec![vec![1.0, 10.0], vec![2.0, 20.0]]);
+ }
}