Elffuss BraidHub

kikocis/utopia public

dev · 4.0 KiBRawPermalink
1//! 抽取丢弃信号:哪些事实抽出来了却没能落地,以及为什么。
2//!
3//! 与 `ontology_misses` 分开是刻意的——那张表说的是"你的本体缺这些",读者是
4//! 本体维护者,动作是加类型;这张说的是"这些事实没落地",读者是上传文档的人,
5//! 动作是改文档或改本体。混在一个面板里两边都讲不清。
6//!
7//! 记录失败不影响抽取(调用方一律 `let _ =`)——信号缺一条,远好过因为记信号
8//! 失败而中断整篇文档的抽取。
9
10use sqlx::PgPool;
11use utopia_core::models::ExtractionDrop;
12use utopia_core::AppResult;
13use uuid::Uuid;
14
15/// 原因码。前端按这个查文案,所以是稳定契约,不要改字面量。
16pub mod reason {
17 /// 主语没在 entities 里声明 → 类型不明,属性无法校验 domain
18 pub const SUBJECT_NOT_DECLARED: &str = "subject_not_declared";
19 /// 属性挂在了不该挂的类上(salary 挂到 Organization)
20 pub const ATTR_DOMAIN_MISMATCH: &str = "attr_domain_mismatch";
21 /// 属性事实既没给 value 也没给 object
22 pub const ATTR_NO_VALUE: &str = "attr_no_value";
23 /// 值不合 datatype,归一化失败
24 pub const ATTR_DATATYPE: &str = "attr_datatype";
25 /// 模型自报置信度低于阈值
26 pub const LOW_CONFIDENCE: &str = "low_confidence";
27 /// 关系事实缺宾语
28 pub const OBJECT_MISSING: &str = "object_missing";
29 /// 模型给的这一条不合结构(缺 predicate 之类)→ 只跳这一条,不牵连整块
30 pub const MALFORMED_ITEM: &str = "malformed_item";
31 /// 主语的类型对不上关系声明的 domain,**且对调也不合法**——那是选错了关系
32 /// 或类型判错,不是方向问题。照原样落库 + 记信号,交给人看,不猜
33 pub const DOMAIN_MISMATCH: &str = "domain_mismatch";
34 /// 模型给的"实体名"其实是一整句话或从句——不是一个东西的名字。
35 /// 这类东西永远匹配不到别处的提及,在图上是孤点,还会拖累消解
36 pub const NOT_AN_ENTITY_NAME: &str = "not_an_entity_name";
37 /// 主语违反 domain 而宾语符合,已按本体声明的方向把主宾掰正。
38 /// **动作必须留痕**:自动的、看不见的改写才是 0001 反对的那种
39 pub const DIRECTION_CORRECTED: &str = "direction_corrected";
40 /// 模型输出被截断(撞上 max_tokens)→ 已完整的那些留下,尾巴丢掉
41 pub const TRUNCATED_REPLY: &str = "truncated_reply";
42}
43
44pub async fn record(
45 pool: &PgPool,
46 kb_id: Uuid,
47 document_id: Uuid,
48 reason: &str,
49 detail: &str,
50 example: Option<&str>,
51) -> AppResult<()> {
52 sqlx::query(
53 "INSERT INTO extraction_drops (kb_id, document_id, reason, detail, example)
54 VALUES ($1, $2, $3, left($4, 120), left($5, 200))
55 ON CONFLICT (kb_id, document_id, reason, detail)
56 DO UPDATE SET count = extraction_drops.count + 1,
57 example = COALESCE(EXCLUDED.example, extraction_drops.example),
58 updated_at = now()",
59 )
60 .bind(kb_id)
61 .bind(document_id)
62 .bind(reason)
63 .bind(detail)
64 .bind(example)
65 .execute(pool)
66 .await?;
67 Ok(())
68}
69
70/// 重抽开始时清掉这篇文档的旧信号——本轮要从头讲一遍这篇文档的故事。
71pub async fn clear_for_document(pool: &PgPool, document_id: Uuid) -> AppResult<()> {
72 sqlx::query("DELETE FROM extraction_drops WHERE document_id = $1")
73 .bind(document_id)
74 .execute(pool)
75 .await?;
76 Ok(())
77}
78
79/// 一个 KB 的全部丢弃信号。行数按 (文档 × 原因 × 具体对象) 聚合后很小,
80/// 一次取回让 Library 既能算每篇的总数、又能直接展开详情,不必逐行发请求。
81pub async fn for_kb(pool: &PgPool, kb_id: Uuid) -> AppResult<Vec<ExtractionDrop>> {
82 Ok(sqlx::query_as(
83 "SELECT document_id, reason, detail, count, example FROM extraction_drops
84 WHERE kb_id = $1 ORDER BY count DESC, reason LIMIT 2000",
85 )
86 .bind(kb_id)
87 .fetch_all(pool)
88 .await?)
89}