B4J Library OpenAI library

Hello All,
Here is my OpenAI library and example code to interface with the library. I started this on August 11.
I started with the library from Blueforcer here: https://www.b4x.com/android/forum/threads/b4x-openai-a-i-text-image-generation.159877/
His example code there was copyrighted. His library code was not.
I have included none of his example code. Thus, my example code is free for anyone to use as is the library. My library is not backwards compatible with his.

I am not a very experienced programmer so I hope everyone finds it acceptable. It all works correctly as is, but there is so much coming from OpenAI that I can't keep up.
I welcome help in continuing this library.

Attached you will find my example code: OAIUtils.zip and the OpenAI library.

I have added a todo list at the bottom of the example code if anyone is interested. Please post in this thread if you take on any todo items or other improvements to the library or example code so no one else will duplicate your effort.
I will help all I can if anyone has questions. I have learned way more than I ever thought I would about OpenAI.
I think the library will work with B4A, but I am not sure. I have not tested that.

I have uploaded the Objects folder as well. This is because I can't write to File.Exists(DirApps, FileName). So, I had to put files that the program requires in a folder that could be easily found. Again, if anyone has a better way for me to do this stuff, please let me know.

28 OCT 2024: I am starting a project actually using the code I wrote and finding small bugs. I will publish updates as soon as I find a bug. So small changes will be released often.

THE LATEST VERSION IS ATTACHED.
 

Attachments

  • OAIUtils.zip
    86.4 KB · Views: 77
  • OpenAIUtils Works.zip
    96.5 KB · Views: 71
  • OpenAI.b4xlib
    11.1 KB · Views: 60
Last edited:

Tim Chapman

Active Member
Licensed User
Longtime User
I have fixed the Create Vector Store operation.
I have cleaned up the example code by making one Pane handle all tables.
I have added right click help for each node in the Visual Designer.
This is version 1.1.
 

Tim Chapman

Active Member
Licensed User
Longtime User
Here is how the code works:
There is the example code (Main module) and the OpenAI library (OpenAI module).
The flow starts with a user clicking on a button in the UI.
List Assistants / Select Assistant button for example.
This calls the Sub btnListAssistants_Click.

The Sub btnListAssistants_Click calls the Sub ShowTable("Assistant List"). This sub sets up the table to show the list of Assistants.
Next, the Sub btnListAssistants_Click calls the Sub ListAssistants in the OpenAI module.
The Sub ListAssistants calls the OpenAI API with a Job.Name created in the Sub.

The response from OpenAI is in JSON format and has an object name among other details.

The Sub JobDone in the OpenAI module receives the response from OpenAI.
The Job.Name is Selected by Select...Case.
The first Select...Case checks for speech and image response. They have special handling because they will next return a non-JSON object.

The second Select...Case falls into a Case that has many job names.
This Case is needed so that the object in the JSON response will be changed to the Job.Name.
The reason for this is because the OpenAI API returns the same object name for many calls to the API. In this example the returned Object is "list".
This is not helpful because we need a unique object in the Main module. So, the Object is changed to the Job.Name so it will be a unique object.

The Sub Job.Done in the OpenAI module calls the Sub OpenAI_Response in the Main module and passes the same JSON that came down from the OpenAI API with the modified object.

The Sub OpenAI_Response in the main code Selects on the object using Select...Case. In this case "ListAssistants" is the modified object.
The code in the Sub OpenAI, Case "ListAssistants" callse the Sub ParseAssistantList.
The Sub ParseAssistantList parses the returned JSON and creates rows in the table that was set up earlier.
The table shows the list of Assistants and the job is done.

This is the typical loop that the code follows.

There is also a google search option that I added to the Main module. The reason for the google search part of the code is because there is a cutoff date on the knowledge in OpenAI. Anything that occurred after the knowledge cutoff date can be added to the AI by searching for what you want and uploading the page that is found to OpenAI. That is what the google search part is all about. It has something like 32 different parameters that you can specify for a google search. Many more than I was origainally away of. Using the google search requires two keys. Click the Settings button in this software to find out more about getting keys.

I also added a right click help menu based on the ContextMenu in B4J. I used OpenAI to generate the text of each help entry (about 230 of them). So they are basic and can be improved, but it took literally about 2 minutes to do what would have taken me 2 hours or more. Such is the benefit of AI.

There are numbers in green on the main page. These are the order of steps to create a Run in OpenAI. See here for details: https://platform.openai.com/docs/assistants/overview

I could write more but that gives you all the general understanding of how it works. If you have any questions, please let me know.
 
Last edited:

Tim Chapman

Active Member
Licensed User
Longtime User
Fixed some bugs. Added the ability to download a file from OpenAI. (NOTE: OpenAI API does not allow this for files with the purpose Assistants.)
This is version 1.2.
 

Tim Chapman

Active Member
Licensed User
Longtime User
Fixed Settings Pane not responding.

Added Listing of files in a Vector Store.
Cleaned up Logging of subs starting and finishing.
This is version 1.3. It is attached to the first post in this thread.
It is a stand alone library now. The library spreadsheet has been updated as well.
This is the first time I have released a library for B4J/B4A, so please tell me if you find anything wrong.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
This is the first time I have released a library for B4J/B4A, so please tell me if you find anything wrong.
Nothing wrong.
This more a "you do it right" or just some comments.

- Adding a new Post with Updated Informations is good.
- Updating the 1. Post with the Updated Library is very good.
It is very frustrating if one find the newest Release "somewhere" in the thread and not in #1.
 

Mashiane

Expert
Licensed User
Longtime User
Hi there

I am trying this out but experiencing some hurdles. It would help if all needed dependencies for this are just posted on this thread.

B4X:
Private tokkit As EncodingRegistry 'See: https://www.b4x.com/android/forum/threads/counting-tokens-for-openai.163361/#post-1002523

UPDATE: Used the OpenAIUtils Works zip above as the OpenAIUtils project does not run.

Dependencies: XLUtils
 
Last edited:

Tim Chapman

Active Member
Licensed User
Longtime User
H
Hi Mashiane,
Thank you for posting this. If I can be of further assistance, please let me know.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…