Published on

Extracting Transaction details with GPT-3.5 Turbo or GPT-4

Authors

As a side project, I'm working on a personal finance app in which I might need to extract/parse some details from an email/SMS notification

If the content is not going to change anytime soon. Then I prefer using Regex to extract those details. However, as a fallback mechanism I'm planning to use GPT-3.5 Turbo (why not 🤖)

Here is a snippet and prompt on how to do it.

For the sake of simplicty, I'm using fetch here. You can also use their official SDK as well.

import fetch from 'node-fetch';

const OPENAI_API_KEY = process.env.OPENAI_API_KEY

const transactionParser = async (transaction) => {
    // Pass less context as possible to save some 🤑 
    const prompt = `Extract the transactional details from the input as json. The response should have accountNumber(last 4), description, amount (double), date(YYYY-MM-DD), time,currency, balance(double).`

    const response = await fetch("https://api.openai.com/v1/chat/completions", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${OPENAI_API_KEY}`
        },
        body: JSON.stringify({
            "model": "gpt-3.5-turbo",
            "messages": [
                { "role": "user", "content": prompt },
                { "role": "user", "content": transaction },
            ],
            "temperature": 0.7
        })
    })
    const { choices } = await response.json()
    return choices;
    
}

 const sampleTransaction = `Your ICICI Bank Credit Card XX3210 has been used for a transaction of INR 127.00 on Mar 23, 2023 at 12:40:41. Info: LINGAM VEGETABLES. The Available Credit Limit on your card is INR 1,50,321.18`


transactionParser(sampleTransaction)
.then(txn => {
  console.log(txn);
})

References

Happy parsing-with AI!