OpenAI GPT content loop CSV File

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?

Hi @Konstantin_Kiss,

Thanks for reaching out!

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 :slightly_smiling_face:

Hi @Tess , thanks for your reply.

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.

Hi @Konstantin_Kiss

Thanks! :crossed_fingers: I believe I'm following the general idea -- you may need to modify some of the code here for your exact case:

Step 1:

Start with a CSV of ids & html descriptions:

Upload it to Retool using a file component with the Parse file setting checked ON:

Next, pass the data to OpenAI as a loop:

Transform each chat gpt response into an object:

You can then use utils.exportData on the Javascript loop query\

Hi @Tess

Thank you so much for your help, I think I'm on the right way!

Is it possible that your CSV has a different delimiter? I use ";" and I think that's why the parsing doesn't work properly.

I think I understood about the ExportData and solved it via a button, through that I then got the CSV.

Now I still have the problem with the column delimiter ";".

I changed my CSV and took "," as delimiter, so I could parse it correctly, there everything went through perfectly. But I need the parsed file as ";"

Hi @Konstantin_Kiss,

Ah, I see :disappointed:. 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

Hi @Tess ,

I have now solved it a little differently :see_no_evil: . 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

let file_data = transformCSV.data

const promises = file_data.map((row) => {
  return daveQuery.trigger({
    additionalScope: {
      id: row.SKU,
      description: row.HTML_description,
    }
  });
});

return Promise.all(promises)

I could not have done it without your help :see_no_evil:, thank you so much :partying_face:

1 Like

Thanks for sharing! Glad you have a solution! :tada: :raised_hands: