Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion skills/rai-discovery/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,12 @@ Each suggestion includes a `reasoners` field — an ordered list specifying the
}
```

After discovery, use the appropriate formulation skill for the chosen reasoner type.
After discovery, route to the appropriate skill based on the `reasoners` tag:

- **prescriptive** → `rai-prescriptive-problem-formulation`
- **graph** → `rai-graph-analysis`
- **rules** → `rai-rules-authoring`
- **predictive** → `rai-predictive-modeling`

---

Expand Down
1 change: 1 addition & 0 deletions skills/rai-graph-analysis/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ description: Graph algorithm selection and execution on PyRel v1 models. Covers
- Ontology design decisions (concept modeling, data mapping) — see `rai-ontology-design`
- Optimization formulation (variables, constraints, objectives) — see `rai-prescriptive-problem-formulation`
- Business rule authoring (validation, classification, alerting) — see `rai-rules-authoring`
- GNN graph construction for predictive pipelines — see `rai-predictive-modeling`

**Overview (process steps):**
1. Study the existing model — understand base definitions, coding conventions, and what's already wired
Expand Down
194 changes: 194 additions & 0 deletions skills/rai-predictive-management/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
---
name: rai-predictive-management
description: Register trained GNN models to Snowflake Model Registry and load previously trained models for inference. Use after training with rai-predictive-training, or when loading a saved model by registry key or run ID.
---

# Predictive Management
<!-- v1-SENSITIVE -->

## Summary

**What:** Encodes the model lifecycle workflow — registering trained GNN models and loading them for later inference.

**When to use:**
- Saving a trained model to Snowflake Model Registry
- Loading a previously registered model by name and version
- Loading a model by run ID
- Understanding the train-register-load multi-session workflow

**When NOT to use:**
- Defining concepts, building graphs, or configuring features — see `rai-predictive-modeling`
- Training a model or generating predictions — see `rai-predictive-training`

**Overview:**
1. Register a trained model with `gnn.register_model()`
2. Load a model by registry key or run ID with `GNN(...).load()`
3. Generate predictions with the loaded model

---

## Quick Reference

**Register:**
```python
gnn.register_model(
model_database="DB",
model_schema="MODEL_REGISTRY",
model_name="fraud_detector",
version_name="v1",
)
```

**Load by registry key:**
```python
gnn = GNN(
database="DB", schema="SCHEMA",
exp_database="DB", exp_schema="EXPERIMENTS",
graph=gnn_graph, pt=pt,
model_database="DB", model_schema="MODEL_REGISTRY",
model_name="fraud_detector", version_name="v1",
)
gnn.load()
```

**Load by run ID:**
```python
gnn = GNN(
database="DB", schema="SCHEMA",
exp_database="DB", exp_schema="EXPERIMENTS",
graph=gnn_graph, pt=pt,
model_run_id="01c2d9a0-0711-c54d-000a-1dc707f7a1e6",
)
gnn.load()
```

**What to include vs. omit when loading:**

| Include | Omit |
|---------|------|
| `database`, `schema` | `train`, `validation` |
| `exp_database`, `exp_schema` | `task_type`, `eval_metric` |
| `graph`, `pt` | hyperparameters (`device`, `n_epochs`, etc.) |
| model identifier (registry key or run ID) | |

---

## Register a Model

After `gnn.fit()` completes, register the model to the Snowflake Model Registry:

```python
gnn.register_model(
model_database="DB",
model_schema="MODEL_REGISTRY",
model_name="fraud_detector",
version_name="v1",
comment="Initial training run", # optional
)
```

The combination of `(model_database, model_schema, model_name, version_name)` uniquely identifies a registered model.

---

## Load a Model

### By Registry Key

```python
gnn = GNN(
database="DB", schema="SCHEMA",
exp_database="DB", exp_schema="EXPERIMENTS",
graph=gnn_graph,
pt=pt,
model_database="DB",
model_schema="MODEL_REGISTRY",
model_name="fraud_detector",
version_name="v1",
)
gnn.load()

User.predictions = gnn.predictions(domain=Test)
```

### By Run ID

```python
gnn = GNN(
database="DB", schema="SCHEMA",
exp_database="DB", exp_schema="EXPERIMENTS",
graph=gnn_graph,
pt=pt,
model_run_id="01c2d9a0-0711-c54d-000a-1dc707f7a1e6",
)
gnn.load()

User.predictions = gnn.predictions(domain=Test)
```

After `gnn.load()`, use `gnn.predictions(domain=Test)` exactly as after `gnn.fit()` — the prediction workflow is the same (see `rai-predictive-training`).

---

## Train-Register-Load Workflow

A typical multi-session workflow:

### Session 1: Train and Register

```python
gnn = GNN(
database="DB", schema="SCHEMA",
exp_database="DB", exp_schema="EXPERIMENTS",
graph=gnn_graph, pt=pt,
train=Train, validation=Val,
task_type="binary_classification", eval_metric="roc_auc",
has_time_column=True,
device="cuda", n_epochs=5,
)
gnn.fit()
gnn.register_model(
model_database="DB", model_schema="MODEL_REGISTRY",
model_name="fraud_detector", version_name="v1",
)
```

### Session 2: Load and Predict

Rebuild the graph and PropertyTransformer with the same structure as training, then load:

```python
# Rebuild graph and pt (same as training session)
gnn_graph = Graph(model, directed=True, weighted=False, aggregator="sum")
# ... define edges ...
pt = PropertyTransformer(...)

gnn = GNN(
database="DB", schema="SCHEMA",
exp_database="DB", exp_schema="EXPERIMENTS",
graph=gnn_graph, pt=pt,
model_database="DB", model_schema="MODEL_REGISTRY",
model_name="fraud_detector", version_name="v1",
)
gnn.load()
User.predictions = gnn.predictions(domain=Test)
```

---

## Common Pitfalls

| Mistake | Cause | Fix |
|---------|-------|-----|
| Calling `register_model()` before `fit()` | Model must be trained first — `RuntimeError`, no weights to serialize | Always call `gnn.fit()` before `gnn.register_model()` |
| Omitting `graph` or `pt` when loading | Loaded models still need the graph structure — `RuntimeError` | Provide the same `graph` and `pt` used during training |
| Passing `train`, `validation`, or hyperparameters when loading | These are training-only parameters — ignored or unexpected behavior | Omit `train`, `validation`, `task_type`, `eval_metric`, and all hyperparameters |
| Reusing the same `(name, version)` tuple | Registry keys must be unique — `RegistryError` | Use a new `version_name` for each registration |

---

## Examples

| Pattern | Description | File |
|---------|-------------|------|
| Register and load | Complete train-register-load workflow across sessions | [examples/register_and_load.py](examples/register_and_load.py) |
52 changes: 52 additions & 0 deletions skills/rai-predictive-management/examples/register_and_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
GNN Model Management — Register and Load Workflow
==================================================
Demonstrates the train-register-load pattern across sessions.

Session 1: Train a model and register it to Snowflake Model Registry.
Session 2: Load the registered model and generate predictions.
"""

# ── Session 1: Train and Register ───────────────────────────────────────────
# Assumes data model from `rai-predictive-modeling`:
# gnn_graph, pt, Train, Val, Test, User

gnn = GNN(
database="DB", schema="SCHEMA",
exp_database="DB", exp_schema="EXPERIMENTS",
graph=gnn_graph, pt=pt,
train=Train, validation=Val,
task_type="binary_classification", eval_metric="roc_auc",
has_time_column=True,
device="cuda", n_epochs=5,
)
gnn.fit()

gnn.register_model(
model_database="DB",
model_schema="MODEL_REGISTRY",
model_name="fraud_detector",
version_name="v1",
comment="Initial training run",
)


# ── Session 2: Load and Predict ─────────────────────────────────────────────
# REQUIRED: Rebuild the same graph and PT structure used during training
gnn_graph = Graph(model, directed=True, weighted=False, aggregator="sum")
Edge = gnn_graph.Edge
# ... define edges (same as training session) ...
pt = PropertyTransformer(...) # same config as training session

gnn = GNN(
database="DB", schema="SCHEMA",
exp_database="DB", exp_schema="EXPERIMENTS",
graph=gnn_graph, pt=pt,
model_database="DB",
model_schema="MODEL_REGISTRY",
model_name="fraud_detector",
version_name="v1",
)
gnn.load()

User.predictions = gnn.predictions(domain=Test)
Loading