This video outlines how to automate invoice processing using local AI tools (n8n and Ollama) to achieve efficiency, cost savings, and data privacy without relying on cloud-based solutions.
Problem with Manual Invoice Processing #
- Manual processing of invoices is repetitive and time-consuming.
- It involves opening each invoice, checking sender, payment terms, and entering data into spreadsheets or forwarding to an accountant.
Disadvantages of Existing Cloud-Based Automation Solutions #
- Cost: Subscription fees for platforms.
- Data Security: Financial data is transferred to external servers, leading to loss of control and reliance on often unread terms and conditions.
Advantages of the Proposed Local Automation System #
- Cost-Free: No subscription charges.
- Local Processing: All processing happens on your local computer, keeping sensitive financial data private.
- Full Control: You retain complete control over your data and the automation process.
Three Main Steps of the Automation System #
- Detecting New Invoices in Gmail: Setting up n8n to listen for new emails with invoice attachments.
- Extracting Data with Local AI: Passing attachments to a local AI model (Ollama) to pull key information.
- Saving to Google Drive: Neatly saving the processed invoices to Google Drive for accounting purposes.
Step-by-Step Implementation in n8n #
- N8N Setup: The presenter uses a locally running n8n instance.
- Trigger Node:
- Starts with a manual trigger for building phase, later to be scheduled.
- Gmail Node (
Get Many Messages
):- Resource:
message
, Operation:get Many
, Return:All
. - Filters:
has attachment
: Only emails with attachments.is:unread
: Only unread emails.subject:(invoice OR faktura OR faktury)
: Emails with specific keywords in the subject.
- Resource:
- Looping Node (
Loop Over Item
): To process each email individually. - Gmail Node (
Get a Message
):- Retrieves the specific message using its ID from the previous node.
Download Attachments
option is enabled.
- Code Node (JavaScript):
- Splits multiple attachments within a single email into individual items for processing. This handles cases where an email contains multiple invoices.
- PDF Node (
Extract From PDF
):- Extracts text content from PDF invoice files.
Input Binary Field
: Uses an expression (object.keys($binary)[0]
) to dynamically select the correct binary name, ensuring proper handling of multiple attachments from the loop.Keep Source -> Binary
: Essential for passing the original binary data to subsequent nodes (e.g., Google Drive upload).
- AI Node (
Basic LLM Chain
):- Prompt Construction: User message prompt that instructs the AI to return data in JSON format, defining the desired JSON structure (
vendorName
,paymentDueDate
,amount
,invoiceNumber
). Require Specific Output Format
: Ensures the AI output adheres to the specified JSON structure.- Output Parser: JSON output parser for structured data.
- Prompt Construction: User message prompt that instructs the AI to return data in JSON format, defining the desired JSON structure (
- Ollama Chat Model Node:
- Configures the connection to the local Ollama instance (e.g.,
localhost
). - Selects
Gemma 3
as the AI model.
- Configures the connection to the local Ollama instance (e.g.,
- Merge Node:
- Combines the AI-extracted data with the original binary data of the invoice.
- Mode:
Combine
, Combine by:Position
. This is crucial for passing both the AI output and the file to the Google Drive node.
- Google Drive Node (
Upload File
):- Uploads the processed invoice file to Google Drive.
Input Data Field Name
: Set via expression (object.keys($binary)[0]
) to handle dynamic binary names from the merge node.File Name
: Customizable file naming convention using data from the AI output (e.g.,vendorName
,paymentDueDate
).Parent Folder ID
: Specifies the destination folder on Google Drive (e.g., "odebrane").
- Gmail Node (
Mark Message as Read
):- Marks the processed email as read using its original message ID, preventing reprocessing.
- Subworkflow Creation:
- Encapsulation: The entire processing chain for a single email (from
Get a Message
toMark Message as Read
) is grouped into a subworkflow. - Benefits: Modularity, easier management, and avoiding complexities with nested loops that might require resetting.
- Encapsulation: The entire processing chain for a single email (from
- Final Workflow Connection: The subworkflow is connected to the initial
Loop Over Item
node to process all detected invoices.
Testing and Verification #
- The user performs a test run with multiple test invoices, including one email with two invoices.
- Verifies that:
- All processed emails are marked as read in Gmail.
- The correct number of invoice files (four in the test case) are uploaded to Google Drive, named according to the specified scheme.
Conclusion and Future Steps #
- The system allows for quick, private, and free invoice automation.
- Limitations: Primarily focuses on PDF invoices; image-based invoices might require additional OCR steps not covered.
- Customization: The workflow can be adapted to various scenarios; testing and iterative improvement are encouraged.
- Support: Offers further consultation for complex business processes.
last updated: