B4J Question Upload a file to OpenAI

Tim Chapman

Active Member
Licensed User
Longtime User
I am trying to upload a file to OpenAI per the instructions here:

I have been trying to use the OpenAI library as an example and have made this code:

B4J code:
Sub Process_Globals
    Private fx As JFX
    'Private MainForm As Form
    Dim apiKey As String = "MyApiKey"
End Sub

Sub AppStart (Form1 As Form, Args() As String)
'    MainForm = Form1
'    MainForm.Show
    UploadFile
End Sub

Sub UploadFile
    Dim j As HttpJob
    j.Initialize("", Me)
    Private parameters As List
    parameters.Initialize

    ' Path to the file you want to upload
    Dim filePath As String = File.Combine("D:\Downloads", "Anne Business Plan.pdf")
    
    parameters.Add(CreateMap("purpose" : "assistants", "file": filePath))
    
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize2(parameters)
    Log("JSON Parameters: " & JSONGenerator.ToPrettyString(4))
    Dim chat_string As String
    chat_string = JSONGenerator.ToString
    Log("Parameters = " & parameters)
    j.PostString("https://api.openai.com/v1/chat/completions", chat_string)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization", "Bearer " & apiKey)

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("OpenAI: ChatResponse ready!")
        Dim response As String = j.GetString
        Log("OpenAI File Upload Response: " & response)
            'ParseJson(response))
    End If
    j.Release
End Sub

The log shows:

WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
JSON Parameters: [
{
"file": "D:\\Downloads\\Anne Business Plan.pdf",
"purpose": "assistants"
}
]
Parameters = (ArrayList) [{purpose=assistants, file=D:\Downloads\Anne Business Plan.pdf}]
ResponseError. Reason: , Response: {
"error": {
"message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)",
"type": "invalid_request_error",
"param": null,
"code": null
}
}

What am I missing about getting the JSON format right?
 
Solution
I got this to work with help from Perplexity AI:

B4J code:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private hc As HttpJob
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    UploadFile
End Sub

Sub UploadFile
    Dim purpose As String = "assistants"
    'Dim filePath As String = "D:/Downloads/Anne Business Plan.pdf"
    Dim apiUrl As String = "https://api.openai.com/v1/files"
    Dim apiKey As String = "API KEY GOES HERE"

    hc.Initialize("job1", Me)
    
    ' Correctly create the MultipartFileData
    Dim mfd As MultipartFileData
    mfd.Initialize
    mfd.Dir = "D:\Downloads" ' Directory where the file is located
    mfd.FileName = "Anne Business Plan.pdf" '...

Tim Chapman

Active Member
Licensed User
Longtime User
I got this to work with help from Perplexity AI:

B4J code:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private hc As HttpJob
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show
    UploadFile
End Sub

Sub UploadFile
    Dim purpose As String = "assistants"
    'Dim filePath As String = "D:/Downloads/Anne Business Plan.pdf"
    Dim apiUrl As String = "https://api.openai.com/v1/files"
    Dim apiKey As String = "API KEY GOES HERE"

    hc.Initialize("job1", Me)
    
    ' Correctly create the MultipartFileData
    Dim mfd As MultipartFileData
    mfd.Initialize
    mfd.Dir = "D:\Downloads" ' Directory where the file is located
    mfd.FileName = "Anne Business Plan.pdf" ' Name of the file
    mfd.KeyName = "file" ' The key name for the file in the form data
    mfd.ContentType = "application/pdf" ' The content type of the file
    
    hc.PostMultipart(apiUrl, CreateMap("purpose": purpose), Array(mfd))
    hc.GetRequest.SetHeader("Authorization", "Bearer " & apiKey)
End Sub

Sub JobDone (Job As HttpJob)
    If Job.Success Then
        Log(Job.GetString)
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub
 
Upvote 0
Solution
Top