packages/@n8n/ai-workflow-builder.ee/evaluations/programmatic/python/README.md
Graph-based workflow similarity comparison using NetworkX and graph edit distance.
This module uses uv for dependency management. No installation is needed - dependencies are automatically managed by uvx.
Install uv:
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
Install just
# on macOS via homebrew
brew install just
# or gloabl install via NPM
npm install -g rust-just
# or cross platform via curl to DEST
curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash -s -- --to DEST
# Using default (standard) configuration
uvx --from . python -m src.compare_workflows generated.json ground_truth.json
# Using a preset
uvx --from . python -m src.compare_workflows generated.json ground_truth.json --preset strict
# Using custom configuration
uvx --from . python -m src.compare_workflows generated.json ground_truth.json --config my-config.yaml
# Output as human-readable summary
uvx --from . python -m src.compare_workflows generated.json ground_truth.json --output-format summary
from config_loader import load_config
from graph_builder import build_workflow_graph
from similarity import calculate_graph_edit_distance
import json
# Load workflows
with open('generated.json') as f:
generated = json.load(f)
with open('ground_truth.json') as f:
ground_truth = json.load(f)
# Load configuration
config = load_config('preset:standard')
# Build graphs
g1 = build_workflow_graph(generated, config)
g2 = build_workflow_graph(ground_truth, config)
# Calculate similarity
result = calculate_graph_edit_distance(g1, g2, config)
print(f"Similarity: {result['similarity_score']:.2%}")
print(f"Edit cost: {result['edit_cost']:.1f}")
print(f"Top edits: {len(result['top_edits'])}")
š For detailed configuration documentation, see CONFIGURATION.md
Create a YAML or JSON file with your custom rules:
version: "1.0"
name: "my-custom-config"
description: "Custom configuration for my use case"
costs:
nodes:
insertion: 10.0
deletion: 10.0
substitution:
same_type: 1.0
similar_type: 5.0
different_type: 15.0
trigger_mismatch: 50.0
edges:
insertion: 5.0
deletion: 5.0
substitution: 3.0
similarity_groups:
triggers:
- "n8n-nodes-base.webhook"
- "n8n-nodes-base.manualTrigger"
ignore:
node_types:
- "n8n-nodes-base.stickyNote"
global_parameters:
- "position"
- "id"
parameter_comparison:
numeric_tolerance:
- parameter: "options.temperature"
tolerance: 0.1
cost_if_exceeded: 2.0
For comprehensive documentation including:
See CONFIGURATION.md
{
"similarity_score": 0.78,
"similarity_percentage": "78.0%",
"edit_cost": 45.0,
"max_possible_cost": 205.0,
"top_edits": [
{
"type": "node_substitute",
"description": "Replace 'Manual Trigger' with 'Webhook Trigger'",
"cost": 25.0,
"priority": "critical"
}
],
"metadata": {
"generated_nodes": 5,
"ground_truth_nodes": 6
}
}
============================================================
WORKFLOW COMPARISON SUMMARY
============================================================
Overall Similarity: 78.0%
Edit Cost: 45.0 / 205.0
Configuration: standard
Standard balanced comparison configuration
Top 3 Required Edits:
------------------------------------------------------------
1. š“ [CRITICAL] Cost: 25.0
Replace 'Manual Trigger' with 'Webhook Trigger'
2. š [MAJOR] Cost: 10.0
Add missing 'HTTP Request' tool node
3. š” [MINOR] Cost: 5.0
Remove connection from 'Agent' to 'Extra Node'
============================================================
ā
PASS - Workflows are sufficiently similar
============================================================
Run the test suite:
# Install dev dependencies
uv sync --dev
# Run tests
uv run pytest
# Run with coverage
uv run pytest --cov
Uses NetworkX's optimize_graph_edit_distance with custom cost functions:
similarity = 1 - (edit_cost / max_possible_cost)
Where max_possible_cost is the cost of deleting all nodes/edges from g1 and inserting all from g2.
For very large or complex workflows, the comparison may timeout. Consider:
--verbose flag to see detailed configuration info