How to upload word docx file and ask question on the document

Below is the code: I am upload file from HTML page and sending to PHP file.
I tried several ways, is this the wrong way to do it?
getting error : $upload_response[‘id’] is not set
{ “error”: { “message”: “‘file’ is a required property”, “type”: “invalid_request_error”, “param”: null, “code”: null } } Error: Failed to upload document.

Index HTML:

<!DOCTYPE html>
<html lang="en">

	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Upload MS Word Document</title>
	<script src=""></script>

	<h2>Upload MS Word Document</h2>
	<input type="file" id="word_document" name="word_document" accept=".docx">
	<button id="uploadButton">Upload Document</button>
	<div id="summary"></div>

		$(document).ready(function () {
			$('#uploadButton').click(function () {
				var formData = new FormData();
				var file = $('#word_document')[0].files[0];
				formData.append('word_document', file);

					url: 'process.php',
					type: 'POST',
					data: formData,
					processData: false,
					contentType: false,
					success: function (response) {
						$('#summary').html("<strong>Document Summary:</strong> " + response);
					error: function (xhr, status, error) {




// Function to call OpenAI API
function call_openai_api($endpoint, $data) {
    $api_key = 'My API KeY'; // Replace with your OpenAI API key
    $url = '' . $endpoint;

    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($ch);
	echo $response;
    return json_decode($response, true);

// Function to generate MCQ questions
function generate_mcq_questions($file_path) {
    // Upload document
    $document_content = file_get_contents($file_path);

    $upload_response = call_openai_api('files', array(
        'purpose' => 'fine-tune',
        'file' => array(
            'data' => $document_content,
            'filename' => basename($file_path),
            'name' => 'word_document' // Specify the name property for the file data

    // Check if upload was successful
    if (!isset($upload_response['id'])) {
        return "Error: Failed to upload document.";

    // Generate MCQ questions
    $systemPrompt = "Create 2 MCQ questions on the provided document. The resulting JSON object should be in this format:";
    $userPrompt = "{'questionArray':[{ 'questionText':'Question text here', 'options':[ {'text':'','isCorrect':true}, {'text':'','isCorrect':false}, {'text':'','isCorrect':false}, {'text':'','isCorrect':false} ], 'correctFeedback':'', 'inCorrectFeedback':'' }]}";

    $mcq_response = call_openai_api('completions', array(
        'model' => 'gpt-4-turbo-preview',
        'temperature' => 0,
        'documents' => [$upload_response['id']],
        'max_tokens' => 100,
        'messages' => array(
                'role' => 'user',
                'content' => $userPrompt
                'role' => 'system',
                'content' => $systemPrompt

    return $mcq_response['choices'][0]['message']['content'];

// Validate uploaded file
if (isset($_FILES['word_document']) && $_FILES['word_document']['error'] === UPLOAD_ERR_OK) {
    $file_name = $_FILES['word_document']['name'];
    $file_size = $_FILES['word_document']['size'];
    $file_tmp = $_FILES['word_document']['tmp_name'];
    $file_type = $_FILES['word_document']['type'];

    // Check file extension
    $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
    $allowed_extensions = array('docx');
    if (!in_array($file_ext, $allowed_extensions)) {
        echo "Error: Only .docx files are allowed.";

    // Check file size (max 5 MB)
    $max_file_size = 5 * 1024 * 1024; // 5 MB
    if ($file_size > $max_file_size) {
        echo "Error: File size exceeds maximum limit (5 MB).";

    // Upload and summarize the document
    $upload_dir = 'uploads/';
    $upload_path = $upload_dir . $file_name;

    if (move_uploaded_file($file_tmp, $upload_path)) {
        $question = 'What is the summary of this document?';
        $summary = generate_mcq_questions($upload_path);
        echo $summary; // Output the summary
    } else {
        echo "Error: Failed to upload file.";
} else {
    echo "Error: No file selected or an error occurred while uploading.";
type or paste code here 
1 Like

The code contains wildly inaccurate employment of OpenAI APIs.

  • Uploading a file for fine-tune (which would be rejected)
  • Calling the completions endpoint for a chat model
  • Completions API can’t access “files”
  • Client-side code

The AI that wrote " Replace with your OpenAI API key didn’t have a clue…

The chat completions endpoint does not accept files or “uploads”. You must place plain text into the context as a message also. Code that can extract documentation and provide the AI an appropriate amount of it based on semantics would be required.

The Assistant API endpoint framework can accept files uploaded for “assistants” and retrieval. I’m fearful that the multitudinous methods that are required to operate this API would lead to a path of more frustration.

You might need to reset your elaborate ideas about file upload, and go to the API Reference or Documentation links on the left bar of this forum. Get the AI talking to you first in chat completions. Then you can proceed to the more elaborate area of document-augmented generation or search functions, using extracted text parsed to plain language.