feat: External workflow file support with dynamic credential replacement
- Add --workflow-file option to install.sh (default: RAGKI-BotPGVector.json) - Add --ollama-model option (default: ministral-3:3b) - Add --embedding-model option (default: nomic-embed-text:latest) - Update libsupabase.sh to read workflow from external JSON file - Add Python script for dynamic credential ID replacement in workflow - Remove id, versionId, meta, tags, active, pinData from imported workflow - Include RAGKI-BotPGVector.json as default workflow template Tested successfully on container sb-1769180683
This commit is contained in:
+75
-10
@@ -618,9 +618,9 @@ n8n_api_cleanup() {
|
||||
pct exec "$ctid" -- bash -c "rm -f /tmp/n8n_cookies.txt /tmp/rag_workflow.json" 2>/dev/null || true
|
||||
}
|
||||
|
||||
# Full n8n setup: Create credentials, import workflow, activate
|
||||
# Full n8n setup: Create credentials, import workflow from file, activate
|
||||
# This version runs all API calls in a single shell session to preserve cookies
|
||||
# Usage: n8n_setup_rag_workflow <ctid> <email> <password> <pg_host> <pg_port> <pg_db> <pg_user> <pg_pass> <ollama_url> [ollama_model] [embedding_model]
|
||||
# Usage: n8n_setup_rag_workflow <ctid> <email> <password> <pg_host> <pg_port> <pg_db> <pg_user> <pg_pass> <ollama_url> <ollama_model> <embedding_model> <workflow_file>
|
||||
# Returns: 0 on success, 1 on failure
|
||||
n8n_setup_rag_workflow() {
|
||||
local ctid="$1"
|
||||
@@ -632,11 +632,23 @@ n8n_setup_rag_workflow() {
|
||||
local pg_user="$7"
|
||||
local pg_pass="$8"
|
||||
local ollama_url="$9"
|
||||
local ollama_model="${10:-llama3.2:3b}"
|
||||
local embedding_model="${11:-nomic-embed-text:v1.5}"
|
||||
local ollama_model="${10:-ministral-3:3b}"
|
||||
local embedding_model="${11:-nomic-embed-text:latest}"
|
||||
local workflow_file="${12:-}"
|
||||
|
||||
info "n8n Setup: Starting RAG workflow setup..."
|
||||
|
||||
# Validate workflow file
|
||||
if [[ -z "$workflow_file" ]]; then
|
||||
warn "n8n Setup: No workflow file specified, using built-in template"
|
||||
workflow_file=""
|
||||
elif [[ ! -f "$workflow_file" ]]; then
|
||||
warn "n8n Setup: Workflow file not found: $workflow_file"
|
||||
return 1
|
||||
else
|
||||
info "n8n Setup: Using workflow file: $workflow_file"
|
||||
fi
|
||||
|
||||
# Wait for n8n to be ready
|
||||
info "n8n Setup: Waiting for n8n to be ready..."
|
||||
local i
|
||||
@@ -654,12 +666,20 @@ n8n_setup_rag_workflow() {
|
||||
local escaped_pg_pass
|
||||
escaped_pg_pass=$(echo "$pg_pass" | sed 's/\\/\\\\/g; s/"/\\"/g')
|
||||
|
||||
# Generate workflow JSON with placeholder credential IDs (will be replaced in container)
|
||||
info "n8n Setup: Generating workflow JSON..."
|
||||
# Read workflow from file or generate from template
|
||||
info "n8n Setup: Preparing workflow JSON..."
|
||||
local workflow_json
|
||||
workflow_json=$(n8n_generate_rag_workflow_json "POSTGRES_CRED_ID" "PostgreSQL (local)" "OLLAMA_CRED_ID" "Ollama (local)" "$ollama_model" "$embedding_model")
|
||||
if [[ -n "$workflow_file" && -f "$workflow_file" ]]; then
|
||||
# Read workflow from external file
|
||||
workflow_json=$(cat "$workflow_file")
|
||||
info "n8n Setup: Loaded workflow from file: $workflow_file"
|
||||
else
|
||||
# Generate workflow from built-in template
|
||||
workflow_json=$(n8n_generate_rag_workflow_json "POSTGRES_CRED_ID" "PostgreSQL (local)" "OLLAMA_CRED_ID" "Ollama (local)" "$ollama_model" "$embedding_model")
|
||||
info "n8n Setup: Generated workflow from built-in template"
|
||||
fi
|
||||
|
||||
# Push workflow JSON to container
|
||||
# Push workflow JSON to container (will be processed by setup script)
|
||||
pct_push_text "$ctid" "/tmp/rag_workflow_template.json" "$workflow_json"
|
||||
|
||||
# Create a setup script that runs all API calls in one session
|
||||
@@ -731,9 +751,54 @@ if [ -z "\$OLLAMA_CRED_ID" ]; then
|
||||
fi
|
||||
echo "Ollama credential created: \$OLLAMA_CRED_ID"
|
||||
|
||||
# Replace placeholder IDs in workflow JSON
|
||||
# Process workflow JSON: replace credential IDs and clean up
|
||||
echo "Preparing workflow JSON..."
|
||||
sed -e "s/POSTGRES_CRED_ID/\$PG_CRED_ID/g" -e "s/OLLAMA_CRED_ID/\$OLLAMA_CRED_ID/g" /tmp/rag_workflow_template.json > /tmp/rag_workflow.json
|
||||
|
||||
# Create a Python script to process the workflow JSON
|
||||
cat > /tmp/process_workflow.py << 'PYTHON_SCRIPT'
|
||||
import json
|
||||
import sys
|
||||
|
||||
# Read the workflow template
|
||||
with open('/tmp/rag_workflow_template.json', 'r') as f:
|
||||
workflow = json.load(f)
|
||||
|
||||
# Get credential IDs from environment/arguments
|
||||
pg_cred_id = sys.argv[1]
|
||||
ollama_cred_id = sys.argv[2]
|
||||
|
||||
# Remove fields that should not be in the import
|
||||
fields_to_remove = ['id', 'versionId', 'meta', 'tags', 'active', 'pinData']
|
||||
for field in fields_to_remove:
|
||||
workflow.pop(field, None)
|
||||
|
||||
# Process all nodes and replace credential IDs
|
||||
for node in workflow.get('nodes', []):
|
||||
credentials = node.get('credentials', {})
|
||||
|
||||
# Replace PostgreSQL credential
|
||||
if 'postgres' in credentials:
|
||||
credentials['postgres'] = {
|
||||
'id': pg_cred_id,
|
||||
'name': 'PostgreSQL (local)'
|
||||
}
|
||||
|
||||
# Replace Ollama credential
|
||||
if 'ollamaApi' in credentials:
|
||||
credentials['ollamaApi'] = {
|
||||
'id': ollama_cred_id,
|
||||
'name': 'Ollama (local)'
|
||||
}
|
||||
|
||||
# Write the processed workflow
|
||||
with open('/tmp/rag_workflow.json', 'w') as f:
|
||||
json.dump(workflow, f)
|
||||
|
||||
print("Workflow processed successfully")
|
||||
PYTHON_SCRIPT
|
||||
|
||||
# Run the Python script to process the workflow
|
||||
python3 /tmp/process_workflow.py "\$PG_CRED_ID" "\$OLLAMA_CRED_ID"
|
||||
|
||||
# Import workflow
|
||||
echo "Importing workflow..."
|
||||
|
||||
Reference in New Issue
Block a user