Analysing file with openai api, curl and php

I’m trying to upload a pdf file in order to ask chatgpt if the file contain a specific word.

To upload the file I’m using this code

$api_upload = '';
$pdfFile = 'myfile.pdf';

$ch = curl_init($api_upload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $api_key));
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'purpose' => 'assistants',
    'file' => new CURLFile($pdfFile, 'application/pdf'),

I get a valid uploaded file
and the status is proceeded.

To ask the question, I’m using this code

api_url = '';
$model = 'gpt-3.5-turbo';
$fileID // <- profide by previous call

$data = array(
'model' => $model,
'messages' => array(
    array('role' => 'system', 'content' => 'Vous êtes un assistant'),
    	'role' => 'user',
    	'content' => "Does this file contains the word: Hello",
   	'file_ids' => [$fileID]

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $api_key

But the chapgpt response is always, that I do not had provide a file to analyze (or something like this).

Can anybody help me.
I have more than 1000 file to parse and analyze :grimacing:


Hi @etienne.sobole

I don’t see any documentation on 3.5 API that refers to file search. I could be wrong but i don’t think it uses the files. Strange that you don’t get an error. (I assume you don’t)

Looking at the documentation , it seems it works only with assistants API

My thoughts: if you don’t want to use the assistants API, use something on your PHP side to read the file contents (probably text only, uggh… and/or extract images), include it in the system/user prompt and send the messages to the API

To have a longer context length (128K tokens), call GPT-4o from PHP

I’d recommend the assistants API though: it will handle text and images better than you reading the file yourself in PHP

Hope this helps!

I’ve tried gpt-4o with the same issue.
I’ve tried to use fine-tune purpose when I download the file but the result is the same.

If I try the online version of chatgpt it works very well.
I submit my file, I ask him my question and he analyzes and answers correctly.

I’m going to try with assistant even if I don’t know what it is at all :smile:

Hi again Etienne

I think uploading a file isn’t enough. You need to fine tune it. Or, put the contents into a vector search and then search it. This is from my limited understanding as, currently, I do not use files in my API implementation. I have a very use cases that do not need files (yet)

The assistants API makes all this seamless and can work with a large amount of files

In your CURL request, are you getting any errors/ warnings/ exceptions thrown back from OpenAI ? You could also ask it, in the prompt, if it is able to read the file to see if it contains the word “hello”, in your example, to see how it replies

Hope this helps

If you want to upload and do a Q&A on a PDF file, you will need to use the Assistants API. Using Chat Completions API will not work.

1 Like

The nice thing is there’s a php library to simplify the things for the Assistant API, the downside is OP didn’t know stuff about Assistants API. Hopefully he’ll find it best when he tries it. GitHub - openai-php/client: ⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.