content/blog/2024-08-25-n8n-sentiment-analysis.org
160 lines · 5744 bytes
1#+date: <2024-08-25 Sunday 09:45:30>
2#+title: Instructions for Automating Email Sentiment Detection with n8n Workflows
3#+description: Detailed procedural steps for configuring an n8n workflow to perform sentiment analysis on incoming email messages, enabling automated classification based on sentiment.
4#+slug: n8n-sentiment-analysis
5#+filetags: :automation:workflow:sentiment:
6
7* n8n
8
9This guide will show you how to self-host [[https://n8n.io/][n8n]], a workflow automation platform,
10and use it to create a workflow that automatically analyzes the sentiment of all
11incoming emails on your account.
12
13This is a completely free process that only requires that you have access to (1)
14an email account with IMAP/SMTP options and (2) a computer or machine where you
15can install and use Docker.
16
17** Installation
18
19To get started, read the [[https://github.com/n8n-io/self-hosted-ai-starter-kit][self-hosted-ai-starter-kit]] project README or simply
20clone the repository and set it up with Docker using the command below.
21
22#+begin_src sh
23git clone https://github.com/n8n-io/self-hosted-ai-starter-kit.git
24cd self-hosted-ai-starter-kit
25docker compose --profile cpu up
26#+end_src
27
28This will clone the repository, start the stack of Docker containers with
29=compose=, and make the n8n web page available at [[https://localhost:5678]]. The
30first run will ask you to configure the administrator account for n8n.
31
32** Reverse Proxy
33
34If you want to use n8n from a public domain name, you'll need to configure a
35reverse proxy. I use Nginx on my server for reverse proxies. Nginx configuration
36locations and practices vary by distribution, but the following commands will
37show you my general reverse proxy configuration.
38
39#+begin_src sh
40cd /etc/nginx/conf.d
41nano n8n.conf
42#+end_src
43
44#+begin_src conf
45server {
46 listen 443 ssl;
47 listen [::]:443 ssl;
48 http2 on;
49 server_name n8n.example.com;
50
51 # SSL
52 ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
53 ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
54 ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
55
56 # reverse proxy
57 location / {
58 set $upstream_n8n http://127.0.0.1:5678;
59
60 proxy_set_header Remote-User $user;
61 proxy_set_header Remote-Email $email;
62 proxy_set_header Remote-Groups $groups;
63
64 client_body_buffer_size 128k;
65 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
66
67 send_timeout 5m;
68 proxy_read_timeout 360;
69 proxy_send_timeout 360;
70 proxy_connect_timeout 360;
71
72 proxy_set_header Host $host;
73 proxy_set_header Upgrade $http_upgrade;
74 proxy_set_header Connection upgrade;
75 proxy_set_header Accept-Encoding gzip;
76 proxy_set_header X-Real-IP $remote_addr;
77 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
78 proxy_set_header X-Forwarded-Proto $scheme;
79 proxy_set_header X-Forwarded-Host $http_host;
80 proxy_set_header X-Forwarded-Uri $request_uri;
81 proxy_set_header X-Forwarded-Ssl on;
82 proxy_redirect http:// $scheme://;
83 proxy_http_version 1.1;
84 proxy_set_header Connection "";
85 proxy_cache_bypass $cookie_session;
86 proxy_no_cache $cookie_session;
87 proxy_buffers 64 256k;
88 }
89
90# HTTP redirect
91server {
92 listen 80;
93 listen [::]:80;
94 server_name n8n.example.com;
95 include custom.d/letsencrypt.conf;
96
97 if ($host ~ ^[^.]+\.example\.com$) {
98 return 301 https://$host$request_uri;
99 }
100}
101#+end_src
102
103Simply restart Nginx to ensure the new configuration is in-effect.
104
105#+begin_src sh
106sudo systemctl restart nginx.service
107#+end_src
108
109** Workflow Configuration
110
111You can open the included workflow at
112[[http://localhost:5678/workflow/srOnR8PAY3u4RSwb]] or simply open the web interface
113and create a new configuration.
114
115#+caption: n8n Workflow
116[[https://img.cleberg.net/blog/20240825-n8n-sentiment-analysis/workflow.png]]
117
118This workflow contains the following nodes:
1191. Email Trigger (IMAP)
120 + Create an IMAP credential with your email's IMAP settings to allow n8n to
121 monitor for new emails.
1222. If
123 + Check ={{ $json.subject }}= to see if it =contains= "n8n Sentiment
124 Analysis" to ensure we don't re-check our own emails.
1253. Sentiment Analysis
126 + Analyze the ={{ $json.textPlain }}= input coming from each email.
127 + Ensure you have =Include Detailed Results= enabled so that we can access
128 the confidence and strength variables.
1294. Ollama Chat Model
130 + The Docker stack we used includes Ollama and Llama3, which provide the
131 easiest way to test this workflow.
1325. Send Email
133 + Create an SMTP credential to allow n8n to send emails.
134 + Subject: =n8n Sentiment Analysis: {{ $json.sentimentAnalysis.category }}=
135 + Email Format: =Text=
136 + Text (Expression): This can contain anything you want, but be sure to
137 include the variables ={{ $json.sentimentAnalysis.category }}=, ={{
138 $json.sentimentAnalysis.strength }}=, and ={{
139 $json.sentimentAnalysis.confidence }}=.
140 + In the additional options, I enabled the =Append n8n Attribution= option in
141 the screenshots below.
142
143** Testing
144
145You can use the =Test Workflow= button at the bottom of the canvas area to test
146the workflow. This relies on receiving new messages in your inbox, so be sure to
147send yourself a test email!
148
149** Results
150
151After testing each step noted above, n8n provided the results below - it works!
152
153#+caption: Positive Results
154[[https://img.cleberg.net/blog/20240825-n8n-sentiment-analysis/positive_results.png]]
155
156#+caption: Negative Results
157[[https://img.cleberg.net/blog/20240825-n8n-sentiment-analysis/negative_results.png]]
158
159While this isn't anything earth-shattering, it does show easy it is to get
160started with n8n and large language models in a self-hosted environment.