Xojo wrapper for the OpenAI API

OpenAI.APIKey = "Your Key Here"
Dim reply As OpenAI.Response = OpenAI.Completion.Create("What is Xojo?")
Dim answer As String = reply.GetResult(0)
OpenAI.APIKey = "Your Key Here"
Dim reply As OpenAI.Response = OpenAI.Image.Generate("A typical Xojo user")
Dim img As Picture = reply.GetResult(0)


You are using Dim. Does this mean it is not API2 ready?

1 Like

I still code primarily in an ancient version of RealStudio, and only test in Xojo to make sure nothing breaks. TBH I’m not even sure what being API2 ready entails.


For all intents and purposes, Dim = Var. after 22 years using the product, I can’t convince myself to use Var and you shouldn’t feel that you have to either.

And no, I do not believe that Dim will ever be removed from the language. It’d break everyone’s projects all at once.


As I use Var everywhere except Xojo, I threw Dim away for convenience, converging the resemblance of several languages.


I tried that for a while. What I found is that because I use so many different yet similar languages now, It’s handy to use Dim in Xojo because it helps me remember which syntax I’m supposed to be using.

Over the last year, I’ve been using Groovy (Jenkins), Bash, C#, Python and Perl at work and Xojo for my personal projects. I still find myself putting semicolons at the end of Xojo lines sometimes.

I suffered that years ago. Also If () { and later returning to put a Then there

Now sometimes I see myself forgetting putting semicolons in SQL transactions.

I kinda dislike Dim because DIMENSION was created to set just the size of arrays, then later people borrowed it to declare Vars. In the beginning, a var was just set attributing a content to a valid symbol like NAME$ = “Name” : VALUE% = 10

Saying DIMENSION X AS INTEGER = 10 is kinda ugly to my eyes.


Oh yes, the THEN, it is the most terrible! ;o)

I haven’t tried it yet, but frankly, a big congrats Andrew! Just knowing that there is already a plugin, and by someone experienced, makes us think of all the possibilities it can bring to our products! :slightly_smiling_face::clap::clap:


New updates:

  Dim reply As OpenAI.ChatCompletion = OpenAI.ChatCompletion.Create("user", "Hello, I've come here looking for an argument.")
  Dim chatresult As String = reply.GetResult() ' assistant: No you haven't!
  reply = reply.GenerateNext("user", "Yes I have!")
  chatresult = reply.GetResult() ' assistant: Sorry, is this the 5 minute argument, or the whole half hour?
 Dim url As String = "https://upload.wikimedia.org/wikipedia/commons/9/99/Aerial_view_of_the_White_House.jpg"
 Dim response As OpenAI.Response= OpenAI.ImageRecognition.Create("What is this a photo of?", url)
 Dim answer As String = response.GetResult() ' This is an aerial photo of the the White House in Washington, DC.
  Dim file As FolderItem = SpecialFolder.Desktop.Child("speech.mp4")
  Dim subtitles As String = OpenAI.AudioTranscription.CreateRaw(file, "srt")
 Dim txt As String = "This is a test of the OpenAI speech synthesis API."
 Dim voice As String = "alloy"
 Dim model As String = "tts-1"
 Dim response As OpenAI.Response = OpenAI.AudioGeneration.Create(txt, model, voice)
 Dim mp3audio As MemoryBlock = response.GetResult()
  • Removed Completion endpoint. Use the ChatCompletion endpoint

New updates:

  • Added Embedding endpoint. An embedding is basically a list of numbers generated from a text that can be compared to other lists generated from other texts to determine the “distance” between them. -1.0 is furthest, +1.0 is closest. Once generated you can save/reload an embedding for future comparisons.
  Dim txt1 As String = "Hello, my name is Alice."
  Dim txt2 As String = "Hello, my name is Bob."
  Dim model As OpenAI.Model = "text-embedding-ada-002"
  Dim embed1 As OpenAI.Embedding = OpenAI.Embedding.Create(txt1, model)
  Dim embed2 As OpenAI.Embedding = OpenAI.Embedding.Create(txt2, model)
  Dim distance As Double = embed1.DistanceFrom(embed2) ' 0.9008139275638367 (very similar)
  • Added FineTuneJob endpoint. This allows you to train one of the base AI models on lots of example prompts/responses that suit your particular use case. You can then use your trained model with the ChatCompletion endpoint.
  ' first, create a local file containing your training lines
  Dim trainer As FolderItem = SpecialFolder.Desktop.Child("trainer.jsonl")
  Dim ftdata As OpenAI.FineTuneData = OpenAI.FineTuneData.Create(trainer)
  ftdata.AddLine("You are anwsering riddles.", "What has to be broken before you can use it?", "An egg")
  ftdata.AddLine("You are anwsering riddles.", "I'm tall when I'm young, and I'm short when I'm old. What am I?", "A candle")
  ftdata.AddLine("You are anwsering riddles.", "What month of the year has 28 days?", "All of them")
  ' etc. minimum 10 lines, ideally 100's or 1000's
  ftdata.Save(trainer, True)
  ' then upload the training file to OpenAI
  Dim file As OpenAI.File = OpenAI.File.Create(trainer, "fine-tune")
  ' then create the fine-tuning job
  Dim basemodel As OpenAI.Model = "gpt-3.5-turbo-1106"
  Dim job As OpenAI.FineTuneJob = OpenAI.FineTuneJob.Create(file, basemodel)
  ' the fine-tune job is now pending. It may take a long time to complete.
  ' some time later...  
  If job.Status = OpenAI.JobStatus.Succeeded Then
    ' use the trained model in your requests
    Dim mymodel As OpenAI.Model = job.FineTunedModel
    Dim chat As OpenAI.ChatCompletion = OpenAI.ChatCompletion.Create("user", "What month has 28 days?", mymodel)
    Dim answer As String = chat.GetResult() ' assistant: All of them.
  End If

Its possible, extend your project for using with GPT4All? I think, it uses also OpenAI API with little modifications … or not?

From what I can tell, GPT4All runs locally on your machine. This wrapper is for the internet-based API.