Is it possible to request a csv file as payload to chatgpt via API?

I have simple sheet with some financial data like Date, Category, Amount, Name, Tag for personal expenses control.

My idea is to provide some questions about this sheet and chatgpt give me answers (eg. give me the 3 most expenses of this month)

I’ve been searching for how to send this file, however, I have not find any example like this.

Currently, I’m converting the csv as json beautified and send it as part of the prompt, but it seems weird.

Anyone have any example on how to attach and send?

response = openai.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are an financial advisor and you have to analyze the JSON object attached"},
        {"role": "user", "content": "Analyze the follow JSON and consider negative values as expenses and positive as incomes"},
        {"role": "user", "content": prompt}
    ],
    temperature=0.7
)

Thanks in advance!

Have you tried dumping the CSV contents into the system prompt? Then ask away.

1 Like

Yes, I did. By some reason, the responses are not so accurate and sometimes the same question has different answers. Any idea what it could be?

It could be your temperature settings. Try setting to 0.

Also it could be how the data is formatted. Try repeating the column headers between each data line and see if that helps.

Example:

Headers
Data Line 1
Headers
Data Line 2
Headers
….

The model may get confused spatially and needs frequent headers to guide it? Just a guess.

If this doesn’t work, then I would go back to an array of JSON structures, one for each line. Or try a single JSON with an array of values for each heading. One of these will resonate with the model if it’s a formatting issue.

2 Likes

Welcome to the dev forum @hugoalves

You cannot directly upload CSV and have the model use it unless you use Assistants.

Given how CSV is a structured data format, you might not even need to send the whole CSV file to the model.

Just include the structure of the CSV in the system message and have the model use function calling to query (using code) the file residing on your system and give the model its results back for it to respond.

3 Likes