AI-Quickstart/demo/sortTransactions.js
2024-06-13 13:22:17 -07:00

29 lines
1.1 KiB
JavaScript

import returnCategoriesCSV from './openai.js';
export default async function (transactions, categories) {
// Split the transactions into array elements and pull out headers
transactions = transactions.split('\n');
const headers = transactions.shift();
// Group the transactions into strings containing chunks of 10 transactions each
const chunks = [];
for (let i = 0; i < transactions.length; i += 10) {
chunks.push(transactions.slice(i, i + 10).join('\n'));
}
let categorizedTransactions = []
for(let i = 0; i < chunks.length; i++) {
const returnedCSVText = await returnCategoriesCSV(headers+',Category\n'+chunks[i], categories);
const returnedTransactions = returnedCSVText.split('\n').slice(3); // Remove blank lines and headers return array
categorizedTransactions = categorizedTransactions.concat(returnedTransactions);
}
// Add headers back to the transactions
categorizedTransactions.unshift(headers+',Category');
const finalCSV = categorizedTransactions.join('\n').toString()
return finalCSV
}