Accounting with LLMs

Large Language Models (LLMs) have gained significant attention in every domain of profession. These sophisticated artificial intelligence models, such as OpenAI’s GPT, are transforming various industries, and accounting is no exception. In this article, we will explore the innovative ways in which LLMs can be utilized to enhance accounting processes. 

NLPs and LLMs

One useful distinction that will help us understand the tools that we will use below is the difference between Natural Language Processing (NLP) and Large Language Models (LLMs)

NLP is a field of artificial intelligence that focuses on the interaction between computers and human language. NLP is a broader field that encompasses the development of algorithms and models for a wide range of language-related tasks. These tasks can include language understanding, translation, summarization, sentiment analysis, and more. NLP focuses on making computers capable of working with and understanding human language in various contexts. LLMs on the other hand are a specific type of NLP model that uses deep learning techniques to process and generate human-like language on a large scale. These models are trained on vast amounts of textual data and can understand context, generate coherent text, and perform various language-related tasks. 

Tools / libraries like NLTK, Spacy, TextBlob, Gensim are used for Natural Language Processing. Using NLPs and deep learning techniques various LLM Models such as GPT, BERT, XLNet have been developed.

LLMs are already capable of Accounting

One of the primary challenges in accounting is the manual entry of vast amounts of financial data into systems, a process that is not only time-consuming but also prone to errors. LLMs can revolutionize this aspect by automating data entry and bookkeeping tasks. These models can analyze and interpret financial documents, invoices, and receipts, extracting relevant information with a high degree of accuracy. This not only saves time but also reduces the risk of human error, ensuring more reliable financial records.

If you have tried to use LLMs like Chat GPT for accounting purposes you might have already noticed that it actually is really good at processing information from documents and extracting them in the format you desire. In the example below I have used the text content of a pdf invoice from a vendor and asked Chat GPT to give me some particular information in a delimited format that can be used for accounting purposes. 

But that is not enough, is it? Ideally we would want this to work with some reliable python libraries so that we are able to (i) to replicate this process across many files, (ii) to make this work for many format of files like images, pdfs and document files, (iii) to integrate other useful python libraries and NLP tools for language processing or optical character recognition, (iv) to be able to transform the LLMs result into other file formats like Excel, CSV, json for as required by the dependent ERPs. – In short we would like to have the GPT API integrated with python in our local machine. That is not impossible – but will cost some money for the license. 

Unless, of course, we were using open source LLMs right in our local computer. Yes, that is possible.

Open Source LLMs and APIs

The vast number of open source LLMs, Audio Processing, Computer Vision and Multimodal Tools that are available in Hugging Face is simply hugging mind blowing. To have these resources floating around for free on the internet is such a blessing. There are many ways to use and try these many LLMs right in the hugging face website – and also there are multiple ways to use them in your local systems – however the performance would depend on the capacity of your system. 

But here we will discuss the recently announced ollama tool that will enable us to to use some of these LLMs right on our computer with APIs with Python programming. The step to use Ollama tool: 

  1. Install Python and VS Code on your system
  2. Download and install Ollama tool 
  3. Download the LLMs of your choice 

Start developing in python using Ollama APIs

An Example - but it may be taxing on your computer

Just like in the example with the chat GPT above I used the Ollama tool and its APIs to create a simple tool that will extract the relevant accounting related information from pdf invoices in a certain folder into a CSV file. Here is the python script if you want to try it in your system. Unfortunately it doesn’t work on the Google Colab Notebook because the size of the LLMs and the required libraries and tools are not compatible with the Google Colab. 

            import os
import csv
from PyPDF2 import PdfReader
import ollama

# Function to extract text content from PDF
def extract_text_from_pdf(pdf_path):
    with open(pdf_path, 'rb') as file:
        reader = PdfReader(file)
        text = ''
        for page in reader.pages:
            text += page.extract_text()
    return text

# Function to process PDF and extract information using Ollama API
def process_pdf(pdf_path):
    content_of_pdf = extract_text_from_pdf(pdf_path)
    question = "Extract information on Seller Name, Buyer Name, Invoice Number, Invoice Date, Invoice Description, and Invoice Amount from the text below in that specific order and join each element with ';'. Don't give me any other response.  " + content_of_pdf

    response = ollama.chat(model='llama2', messages=[
        {
            'role': 'user',
            'content': question,
        },
    ])
    
    print(response['message']['content'])
    return response['message']['content']

# Folder containing PDF files
pdf_folder_path = 'testFiles'

# CSV file to save the results
csv_file_path = 'output.csv'

# Header for the CSV file
csv_header = ['File Name', 'Seller Name', 'Invoice Number', 'Invoice Date', 'Buyer Name', 'Invoice Description', 'Invoice Amount']

# List to store results
results = []

# Process each PDF file in the folder
for pdf_file_name in os.listdir(pdf_folder_path):
    if pdf_file_name.endswith('.pdf'):
        pdf_path = os.path.join(pdf_folder_path, pdf_file_name)
        pdf_result = process_pdf(pdf_path)
        results.append([pdf_file_name] + pdf_result.split(';'))
    print("completed " + str(pdf_file_name))

# Write results to CSV file
with open(csv_file_path, 'w', newline='') as csvfile:
    csv_writer = csv.writer(csvfile)
    csv_writer.writerow(csv_header)
    csv_writer.writerows(results)

print(f"Results saved to {csv_file_path}")