P paper2skillsPlaybook
开始业务诊断
访客模式
08-知识图谱

AI Agent 驱动的电商知识图谱自动构建

Skill-KG-Auto-Construction-Agent-Driven

声明类型推断验证状态待复核

业务问题: 母婴出海电商管理数千个 SKU,商品信息分散在 Amazon、Shopify、供应商提供的不同格式描述中

来源
paper2skills-vault/08-知识图谱/Skill-KG-Auto-Construction-Agent-Driven.md
适用边界
论文方法到业务问题的项目侧映射,不自动证明客户根因。
声明类型示例验证状态待企业数据验证

48-72 万元

来源
paper2skills-vault/08-知识图谱/Skill-KG-Auto-Construction-Agent-Driven.md#business-value
适用边界
方法估算或场景示例,不等于客户结果或收益承诺。

待企业数据验证 · 缺失:样本、基线、时间窗、币种、计算方法、证据来源

业务视角
适用角色选品负责人 / 运营负责人 · 数据分析师 · 供应链负责人
适用平台Amazon 品类体系 · 竞品 ASIN 网络分析
什么情况下用品类很多,不清楚品类间的关联,没法做系统性类目扩张规划;竞品矩阵太复杂,品牌/SKU/渠道理不清
示例目标(需企业数据验证)建立品类知识图谱,清晰看到哪些是入口品/引流品/利润品,指导下一步选品扩张方向
业务痛点
品类太多不知道先做哪个竞品关系理不清楚不知道用户买了奶瓶还会买什么类目扩张没有逻辑

1. 解决的问题

业务问题: 母婴出海电商管理数千个 SKU,商品信息分散在 Amazon、Shopify、供应商提供的不同格式描述中

2. 核心算法逻辑

核心思想

传统知识图谱构建依赖人工定义 Schema 和编写抽取规则,成本高、扩展性差。AI Agent 驱动的 KG 自动构建 将全流程拆解为三个由 LLM Agent 协作完成的阶段,从非结构化产品描述中自动产出结构化知识图谱,无需预定义 Schema 或人工规则。

三阶段流水线:

  1. Ontology Creation & Expansion(本体创建与扩展)
  • 从产品描述语料中采样代表性样本
  • LLM Agent 提取产品类、属性、关系,组织为 RDF/Turtle 格式
  • 迭代扩展:持续送入新样本,Agent 自动发现新类/属性并扩展 Schema
  • 直到每轮新增元素显著衰减(plateau),平衡覆盖率与可管理性
  1. Ontology Refinement(本体精炼)
  • 将完整本体作为输入,LLM Agent 执行零样本精炼
  • 建议修订:拆分复合属性、合并冗余类、明确域/范围约束
  • 消除歧义、提升通用性、增强跨品类适应能力
  1. Knowledge Graph Population(图谱填充)
  • 基于精炼后的本体,逐产品描述生成实例级 RDF 三元组
  • Agent 严格按照本体约束映射属性值,避免幻觉
  • 仅当描述中明确包含值时才生成三元组

数学直觉

本体覆盖度评分:

\text{Coverage}(O, D) = \frac{|\{p \in \text{Properties}(O) : \exists d \in D, \text{extracts}(d, p)\}|}{|\text{Properties}(O)|}

其中 $O$ 为本体,$D$ 为产品描述集合。论文实验中达到 97.1%。

迭代扩展收敛条件:

\Delta_t = \frac{|\text{NewElements}_t|}{|\text{TotalElements}_{t-1}|} < \epsilon \quad (\epsilon = 0.05)

第 $t$ 轮迭代中,新增元素占比低于阈值时停止扩展。

三元组生成约束:

对于产品描述 $d$ 和本体属性 $p$,仅当 $d$ 中显式包含 $p$ 的值时生成三元组:

(s, p, o) \in \text{KG} \iff \exists \text{span} \in d : \text{matches}(\text{span}, p)

这避免了 LLM 的推理幻觉,确保数据忠实于原文。

关键假设

  • 产品描述质量:输入文本需包含足够的结构化信息(规格、功能、适用场景)
  • LLM 一致性:同一模型在多次调用中对相同描述产生一致输出
  • 品类内相似性:同品类产品共享大部分属性和关系
  • 可验证性:生成的三元组应与原文可对应,便于人工抽检

3. 业务应用场景

业务问题: 母婴出海店铺在售约 3000 个 SKU,核心品类是吸奶器、储奶袋、温奶器、奶瓶。以四个主力 SKU 为例:Spectra S1 Plus 双边电动吸奶器、Lansinoh 储奶袋(100 片装)、Philips Avent 温奶器、Dr. Brown's Options+ 宽口径奶瓶。它们的标题、五点描述、A+ 页面分散在 Amazon、Shopify 和供应商 Excel 中,人工整理"商品-属性-关系"三元组每个 SKU 约 0.5 小时,新品上架后图谱常常滞后两周以上。

数据要求: - 上述四个 SKU 的 Amazon Listing 文本(标题 + Bullet Points + 描述段落) - 品类归属:吸奶器 / 储奶袋 / 温奶器 / 奶瓶 - 产品规格表:吸奶器口径尺寸(如 21mm/24mm)、奶瓶容量(如 270ml)、储奶袋容量(如 180ml)、温奶器加热方式

预期产出: - 自动生成的商品本体 Schema: - 实例级三元组:

4. 输入数据要求

  • 上述四个 SKU 的 Amazon Listing 文本(标题 + Bullet Points + 描述段落)
  • 品类归属:吸奶器 / 储奶袋 / 温奶器 / 奶瓶
  • 产品规格表:吸奶器口径尺寸(如 21mm/24mm)、奶瓶容量(如 270ml)、储奶袋容量(如 180ml)、温奶器加热方式
  • Amazon/Reddit 上上述三个 SKU 的用户评论文本
  • 已有的商品知识图谱(由场景一构建)

5. 输出结果

  • 自动生成的商品本体 Schema:
  • 实例级三元组:
  • 从评论中提取的扩展三元组:
  • 问题聚类:自动发现高频产品问题,反馈给产品团队

6. 业务价值与 ROI

  • 数据要求:需要商品描述文本数据,门槛较低
  • 技术门槛:中等,核心依赖 LLM API 调用和 prompt 设计
  • 工程复杂度:中,三阶段流水线需要模块化设计
  • 维护成本:低,新商品自动扩展,人工仅需抽检
  • 战略价值极高:是 08-知识图谱领域的核心缺口,直接影响 GraphRAG 的可用性
  • 业务匹配度完美:电商产品 KG 构建是母婴出海的刚需场景

7. 代码模板

代码块数量:5

Python699 行 · 完整显示
"""
AI Agent 驱动的电商知识图谱自动构建系统
基于 arXiv:2511.11017 (Peshevski et al., 2025)

功能:
1. 本体创建与扩展(Ontology Creation & Expansion)
2. 本体精炼(Ontology Refinement)
3. 知识图谱填充(Knowledge Graph Population)

接入真实模型:LLMAgent(llm_fn=lambda prompt: your_llm(prompt)),
llm_fn 只负责把 prompt 发给模型并原样返回 JSON 字符串;未注入时走规则 mock。

Author: paper2skills
Date: 2026-05-01
"""

import json
import re
from typing import List, Dict, Tuple, Optional, Set, Callable
from dataclasses import dataclass, field, asdict
from collections import defaultdict
import random


# ============================================================
# 数据模型
# ============================================================

@dataclass
class ProductDescription:
    """产品描述"""
    product_id: str
    title: str
    bullets: List[str]
    description: str
    category: str

    def full_text(self) -> str:
        return f"{self.title}\n" + "\n".join(self.bullets) + f"\n{self.description}"


@dataclass
class OntologyClass:
    """本体类"""
    name: str
    comment: str = ""
    parent: Optional[str] = None


@dataclass
class OntologyProperty:
    """本体属性"""
    name: str
    domain: str
    range_type: str  # "string", "integer", "float", "ClassName"
    comment: str = ""
    is_object_property: bool = False


@dataclass
class Ontology:
    """本体定义"""
    classes: List[OntologyClass] = field(default_factory=list)
    properties: List[OntologyProperty] = field(default_factory=list)

    def to_dict(self) -> Dict:
        return {
            "classes": [asdict(c) for c in self.classes],
            "properties": [asdict(p) for p in self.properties]
        }


@dataclass
class Triple:
    """RDF 三元组"""
    subject: str
    predicate: str
    object: str
    subject_type: str = ""
    object_type: str = ""

    def to_turtle(self) -> str:
        return f"<:{self.subject}> <:{self.predicate}> <:{self.object}> ."


# ============================================================
# LLM Agent 接口(模拟/真实)
# ============================================================

class LLMAgent:
    """
    LLM Agent。

    llm_fn 为真实模型调用,签名 (prompt: str) -> str,必须返回符合下方 schema 的 JSON。
    不注入时使用确定性规则 mock,保证模板离线可跑、结果可复现。
    """

    ONTOLOGY_SCHEMA = (
        '{"classes":[{"name":str,"comment":str,"parent":str|null}],'
        '"properties":[{"name":str,"domain":str,"range_type":str,'
        '"comment":str,"is_object_property":bool}]}'
    )
    TRIPLE_SCHEMA = (
        '{"triples":[{"subject":str,"predicate":str,"object":str,'
        '"subject_type":str,"object_type":str}]}'
    )
    LITERAL_TYPES = ("string", "integer", "float", "boolean")

    def __init__(self, llm_fn: Optional[Callable[[str], str]] = None):
        self.llm_fn = llm_fn

    def _ask(self, prompt: str) -> Dict:
        raw = self.llm_fn(prompt).strip()
        if raw[:1] == "`":  # 模型常把 JSON 包在 markdown 围栏里
            raw = raw.strip("`").split("\n", 1)[-1]
        return json.loads(raw)

    @staticmethod
    def _to_ontology(payload: Dict) -> Ontology:
        return Ontology(
            classes=[OntologyClass(**c) for c in payload["classes"]],
            properties=[OntologyProperty(**p) for p in payload["properties"]],
        )

    def _mock_extract_ontology(self, descriptions: List[ProductDescription]) -> Ontology:
        """模拟从描述中提取本体"""
        # 基于母婴商品常见属性构建模拟本体
        classes = [
            OntologyClass("Product", "通用商品类"),
            OntologyClass("BreastPump", "吸奶器", "Product"),
            OntologyClass("StorageBag", "储奶袋", "Product"),
            OntologyClass("Bottle", "奶瓶", "Product"),
            OntologyClass("Brand", "品牌"),
            OntologyClass("Feature", "产品特性"),
            OntologyClass("Material", "材质"),
        ]

        properties = [
            OntologyProperty("hasBrand", "Product", "Brand", "商品品牌", True),
            OntologyProperty("hasPrice", "Product", "float", "商品价格"),
            OntologyProperty("hasFeature", "Product", "string", "产品特性"),
            OntologyProperty("hasMaterial", "Product", "Material", "产品材质", True),
            OntologyProperty("compatibleWith", "Product", "Product", "兼容配件", True),
            OntologyProperty("alternativeTo", "Product", "Product", "替代品", True),
            OntologyProperty("recommendedFor", "Product", "string", "适用人群"),
        ]

        # 根据实际描述动态扩展
        all_text = " ".join([d.full_text().lower() for d in descriptions])

        if "baby bottle" in all_text or "奶瓶" in all_text:
            classes.append(OntologyClass("BabyBottle", "婴儿奶瓶", "Bottle"))
            properties.append(OntologyProperty("hasCapacity", "BabyBottle", "integer", "容量(ml)"))
            properties.append(OntologyProperty("isAntiColic", "BabyBottle", "boolean", "是否防胀气"))

        if "nipple" in all_text or "奶嘴" in all_text:
            classes.append(OntologyClass("Nipple", "奶嘴", "Product"))
            properties.append(OntologyProperty("nippleSize", "Nipple", "string", "奶嘴尺寸"))

        if "warmer" in all_text or "温奶" in all_text:
            classes.append(OntologyClass("BottleWarmer", "温奶器", "Product"))
            properties.append(OntologyProperty("heatingMode", "BottleWarmer", "string", "加热方式"))

        return Ontology(classes=classes, properties=properties)

    def _mock_refine_ontology(self, ontology: Ontology) -> Ontology:
        """模拟本体精炼"""
        refined = Ontology(
            classes=[OntologyClass(**asdict(c)) for c in ontology.classes],
            properties=[OntologyProperty(**asdict(p)) for p in ontology.properties]
        )

        # 合并冗余属性:hasCapacity 和 capacity 统一
        seen_names = set()
        deduped = []
        for p in refined.properties:
            if p.name not in seen_names:
                seen_names.add(p.name)
                deduped.append(p)
        refined.properties = deduped

        # 拆分复合属性
        refined.properties.append(
            OntologyProperty("hasColor", "Product", "string", "产品颜色")
        )

        # 添加通用性注释
        for c in refined.classes:
            if not c.comment:
                c.comment = f"{c.name} 类商品"

        return refined

    def _mock_populate_kg(self, desc: ProductDescription, ontology: Ontology) -> List[Triple]:
        """模拟从单个描述填充三元组"""
        triples = []
        text = desc.full_text().lower()
        product_uri = re.sub(r'[^\w]', '_', desc.product_id)

        # 确定产品类型(优先级关键字匹配,避免简单子串误匹配)
        product_type = "Product"
        type_priority = [
            ("BreastPump", ["breast pump", "breastpump", "吸奶器"]),
            ("BabyBottle", ["baby bottle", "babybottle", "婴儿奶瓶"]),
            ("BottleWarmer", ["bottle warmer", "warmer", "温奶器"]),
            ("StorageBag", ["storage bag", "milk bag", "储奶袋"]),
            ("Bottle", ["feeding bottle", "奶瓶"]),
            ("Nipple", ["nipple", "奶嘴"]),
        ]
        for cls_name, keywords in type_priority:
            if any(kw in text for kw in keywords):
                product_type = cls_name
                break

        # rdf:type
        triples.append(Triple(product_uri, "rdf:type", product_type, product_type))

        # 属性提取(基于关键词匹配模拟 LLM 抽取)
        # 品牌
        brand_patterns = [
            r'(spectra|medela|lansinoh|avent|dr\.?\s*brown|philips)',
        ]
        for pattern in brand_patterns:
            match = re.search(pattern, text, re.I)
            if match:
                brand = match.group(1).replace(" ", "_")
                triples.append(Triple(product_uri, "hasBrand", brand, product_type, "Brand"))
                break

        # 价格
        price_match = re.search(r'\$(\d+(?:\.\d+)?)', desc.full_text())
        if price_match:
            triples.append(Triple(product_uri, "hasPrice", price_match.group(1), product_type, "float"))

        # 特性(从 bullet points 提取)
        for bullet in desc.bullets:
            bullet_lower = bullet.lower()
            if any(kw in bullet_lower for kw in ['electric', '电动', 'battery', '电池']):
                triples.append(Triple(product_uri, "hasFeature", "电动", product_type, "string"))
            if any(kw in bullet_lower for kw in ['double', '双边', 'dual']):
                triples.append(Triple(product_uri, "hasFeature", "双边", product_type, "string"))
            if any(kw in bullet_lower for kw in ['silent', '静音', 'quiet']):
                triples.append(Triple(product_uri, "hasFeature", "静音", product_type, "string"))
            if any(kw in bullet_lower for kw in ['anti-colic', '防胀气', 'vent']):
                triples.append(Triple(product_uri, "isAntiColic", "true", product_type, "boolean"))

        # 适用人群
        if any(kw in text for kw in ['newborn', '新生儿', '0-3']):
            triples.append(Triple(product_uri, "recommendedFor", "新生儿", product_type, "string"))
        elif any(kw in text for kw in ['new mom', '新手妈妈', 'first time']):
            triples.append(Triple(product_uri, "recommendedFor", "新手妈妈", product_type, "string"))

        # 兼容性(基于品类推断)
        if product_type == "BreastPump":
            triples.append(Triple(product_uri, "compatibleWith", "StorageBag", product_type, "Product"))
            triples.append(Triple(product_uri, "compatibleWith", "BottleWarmer", product_type, "Product"))

        return triples

    def extract_ontology(self, descriptions: List[ProductDescription]) -> Ontology:
        """本体创建与扩展"""
        if self.llm_fn is None:
            return self._mock_extract_ontology(descriptions)
        corpus = "\n\n".join(d.full_text() for d in descriptions)
        payload = self._ask(
            "从下列电商产品描述中抽取本体,类与属性组织为 RDF 风格。"
            f"只输出 JSON,schema: {self.ONTOLOGY_SCHEMA}\n\n{corpus}"
        )
        return self._to_ontology(payload)

    def refine_ontology(self, ontology: Ontology) -> Ontology:
        """本体精炼"""
        if self.llm_fn is None:
            return self._mock_refine_ontology(ontology)
        payload = self._ask(
            "精炼下列本体:拆分复合属性、合并冗余类、补齐 domain/range。"
            f"只输出 JSON,schema: {self.ONTOLOGY_SCHEMA}\n\n"
            f"{json.dumps(ontology.to_dict(), ensure_ascii=False)}"
        )
        return self._to_ontology(payload)

    def populate_kg(self, description: ProductDescription, ontology: Ontology) -> List[Triple]:
        """知识图谱填充"""
        if self.llm_fn is None:
            return self._mock_populate_kg(description, ontology)
        text = description.full_text()
        payload = self._ask(
            "按本体把产品描述转成 RDF 三元组。"
            f"只输出 JSON,schema: {self.TRIPLE_SCHEMA}\n\n"
            f"本体: {json.dumps(ontology.to_dict(), ensure_ascii=False)}\n\n描述: {text}"
        )
        triples = [Triple(**t) for t in payload["triples"]]
        # 论文的无幻觉约束:字面量对象必须能在原文找到,否则丢弃
        return [
            t for t in triples
            if t.object_type not in self.LITERAL_TYPES or t.object.lower() in text.lower()
        ]


# ============================================================
# 主框架:三阶段流水线
# ============================================================

class KGAutoConstructionFramework:
    """
    AI Agent 驱动的知识图谱自动构建框架
    """

    def __init__(self, llm_agent: Optional[LLMAgent] = None):
        self.agent = llm_agent or LLMAgent(use_mock=True)
        self.ontology: Optional[Ontology] = None
        self.knowledge_graph: List[Triple] = []
        self.iteration_log: List[Dict] = []

    def stage1_create_ontology(self, sample_descriptions: List[ProductDescription],
                                max_iterations: int = 5,
                                convergence_threshold: float = 0.05) -> Ontology:
        """
        阶段1:本体创建与迭代扩展

        Args:
            sample_descriptions: 代表性产品描述样本
            max_iterations: 最大迭代轮数
            convergence_threshold: 收敛阈值(新增元素占比低于此值时停止)

        Returns:
            最终本体定义
        """
        print("[Stage 1] Ontology Creation & Expansion")

        # 初始本体
        ontology = self.agent.extract_ontology(sample_descriptions)
        initial_size = len(ontology.classes) + len(ontology.properties)

        print(f"  Initial ontology: {len(ontology.classes)} classes, {len(ontology.properties)} properties")

        # 迭代扩展
        for i in range(max_iterations):
            # 模拟:每轮送入新样本,发现新元素
            # 实际实现中,这里应该分批送入不同的产品描述
            expanded = self.agent.extract_ontology(sample_descriptions)
            new_size = len(expanded.classes) + len(expanded.properties)

            delta = (new_size - initial_size) / max(initial_size, 1)
            self.iteration_log.append({
                "stage": "expansion",
                "iteration": i + 1,
                "new_elements": new_size - initial_size,
                "delta_ratio": delta
            })

            print(f"  Iteration {i+1}: {len(expanded.classes)} classes, {len(expanded.properties)} properties "
                  f"(delta: {delta:.3f})")

            if delta < convergence_threshold:
                print(f"  Converged at iteration {i+1}")
                ontology = expanded
                break

            ontology = expanded
            initial_size = new_size

        self.ontology = ontology
        return ontology

    def stage2_refine_ontology(self) -> Ontology:
        """
        阶段2:本体精炼

        Returns:
            精炼后的本体
        """
        if not self.ontology:
            raise ValueError("Must run stage1_create_ontology first")

        print("\n[Stage 2] Ontology Refinement")
        before = len(self.ontology.properties)

        refined = self.agent.refine_ontology(self.ontology)
        after = len(refined.properties)

        print(f"  Before: {len(self.ontology.classes)} classes, {before} properties")
        print(f"  After:  {len(refined.classes)} classes, {after} properties")
        print(f"  Changes: property delta = {after - before}")

        self.ontology = refined
        return refined

    def stage3_populate_kg(self, descriptions: List[ProductDescription]) -> List[Triple]:
        """
        阶段3:知识图谱填充

        Args:
            descriptions: 所有产品描述

        Returns:
            生成的三元组列表
        """
        if not self.ontology:
            raise ValueError("Must run stage2_refine_ontology first")

        print(f"\n[Stage 3] Knowledge Graph Population")
        print(f"  Processing {len(descriptions)} product descriptions...")

        all_triples = []
        success_count = 0

        for desc in descriptions:
            try:
                triples = self.agent.populate_kg(desc, self.ontology)
                all_triples.extend(triples)
                success_count += 1
            except Exception as e:
                print(f"  Warning: failed to process {desc.product_id}: {e}")

        self.knowledge_graph = all_triples

        # 统计
        unique_predicates = set(t.predicate for t in all_triples)
        unique_subjects = set(t.subject for t in all_triples)

        print(f"  Success: {success_count}/{len(descriptions)} ({success_count/len(descriptions)*100:.1f}%)")
        print(f"  Total triples: {len(all_triples)}")
        print(f"  Unique subjects: {len(unique_subjects)}")
        print(f"  Unique predicates: {len(unique_predicates)}")

        # 计算属性覆盖率
        populated_props = set(t.predicate for t in all_triples
                              if t.predicate not in ["rdf:type"])
        total_props = len(self.ontology.properties)
        coverage = len(populated_props) / max(total_props, 1)
        print(f"  Property coverage: {coverage*100:.1f}%")

        return all_triples

    def run_pipeline(self, descriptions: List[ProductDescription]) -> Dict:
        """
        运行完整的三阶段流水线

        Returns:
            {
                'ontology': 最终本体,
                'triples': 所有三元组,
                'statistics': 统计信息
            }
        """
        print("=" * 70)
        print("AI Agent 驱动的知识图谱自动构建")
        print("=" * 70)

        # Stage 1
        self.stage1_create_ontology(descriptions)

        # Stage 2
        self.stage2_refine_ontology()

        # Stage 3
        triples = self.stage3_populate_kg(descriptions)

        stats = {
            "total_descriptions": len(descriptions),
            "ontology_classes": len(self.ontology.classes),
            "ontology_properties": len(self.ontology.properties),
            "total_triples": len(triples),
            "unique_subjects": len(set(t.subject for t in triples)),
        }

        return {
            "ontology": self.ontology.to_dict(),
            "triples": [asdict(t) for t in triples],
            "statistics": stats,
            "iteration_log": self.iteration_log
        }

    def export_to_turtle(self, filepath: str) -> str:
        """导出为 Turtle 格式,返回文本"""
        lines = []
        lines.append("@prefix : <http://paper2skills.com/maternal-baby#> .")
        lines.append("@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .")
        lines.append("@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .")
        lines.append("@prefix owl: <http://www.w3.org/2002/07/owl#> .")
        lines.append("")

        # 本体定义
        if self.ontology:
            for cls in self.ontology.classes:
                lines.append(f":{cls.name} rdf:type rdfs:Class ;")
                lines.append(f"    rdfs:comment \"{cls.comment}\" .")
                lines.append("")

            for prop in self.ontology.properties:
                prop_type = "owl:ObjectProperty" if prop.is_object_property else "owl:DatatypeProperty"
                lines.append(f":{prop.name} rdf:type {prop_type} ;")
                lines.append(f"    rdfs:domain :{prop.domain} ;")
                lines.append(f"    rdfs:range :{prop.range_type} ;")
                lines.append(f"    rdfs:comment \"{prop.comment}\" .")
                lines.append("")

        # 实例数据
        for triple in self.knowledge_graph:
            lines.append(triple.to_turtle())

        with open(filepath, 'w', encoding='utf-8') as f:
            f.write('\n'.join(lines))

        print(f"\nExported to: {filepath}")
        return '\n'.join(lines)


# ============================================================
# 测试数据:母婴出海商品
# ============================================================

def create_test_data() -> List[ProductDescription]:
    """创建母婴商品测试数据"""
    return [
        ProductDescription(
            product_id="spectra-s1",
            title="Spectra S1 Plus Electric Breast Pump",
            bullets=[
                "Hospital grade double electric breast pump",
                "Built-in rechargeable battery for portability",
                "Ultra-quiet motor (45dB) for discreet pumping",
                "Adjustable suction levels 1-12 with massage mode",
                "Compatible with all Spectra bottles and accessories",
            ],
            description=("The Spectra S1 Plus is a hospital-grade double electric breast pump "
                        "designed for new moms who need efficient and comfortable milk expression. "
                        "Its built-in battery allows for portable use anywhere. The quiet motor "
                        "ensures discreet pumping sessions at home or work."),
            category="BreastPump"
        ),
        ProductDescription(
            product_id="medela-pump",
            title="Medela Pump In Style with MaxFlow",
            bullets=[
                "Double electric breast pump with 2-Phase Expression",
                "Hospital performance in a personal-use pump",
                "Compact and lightweight design",
                "Includes cooler bag, bottles, and breast shields",
            ],
            description=("Medela Pump In Style delivers hospital-performance pumping with "
                        "MaxFlow technology. The 2-Phase Expression technology mimics baby's "
                        "natural nursing rhythm for more milk in less time."),
            category="BreastPump"
        ),
        ProductDescription(
            product_id="lansinoh-bags",
            title="Lansinoh Breastmilk Storage Bags, 100 Count",
            bullets=[
                "Pre-sterilized and BPA/BPS free",
                "Double zipper seal prevents leaks",
                "Lay flat for efficient storage and quick thawing",
                "Compatible with all major breast pump brands",
            ],
            description=("Lansinoh Breastmilk Storage Bags are designed for safe and convenient "
                        "breast milk storage. The double zipper seal ensures no leaks during "
                        "storage or transport. Pre-sterilized for immediate use."),
            category="StorageBag"
        ),
        ProductDescription(
            product_id="avent-warmer",
            title="Philips Avent Fast Baby Bottle Warmer",
            bullets=[
                "Warms milk in just 3 minutes",
                "Gentle defrost setting for frozen breast milk",
                "Compatible with all bottle sizes and brands",
                "Automatic shut-off for safety",
            ],
            description=("Philips Avent Fast Bottle Warmer gently and evenly warms breast milk "
                        "or formula in just 3 minutes. The smart temperature control prevents "
                        "hot spots and preserves nutrients."),
            category="BottleWarmer"
        ),
        ProductDescription(
            product_id="dr-brown-bottle",
            title="Dr. Brown's Options+ Wide-Neck Baby Bottle",
            bullets=[
                "Anti-colic vent system reduces gas and spit-up",
                "Wide-neck design for easy cleaning",
                "BPA-free glass and silicone materials",
                "Includes Level 1 slow flow nipple for newborns",
            ],
            description=("Dr. Brown's Options+ bottles feature a patented vent system that "
                        "reduces colic, gas, and spit-up. The wide-neck design makes filling "
                        "and cleaning easy. Perfect for newborns 0-3 months."),
            category="BabyBottle"
        ),
    ]


# ============================================================
# 自检
# ============================================================

def self_test(result: Dict, turtle: str, sample: ProductDescription) -> None:
    """断言真实行为;mock 退化或接口失联时这里必须先失败。"""
    classes = {c["name"] for c in result["ontology"]["classes"]}
    assert {"BreastPump", "BabyBottle", "BottleWarmer", "StorageBag"} <= classes, classes

    facts: Dict[str, Set[Tuple[str, str]]] = defaultdict(set)
    for t in result["triples"]:
        facts[t["subject"]].add((t["predicate"], t["object"]))
    # 优先级匹配:Dr. Brown's 必须落成 BabyBottle,而不是更宽泛的 Bottle
    assert ("rdf:type", "BabyBottle") in facts["dr_brown_bottle"], facts["dr_brown_bottle"]
    assert ("hasBrand", "spectra") in facts["spectra_s1"], facts["spectra_s1"]

    class FakeTripleLLM:
        """模拟真实模型:一条有原文依据,一条凭空捏造。"""

        def __call__(self, prompt: str) -> str:
            return json.dumps({"triples": [
                {"subject": "p", "predicate": "hasFeature", "object": "double",
                 "subject_type": "BreastPump", "object_type": "string"},
                {"subject": "p", "predicate": "hasFeature", "object": "titanium",
                 "subject_type": "BreastPump", "object_type": "string"},
            ]})

    # 注入式接口:不经过 mock,且无原文依据的字面量必须被 grounding 丢弃
    kept = LLMAgent(llm_fn=FakeTripleLLM()).populate_kg(sample, Ontology())
    assert [t.object for t in kept] == ["double"], kept

    class FakeOntologyLLM:
        def __call__(self, prompt: str) -> str:
            # 故意包一层 markdown 围栏,验证 _ask 的剥离逻辑
            body = json.dumps({"classes": [{"name": "Product", "comment": "", "parent": None}],
                               "properties": [{"name": "hasBrand", "domain": "Product",
                                               "range_type": "Brand", "comment": "",
                                               "is_object_property": True}]})
            fence = "`" * 3
            return f"{fence}json\n{body}\n{fence}"

    online = LLMAgent(llm_fn=FakeOntologyLLM()).extract_ontology([sample])
    assert [(c.name, c.parent) for c in online.classes] == [("Product", None)]
    assert online.properties[0].is_object_property is True

    assert "@prefix owl:" in turtle and "@prefix rdfs:" in turtle
    print("  [self-test] 7 assertions passed")


# ============================================================
# 主函数
# ============================================================

def main():
    """主函数:演示完整的 KG 自动构建流程"""
    print("=" * 70)
    print("母婴出海 - AI Agent 驱动的知识图谱自动构建系统")
    print("=" * 70)

    # 1. 准备测试数据
    print("\n[1] 加载母婴商品测试数据...")
    descriptions = create_test_data()
    print(f"   商品数量: {len(descriptions)}")
    for d in descriptions:
        print(f"   - {d.product_id}: {d.title}")

    # 2. 初始化框架(llm_fn 注入真实模型即切到在线路径)
    print("\n[2] 初始化 KG 自动构建框架...")
    agent = LLMAgent()
    framework = KGAutoConstructionFramework(agent)

    # 3. 运行三阶段流水线
    print("\n[3] 运行三阶段流水线...")
    result = framework.run_pipeline(descriptions)

    # 4. 输出结果
    print("\n[4] 输出结果...")
    print(f"\n   本体统计:")
    print(f"   - 类数量: {result['statistics']['ontology_classes']}")
    print(f"   - 属性数量: {result['statistics']['ontology_properties']}")

    print(f"\n   知识图谱统计:")
    print(f"   - 三元组总数: {result['statistics']['total_triples']}")
    print(f"   - 唯一主体: {result['statistics']['unique_subjects']}")

    # 5. 展示部分三元组
    print(f"\n[5] 示例三元组:")
    for t in result['triples'][:8]:
        print(f"   {t['subject']} --{t['predicate']}--> {t['object']}")

    # 6. 导出
    print("\n[6] 导出知识图谱...")
    turtle = framework.export_to_turtle("/tmp/maternal_baby_kg.ttl")

    # 7. 自检
    print("\n[7] 自检...")
    self_test(result, turtle, descriptions[0])

    print("\n" + "=" * 70)
    print("演示完成!")
    print("=" * 70)

    return result


if __name__ == '__main__':
    result = main()
print("[✓] Skill-KG-Auto-Construction-Agent-Driven 测试通过")

8. 论文来源

论文链接用于核验方法来源,不自动证明本页商业结果。