Hello, I am uploading a CSV file using the fileButton1 and I have neatly packed the data into an array. Now I want to create a prompt that loops the HTML description from the CSV file and sends the data to the OpenAi API. I want to save the result as a new CSV file after all lines have been processed.
I have connected the API and solve this already via Python using the Chat Completions, which can also be set in retool. As model I have gpt-3.5-turbo, role user and in content should now my prompt pure, which runs through the loop. I'm not sure right now if I have a thinking error, but how would you approach this? Completely write your own javascript without using the connection from Retool to OpenAi? Is that even possible what I have in mind? It would have to run asynchronously, is that even possible with Retool?
Just to confirm, is your question whether to write your own queries or use AI?
Yes, it is certainly possible to loop through a csv and send the data to an API. You could also use Retool's utils.exportData in an event handler or JS query to get the data back into a new csv. We have docs for writing Js loops in Retool here
I'll try to explain again in a simpler way what exactly I have in mind.
I have as an example a HTML description in which it says <p>The banana is blue and 30 cm wide, 20 cm high, 5 cm deep</p>.
With OpenAI I run the prompt via gpt-3.5-turbo that I want the color, width, height and depth to be output. About the Chat Completions I get then as answer: "Width: 30 cm; Height: 20 cm; Depth: 5 cm; Color: Blue;"
Then I use the ";" as separator to put the results into an array. I then want to write the individual results back into a CSV file.
What I can do is to send the query in Retool to OpenAI and get the result displayed, but only with a test text field.
What I need is to upload a CSV file, there are then as an example two columns: ID and HTML_Description. I would then have to pack these into an array and want to send each individual ID with the HTML_Description to OpenAI so that everything is processed there and I can then get the individual values back into a CSV file and download it.
Ah, I see . The data should be valid JSON, so it expects arrays with comma separated items ([item,item,item]) and objects formatted as {key: value}.
Can you use Javascript code to refactor the parsedValue?
Here's an example:
Otherwise, I believe we use PapaParse to generate the parsedValue. We do have the Papa.parse library built into Retool and that could be used in a transformer, or JS query. I haven't explored whether you can identify a different delimiter, but they will support some more specific CSV parsing and exporting options if you want to try doing your own parsing
I have now solved it a little differently . First of all I have the following JS
let parsed = fileButton1.parsedValue;
if (parsed && Array.isArray(parsed) && parsed.length > 0) {
let itemsArray = parsed[0]; // Da es eine zweidimensionale Struktur hat
let transformedItems = itemsArray.map(item => {
let [sku, descriptionStart] = item["SKU;HTML_description"].split(";");
let descriptionExtra = item["__parsed_extra"].join("");
let fullDescription = descriptionStart + descriptionExtra;
return {
SKU: sku,
HTML_description: fullDescription
};
});
return transformedItems;
} else {
return "No valid data";
}
and from there I take the data fitting into the next JS and trigger the openAI API