mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
Fix causal and explain skill API examples
This commit is contained in:
@@ -17,13 +17,22 @@ Build and inspect causal chains for a subject or category.
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
from semantica.context.causal_analyzer import CausalChainAnalyzer
|
from semantica.context.causal_analyzer import CausalChainAnalyzer
|
||||||
from semantica.context import ContextGraph
|
from semantica.context import AgentContext
|
||||||
|
|
||||||
graph = ContextGraph(advanced_analytics=True)
|
# Option 1: Use an existing AgentContext decision backend
|
||||||
analyzer = CausalChainAnalyzer(graph=graph)
|
chain = ctx.get_causal_chain(
|
||||||
|
decision_id=decision_id,
|
||||||
|
direction="upstream",
|
||||||
|
max_depth=depth,
|
||||||
|
)
|
||||||
|
|
||||||
chain = analyzer.build_causal_chain(subject=subject, depth=depth)
|
# Option 2: Use CausalChainAnalyzer directly
|
||||||
metrics = analyzer.compute_causal_metrics(chain)
|
analyzer = CausalChainAnalyzer(graph_store=ctx.knowledge_graph)
|
||||||
|
downstream = analyzer.get_causal_chain(
|
||||||
|
decision_id=decision_id,
|
||||||
|
direction="downstream",
|
||||||
|
max_depth=depth,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
Output: chain steps, cause strength, effect reach, and summary graph.
|
Output: chain steps, cause strength, effect reach, and summary graph.
|
||||||
@@ -32,22 +41,34 @@ Output: chain steps, cause strength, effect reach, and summary graph.
|
|||||||
|
|
||||||
## `intervene <node> <action> [--scenario <json>]`
|
## `intervene <node> <action> [--scenario <json>]`
|
||||||
|
|
||||||
Simulate an intervention on a node and measure downstream effects.
|
Analyze decision impact and influenced decisions (current causal API).
|
||||||
|
|
||||||
```python
|
```python
|
||||||
result = analyzer.simulate_intervention(node=node, action=action, scenario=scenario)
|
analyzer = CausalChainAnalyzer(graph_store=ctx.knowledge_graph)
|
||||||
|
impact_score = analyzer.get_causal_impact_score(decision_id=decision_id)
|
||||||
|
influenced = analyzer.get_influenced_decisions(
|
||||||
|
decision_id=decision_id,
|
||||||
|
max_depth=depth,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
Return: effect magnitudes, changed outcomes, and intervention recommendations.
|
Return: impact score, influenced decisions, and downstream scope.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## `counterfactual <fact> [--weight N]`
|
## `counterfactual <fact> [--weight N]`
|
||||||
|
|
||||||
Generate counterfactual explanations and alternate outcomes.
|
Trace root causes and temporal causal paths.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
counterfactuals = analyzer.generate_counterfactuals(fact=fact)
|
analyzer = CausalChainAnalyzer(graph_store=ctx.knowledge_graph)
|
||||||
|
roots = analyzer.find_root_causes(decision_id=decision_id, max_depth=depth)
|
||||||
|
historical_chain = analyzer.trace_at_time(
|
||||||
|
event_id=decision_id,
|
||||||
|
at_time="2026-01-01T00:00:00Z",
|
||||||
|
direction="upstream",
|
||||||
|
max_depth=depth,
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
Output: alternate causal paths, likelihood change, and decision impact.
|
Output: root decision lineage and time-bounded causal context.
|
||||||
|
|||||||
@@ -16,10 +16,14 @@ Produce explanations for decisions, rules, and graph analytics. Usage: `/semanti
|
|||||||
Explain why a decision was reached.
|
Explain why a decision was reached.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from semantica.explain import Explainer
|
from semantica.reasoning.explanation_generator import ExplanationGenerator
|
||||||
|
|
||||||
explainer = Explainer()
|
# For decision explainability in Semantica contexts:
|
||||||
explanation = explainer.explain_decision(decision_id=decision_id, detail=detail)
|
decision_trace = ctx.trace_decision_explainability(decision_id=decision_id)
|
||||||
|
|
||||||
|
# For reasoning/proof explanations:
|
||||||
|
generator = ExplanationGenerator(detail_level=detail)
|
||||||
|
explanation = generator.generate_explanation(reasoning_result)
|
||||||
```
|
```
|
||||||
|
|
||||||
Output: decision factors, rule traces, confidence, and suggested next steps.
|
Output: decision factors, rule traces, confidence, and suggested next steps.
|
||||||
@@ -31,7 +35,10 @@ Output: decision factors, rule traces, confidence, and suggested next steps.
|
|||||||
Explain graph relationships and why a node is connected.
|
Explain graph relationships and why a node is connected.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
explanation = explainer.explain_graph_connection(node_id=node_id, depth=depth)
|
# Use AgentContext explainability + causal tracing for graph-connected decisions
|
||||||
|
graph_explanation = ctx.trace_decision_explainability(decision_id=node_id)
|
||||||
|
upstream = ctx.get_causal_chain(decision_id=node_id, direction="upstream", max_depth=depth)
|
||||||
|
downstream = ctx.get_causal_chain(decision_id=node_id, direction="downstream", max_depth=depth)
|
||||||
```
|
```
|
||||||
|
|
||||||
Return: cause/effect chains, supporting evidence, and relevant metadata.
|
Return: cause/effect chains, supporting evidence, and relevant metadata.
|
||||||
|
|||||||
Reference in New Issue
Block a user