Building a Xojo App to Interact with OpenAI API

Written by on March 16, 2023 in OpenAI, Programming, Xojo with 1 Comment

A few weeks ago I registered with OpenAI to access its services through API and created a secret Key for my use. I then decided to build a simple application to try out the “Chat” and “Completion” models. Since I have several tools at my disposal (RAD Studio, LispWorks, Allegro CommonLisp, Visual Studio, and Xojo), I debated briefly as to which of the tools I should use for the current task. Normally I would have selected LispWorks or RAD Studio, but this time I decided to use Xojo. Xojo is multplatform (Desktop, Web and Mobile) enabled and fortunately I have a Pro license allowing me to build and deploy in all three formats.

Building the application itself was quite a breeze and there is nothing fancy in the programming logic. I am sharing the project so that you can take a look at what I have done and change the functionality to suit your requirements.

Here is the initial screen.

Initial Screen

Initial Screen

Although the API has several models and corresponding parameters to tweak, I decided to use just two models and two parameters. The “Chat” option uses the underlying “gpt-3.5-turbo” model while the “Completions” option uses “text-davinci-003” model. As of this writing I have not got access to the latest “GPT-4” model although it was released a couple of days ago. For exploring the API, it really doesn’t matter since we are not solving any specific problem.

The “Num Tokens” field is bound to “max_tokens” parameter, allowing us to control the maximum number of tokens that the system will generate. The “Temperature” field is mapped to “temperature” parameter, which decides how deterministic the system behaviour is. I keep it at “Zero” by default since I want crisp and precise response to my input. You can learn more about these options in the official documentation.

What you might find interesting is the “Result Type” option. Since I use Lisp a lot, I find it convenient to get the result as an “S-Expression” as it is easily parsed in the Lisp environment. Outside of Lisp, when I wish to use the result in another application, JSON format is quite useful. For all other use cases, I support the “Normal Text” option.

How to convey these format options to the API model? I chose a simple trick of prefixing all input text with an appropriate “hint” context:

S-Expression => “Give just the answer as S-Expression: ”
JSON => “Give just the answer as JSON: ”
Normal Text => “Give just the answer: ”

With this background, let us look at some interactions.
The following shows the use of “S-Expression” in the “Result Type”:

Result as S-Expression

Result as S-Expression

What is weird in this case is that we get a representation of the input and not the calculated value! Let us try another example:

Another S-Expression Example

Another S-Expression Example

This works as expected! We can confirm that the generated structure is correct by comparing it with Mathematica’s output for the same text:

Structure as Identified by Mathematica

Structure as Identified by Mathematica

Here is an example of “Completions” using “Normal Text” result type:

Normal Text Result Type

Normal Text Result Type

Let use “Chat” model instead of “Completions”:

Using Chat Model

Using Chat Model

Finally, here is an example that uses “JSON”.

Using JSON Type

Using JSON Type

As the above scenarios show, the API works as expected. I used Xojo 2022 Release 4.1 to build this application. You can download the project from here. If you are planning to build and run the application, make sure you get a valid API Key and use it in the project.

Have great day!

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

1 Reader Comment

Trackback URL Comments RSS Feed

  1. T Ashok says:

    An interesting experiment on ChatGPT this is to explore Xoxo!
    XOXO ! (aka Hugs and Kisses)

Leave a Reply

Your email address will not be published. Required fields are marked *

Top