-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflows.json
More file actions
204 lines (204 loc) · 9.38 KB
/
Copy pathflows.json
File metadata and controls
204 lines (204 loc) · 9.38 KB
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
198
199
200
201
202
203
204
[
{
"id": "6a5d113e75ed72e1",
"type": "tab",
"label": "Customer Support Agent",
"disabled": false,
"info": "Customer support agent integration with Cloudflare AutoRAG",
"env": []
},
{
"id": "c072fd14f56e3d17",
"type": "http in",
"z": "6a5d113e75ed72e1",
"name": "Customer Support Endpoint",
"url": "/customer-support",
"method": "post",
"upload": false,
"swaggerDoc": "",
"x": 170,
"y": 120,
"wires": [
[
"ec0c2d0b20a1c6e9"
]
]
},
{
"id": "ec0c2d0b20a1c6e9",
"type": "function",
"z": "6a5d113e75ed72e1",
"name": "Parse Message",
"func": "// Parse the incoming message to handle different formats\nlet message = msg.payload;\n\nif (typeof message === 'object' && message.message) {\n // Already in the right format\n msg.payload = message;\n} else if (typeof message === 'string') {\n // Convert string to object format\n msg.payload = {\n message: message\n };\n} else {\n // Handle unexpected format\n msg.payload = {\n message: JSON.stringify(message)\n };\n}\n\n// Store original query for later use\nmsg.original = msg.payload.message;\n\n// Initialize cache in flow context if it doesn't exist\nif (!flow.get('query_cache')) {\n flow.set('query_cache', {});\n}\n\n// Check if the query is already in the cache\nconst cache = flow.get('query_cache');\nconst query = msg.payload.message;\nconst now = Date.now();\nconst TTL = 3600000; // 1 hour cache TTL\n\n// Clean expired cache entries\nObject.keys(cache).forEach(key => {\n if (now - cache[key].timestamp > TTL) {\n delete cache[key];\n }\n});\n\n// Check for exact cache match\nif (cache[query] && now - cache[query].timestamp < TTL) {\n msg.payload = cache[query].response;\n msg.cached = true;\n return [null, msg]; // Skip AutoRAG, send to output formatting\n}\n\n// No cache hit, continue normal flow\nmsg.cached = false;\nflow.set('query_cache', cache);\nreturn [msg, null];",
"outputs": 2,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 380,
"y": 120,
"wires": [
[
"ede56b6a1d3bb94c"
],
[
"dcc1a5da8b3e6dfd"
]
]
},
{
"id": "ede56b6a1d3bb94c",
"type": "function",
"z": "6a5d113e75ed72e1",
"name": "Prepare AutoRAG Request",
"func": "// Store configuration in flow context for reuse\nif (!flow.get('autorag_config')) {\n flow.set('autorag_config', {\n account_id: 'ced558f4eac9172b07993051961ac91e', // Your Cloudflare Account ID\n autorag_name: 'sweet-glitter-e320', // Your existing AutoRAG instance\n api_token: 'd8WD_2cCQ3KjQQ5xvUFp3PUWqWdx_wNg5skySkfx' // Your AutoRAG API token\n });\n}\n\nconst config = flow.get('autorag_config');\n\n// Prepare the request to Cloudflare AutoRAG\nmsg.url = `https://api.cloudflare.com/client/v4/accounts/${config.account_id}/autorag/rags/${config.autorag_name}/ai-search`;\n\nmsg.payload = {\n query: msg.payload.message,\n model: '@cf/meta/llama-3.1-8b-instruct-fast', // Using your existing AutoRAG model\n rewrite_query: true,\n max_num_results: 5,\n ranking_options: {\n score_threshold: 0.6\n },\n stream: false\n};\n\nmsg.headers = {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.api_token}`\n};\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 610,
"y": 120,
"wires": [
[
"ba4e17b99fb02f5e"
]
]
},
{
"id": "ba4e17b99fb02f5e",
"type": "http request",
"z": "6a5d113e75ed72e1",
"name": "AutoRAG Request",
"method": "POST",
"ret": "obj",
"paytoqs": "ignore",
"url": "",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": {},
"x": 840,
"y": 120,
"wires": [
[
"dcc1a5da8b3e6dfd",
"a0c4a6d4cbfe2ead"
]
]
},
{
"id": "dcc1a5da8b3e6dfd",
"type": "function",
"z": "6a5d113e75ed72e1",
"name": "Format Response",
"func": "if (msg.cached) {\n // This is a cached response, just pass it through\n // Dashboard update already happened during caching\n return msg;\n} else if (msg.statusCode === 200 && msg.payload.success) {\n const autoragResponse = msg.payload.result;\n msg.payload = {\n status: \"success\",\n response: autoragResponse.response,\n timestamp: new Date().toISOString(),\n inquiryId: Math.random().toString(36).substring(2, 12),\n sources: autoragResponse.data.map(doc => ({\n filename: doc.filename,\n score: doc.score\n }))\n };\n \n // Store for dashboard\n const recentInquiries = flow.get('recentInquiries') || [];\n recentInquiries.unshift(msg.payload);\n if (recentInquiries.length > 10) {\n recentInquiries.pop();\n }\n flow.set('recentInquiries', recentInquiries);\n \n // Cache the response\n const query_cache = flow.get('query_cache') || {};\n query_cache[msg.original] = {\n response: msg.payload,\n timestamp: Date.now()\n };\n flow.set('query_cache', query_cache);\n \n return msg;\n} else {\n // Error handling\n msg.payload = {\n status: \"error\",\n error: \"Failed to get response from Cloudflare AutoRAG\",\n details: msg.payload\n };\n return msg;\n}",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 840,
"y": 200,
"wires": [
[
"8f9f5c4c32bbdc75"
]
]
},
{
"id": "a0c4a6d4cbfe2ead",
"type": "function",
"z": "6a5d113e75ed72e1",
"name": "Detect Complex Issues",
"func": "// Check if AutoRAG response indicates a complex issue\n// This will depend on your specific criteria\n\nlet isComplex = false;\nlet hasLowConfidence = false;\n\n// If response was successful\nif (msg.statusCode === 200 && msg.payload.success) {\n const autoragResponse = msg.payload.result;\n \n // Check if confidence scores are too low\n const lowConfidenceThreshold = 0.7;\n hasLowConfidence = autoragResponse.data.every(doc => doc.score < lowConfidenceThreshold);\n \n // Check if response is too short or contains uncertainty phrases\n const responseText = autoragResponse.response || '';\n const uncertaintyPhrases = [\n \"I'm not sure\", \n \"I don't know\", \n \"cannot determine\",\n \"insufficient information\",\n \"please contact support\"\n ];\n \n const containsUncertainty = uncertaintyPhrases.some(phrase => \n responseText.toLowerCase().includes(phrase.toLowerCase())\n );\n \n isComplex = hasLowConfidence || containsUncertainty;\n}\n\n// Set complex flag and forward original message\nmsg.complex = isComplex;\n\n// Add reason for easier debugging\nif (isComplex) {\n msg.complexReason = hasLowConfidence ? \"Low confidence scores\" : \"Contains uncertainty phrases\";\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1070,
"y": 120,
"wires": [
[
"01d56ece20c13c5d"
]
]
},
{
"id": "01d56ece20c13c5d",
"type": "switch",
"z": "6a5d113e75ed72e1",
"name": "Route Complex Issues",
"property": "complex",
"propertyType": "msg",
"rules": [
{
"t": "true"
},
{
"t": "false"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 1290,
"y": 120,
"wires": [
[
"3d3ba6d8d69df0ef"
],
[
"a0ddd69e2a0889b7"
]
]
},
{
"id": "3d3ba6d8d69df0ef",
"type": "e-mail",
"z": "6a5d113e75ed72e1",
"server": "smtp.example.com",
"port": "587",
"secure": true,
"tls": true,
"name": "Email Notification",
"dname": "Email Support Team",
"x": 1290,
"y": 60,
"wires": []
},
{
"id": "a0ddd69e2a0889b7",
"type": "function",
"z": "6a5d113e75ed72e1",
"name": "Passthrough",
"func": "// Just passing through for non-complex issues\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1490,
"y": 120,
"wires": [
[]
]
},
{
"id": "8f9f5c4c32bbdc75",
"type": "http response",
"z": "6a5d113e75ed72e1",
"name": "Send Response",
"statusCode": "200",
"headers": {
"content-type": "application/json"
},
"x": 1050,
"y": 200,
"wires": []
}
]