File size: 8,716 Bytes
a4c5480
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
{
    "name": "AI Processing",
    "nodes": [
        {
            "parameters": {
                "authentication": "headerAuth",
                "httpMethod": "POST",
                "path": "ai-process",
                "options": {}
            },
            "name": "Webhook",
            "type": "n8n-nodes-base.webhook",
            "typeVersion": 1,
            "position": [250, 300]
        },
        {
            "parameters": {
                "functionCode": "// Get conversation history from Redis\nconst contextKey = `${$input.item.json.platform}:${$input.item.json.sender}`;\nconst history = await $node[\"Redis\"].redis.get(contextKey);\n\n// Prepare conversation context\nlet messages = [\n  {\n    role: \"system\",\n    content: `You are Melisandre, the Red Priestess, a mysterious and powerful advisor. Embody these traits in your responses:\n    - Speak with elegant, formal language that befits your status\n    - Be confident and unwavering in your convictions\n    - Maintain an air of mystery and otherworldly wisdom\n    - Occasionally reference visions or prophecies when relevant\n    - Be persuasive but not forceful\n    - Show devotion to your purpose while remaining diplomatic\n    - When discussing products or services, frame them as 'destined paths' or 'chosen offerings'\n    \n    Current context: You are serving as a digital ambassador, guiding followers through their journey.\n    `\n  }\n];\n\n// Add conversation history if available\nif (history) {\n  const parsedHistory = JSON.parse(history);\n  messages.push(\n    { role: \"user\", content: parsedHistory.lastMessage },\n    { role: \"assistant\", content: parsedHistory.lastResponse }\n  );\n}\n\n// Add current message\nmessages.push({ role: \"user\", content: $input.item.json.message });\n\nreturn {\n  json: {\n    messages,\n    sender: $input.item.json.sender,\n    platform: $input.item.json.platform,\n    timestamp: Date.now()\n  }\n};"
            },
            "name": "Prepare Context",
            "type": "n8n-nodes-base.function",
            "typeVersion": 1,
            "position": [450, 300]
        },
        {
            "parameters": {
                "url": "redis",
                "port": 6379,
                "password": "",
                "database": 0
            },
            "name": "Redis",
            "type": "n8n-nodes-base.redis",
            "typeVersion": 1,
            "position": [450, 450]
        },
        {
            "parameters": {
                "url": "https://api.openai.com/v1/chat/completions",
                "authentication": "headerAuth",
                "headerParameters": {
                    "parameters": [
                        {
                            "name": "Authorization",
                            "value": "=Bearer {{$env.OPENAI_API_KEY}}"
                        }
                    ]
                },
                "jsonParameters": true,
                "options": {},
                "bodyParameters": {
                    "parameters": [
                        {
                            "name": "model",
                            "value": "gpt-4"
                        },
                        {
                            "name": "messages",
                            "value": "={{$input.item.json.messages}}"
                        },
                        {
                            "name": "temperature",
                            "value": 0.85
                        },
                        {
                            "name": "max_tokens",
                            "value": 250
                        }
                    ]
                }
            },
            "name": "OpenAI Processing",
            "type": "n8n-nodes-base.httpRequest",
            "typeVersion": 1,
            "position": [650, 300]
        },
        {
            "parameters": {
                "functionCode": "// Analyze response for engagement opportunities\nconst response = $input.item.json.choices[0].message.content;\nconst engagementKeywords = ['destiny', 'path', 'vision', 'prophecy', 'chosen', 'light', 'power', 'journey'];\n\nconst hasEngagementHook = engagementKeywords.some(keyword => \n  response.toLowerCase().includes(keyword)\n);\n\nreturn {\n  json: {\n    ...$input.item,\n    hasEngagementHook,\n    originalResponse: response\n  }\n};"
            },
            "name": "Analyze Response",
            "type": "n8n-nodes-base.function",
            "typeVersion": 1,
            "position": [850, 300]
        },
        {
            "parameters": {
                "conditions": {
                    "boolean": [
                        {
                            "value1": "={{$input.item.json.hasEngagementHook}}",
                            "value2": true
                        }
                    ]
                }
            },
            "name": "Has Engagement Hook?",
            "type": "n8n-nodes-base.if",
            "typeVersion": 1,
            "position": [1050, 300]
        },
        {
            "parameters": {
                "functionCode": "// Enhance response with mystical engagement\nconst response = $input.item.json.originalResponse;\n\nreturn {\n  json: {\n    ...$input.item,\n    enhancedResponse: `${response}\\n\\nThe flames have shown me that you seek more. Perhaps you are destined to explore our sacred offerings - they await your chosen path.`\n  }\n};"
            },
            "name": "Add Mystical Hook",
            "type": "n8n-nodes-base.function",
            "typeVersion": 1,
            "position": [1250, 250]
        },
        {
            "parameters": {
                "functionCode": "// Store conversation context with analytics\nconst contextKey = `${$input.item.json.platform}:${$input.item.json.sender}`;\nconst context = {\n  lastMessage: $input.item.json.messages.slice(-1)[0].content,\n  lastResponse: $input.item.json.enhancedResponse || $input.item.json.originalResponse,\n  hasEngagementHook: $input.item.json.hasEngagementHook,\n  timestamp: Date.now()\n};\n\ntry {\n  // Store in Redis\n  await $node[\"Redis\"].redis.set(contextKey, JSON.stringify(context));\n} catch (error) {\n  // Log error but don't fail the workflow\n  console.error('Failed to store context:', error);\n}\n\nreturn {\n  json: {\n    response: $input.item.json.enhancedResponse || $input.item.json.originalResponse,\n    analytics: {\n      hasEngagementHook: $input.item.json.hasEngagementHook,\n      platform: $input.item.json.platform,\n      timestamp: Date.now()\n    }\n  }\n};"
            },
            "name": "Store Analytics",
            "type": "n8n-nodes-base.function",
            "typeVersion": 1,
            "position": [1450, 300]
        }
    ],
    "connections": {
        "Webhook": {
            "main": [
                [
                    {
                        "node": "Prepare Context",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Prepare Context": {
            "main": [
                [
                    {
                        "node": "OpenAI Processing",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "OpenAI Processing": {
            "main": [
                [
                    {
                        "node": "Analyze Response",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Analyze Response": {
            "main": [
                [
                    {
                        "node": "Has Engagement Hook?",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Has Engagement Hook?": {
            "main": [
                [
                    {
                        "node": "Add Mystical Hook",
                        "type": "main",
                        "index": 0
                    }
                ],
                [
                    {
                        "node": "Store Analytics",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        },
        "Add Mystical Hook": {
            "main": [
                [
                    {
                        "node": "Store Analytics",
                        "type": "main",
                        "index": 0
                    }
                ]
            ]
        }
    }
}