cmc/cleberg.net

My personal web garden & blog.

clone: git clone https://gitbay.org/cmc/cleberg.net.git

main: content/blog/2024-08-25-n8n-sentiment-analysis.org · raw

  1#+date:        [2024-08-25 Sun 09:45:30]
  2#+title:       Email Sentiment Analysis with n8n
  3#+description: Using n8n to automatically classify incoming emails by sentiment.
  4#+slug:        n8n-sentiment-analysis
  5#+filetags:    :self-hosting:
  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#+attr_html: :alt A canvas view of the worklow, showing an email trigger, if statement, sentiment analysis via Ollama, and send email action.
117[[https://img.cleberg.net/blog/20240825-n8n-sentiment-analysis/workflow.webp]]
118
119This workflow contains the following nodes:
1201. Email Trigger (IMAP)
121   + Create an IMAP credential with your email's IMAP settings to allow n8n to
122      monitor for new emails.
1232. If
124   + Check ={{ $json.subject }}= to see if it =contains= "n8n Sentiment
125     Analysis" to ensure we don't re-check our own emails.
1263. Sentiment Analysis
127   + Analyze the ={{ $json.textPlain }}= input coming from each email.
128   + Ensure you have =Include Detailed Results= enabled so that we can access
129     the confidence and strength variables.
1304. Ollama Chat Model
131   + The Docker stack we used includes Ollama and Llama3, which provide the
132     easiest way to test this workflow.
1335. Send Email
134   + Create an SMTP credential to allow n8n to send emails.
135   + Subject: =n8n Sentiment Analysis: {{ $json.sentimentAnalysis.category }}=
136   + Email Format: =Text=
137   + Text (Expression): This can contain anything you want, but be sure to
138     include the variables ={{ $json.sentimentAnalysis.category }}=, ={{
139     $json.sentimentAnalysis.strength }}=, and ={{
140     $json.sentimentAnalysis.confidence }}=.
141   + In the additional options, I enabled the =Append n8n Attribution= option in
142     the screenshots below.
143
144** Testing
145
146You can use the =Test Workflow= button at the bottom of the canvas area to test
147the workflow. This relies on receiving new messages in your inbox, so be sure to
148send yourself a test email!
149
150** Results
151
152After testing each step noted above, n8n provided the results below - it works!
153
154#+caption: Positive Results
155#+attr_html: :alt An example showing that n8n marked an email as "Positive".
156[[https://img.cleberg.net/blog/20240825-n8n-sentiment-analysis/positive_results.webp]]
157
158#+caption: Negative Results
159#+attr_html: :alt An example showing that n8n marked an email as "Negative".
160[[https://img.cleberg.net/blog/20240825-n8n-sentiment-analysis/negative_results.webp]]
161
162While this isn't anything earth-shattering, it does show easy it is to get
163started with n8n and large language models in a self-hosted environment.