Large reports, contracts, or documentation files often contain more information than can be quickly processed. Wouldn’t it be nice if you could simply upload a document PDF from your 4D application and let AI help you summarize it, extract key data, or answer questions about its content?
Thanks to the new OpenAI.files class, this workflow is now very simple. You can seamlessly upload files and use AI to interact with their content in many ways.
Step 1 – Upload your document
The OpenAI.files.create() function lets you upload a file that can later be used across OpenAI endpoints.
Here’s an example that uploads a PDF file and sets its purpose to user_data:
var $file := File("/RESOURCES/MeetingReport.pdf")
var $clientAI : cs.AIKit.OpenAI:=cs.AIManagement.new().clientAI
// Upload the file
// $file: the 4D.File object representing the local document
// "user_data": the intended purpose of the file
// expires_after: optional policy to automatically delete the file after 1 hour
var $result:=$clientAI.files.create($file; "user_data"; {expires_after: {anchor: "created_at"; seconds: 3600}})
//$result.file{
// "object":"file",
// "id":"file-8WJG5F9PYR3SCbL2dKJ5b2",
// "purpose":"user_data",
// "filename":"2510.07311v1.pdf",
// "bytes":912072,
// "created_at":1760433437,
// "expires_at":1760437037,
// "status":"processed",
// "status_details":null
//}
Once uploaded, the result contains detailed information about the file, including its id, which will be used later.
⚠️ Important: Not all file types are supported by every model. Always check the documentation of the model you’re using to ensure your files are compatible.
Step 2 – Link the file to your message
After the file is uploaded, you can associate it with a message using the OpenAIMessage.addFileId() function. This tells the AI model to consider the content of the uploaded file when generating its response.
var $clientAI:=cs.AIManagement.new().clientAI
var $chatHelper:=$clientAI.chat.create("You are an assistant specializing in file analysis and parsing.")
// Create a new OpenAIMessage object with role "user" and a prompt
var $message:=cs.AIKit.OpenAIMessage.new({role: "user"; content: "Could you summarize this document?"})
// Attach the uploaded file to the message by adding its file id
// This tells the AI which document should be summarized
$message.addFileId($result.file.id)
// Send the message to the AI chat helper and store the summarized text
var $summary:=$chatHelper.prompt($message)
The model will analyze the content of the uploaded file and return a concise summary of its main points.
Step 3 – Delete the file from the server
Once the summarization or analysis is done, you may want to delete the uploaded file to free space or maintain privacy. You can do this using the OpenAI.files.delete() function:
// Delete the uploaded file using its ID
var $delete:=$ClientAI.files.delete($result.file.id)
Manage uploaded files
After uploading a file, you can list all files in your organization or retrieve details about a specific file.
List all uploaded files
// List all files uploaded by your organization
var $filesList:=$clientAI.files.list()
Retrieve a specific file
// Get detailed info about a single file using its id
var $fileInfo:=$clientAI.files.retrieve($result.file.id)
Conclusion
By combining OpenAI.files.create() and OpenAIChatHelper.prompt(), you can offer a seamless document analysis feature in your 4D applications. Users simply upload a file, and within seconds, receive a clear and accurate summary powered by AI.
But it doesn’t stop at summarization. The same approach can be used to extract specific information, answer questions, or search for particular details within a document — all without manually reading through it. This opens the door to countless possibilities: automatic meeting minutes, fast onboarding to long technical documents, or quick extraction of key data — all within your 4D environment.
