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.
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”:
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:
This works as expected! We can confirm that the generated structure is correct by comparing it with Mathematica’s output for the same text:
Here is an example of “Completions” using “Normal Text” result type:
Let use “Chat” model instead of “Completions”:
Finally, here is an example that uses “JSON”.
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!
An interesting experiment on ChatGPT this is to explore Xoxo!
XOXO ! (aka Hugs and Kisses)