I have used n8n workflow to fetch emails from my inbox and store the metadata, body, and attachments in the Postgres database table (pgvector extension enabled). The next step is to generate embeddings from the stored data from the fetched email and save the generated embeddings into the ‘embedding’ column in the same row of the database table.
if I use the javascript below in the Code node, I face the problem where ‘axios’ module is not native for n8n. I have tried to use the HTTP Request node to request self-hosted llama3.2 before the Code node but the output is empty. Need help with a proper approach in n8n to achieve my desired result, thank you very much for your support, and I hope to receive your advice soon.
javascript:
const axios = require(“axios”);
async function generateEmbedding(text) {
const response = await axios.post(“http://docker.for.mac.host.internal:11434/api/embeddings”, {
model: “llama3.2”,
input: text
});return response.data.embedding;
}
let output = ;
for (let item of $input.all()) {
const messageId = item.json[“message_id”];
const mailEmbedding = item.json[“mail_embedding”]; // Check existing embedding// Skip processing if embedding is already present if (mailEmbedding !== null) { output.push({ json: { message_id: messageId, mail_embedding: mailEmbedding // Retain existing value } }); continue; } // Extract necessary fields const emailText = item.json["email_text"] || ""; const subject = item.json["subject"] || ""; const sender = item.json["sender"] || ""; const recipient = item.json["recipient"] || ""; // Combine fields for vectorization const fullText = `${subject} ${emailText} From: ${sender} To: ${recipient}`; // Generate embedding const embedding = await generateEmbedding(fullText); output.push({ json: { message_id: messageId, mail_embedding: embedding } });
}
return output;
Information on my n8n setup
- n8n version:: self-host docker latest version
- Database: Postgres SQL (pgvector)
- n8n EXECUTIONS_PROCESS setting (default: own, main):: Default
- Running n8n via (Docker, npm, n8n cloud, desktop app):: Docker
- Operating system:: MacOS 15.2 (M2 Pro chip)