Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS CURL Plugin
Last modified Tue, 13th Oct 2025.
You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /CURL/ChatGPT
Download this example: ChatGPT.zip
Project "ChatGPT.xojo_binary_project"
Class App Inherits DesktopApplication
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits DesktopWindow
Control InfoLabel Inherits DesktopLabel
ControlInstance InfoLabel Inherits DesktopLabel
End Control
Control SystemField Inherits DesktopTextArea
ControlInstance SystemField Inherits DesktopTextArea
End Control
Control Label2 Inherits DesktopLabel
ControlInstance Label2 Inherits DesktopLabel
End Control
Control UserField Inherits DesktopTextArea
ControlInstance UserField Inherits DesktopTextArea
End Control
Control Label3 Inherits DesktopLabel
ControlInstance Label3 Inherits DesktopLabel
End Control
Control ResultField Inherits DesktopTextArea
ControlInstance ResultField Inherits DesktopTextArea
End Control
Control Label4 Inherits DesktopLabel
ControlInstance Label4 Inherits DesktopLabel
End Control
Control DebugField Inherits DesktopTextArea
ControlInstance DebugField Inherits DesktopTextArea
End Control
Control Label5 Inherits DesktopLabel
ControlInstance Label5 Inherits DesktopLabel
End Control
Control Label6 Inherits DesktopLabel
ControlInstance Label6 Inherits DesktopLabel
End Control
Control OrganizationField Inherits DesktopTextField
ControlInstance OrganizationField Inherits DesktopTextField
End Control
Control Label7 Inherits DesktopLabel
ControlInstance Label7 Inherits DesktopLabel
End Control
Control BearerTokenField Inherits DesktopTextField
ControlInstance BearerTokenField Inherits DesktopTextField
End Control
Control RunButton Inherits DesktopButton
ControlInstance RunButton Inherits DesktopButton
EventHandler Sub Pressed()
me.Enabled = false
RunQuery
End EventHandler
End Control
Control ResultLabel Inherits DesktopLabel
ControlInstance ResultLabel Inherits DesktopLabel
End Control
Sub Finished(curl as CURLSMBS, ErrorCode as Integer)
// we got a result asynchronously
ResultLabel.Text = ErrorCode.ToString+" "+curl.LastErrorText
DebugField.Text = curl.DebugMessages
ResultField.Text = curl.OutputData
RunButton.Enabled = true
Var j As New JSONItem(curl.OutputData)
If j.HasKey("choices") Then
Var choices As JSONItem = j.Value("choices")
Var choice As JSONItem = choices.ChildAt(0)
Var message As JSONItem = choice.Value("message")
Var content As String = message.Value("content")
ResultField.Text = content
End If
Exception je As JSONException
// handle?
End Sub
Sub RunQuery()
Var curl As New CURLSMBS
Var MessageSystem as new JSONItem
MessageSystem.Value("role") = "system"
MessageSystem.Value("content") = SystemField.Text
Var MessageUser as new JSONItem
MessageUser.Value("role") = "user"
MessageUser.Value("content") = UserField.Text
Var messages as new JSONItem
messages.Add MessageSystem
messages.Add MessageUser
Var j as new JSONItem
j.Value("model") = "gpt-4o-mini"
j.Value("max_tokens") = 100
j.Value("messages") = messages
curl.SetOptionHTTPHeader _
array(_
"OpenAI-Organization: " +OrganizationField.text, _
"Authorization: Bearer " +BearerTokenField.text, _
"Content-Type: application/json" )
curl.OptionPostFields = j.ToString
curl.OptionURL = "https://api.openai.com/v1/chat/completions"
call CURLSMultiMBS.SharedInstance.AddCURL curl, AddressOf Finished
End Sub
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem HelpMenu = "&Help"
End MenuBar
Sign
End Sign
End Project
Download this example: ChatGPT.zip
The items on this page are in the following plugins: MBS CURL Plugin.