Generative AI reading and understanding e-mails from g-Mail

This simpe, flow, it read e-mail from g-Mail, send it with a prompt engineering to extract some data and understand information inside e-mail, and the information extracted come in JSON format as oriented in prompt. It's a accelerator to a system integration since the information is in JSON format.

[{"id":"9f8d2ff04e28440f","type":"e-mail in","z":"a32db52877bf45c3","name":"","protocol":"IMAP","server":"imap.gmail.com","useSSL":true,"autotls":"never","port":"993","authtype":"BASIC","saslformat":true,"token":"oauth2Response.access_token","box":"INBOX","disposition":"Read","criteria":"UNSEEN","repeat":"300","fetch":"auto","inputs":0,"x":70,"y":120,"wires":[["89a94dd0ca262bfb"]]},{"id":"89a94dd0ca262bfb","type":"function","z":"a32db52877bf45c3","name":"Assunto = 'GenAI'","func":"if (msg.topic == 'GenAI') {\n//    msg.payload = msg.payload.replace(/[\\r\\n]/gm, '').trim();\n//    msg.payload = msg.payload.replace(/\\n\\n/g, '');\n    msg.payload = msg.payload.replace(/data: /g, '');\n    msg.payload = msg.payload.replace(/data:/g, '');\n    return msg;\n}\nreturn null;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":120,"wires":[["4dc68e277124fc51"]]},{"id":"de177a786e67ce30","type":"openai","z":"a32db52877bf45c3","name":"OpenAI","api":"","creds":"1d26833ca8b2489a","x":120,"y":240,"wires":[["306430201ee0e329"]]},{"id":"d869fc244e8f52ae","type":"debug","z":"a32db52877bf45c3","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":620,"y":240,"wires":[]},{"id":"4dc68e277124fc51","type":"function","z":"a32db52877bf45c3","name":"Parametros OpenAI","func":"msg.api = 'completions';\nvar prompt = \"Leia o e-mail abaixo e identifique, quando possível, 'NOME', 'E-MAIL', 'TELEFONE', 'SENTIMENTO'\";\nprompt += \", 'ELOGIO', 'OFENSA', 'NÚMERO DO PEDIDO', 'PROBLEMA'\";\nprompt += \", e quando uma informação não existir, traga 'VAZIO' como o dado, ex: {'SENTIMENTO':'VAZIO'}\\n\";\nprompt += \"Retorne no formato JSON. \\n\\nE-MAIL: \\n\"\nprompt += \"\\nFrom: \" + msg.header.from.text + \"\\n\\n\";\nmsg.params = {\n    \"model\": \"gpt-3.5-turbo-instruct\",\n    \"prompt\": prompt + msg.payload,\n    \"max_tokens\": 1000,\n    \"temperature\": 0.7\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":120,"wires":[["de177a786e67ce30"]]},{"id":"306430201ee0e329","type":"function","z":"a32db52877bf45c3","name":"Resultado","func":"msg.payload = msg.payload.choices[0].text;\nmsg.payload = msg.payload.replace(/[\\r\\n]/gm, '').trim();\nmsg.payload = msg.payload.replace(/\\n\\n/g, '');\nmsg.payload = msg.payload.replace(/'/g, '\"');\n//msg.payload = msg.payload.replace(/data: /g, '');\n//msg.payload = msg.payload.replace(/data:/g, '');\n\n//msg.payload = JSON.parse(msg.payload);\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":240,"wires":[["97e91a40eb397148"]]},{"id":"97e91a40eb397148","type":"json","z":"a32db52877bf45c3","name":"","property":"payload","action":"","pretty":false,"x":470,"y":240,"wires":[["d869fc244e8f52ae"]]},{"id":"1d26833ca8b2489a","type":"openaiApiKey","name":""}]

Flow Info

Created 1 year, 11 months ago
Rating: 5 1

Owner

Actions

Rate:

Node Types

Core
  • debug (x1)
  • function (x3)
  • json (x1)
Other

Tags

  • #GenAI
  • GeneratieAI
  • AI
  • e-Mail
  • gMail
  • OpenAI
  • JSON
  • NLU
  • NLP
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option