one sec

- 5 mins

Sometime in summer 2020, my friend George pitched a side-project idea to me based on what he was learning in his accounting class.

He thought we could use openAI’s GPT-3 to let anyone generate accounting statements. We spent a weekend hacking it together and ultimately built this:

Frankly, the results completely blew us away. Within a really small amount of time, we were able to build a demo with a really compelling use case.

Half a year later, we still weekly recieve emails/messages asking us to explain how exactly we built this. While we’ve briefly explained it in an article on InfoQ, we wanted to explain our thought process further here.

[If you’d rather do your own digging instead, the code is fully open source here. (ADD A LINK) (ADD ADD ADD LINK)]

GPT-3

If you have no idea what GPT-3 is, here’s a simple explaination from the openAI blog:

Given any text prompt, [GPT-3] will return a text completion, attempting to match the pattern you gave it.

So for example, you get GPT-3 to write code for you by prompting it with examples of code snippets.

In our case, we prompted GPT-3 with a text pattern containing 4-5 examples of input and output pairs. This what 1 input/output pair looks like:

Q: I bought a $30000 car on credit to be repaid in 5 years. 
A: [["add", 30000, "Property, Plant, Equipment"], ["add", 30000, "Long-Term Debt"]]

From the demo, you can see that our input describes a transaction in plain English. This was pretty easy, we just described that as our “question”.

The output was a little more complicated. We knew we had to generate some command to update a spreadsheet. Frankly, this took many iterations to get right. Approaches like outputting a full Python script, a terminal command, and a csv file didn’t work well enough.

Ultimately, we decided the best option was to generate a 2D Python array. Each element in the larger array is an area of the balance sheet to be updated. The inner arrays contain three elements: add/remove from a section, by how much, and what section of the balance sheet respectively.

We then generated a few examples, and our fine-tuning data looked like this:

Q: I bought a $30000 car on credit to be repaid in 5 years. 
A: [["add", 30000, "Property, Plant, Equipment"], ["add", 30000, "Long-Term Debt"]]

Q: I bought $20000 of equipment to be repaid in 2 months. 
A: [["add", 20000, "Property, Plant, Equipment"], ["add", 20000, "Short-Term Loans"]]

Q: A customer owes me $150 for his purchases. 
A: [["add", 150, "Accounts Receivable"]]

Q: I bought an additional $12000 worth of inventory which I paid for immediately.
A: [["add", 1200, "Inventory"], ["remove", 12000, "Cash"]]

Q: I bought new furniture for the store that cost me $99 in cash.
A: [["add", 99, "Property, Plant, Equipment"], ["remove", 99, "Cash"]]

Q: I just took out a loan for $12900 that I need to pay back in 3 years.
A: [["add", 12900, "Long-Term Debt"], ["add", 12900, "Cash"]]

Q: I prepaid $12000 for the rent for the next 3 months.
A: [["add", 12000, "Prepaid Expenses"], ["remove", 12000, "Cash"]]

Once we had designed our prompts to a point where we were happy with them. We spent a bit of time tweaking parameters and ultimately created a basic API request to be called every time we wanted to ask a new question:

def getGPT3(request):

	# setup key and fine tuning data
	key = open("key.txt", "r")
	fineTuneData = open("fineTuneData.txt", "r")
	question = "Q: " + request
	openai.api_key = key.read()

	# request completion from GPT-3
	output = openai.Completion.create(
	  engine="davinci",
	  prompt= fineTuneData.read() + question + "\n",
	  max_tokens=100,
	  temperature=0.4,
	  stop=["Q: ",'\n']	
	)

	# process output
	try:
		output = json.loads(output["choices"][0]["text"].replace('A:',''))
	except:
		output = output["choices"][0]["text"].replace('A: ','')


	return output

That’s pretty much it! Since we designed this bot, there’s been a lot of progress in the community to make prompt design much easier such as chronology. I’d also recommend reading Russel Foltz-Smith’s great article on the fundamentals of GPT-3 to help you better understand the ins and outs of effective prompt design.

This demo shows just how powerful GPT-3 can be in the field of accounting and finance. Despite it being just a demo, the results it provided were remarkable. This can further be expanded out to fill out more fields of an accounting balance sheet and further on to other financial statements, including income, cashflow, and shareholder’s equity statements. GPT-3 is showing us that things that we (even recently) thought were impossible can be done with the expansion of AI.

rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora