Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(cli): make the default extract --mode all actually run (#1793)
Kevin4f4a0031776c
diff --git a/semantica/cli.py b/semantica/cli.pyindex 47776f17..7be2b023 100644--- a/semantica/cli.py+++ b/semantica/cli.py@@ -2242,29 +2242,56 @@ def extract(if model:extractor_config["llm_model"] = model- def _run_extraction() -> Any:- if mode == "triplets":+ def _run_single(+ target: str,+ *,+ entities: Optional[List[Any]] = None,+ relations: Optional[List[Any]] = None,+ ) -> Any:+ if target == "triplets":extractor = TripletExtractor(method=method, include_temporal=temporal, **extractor_config)- return extractor.extract(text)- elif mode == "relations":- ner = NERExtractor(method=method, **extractor_config)- entities = ner.extract(text)+ return extractor.extract(text, entities=entities, relations=relations)+ if target == "relations":+ if entities is None:+ ner = NERExtractor(method=method, **extractor_config)+ entities = ner.extract(text)extractor = RelationExtractor(method=method, confidence_threshold=confidence, **extractor_config)return extractor.extract(text, entities=entities)- elif mode == "ner":+ if target == "ner":extractor = NERExtractor(method=method, **extractor_config)return extractor.extract(text)- elif mode == "events":+ if target == "events":extractor = EventDetector(method=method, **extractor_config)return extractor.extract(text)- else:- raise click.ClickException(- f"Extraction mode '{mode}' is not yet wired to a runtime extractor."+ raise click.ClickException(+ f"Extraction mode '{target}' is not yet wired to a runtime extractor."+ )++ def _run_extraction() -> Any:+ # 'all' is the default, so it must not be the one value that+ # raises. Run every mode that has a runtime extractor and nest+ # each result under its own key (#1789).+ if mode == "all":+ # Reuse each stage's output instead of recomputing it: the+ # triplet stage would otherwise run its own NER + relation+ # pass on top of the ones already run here, tripling the+ # inference cost of the default command.+ entities = _run_single("ner")+ relations = _run_single("relations", entities=entities)+ triplets = _run_single(+ "triplets", entities=entities, relations=relations)+ return {+ "ner": entities,+ "relations": relations,+ "triplets": triplets,+ "events": _run_single("events"),+ }+ return _run_single(mode)if cli_ctx.quiet or cli_ctx.json_output:result = _run_extraction()diff --git a/semantica/semantic_extract/methods.py b/semantica/semantic_extract/methods.pyindex b89a7321..235d812b 100644--- a/semantica/semantic_extract/methods.py+++ b/semantica/semantic_extract/methods.py@@ -90,6 +90,7 @@ Main Functions:- extract_relations_huggingface: HuggingFace relation extraction- extract_relations_llm: LLM-based relation extraction- extract_triplets_pattern: Pattern-based triplet extraction+ - extract_triplets_ml: ML-based (spaCy dependency) triplet extraction- extract_triplets_rules: Rule-based triplet extraction- extract_triplets_huggingface: HuggingFace triplet extraction- extract_triplets_llm: LLM-based triplet extraction@@ -2531,6 +2532,41 @@ def extract_triplets_pattern(return triplets+def extract_triplets_ml(+ text: str,+ entities: Optional[List[Entity]] = None,+ relations: Optional[List[Relation]] = None,+ **kwargs,+) -> List[Triplet]:+ """ML-based triplet extraction.++ Mirrors what ``ml`` means for the entity and relation stages, where it is+ the spaCy-backed backend: relations are resolved with dependency parsing+ and every subject-predicate-object relation becomes a triplet. Relations+ are generated here when the caller has none, so the method can also be+ used on its own.+ """+ if relations is None and entities:+ relations = extract_relations_dependency(text, entities, **kwargs)++ triplets = []+ for relation in relations or []:+ triplets.append(+ Triplet(+ subject=relation.subject.text,+ predicate=relation.predicate,+ object=relation.object.text,+ confidence=relation.confidence,+ # Keep the relation's provenance (context, dependency path, …)+ # but record the method the caller asked for last, so the+ # output shows which method actually produced the triplet.+ metadata={**relation.metadata, "extraction_method": "ml"},+ )+ )++ return triplets++def extract_triplets_rules(text: str, entities: Optional[List[Entity]] = None, **kwargs) -> List[Triplet]:@@ -2974,6 +3010,7 @@ def get_triplet_method(method_name: str):# Built-in methodsbuiltin = {"pattern": extract_triplets_pattern,+ "ml": extract_triplets_ml, # ML (spaCy dependency) relations"rules": extract_triplets_rules,"huggingface": extract_triplets_huggingface,"llm": extract_triplets_llm,diff --git a/semantica/semantic_extract/triplet_extractor.py b/semantica/semantic_extract/triplet_extractor.pyindex 0a59e6f1..698e31ea 100644--- a/semantica/semantic_extract/triplet_extractor.py+++ b/semantica/semantic_extract/triplet_extractor.py@@ -7,6 +7,7 @@ methods, with validation and serialization support.Supported Methods:- "pattern": Pattern-based triplet extraction from relations (default)+ - "ml": ML-based triplet extraction from spaCy dependency relations- "rules": Rule-based triplet extraction using linguistic rules- "huggingface": Custom HuggingFace triplet extraction models- "llm": LLM-based triplet extraction using various providers@@ -28,6 +29,7 @@ Algorithms Used:Key Features:- Multiple extraction methods:* Pattern-based: Pattern matching for triplet extraction (default)+ * ML-based: spaCy dependency relations converted to triplets* Rules-based: Rule-based triplet extraction* HuggingFace: Custom HuggingFace triplet models* LLM-based: LLM-powered triplet extraction@@ -97,6 +99,7 @@ class TripletExtractor:Args:method: Extraction method(s). Can be:- "pattern": Pattern-based extraction (default)+ - "ml": ML-based extraction from spaCy dependency relations- "rules": Rule-based extraction- "huggingface": HuggingFace model- "llm": LLM-based extractiondiff --git a/tests/semantic_extract/test_imports.py b/tests/semantic_extract/test_imports.pyindex 54db9fba..f8cd4327 100644--- a/tests/semantic_extract/test_imports.py+++ b/tests/semantic_extract/test_imports.py@@ -54,6 +54,45 @@ def test_method_dispatchers_import_without_extractor_cycle() -> None:assert callable(get_triplet_method("pattern"))+def test_ml_is_a_registered_triplet_method() -> None:+ """#1789 (Qodo) — 'ml' must resolve to a real triplet method.++ The CLI defaults to ``--method ml`` across every mode, so the triplet+ registry has to know that name. Otherwise the extractor swallows the+ resulting ValueError and the requested method is silently ignored.+ """+ from semantica.semantic_extract.methods import (+ extract_triplets_ml,+ get_triplet_method,+ )++ assert get_triplet_method("ml") is extract_triplets_ml+++def test_ml_triplets_convert_supplied_relations() -> None:+ """The ml triplet path must emit real triplets, not degrade silently."""+ from semantica.semantic_extract.methods import extract_triplets_ml+ from semantica.semantic_extract.types import Entity, Relation++ alice = Entity(text="Alice", label="PER", start_char=0, end_char=5)+ acme = Entity(text="Acme", label="ORG", start_char=15, end_char=19)+ relation = Relation(+ subject=alice, predicate="works_at", object=acme, confidence=0.9+ )++ triplets = extract_triplets_ml(+ "Alice works at Acme", entities=[alice, acme], relations=[relation]+ )++ assert len(triplets) == 1+ assert (+ triplets[0].subject,+ triplets[0].predicate,+ triplets[0].object,+ ) == ("Alice", "works_at", "Acme")+ assert triplets[0].metadata["extraction_method"] == "ml"++def test_import_order_methods_before_extractors() -> None:result = _run_python("""diff --git a/tests/test_cli_commands.py b/tests/test_cli_commands.pyindex 6b95fb8c..d8ff55c9 100644--- a/tests/test_cli_commands.py+++ b/tests/test_cli_commands.py@@ -652,6 +652,73 @@ class TestExtract:)_ok(result)+ def test_default_mode_runs_every_extractor(self, runner, monkeypatch):+ """#1789 — the default --mode all must dispatch, not raise."""+ _ner_result = [MagicMock(text="Alice", label="PER", confidence=0.9,+ start_char=0, end_char=5, metadata={})]+ fake_ext = _fake_module(+ NERExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: _ner_result),+ RelationExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ TripletExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ EventDetector=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ )+ monkeypatch.setitem(+ __import__("sys").modules, "semantica.semantic_extract", fake_ext+ )+ # No --mode: fall back to the command default.+ result = runner.invoke(+ cli_module.main, ["extract", "Alice works at Acme.", "--json"]+ )+ _ok(result)+ data = _json_output(result)+ assert set(data) == {"ner", "relations", "triplets", "events"}++ def test_all_mode_reuses_ner_and_relations(self, runner, monkeypatch):+ """#1789 (Qodo) — 'all' must not recompute NER/relations per stage."""+ calls = {"ner": 0, "relations": 0, "triplets": 0}+ seen = {}+ ner_result = [MagicMock(text="Alice", label="PER", confidence=0.9,+ start_char=0, end_char=5, metadata={})]+ relation_result = [MagicMock(subject="Alice", predicate="works_at",+ object="Acme", confidence=0.9, metadata={})]++ def _triplet_extract(text, entities=None, relations=None, **kw):+ seen["entities"] = entities+ seen["relations"] = relations+ return []++ fake_ext = _fake_module(+ NERExtractor=lambda **kw: (+ calls.__setitem__("ner", calls["ner"] + 1)+ or MagicMock(extract=lambda text, **kw2: ner_result)+ ),+ RelationExtractor=lambda **kw: (+ calls.__setitem__("relations", calls["relations"] + 1)+ or MagicMock(extract=lambda text, entities=None, **kw2: relation_result)+ ),+ TripletExtractor=lambda **kw: (+ calls.__setitem__("triplets", calls["triplets"] + 1)+ or MagicMock(extract=_triplet_extract)+ ),+ EventDetector=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ )+ monkeypatch.setitem(+ __import__("sys").modules, "semantica.semantic_extract", fake_ext+ )+ result = runner.invoke(+ cli_module.main, ["extract", "Alice works at Acme.", "--json"]+ )+ _ok(result)+ # NER (for the ner stage) and relations run once each; the triplet+ # stage reuses their output instead of building a second NER/relation+ # extractor of its own.+ assert calls["ner"] == 1+ assert calls["relations"] == 1+ assert calls["triplets"] == 1+ # ...and the triplet stage actually received that shared output.+ assert seen["entities"] is ner_result+ assert seen["relations"] is relation_result+def test_import_error_is_clean(self, runner):with patch("builtins.__import__", side_effect=lambda n, *a, **k: ((_ for _ in ()).throw(ImportError(n))