World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
Skip embedding signatures and kind words when no property has a vector to compare them with (#1107)
evolv3.ai5be57cb5783e
diff --git a/crates/utopia-server/src/phrase_alignment.rs b/crates/utopia-server/src/phrase_alignment.rsindex e5e9d4c2..63160b2a 100644--- a/crates/utopia-server/src/phrase_alignment.rs+++ b/crates/utopia-server/src/phrase_alignment.rs@@ -289,7 +289,23 @@ async fn shortlist(return Ok(out);}let pool = &state.pool;- for batch in wide.chunks(SHORTLIST_EMBED_BATCH) {+ // 先问这一类属性有没有向量,没有的签名不嵌:嵌了最近邻也是空,照样看全部。一个没跑过+ // `embed_ontology` 的库从前每轮把一万七千条签名送去嵌入、短名单一条没开(#1097)。+ // 一类问一句,至多两句+ let mut has_vectors = HashMap::new();+ for kind in ["relation", "attribute"] {+ if wide.iter().any(|s| property_kind(s) == kind) {+ let has =+ utopia_store::ontology::has_relation_type_vectors(pool, kb_id, Some(kind)).await?;+ has_vectors.insert(kind, has);+ }+ }+ let usable: Vec<&PhraseSignature> = wide+ .iter()+ .copied()+ .filter(|s| has_vectors.get(property_kind(s)) == Some(&true))+ .collect();+ for batch in usable.chunks(SHORTLIST_EMBED_BATCH) {let texts: Vec<String> = batch.iter().map(|s| match (s.examples.first(), s.quotes.first()) {@@ -314,17 +330,12 @@ async fn shortlist(};for (s, vector) in batch.iter().zip(vectors) {let (fitting, _) = &full[&s.key()];- let kind = if s.object_is_value {- "attribute"- } else {- "relation"- };let near = utopia_store::ontology::nearest_relation_type_ids(pool,kb_id,&vector,(fitting.len() * 2) as i64,- Some(kind),+ Some(property_kind(s)),).await?;if near.is_empty() {@@ -343,10 +354,25 @@ async fn shortlist();}}- tracing::info!(%kb_id, wide = wide.len(), shortlisted = out.len(), "候选短名单开好");+ tracing::info!(+ %kb_id,+ wide = wide.len(),+ skipped = wide.len() - usable.len(),+ shortlisted = out.len(),+ "候选短名单开好"+ );Ok(out)}+/// 签名能绑的是哪一类属性:宾语是字面值的绑属性,否则绑关系+fn property_kind(s: &PhraseSignature) -> &'static str {+ if s.object_is_value {+ "attribute"+ } else {+ "relation"+ }+}+/// 类别词 → 留给提规则看的属性 id(按相关度)。词的文本加几个例名嵌入,取最近的/// [`SHORTLIST`] 条;没配嵌入模型、属性没向量的不进表async fn shortlist_kind_words(@@ -359,7 +385,10 @@ async fn shortlist_kind_words(let Some(client) = llm_util::embed_client(settings) else {return Ok(out);};- if words.is_empty() {+ // 一条带向量的属性都没有,嵌了也是空手而回(理由同 `shortlist`)+ if words.is_empty()+ || !utopia_store::ontology::has_relation_type_vectors(&state.pool, kb_id, None).await?+ {return Ok(out);}for batch in words.chunks(SHORTLIST_EMBED_BATCH) {diff --git a/crates/utopia-server/src/phrase_alignment_tests.rs b/crates/utopia-server/src/phrase_alignment_tests.rsindex cfa15275..e01544b9 100644--- a/crates/utopia-server/src/phrase_alignment_tests.rs+++ b/crates/utopia-server/src/phrase_alignment_tests.rs@@ -14,6 +14,8 @@ struct Model {hold: Arc<std::sync::atomic::AtomicBool>,entered: Arc<tokio::sync::Notify>,release: Arc<tokio::sync::Notify>,+ /// 嵌入端点被叫了几次:短名单该不该嵌签名(#1097)看的是这个数+ embeds: Arc<std::sync::atomic::AtomicUsize>,}async fn reply(State(m): State<Model>, Json(body): Json<Value>) -> impl IntoResponse {let n = {@@ -41,7 +43,8 @@ async fn reply(State(m): State<Model>, Json(body): Json<Value>) -> impl IntoResp/// 脚本化的嵌入端点:每段文字一个由字节算出的四维向量。只要求确定、条数对得上——/// 这里测的是「向量有没有」,不是近不近-async fn embed(Json(body): Json<Value>) -> impl IntoResponse {+async fn embed(State(m): State<Model>, Json(body): Json<Value>) -> impl IntoResponse {+ m.embeds.fetch_add(1, std::sync::atomic::Ordering::SeqCst);let data: Vec<Value> = body["input"].as_array().cloned()@@ -130,6 +133,7 @@ impl Fx {hold: Arc::new(std::sync::atomic::AtomicBool::new(false)),entered: Arc::new(tokio::sync::Notify::new()),release: Arc::new(tokio::sync::Notify::new()),+ embeds: Arc::new(std::sync::atomic::AtomicUsize::new(0)),};let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;let endpoint = format!("http://{}", listener.local_addr()?);@@ -251,6 +255,9 @@ impl Fx {fn requests(&self) -> Vec<Value> {self.model.requests.lock().unwrap().clone()}+ fn embed_calls(&self) -> usize {+ self.model.embeds.load(std::sync::atomic::Ordering::SeqCst)+ }fn prompt_of(&self, n: usize) -> String {self.requests()[n]["messages"][1]["content"].as_str()@@ -513,6 +520,44 @@ async fn overflow_is_recorded_for_a_person_and_recovers_when_candidates_shrink(run}+/// 属性还都没有向量时,短名单不嵌签名(#1097):嵌了最近邻也是空,模型照样看全部结构+/// 候选。从前一个没跑过 `embed_ontology` 的库每轮把候选多的签名全送去嵌入,一条也用不上。+/// 属性有了向量,同一条签名照常嵌、照常开短名单+#[tokio::test]+async fn signatures_are_not_embedded_while_no_property_has_a_vector() -> anyhow::Result<()> {+ let Some(f) = Fx::new().await? else {+ return Ok(());+ };+ let run = async {+ f.enable_embeddings().await?;+ // 12 条不声明域/值域的关系,加上 based_in 共 13 > SHORTLIST:这条签名要开短名单+ for i in 0..12 {+ sqlx::query("INSERT INTO relation_types(id,kb_id,key,label,kind,temporal) VALUES($1,$2,$3,$3,'relation','state')")+ .bind(Uuid::now_v7()).bind(f.kb).bind(format!("filler_{i}")).execute(&f.pool).await?;+ }+ f.script(bound());+ f.run().await?;+ assert_eq!(f.embed_calls(), 0, "no property has a vector: nothing to embed for");+ assert_eq!(f.binding().await?.status, "bound");+ assert!(+ f.prompt_of(0).contains("filler_11"),+ "without a shortlist the model sees every fitting candidate"+ );++ sqlx::query("UPDATE relation_types SET embedding = '[1,0,0,0]' WHERE kb_id=$1")+ .bind(f.kb)+ .execute(&f.pool)+ .await?;+ f.script(bound());+ f.run().await?;+ assert_eq!(f.embed_calls(), 1, "with property vectors the signature is embedded");+ anyhow::Ok(())+ }+ .await;+ f.cleanup().await?;+ run+}+#[tokio::test]async fn an_endpoint_class_change_moves_the_signature_and_the_old_row_stops_looping() -> anyhow::Result<()> {diff --git a/crates/utopia-store/src/ontology.rs b/crates/utopia-store/src/ontology.rsindex a4ebb76e..87f4c11a 100644--- a/crates/utopia-store/src/ontology.rs+++ b/crates/utopia-store/src/ontology.rs@@ -1773,6 +1773,28 @@ pub async fn nearest_relation_type_ids(Ok(rows.into_iter().map(|(id,)| id).collect())}+/// 这个库里有没有带向量的关系/属性(`only_kind` 同上)。谓词与+/// [`nearest_relation_type_ids`] 的一字不差:这里答「没有」,那边对任何向量都返回空。+///+/// 存在的理由:短语对齐的短名单先把每条签名嵌入、再逐条问最近的属性,问回来是空才知道+/// 属性还没向量。一个从没跑过 `embed_ontology` 的库每轮都把一万七千条签名送去嵌入,+/// 一条也用不上(#1097)。先问这一句,用不上就不嵌+pub async fn has_relation_type_vectors(+ pool: &PgPool,+ kb_id: Uuid,+ only_kind: Option<&str>,+) -> AppResult<bool> {+ Ok(sqlx::query_scalar(+ "SELECT EXISTS (SELECT 1 FROM relation_types+ WHERE kb_id = $1 AND embedding IS NOT NULL+ AND ($2::text IS NULL OR kind = $2))",+ )+ .bind(kb_id)+ .bind(only_kind)+ .fetch_one(pool)+ .await?)+}+/// 一次插完一批类,返回 key → id。////// **存在的理由是 fsync。** 逐条 `execute(pool)` 每条各自提交,导入 schema.org