Skip to content

World's first open-source enterprise world model.

A second question should not re-find what the first one already found (#94)

WaylandYange4f38a553474
diff --git a/crates/utopia-server/src/api/chat.rs b/crates/utopia-server/src/api/chat.rs
index fc6129b2..e6857048 100644
--- a/crates/utopia-server/src/api/chat.rs
+++ b/crates/utopia-server/src/api/chat.rs
@@ -20,6 +20,10 @@ use crate::llm_util;
use crate::retrieval;
use crate::state::AppState;
+/// 回放几个已认下的实体。上限是因为长会话会攒出几十个,全贴回去就把
+/// 省下来的上下文又花掉了;按首次出现排序,早认下的通常是这场对话的主角。
+const KNOWN_ENTITY_LIMIT: usize = 20;
+
const MAX_HISTORY: usize = 20;
const MAX_ROUNDS: usize = 6;
const SEARCH_TOP_K: usize = 6;
@@ -262,9 +266,10 @@ pub async fn chat(
&query,
&json!([]),
&json!([]),
+ &json!([]),
)
.await?;
- let history: Vec<(String, String)> = utopia_store::conversations::recent_context(
+ let (history, known_entities) = utopia_store::conversations::recent_context(
&state.pool,
conversation_id,
MAX_HISTORY as i64,
@@ -301,6 +306,37 @@ pub async fn chat(
for (role, content) in &history {
msgs.push(json!({ "role": role, "content": content }));
}
+ // **前几轮已经认下的实体,连 id 一起交回去。**
+ //
+ // 少了这一段,模型只看得见上一轮的最终答案文字,不知道自己搜过什么、
+ // 拿到过哪些 id,于是从名字重搜一遍。更隐蔽的是同名歧义时两轮可能落到
+ // **不同的实体**上,前后两个答案讲的不是同一个节点。
+ //
+ // 贴在历史之后、当前问题之前——位置就是服从性,跟抽取里 known_block
+ // 紧挨正文是同一条理由。
+ if !known_entities.is_empty() {
+ let lines: Vec<String> = known_entities
+ .iter()
+ .take(KNOWN_ENTITY_LIMIT)
+ .map(|e| {
+ format!(
+ "{} | {} | {}",
+ e["id"].as_str().unwrap_or("?"),
+ e["name"].as_str().unwrap_or("?"),
+ e["type"].as_str().unwrap_or("?")
+ )
+ })
+ .collect();
+ msgs.push(json!({
+ "role": "user",
+ "content": format!(
+ "Entities already identified earlier in this conversation (id | name | type). Call entity_facts with these ids directly; do not look them up by name again:
+ {}",
+ lines.join("
+ ")
+ )
+ }));
+ }
// 会话 id 先行下发(新会话由此告知前端)
yield Ok(Event::default()
@@ -313,6 +349,9 @@ pub async fn chat(
// 落库累积:assistant 全文与行动轨迹(历史回放用)
let mut answer_acc = String::new();
let mut steps_acc: Vec<serde_json::Value> = Vec::new();
+ // 这一轮认下的实体,落库供下一轮回放。**只记身份不记正文**——
+ // 工具结果里是 chunk 全文,每轮重复堆进上下文几轮就把窗口吃光
+ let mut resolved_acc: Vec<serde_json::Value> = Vec::new();
let mut rounds = 0usize;
loop {
@@ -335,6 +374,7 @@ pub async fn chat(
&state.pool, conversation_id, "assistant", &answer_acc,
&serde_json::Value::Array(steps_acc.clone()),
&serde_json::Value::Array(sources.clone()),
+ &serde_json::Value::Array(resolved_acc.clone()),
).await;
yield done_event();
}
@@ -381,6 +421,7 @@ pub async fn chat(
&state.pool, conversation_id, "assistant", &answer_acc,
&serde_json::Value::Array(steps_acc.clone()),
&serde_json::Value::Array(legacy_sources.clone()),
+ &serde_json::Value::Array(resolved_acc.clone()),
).await;
yield done_event();
}
@@ -422,6 +463,7 @@ pub async fn chat(
&state.pool, conversation_id, "assistant", &answer_acc,
&serde_json::Value::Array(steps_acc.clone()),
&serde_json::Value::Array(sources.clone()),
+ &serde_json::Value::Array(resolved_acc.clone()),
).await;
yield done_event();
}
@@ -520,6 +562,11 @@ pub async fn chat(
.collect::<Vec<_>>()
.join("\n")
};
+ for n in &hits {
+ resolved_acc.push(json!({
+ "id": n.id.to_string(), "name": n.name, "type": n.type_label
+ }));
+ }
(text, json!({ "kind": "entity", "label": name, "detail": format!("{} matches", hits.len()) }))
}
"entity_facts" => {
diff --git a/crates/utopia-store/src/conversations.rs b/crates/utopia-store/src/conversations.rs
index 31dfd933..6168129e 100644
--- a/crates/utopia-store/src/conversations.rs
+++ b/crates/utopia-store/src/conversations.rs
@@ -67,13 +67,19 @@ pub async fn messages(pool: &PgPool, conversation_id: Uuid) -> AppResult<Vec<Con
}
/// 服务端拼上下文用:最近 n 条的 (role, content),按时间正序。
+/// 回放最近几轮:角色、正文,**以及那几轮认下的实体**。
+///
+/// 只回放角色与正文时,模型看不见自己上一轮搜过什么、拿到过哪些 id,只能从
+/// 名字重搜一遍——实测就是这个样子。**但整段工具结果也不该回放**:那里面是
+/// chunk 正文,每轮重复堆进上下文,几轮就把窗口吃光。回放身份足矣:有了 id,
+/// 下一轮直接调 entity_facts。
pub async fn recent_context(
pool: &PgPool,
conversation_id: Uuid,
n: i64,
-) -> AppResult<Vec<(String, String)>> {
- let mut rows: Vec<(String, String, DateTime<Utc>)> = sqlx::query_as(
- "SELECT role, content, created_at FROM conversation_messages
+) -> AppResult<(Vec<(String, String)>, Vec<serde_json::Value>)> {
+ let mut rows: Vec<(String, String, serde_json::Value, DateTime<Utc>)> = sqlx::query_as(
+ "SELECT role, content, resolved, created_at FROM conversation_messages
WHERE conversation_id = $1 ORDER BY created_at DESC LIMIT $2",
)
.bind(conversation_id)
@@ -81,7 +87,22 @@ pub async fn recent_context(
.fetch_all(pool)
.await?;
rows.reverse();
- Ok(rows.into_iter().map(|(r, c, _)| (r, c)).collect())
+ // 实体按 id 去重、保持首次出现的顺序:同一个实体在几轮里反复出现是常态,
+ // 每轮各列一遍只是把同一件事说三遍
+ let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
+ let mut entities: Vec<serde_json::Value> = Vec::new();
+ for (_, _, res, _) in &rows {
+ for e in res.as_array().into_iter().flatten() {
+ let Some(id) = e["id"].as_str() else { continue };
+ if seen.insert(id.to_string()) {
+ entities.push(e.clone());
+ }
+ }
+ }
+ Ok((
+ rows.into_iter().map(|(r, c, _, _)| (r, c)).collect(),
+ entities,
+ ))
}
pub async fn append_message(
@@ -91,12 +112,15 @@ pub async fn append_message(
content: &str,
steps: &serde_json::Value,
sources: &serde_json::Value,
+ // 这一轮认下的实体(id / 名字 / 类型)。下一轮回放,让模型接着走而不是重搜
+ resolved: &serde_json::Value,
) -> AppResult<Uuid> {
let id = Uuid::now_v7();
let mut tx = pool.begin().await?;
sqlx::query(
- "INSERT INTO conversation_messages (id, conversation_id, role, content, steps, sources)
- VALUES ($1, $2, $3, $4, $5, $6)",
+ "INSERT INTO conversation_messages
+ (id, conversation_id, role, content, steps, sources, resolved)
+ VALUES ($1, $2, $3, $4, $5, $6, $7)",
)
.bind(id)
.bind(conversation_id)
@@ -104,6 +128,7 @@ pub async fn append_message(
.bind(content)
.bind(steps)
.bind(sources)
+ .bind(resolved)
.execute(&mut *tx)
.await?;
sqlx::query("UPDATE conversations SET updated_at = now() WHERE id = $1")
diff --git a/crates/utopia-store/src/resolution.rs b/crates/utopia-store/src/resolution.rs
index 6af9456d..b84ea697 100644
--- a/crates/utopia-store/src/resolution.rs
+++ b/crates/utopia-store/src/resolution.rs
@@ -334,13 +334,20 @@ const CONTAIN_SCAN_LIMIT: i64 = 16;
/// 包含关系绝不能走它:`华瑞集团技术中心` 与 `星云科技技术中心` 都含「技术中心」,
/// 同一篇文档里上下文相似度很容易过线,而它们是两个部门。宁分勿合。
///
-/// **已知不覆盖**:中间插词(`启明 X7 加速卡` 与 `启明 X7 推理加速卡` 谁也不含谁)。
-/// 那要三元组相似度,而 `CREATE EXTENSION pg_trgm` 需要超级权限——本仓库刚改成
-/// 受限角色连库(迁移 0031),装扩展会在部署上失败。留给需要时再说。
+/// **别名一并参与**:合并会把名字搬进 `aliases`,只查 `canonical_name` 的话,
+/// 每成功合并一次就少一条召回的桥——`Holmes` 并入 `Sherlock Holmes` 之后,
+/// 后来的 `Mr. Holmes` 就再也搭不上了(它跟 `Sherlock Holmes` 谁也不含谁)。
+/// 合并越成功、漏得越多,是个会自我加剧的洞。
///
-/// **性能**:反向那半(新名字包含旧名字)用不上任何索引,所以这里靠
-/// `kb_id + type_id` 收窄行集并设上限,且只在**新建实体时**跑一次,不是每条 mention。
-/// 大库上如果不够,正解是建一张「后缀键」表走等值查,而不是加模糊索引。
+/// **已知不覆盖**:两个名字既不互相包含、又没有共同别名做桥的情形
+///(`启明 X7 加速卡` 与 `启明 X7 推理加速卡`)。那要三元组相似度,而
+/// `CREATE EXTENSION pg_trgm` 需要超级权限——本仓库是受限角色连库(迁移 0031),
+/// 装扩展会在部署上失败。留给需要时再说。
+///
+/// **性能**:反向那半(新名字包含旧名字)用不上任何索引,别名那半同样,
+/// 所以这里靠 `kb_id` 收窄行集并设上限,且只在**新建实体时**跑一次,
+/// 不是每条 mention。大库上如果不够,正解是建一张「后缀键」表走等值查,
+/// 而不是加模糊索引。
async fn containment_reviews(
pool: &PgPool,
kb_id: Uuid,
@@ -368,9 +375,25 @@ async fn containment_reviews(
WHERE e.kb_id = $1 AND e.merged_into IS NULL
AND e.id <> $2
AND lower(e.canonical_name) <> $3
- AND char_length(e.canonical_name) >= $4
- AND (lower(e.canonical_name) LIKE '%' || $3 || '%'
- OR $3 LIKE '%' || lower(e.canonical_name) || '%')
+ AND (
+ (char_length(e.canonical_name) >= $4
+ AND (lower(e.canonical_name) LIKE '%' || $3 || '%'
+ OR $3 LIKE '%' || lower(e.canonical_name) || '%'))
+ -- **别名也要参与召回,否则每合并一次就少一条桥。**
+ --
+ -- 合并把名字搬进 aliases:Holmes 并入 Sherlock Holmes 之后,
+ -- Holmes 那一行 merged_into 非空、被上面第一个条件滤掉了。可十分钟后
+ -- 出现的 Mr. Holmes 跟 Sherlock Holmes 谁也不含谁——本来正是靠
+ -- Holmes 才桥得上。实测就是这么漏的:同类型那个 bug 修好、Holmes
+ -- 正确合并之后,Mr. Holmes 反而永远进不了队列。
+ --
+ -- 合并越成功,桥拆得越多。这个洞会自我加剧。
+ OR EXISTS (
+ SELECT 1 FROM unnest(e.aliases) AS alias
+ WHERE char_length(alias) >= $4
+ AND (lower(alias) LIKE '%' || $3 || '%'
+ OR $3 LIKE '%' || lower(alias) || '%'))
+ )
ORDER BY char_length(e.canonical_name)
LIMIT $5",
)
diff --git a/migrations/0043_conversation_resolved_entities.sql b/migrations/0043_conversation_resolved_entities.sql
new file mode 100644
index 00000000..813cf6dd
--- /dev/null
+++ b/migrations/0043_conversation_resolved_entities.sql
@@ -0,0 +1,16 @@
+-- 这一轮认下了哪些实体:id、名字、类型。下一轮回放它。
+--
+-- 会话历史此前只回放 role + content,工具调用与结果一条都不进。于是第二轮
+-- 开始时模型只看得见上一轮的最终答案文字,不知道自己搜过什么、拿到过哪些 id,
+-- 只能从名字重搜一遍。实测就是这个样子:问"华生和夏洛克的关系",再问一次
+-- 换个名字,两轮各检索一遍。
+--
+-- **不回放整段工具结果**:那里面是 chunk 正文,每轮重复堆进上下文,几轮就把
+-- 窗口吃光。要回放的是身份——有了 id,下一轮直接调 entity_facts,不必再从
+-- 名字查起。顺带治掉一个更隐蔽的毛病:同名歧义时两轮可能查到**不同的实体**,
+-- 于是前后两个答案讲的不是同一个节点。
+--
+-- 与抽取里 known_block 同一个套路(前面分块已认下的实体注入后续分块),
+-- 理由也一样:让模型接着上一次的结论走,而不是每次从零开始猜。
+ALTER TABLE conversation_messages
+ ADD COLUMN resolved jsonb NOT NULL DEFAULT '[]'::jsonb;