Using ChatGPT in Xojo

From time to time we get asked to provide an example for ChatGPT. Since this is just another web service, we can just handle it with the MBS Xojo CURL Plugin. But since we don’t like to block the user interface while ChatGPT processes the request, we use our CURLSMultiMBS class to run it in the background. Later when finished, it performs a delegate to call our Finished method to process the result.

The request is build with the JSONItem class to fill in the various values. We include a system message with the request, e.g. “Please translate text to English.” and then pass the text to translate in the user role. This way the user should not be able to provide instructions to the LLM in their text.

To test the project, please get an account for OpenAI. In the headers we need to pass your organization ID from and the bearer token you got there. Each query will use some tokens, but we limit the answer to 100 tokens.

Here is the full method to start the query:

Sub RunQuery()
	Dim curl As New CURLSMBS
	
	dim MessageSystem as new JSONItem
	MessageSystem.Value("role") = "system"
	MessageSystem.Value("content") = SystemField.Text
	
	dim MessageUser as new JSONItem
	MessageUser.Value("role") = "user"
	MessageUser.Value("content") = UserField.Text
	
	dim messages as new JSONItem
	messages.Add MessageSystem
	messages.Add MessageUser
	
	dim 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

Here is the finished method, which gets called when the transfer is done. The script parameter is the CURL session reference number. We query debug messages and the result as JSON. We can then pick the output message from the JSON and show it in the field:

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
	
	Dim j As New JSONItem(curl.OutputData)
	
	If j.HasKey("choices") Then
		Dim choices As JSONItem = j.Value("choices")
		Dim choice As JSONItem = choices.ChildAt(0)
		Dim message As JSONItem = choice.Value("message")
		Dim content As String = message.Value("content")
		
		ResultField.Text = content
	End If
	
	Exception je As JSONException
	// handle?
End Sub

Please try and see whether this can help you add ChatGPT to the projects. Translation is a good thing and you can even specify whether you like formal or informal text. We will include the example file with the next plugin for you to use.

6 Likes

You know I’m fine using plugins when I need to, but is there something CURLMBS offers here for interacting with the ChatGPT API that URLConnection does not?

The blog article is about showing how to do it with CURL functions.

While normal people may not see a difference, some clients may enjoy some of CURL’s options for proxy handling, TLS or DNS options and go around Apple’s NSURL security limitations.

2 Likes

You can ask ChatGPT to convert the code to use URLConnection.

Sorry, I couldn’t resist. URLConnection should be fine.
image

6 Likes

I use chatgpt + customized AI models (GPT’s) focused on medicine within my app. Uses URLConnection inside a custom server control… works like a breeze, nothing else is needed.

Initial Q&A screen

Previous questions & chats stored per user

5 Likes

Hello Svein,

are you sharing the Code?

Regards Thomas

Hello Christian,

do you have plans to use CURL in Android?

Regards Thomas

Sorry, but we have not yet seen a Plugin SDK for Android.

Thanks Christian,

I did not notice that! I will try a HtmlViewer.

Hello Sveinn, I solved it.
I have seen in the moment the URLonnection.

1 Like

Glad to hear, I can provide you with code samples in PM if you need.

1 Like

Hello Sveinn, it would be great to have some code to learn how to use CHATGPT-4o.

Thank you for your help
Thomas

nauck.tna@gmail.com

Hi @Sveinn_Runar_Sigurdsson

I had trained some GPTs with my ChatGPT PLUS subscription for varios Bariatric Surgery Heathcare Centers. Using the OpenAI interface they are doing really well. It respect articule the instructions an mecer alucinates.

But as they are orientated for patients use, not all patients have a PLUS subscription, and with the free one I am having two limitations: 1) if the model used is gpt-4o the limited quantity of tokens available for Free subscription lets patients only be able to ask 5 to 8 questions máximum before arriving to the 4096 tokens máximum available, and 2) in the other hand, if the model used is GPT-3.5 the trained GPT not always adjusts itself to the instructions and some times alucinates.

So I want to solve this using OpenAI’s APIs

Just for test purposes I used the Xojo AI example. It works fine just using my APIkey with any of the Standard models (gpt-3.5-turbo or gpt-4o) but not with the trained model. The only one thing I changed was the model, from GPT-3.5-turbo to the ID of the personalized GPT.

I notice that you said that you have no problem on using custom trained GPTs.

Any advise in order to let me do it with my one GPTs ???

the new responses api is better, it can query web (optional) too.
just disable the event base response and you get a single json as before. and a bit renaming.
https://platform.openai.com/docs/api-reference/responses

responses api can also process pdf documents.
completions could only images and text.

Probably I was not explained well my problem.

In this example:

I only have to include my OpenAI APIKey here:

and the application works fine.

BUT I don’t want to use any of the OpenAI standard models, I want to use my own custom GPT, in my case the following one:

where the link (you could check that it is available for anyone who have the URL link) is:

So in order to access directly using APIs and the XOJO App I changed the model here:

The ID of the model I understand is:
g-681649aaff14819186550380b4b66e6d
but I receive the following error:
The model g-681649aaff14819186550380b4b66e6d does not exist or you do not have access to it.

I also make different combinations on how to pass the ID Model, like:
j.Value(“model”) = “gpt-681649aaff14819186550380b4b66e6d”
j.Value(“model”) = “681649aaff14819186550380b4b66e6d”
j.Value(“model”) = “g-681649aaff14819186550380b4b66e6d-bari-center”
j.Value(“model”) = “gpt-681649aaff14819186550380b4b66e6d-bari-center”

And none of them works.

Anyone knows the way to avoid this in order to use a personal trained GPT model in a XOJO App ?

I believe you are talking about an Assistant and afaik they are only available via a BETA API (https://platform.openai.com/docs/assistants/quickstart).

1 Like

Use the openai api playground page first

Thanks @MarkusR
Please tell me if I understand the difference between a Custom GPT and a Playground Assistant. The former is to be used with the OpenAI interface and not with the APIs an the Assistants are similar ones but to be used using the APIs. And that may be that why the message I receive is that the Model (the custom GPT) is not accesible.
Am I right ?

i cant create a Custom GPT because i not have plus.
Playground is just the web page for testing all api interfaces, to see the request and response data.
i would expect that you then can select your model there.

@Mariano_Poli

oki, this is the issue : you cannot call custom GPTs (from chat.openai.com) by ID via API.

The custom GPTs you create inside ChatGPT (e.g. via the “Explore GPTs” tab) cannot be used directly via the OpenAI API, including in Xojo or any external application. These GPTs are currently only accessible within the ChatGPT interface itself, not via https://api.openai.com.

  • Use OpenAI Assistants API instead, to get the job done (recommended)
  • Use a detailed system prompt, each time

Lastly, you need to implement max-question per user per time interval as well, the cost can quickly accumulate other-wise.

Sincerely,
Svenni